Skip to content

Commit

Permalink
Only use random_shuffle with older Boost
Browse files Browse the repository at this point in the history
std::shuffle is superior to std::random_shuffle
so we should only use the old deprecated function
if we really need to - specifically, with older
Boost (e.g. on Ubuntu 22.04).
  • Loading branch information
benmwebb committed Jul 13, 2024
1 parent c95a236 commit b9c8da9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
7 changes: 5 additions & 2 deletions modules/domino/src/particle_states.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <IMP/core/rigid_bodies.h>
#include <IMP/random.h>
#include <algorithm>
#include <boost/version.hpp>
#include <boost/random/uniform_int_distribution.hpp>

IMPDOMINO_BEGIN_NAMESPACE
Expand Down Expand Up @@ -173,7 +174,7 @@ void RecursiveStates::load_particle_state(unsigned int i,
}
}

#if IMP_COMPILER_HAS_RANDOM_SHUFFLE
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE && BOOST_VERSION < 107500
namespace {
struct RandomWrapper {
int operator()(int i) {
Expand All @@ -193,7 +194,9 @@ PermutationStates::PermutationStates(ParticleStates *inner)
for (unsigned int i = 0; i < permutation_.size(); ++i) {
permutation_[i] = i;
}
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE && BOOST_VERSION < 107500
// Older Boost RNG has non-constexpr min(), max() which won't compile
// with std::shuffle, so use the older random_shuffle instead
RandomWrapper rr;
std::random_shuffle(permutation_.begin(), permutation_.end(), rr);
#else
Expand Down
5 changes: 4 additions & 1 deletion modules/integrative_docking/bin/interface_cross_links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string>
#include <algorithm>

#include <boost/version.hpp>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <boost/random/uniform_real_distribution.hpp>
Expand Down Expand Up @@ -232,7 +233,9 @@ int main(int argc, char** argv) {
}

select_cross_links(cross_links, selected_cross_links);
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE && BOOST_VERSION < 107500
// Older Boost RNG has non-constexpr min(), max() which won't compile
// with std::shuffle, so use the older random_shuffle instead
std::random_shuffle(selected_cross_links.begin(), selected_cross_links.end());
#else
std::shuffle(selected_cross_links.begin(), selected_cross_links.end(),
Expand Down
7 changes: 5 additions & 2 deletions modules/kinematics/src/RRT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
#include <IMP/kinematics/RRT.h>
#include <IMP/random.h>
#include <boost/version.hpp>

IMPKINEMATICS_BEGIN_NAMESPACE

Expand All @@ -21,14 +22,16 @@ std::ostream& operator<<(std::ostream& s, const RRT::Parameters& p) {

namespace {

#if IMP_COMPILER_HAS_RANDOM_SHUFFLE
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE && BOOST_VERSION < 107500
int myrandom (int i) { return std::rand()%i;}
#endif

std::vector<bool> select_k_out_of_n_dofs(unsigned int k, unsigned int n) {
std::vector<unsigned int> arr(n);
for(unsigned int i=0; i<n; i++) arr[i] = i;
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE && BOOST_VERSION < 107500
// Older Boost RNG has non-constexpr min(), max() which won't compile
// with std::shuffle, so use the older random_shuffle instead
std::random_shuffle(arr.begin(), arr.end(), myrandom);
#else
std::shuffle(arr.begin(), arr.end(), random_number_generator);
Expand Down
5 changes: 4 additions & 1 deletion modules/misc/include/MetricClosePairsFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <IMP/Pointer.h>
#include <IMP/random.h>
#include <IMP/particle_index.h>
#include <boost/version.hpp>
#include <boost/unordered_map.hpp>
#include <algorithm>
#include <limits>
Expand Down Expand Up @@ -51,7 +52,9 @@ class MetricClosePairsFinder : public core::ClosePairsFinder {
Index get_index(Model *m, ParticleIndexes inputs) const {
unsigned int index_size = std::min<unsigned int>(
1U, std::sqrt(static_cast<double>(inputs.size())));
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE
#if IMP_COMPILER_HAS_RANDOM_SHUFFLE && BOOST_VERSION < 107500
// Older Boost RNG has non-constexpr min(), max() which won't compile
// with std::shuffle, so use the older random_shuffle instead
std::random_shuffle(inputs.begin(), inputs.end());
#else
std::shuffle(inputs.begin(), inputs.end(), random_number_generator);
Expand Down

0 comments on commit b9c8da9

Please sign in to comment.