From c4fc520b19df53e98e6eaed4c1f1ade35f903975 Mon Sep 17 00:00:00 2001 From: khizmax Date: Sat, 11 Apr 2015 21:27:55 +0300 Subject: [PATCH] Issue #23: replaced std::random_shuffle with std::random --- cds/opt/permutation.h | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/cds/opt/permutation.h b/cds/opt/permutation.h index eb98733d..62fe61eb 100644 --- a/cds/opt/permutation.h +++ b/cds/opt/permutation.h @@ -5,7 +5,7 @@ #include #include // rand, srand -#include // random_shuffle +#include namespace cds { namespace opt { @@ -69,7 +69,7 @@ namespace cds { namespace opt { /// Permutation generator of arbitrary length based on \p rand() /** - The class is suitable for opt::permutation_generator option. + The class is suitable for \p opt::permutation_generator option. The generator calculates n = rand() and produces the sequence [n % nLen, (n + 1) % nLen, ..., (n + nLen - 1) % nLen]. @@ -120,7 +120,7 @@ namespace cds { namespace opt { /// Permutation generator of power-of-2 length based on \p rand() /** - The class is suitable for opt::permutation_generator option. + The class is suitable for \p opt::permutation_generator option. The generator calculates n = rand() and produces the sequence [n % nLen, (n + 1) % nLen, ..., (n + nLen - 1) % nLen]. @@ -176,32 +176,41 @@ namespace cds { namespace opt { } }; - /// Permutation generator based on \p std::random_shuffle + /// Permutation generator based on \p std::shuffle /** - The class is suitable for opt::permutation_generator option. + The class is suitable for \p opt::permutation_generator option. - The generator produces a permutation of [0, nLen) sequence. + The generator produces a permutation of [0, nLen) sequence. The generator instance allocates a memory block. - \p Int template argument specifies the type of generated value, it should be any integer. + Template parameters: + - \p Int - specifies the type of generated value, it should be any integer. + - \p RandomGenerator - random generator, default is \p std::mt19937 + - \p RandomDevice - random device, default is \p std::random_device */ - template + template class random_shuffle_permutation { public: - typedef Int integer_type; ///< Type of generated value + typedef Int integer_type; ///< Type of generated value + typedef RandomGenerator random_generator; ///< random generator + typedef RandomDevice random_device; ///< random device protected: //@cond integer_type * m_pCur; integer_type * m_pFirst; integer_type * m_pLast; + + random_generator m_RandomGenerator; + random_device m_RandomDevice; //@endcond public: /// Initializes the generator of arbitrary length \p nLength random_shuffle_permutation( size_t nLength ) : m_pCur( nullptr ) + , m_RandomGenerator( m_RandomDevice() ) { m_pFirst = new integer_type[nLength]; m_pLast = m_pFirst + nLength; @@ -230,7 +239,7 @@ namespace cds { namespace opt { /// Resets the generator to produce new sequence void reset() { - std::random_shuffle( m_pFirst, m_pLast ); + std::shuffle( m_pFirst, m_pLast, m_RandomGenerator ); m_pCur = m_pFirst; } }; @@ -241,7 +250,7 @@ namespace cds { namespace opt { int(Generator) + nOffset where \p Generator - a permutation generator. - The class is suitable for opt::permutation_generator option + The class is suitable for \p opt::permutation_generator option if the goal sequence should be a permutation of [nOffset, nOffset + nLength) */ template -- 2.34.1