use std::shuffle instead of random_shuffle in C++11 and later

This commit is contained in:
Marshall Clow
2016-11-20 17:24:19 -08:00
parent 7977bd0cdc
commit 9b19fc00c2
2 changed files with 48 additions and 20 deletions

View File

@ -7,6 +7,20 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#if __cplusplus >= 201103L
#include <random>
std::default_random_engine gen;
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::shuffle(first, last, gen); }
#else
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::random_shuffle(first, last); }
#endif
namespace ba = boost::algorithm; namespace ba = boost::algorithm;
template <typename Iter> template <typename Iter>
@ -72,14 +86,14 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[5], 5); // BOOST_CHECK_EQUAL(v[5], 5);
// Mix them up and try again - single element // Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, b + 8); ba::partition_subrange(b, v.end(), b + 7, b + 8);
check_sequence (b, v.end(), b + 7, b + 8); check_sequence (b, v.end(), b + 7, b + 8);
// BOOST_CHECK_EQUAL(v[7], 7); // BOOST_CHECK_EQUAL(v[7], 7);
// Mix them up and try again - at the end // Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, v.end()); ba::partition_subrange(b, v.end(), b + 7, v.end());
check_sequence (b, v.end(), b + 7, v.end()); check_sequence (b, v.end(), b + 7, v.end());
@ -88,7 +102,7 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[9], 9); // BOOST_CHECK_EQUAL(v[9], 9);
// Mix them up and try again - at the beginning // Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b + 2); ba::partition_subrange(b, v.end(), b, b + 2);
check_sequence (b, v.end(), b, b + 2); check_sequence (b, v.end(), b, b + 2);
@ -96,12 +110,12 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[1], 1); // BOOST_CHECK_EQUAL(v[1], 1);
// Mix them up and try again - empty subrange // Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b); ba::partition_subrange(b, v.end(), b, b);
check_sequence (b, v.end(), b, b); check_sequence (b, v.end(), b, b);
// Mix them up and try again - entire subrange // Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, v.end()); ba::partition_subrange(b, v.end(), b, v.end());
check_sequence (b, v.end(), b, v.end()); check_sequence (b, v.end(), b, v.end());
} }
@ -120,14 +134,14 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[5], 4); // BOOST_CHECK_EQUAL(v[5], 4);
// Mix them up and try again - single element // Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, b + 8, std::greater<int>()); ba::partition_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>()); check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
// BOOST_CHECK_EQUAL(v[7], 2); // BOOST_CHECK_EQUAL(v[7], 2);
// Mix them up and try again - at the end // Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b + 7, v.end(), std::greater<int>()); ba::partition_subrange(b, v.end(), b + 7, v.end(), std::greater<int>());
check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>()); check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>());
@ -136,7 +150,7 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[9], 0); // BOOST_CHECK_EQUAL(v[9], 0);
// Mix them up and try again - at the beginning // Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b + 2, std::greater<int>()); ba::partition_subrange(b, v.end(), b, b + 2, std::greater<int>());
check_sequence (b, v.end(), b, b + 2, std::greater<int>()); check_sequence (b, v.end(), b, b + 2, std::greater<int>());
@ -144,12 +158,12 @@ BOOST_AUTO_TEST_CASE( test_main )
// BOOST_CHECK_EQUAL(v[1], 8); // BOOST_CHECK_EQUAL(v[1], 8);
// Mix them up and try again - empty subrange // Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, b, std::greater<int>()); ba::partition_subrange(b, v.end(), b, b, std::greater<int>());
check_sequence (b, v.end(), b, b, std::greater<int>()); check_sequence (b, v.end(), b, b, std::greater<int>());
// Mix them up and try again - entire subrange // Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::partition_subrange(b, v.end(), b, v.end(), std::greater<int>()); ba::partition_subrange(b, v.end(), b, v.end(), std::greater<int>());
check_sequence (b, v.end(), b, v.end(), std::greater<int>()); check_sequence (b, v.end(), b, v.end(), std::greater<int>());
} }

View File

@ -7,6 +7,20 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#if __cplusplus >= 201103L
#include <random>
std::default_random_engine gen;
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::shuffle(first, last, gen); }
#else
template<typename RandomIt>
void do_shuffle(RandomIt first, RandomIt last)
{ std::random_shuffle(first, last); }
#endif
namespace ba = boost::algorithm; namespace ba = boost::algorithm;
template <typename Iter> template <typename Iter>
@ -53,14 +67,14 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[5], 5); BOOST_CHECK_EQUAL(v[5], 5);
// Mix them up and try again - single element // Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, b + 8); ba::sort_subrange(b, v.end(), b + 7, b + 8);
check_sequence (b, v.end(), b + 7, b + 8); check_sequence (b, v.end(), b + 7, b + 8);
BOOST_CHECK_EQUAL(v[7], 7); BOOST_CHECK_EQUAL(v[7], 7);
// Mix them up and try again - at the end // Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, v.end()); ba::sort_subrange(b, v.end(), b + 7, v.end());
check_sequence (b, v.end(), b + 7, v.end()); check_sequence (b, v.end(), b + 7, v.end());
@ -69,7 +83,7 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[9], 9); BOOST_CHECK_EQUAL(v[9], 9);
// Mix them up and try again - at the beginning // Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b + 2); ba::sort_subrange(b, v.end(), b, b + 2);
check_sequence (b, v.end(), b, b + 2); check_sequence (b, v.end(), b, b + 2);
@ -77,12 +91,12 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[1], 1); BOOST_CHECK_EQUAL(v[1], 1);
// Mix them up and try again - empty subrange // Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b); ba::sort_subrange(b, v.end(), b, b);
check_sequence (b, v.end(), b, b); check_sequence (b, v.end(), b, b);
// Mix them up and try again - entire subrange // Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, v.end()); ba::sort_subrange(b, v.end(), b, v.end());
check_sequence (b, v.end(), b, v.end()); check_sequence (b, v.end(), b, v.end());
} }
@ -101,14 +115,14 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[5], 4); BOOST_CHECK_EQUAL(v[5], 4);
// Mix them up and try again - single element // Mix them up and try again - single element
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, b + 8, std::greater<int>()); ba::sort_subrange(b, v.end(), b + 7, b + 8, std::greater<int>());
check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>()); check_sequence (b, v.end(), b + 7, b + 8, std::greater<int>());
BOOST_CHECK_EQUAL(v[7], 2); BOOST_CHECK_EQUAL(v[7], 2);
// Mix them up and try again - at the end // Mix them up and try again - at the end
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b + 7, v.end(), std::greater<int>()); ba::sort_subrange(b, v.end(), b + 7, v.end(), std::greater<int>());
check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>()); check_sequence (b, v.end(), b + 7, v.end(), std::greater<int>());
@ -117,7 +131,7 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[9], 0); BOOST_CHECK_EQUAL(v[9], 0);
// Mix them up and try again - at the beginning // Mix them up and try again - at the beginning
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b + 2, std::greater<int>()); ba::sort_subrange(b, v.end(), b, b + 2, std::greater<int>());
check_sequence (b, v.end(), b, b + 2, std::greater<int>()); check_sequence (b, v.end(), b, b + 2, std::greater<int>());
@ -125,12 +139,12 @@ BOOST_AUTO_TEST_CASE( test_main )
BOOST_CHECK_EQUAL(v[1], 8); BOOST_CHECK_EQUAL(v[1], 8);
// Mix them up and try again - empty subrange // Mix them up and try again - empty subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, b, std::greater<int>()); ba::sort_subrange(b, v.end(), b, b, std::greater<int>());
check_sequence (b, v.end(), b, b, std::greater<int>()); check_sequence (b, v.end(), b, b, std::greater<int>());
// Mix them up and try again - entire subrange // Mix them up and try again - entire subrange
std::random_shuffle(v.begin(), v.end()); do_shuffle(v.begin(), v.end());
ba::sort_subrange(b, v.end(), b, v.end(), std::greater<int>()); ba::sort_subrange(b, v.end(), b, v.end(), std::greater<int>());
check_sequence (b, v.end(), b, v.end(), std::greater<int>()); check_sequence (b, v.end(), b, v.end(), std::greater<int>());
} }