diff options
author | Jose <jose@zeroc.com> | 2018-09-07 18:47:28 +0200 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2018-09-07 18:47:28 +0200 |
commit | 8cddd264e57ab89fc4c6952b2b390457a4bfbedf (patch) | |
tree | 66bb34aa13289ed0017a9460e800740516bb1278 /cpp/include/IceUtil/Random.h | |
parent | Enable test workers for PHP, Ruby and Python (diff) | |
download | ice-8cddd264e57ab89fc4c6952b2b390457a4bfbedf.tar.bz2 ice-8cddd264e57ab89fc4c6952b2b390457a4bfbedf.tar.xz ice-8cddd264e57ab89fc4c6952b2b390457a4bfbedf.zip |
Fixes for C++17 mode
Close #180
Diffstat (limited to 'cpp/include/IceUtil/Random.h')
-rw-r--r-- | cpp/include/IceUtil/Random.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/cpp/include/IceUtil/Random.h b/cpp/include/IceUtil/Random.h index f576205d201..69d4c0b4dea 100644 --- a/cpp/include/IceUtil/Random.h +++ b/cpp/include/IceUtil/Random.h @@ -13,12 +13,46 @@ #include <IceUtil/Config.h> #include <IceUtil/Exception.h> +#ifdef ICE_CPP11_COMPILER +# include <algorithm> +# include <random> +#endif + namespace IceUtilInternal { ICE_API void generateRandom(char*, size_t); ICE_API unsigned int random(int = 0); +#ifdef ICE_CPP11_COMPILER + +template<class T> +void shuffle(T first, T last) +{ + std::random_device rd; + std::mt19937 rng(rd()); + std::shuffle(first, last, rng); +} + +#else + +struct RandomNumberGenerator : public std::unary_function<std::ptrdiff_t, std::ptrdiff_t> +{ + std::ptrdiff_t operator()(std::ptrdiff_t d) + { + return IceUtilInternal::random(static_cast<int>(d)); + } +}; + +template<class T> +void shuffle(T first, T last) +{ + RandomNumberGenerator rng; + random_shuffle(first, last, rng); +} + +#endif + } #endif |