From 7021de2fe7a69d4c788ccf43b8b096dbcc8597c8 Mon Sep 17 00:00:00 2001 From: ddzama Date: Wed, 30 Mar 2022 12:30:01 +0300 Subject: [PATCH] 0032909: Coding Rules - replace (removed from C++17) `std::random_shuffle` with `std::shuffle` for random permutation algorithm Since C++17 the accessible version of permutation function left the function: template< class RandomIt, class URBG > void shuffle( RandomIt first, RandomIt last, URBG&& g ); See details and example of use: https://en.cppreference.com/w/cpp/algorithm/random_shuffle --- src/QANCollection/QANCollection_Stl.cxx | 31 +++++++++++-------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/QANCollection/QANCollection_Stl.cxx b/src/QANCollection/QANCollection_Stl.cxx index 656fbbfdbd..81528e633d 100644 --- a/src/QANCollection/QANCollection_Stl.cxx +++ b/src/QANCollection/QANCollection_Stl.cxx @@ -38,18 +38,11 @@ #include #include #include +#include //! Size of test data sets. const int THE_TEST_SIZE = 5000; -namespace { - // Auxiliary class to use in std::random_shuffle() - struct RandomGenerator { - RandomGenerator () { srand(1); } - ptrdiff_t operator () (ptrdiff_t upper) const { return rand() % upper; } - }; -} - template struct CollectionFiller { @@ -951,11 +944,13 @@ void TestPerformanceRandomIterator(Draw_Interpretor& di) aTimer.Reset(); aTimer.Start(); { - RandomGenerator aRandomGen; + std::random_device ran_dev; + std::mt19937 gen(ran_dev()); + gen.seed(0x03ac38f2); for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx) { - std::sort (aVector->begin(), aVector->end()); - std::random_shuffle (aVector->begin(), aVector->end(), aRandomGen); + std::sort (aVector->begin(), aVector->end()); + std::shuffle (aVector->begin(), aVector->end(), gen); } } aTimer.Stop(); @@ -965,11 +960,13 @@ void TestPerformanceRandomIterator(Draw_Interpretor& di) aTimer.Reset(); aTimer.Start(); { - RandomGenerator aRandomGen; + std::random_device ran_dev; + std::mt19937 gen(ran_dev()); + gen.seed(0x03ac38f2); for (Standard_Integer anIdx = 0; anIdx < 10; ++anIdx) { - std::sort (aCollec->begin(), aCollec->end()); - std::random_shuffle (aCollec->begin(), aCollec->end(), aRandomGen); + std::sort (aCollec->begin(), aCollec->end()); + std::shuffle (aCollec->begin(), aCollec->end(), gen); } } aTimer.Stop(); @@ -981,7 +978,7 @@ void TestPerformanceRandomIterator(Draw_Interpretor& di) // check that result is the same if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) ) - di << "Error: sequences are not the same at the end!\n"; + di << "Error: sequences are not the same at the end (random iterator)!\n"; delete aVector; delete aCollec; @@ -1033,7 +1030,7 @@ void TestPerformanceForwardIterator(Draw_Interpretor& di) // check that result is the same if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) ) - di << "Error: sequences are not the same at the end!\n"; + di << "Error: sequences are not the same at the end (forward iterator)!\n"; delete aVector; delete aCollec; @@ -1085,7 +1082,7 @@ void TestPerformanceBidirIterator(Draw_Interpretor& di) // check that result is the same if ( ! std::equal (aVector->begin(), aVector->end(), aCollec->begin()) ) - di << "Error: sequences are not the same at the end!\n"; + di << "Error: sequences are not the same at the end (bidir iterator)!\n"; delete aVector; delete aCollec;