random_shuffle.hpp 1010 B

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