1 #ifndef BOOST_MOVE_TEST_RANDOM_SHUFFLE_HPP 2 #define BOOST_MOVE_TEST_RANDOM_SHUFFLE_HPP 3 4 5 #include <boost/move/adl_move_swap.hpp> 6 #include <boost/move/detail/iterator_traits.hpp> 7 #include <stdlib.h> 8 rand_15_bit()9inline unsigned long long rand_15_bit() 10 { 11 //Many rand implementation only use 15 bits 12 //so make sure we have only 15 bits 13 return (unsigned long long)((std::rand()) & 0x7fffu); 14 } 15 ullrand()16inline unsigned long long ullrand() 17 { 18 return (rand_15_bit() << 54u) ^ (rand_15_bit() << 39u) 19 ^ (rand_15_bit() << 26u) ^ (rand_15_bit() << 13u) 20 ^ rand_15_bit(); 21 } 22 23 template< class RandomIt > random_shuffle(RandomIt first,RandomIt last)24void random_shuffle( RandomIt first, RandomIt last ) 25 { 26 typedef typename boost::movelib::iterator_traits<RandomIt>::difference_type difference_type; 27 difference_type n = last - first; 28 for (difference_type i = n-1; i > 0; --i) { 29 difference_type j = ullrand() % (i+1); 30 if(j != i) { 31 boost::adl_move_swap(first[i], first[j]); 32 } 33 } 34 } 35 36 37 #endif// BOOST_MOVE_TEST_RANDOM_SHUFFLE_HPP 38