|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #ifndef RANGES_V3_UTILITY_RANDOM_HPP
- #define RANGES_V3_UTILITY_RANDOM_HPP
-
- #include <array>
- #include <cstddef>
- #include <cstdint>
- #include <initializer_list>
- #include <new>
- #include <random>
-
- #include <meta/meta.hpp>
-
- #include <concepts/concepts.hpp>
-
- #include <range/v3/range_fwd.hpp>
-
- #include <range/v3/algorithm/copy.hpp>
- #include <range/v3/algorithm/generate.hpp>
- #include <range/v3/functional/invoke.hpp>
- #include <range/v3/functional/reference_wrapper.hpp>
- #include <range/v3/iterator/concepts.hpp>
-
- #if !RANGES_CXX_THREAD_LOCAL
- #include <mutex>
- #endif
-
- RANGES_DIAGNOSTIC_PUSH
- RANGES_DIAGNOSTIC_IGNORE_CXX17_COMPAT
-
- namespace ranges
- {
-
-
-
- CPP_def
- (
- template(typename Gen)
- concept uniform_random_bit_generator,
- requires(int)
- (
- Gen::min(),
- Gen::max(),
- concepts::requires_<same_as<invoke_result_t<Gen&>, decltype(Gen::min())>>,
- concepts::requires_<same_as<invoke_result_t<Gen&>, decltype(Gen::max())>>
- ) &&
- invocable<Gen &> &&
- unsigned_integral<invoke_result_t<Gen&>>
- );
-
-
-
-
- namespace detail
- {
- namespace randutils
- {
- inline std::array<std::uint32_t, 8> get_entropy()
- {
- std::array<std::uint32_t, 8> seeds;
-
-
- #if defined(__GLIBCXX__) && defined(RANGES_WORKAROUND_VALGRIND_RDRAND)
- std::random_device rd{"/dev/urandom"};
- #else
- std::random_device rd;
- #endif
- std::uniform_int_distribution<std::uint32_t> dist{};
- ranges::generate(seeds, [&] { return dist(rd); });
-
- return seeds;
- }
-
- template<typename I>
- constexpr auto fast_exp(I x, I power, I result = I{1}) -> CPP_ret(I)(
- requires unsigned_integral<I>)
- {
- return power == I{0}
- ? result
- : randutils::fast_exp(
- x * x, power >> 1, result * (power & I{1} ? x : 1));
- }
-
-
-
-
-
-
-
-
-
-
- template<std::size_t count, typename IntRep = std::uint32_t>
- struct seed_seq_fe
- {
- public:
- CPP_assert(unsigned_integral<IntRep>);
- typedef IntRep result_type;
-
- private:
- static constexpr std::size_t mix_rounds = 1 + (count <= 2);
-
- static constexpr std::uint32_t INIT_A = 0x43b0d7e5;
- static constexpr std::uint32_t MULT_A = 0x931e8875;
-
- static constexpr std::uint32_t INIT_B = 0x8b51f9dd;
- static constexpr std::uint32_t MULT_B = 0x58f38ded;
-
- static constexpr std::uint32_t MIX_MULT_L = 0xca01f9dd;
- static constexpr std::uint32_t MIX_MULT_R = 0x4973f715;
- static constexpr std::uint32_t XSHIFT = sizeof(IntRep) * 8 / 2;
-
- std::array<IntRep, count> mixer_;
-
- template<typename I, typename S>
- auto mix_entropy(I first, S last) -> CPP_ret(void)(
- requires input_iterator<I> && sentinel_for<S, I> &&
- convertible_to<iter_reference_t<I>, IntRep>)
- {
- auto hash_const = INIT_A;
- auto hash = [&](IntRep value) RANGES_INTENDED_MODULAR_ARITHMETIC {
- value ^= hash_const;
- hash_const *= MULT_A;
- value *= hash_const;
- value ^= value >> XSHIFT;
- return value;
- };
- auto mix = [](IntRep x, IntRep y) RANGES_INTENDED_MODULAR_ARITHMETIC {
- IntRep result = MIX_MULT_L * x - MIX_MULT_R * y;
- result ^= result >> XSHIFT;
- return result;
- };
-
- for(auto & elem : mixer_)
- {
- if(first != last)
- {
- elem = hash(static_cast<IntRep>(*first));
- ++first;
- }
- else
- elem = hash(IntRep{0});
- }
- for(auto & src : mixer_)
- for(auto & dest : mixer_)
- if(&src != &dest)
- dest = mix(dest, hash(src));
- for(; first != last; ++first)
- for(auto & dest : mixer_)
- dest = mix(dest, hash(static_cast<IntRep>(*first)));
- }
-
- public:
- seed_seq_fe(const seed_seq_fe &) = delete;
- void operator=(const seed_seq_fe &) = delete;
-
- template<typename T>
- CPP_ctor(seed_seq_fe)(std::initializer_list<T> init)(
- requires convertible_to<T const &, IntRep>)
- {
- seed(init.begin(), init.end());
- }
-
- template<typename I, typename S>
- CPP_ctor(seed_seq_fe)(I first, S last)(
- requires input_iterator<I> && sentinel_for<S, I> &&
- convertible_to<iter_reference_t<I>, IntRep>)
- {
- seed(first, last);
- }
-
-
- template<typename I, typename S>
- RANGES_INTENDED_MODULAR_ARITHMETIC auto generate(I first,
- S const last) const
- -> CPP_ret(void)(
- requires random_access_iterator<I> && sentinel_for<S, I>)
- {
- auto src_begin = mixer_.begin();
- auto src_end = mixer_.end();
- auto src = src_begin;
- auto hash_const = INIT_B;
- for(; first != last; ++first)
- {
- auto dataval = *src;
- if(++src == src_end)
- src = src_begin;
- dataval ^= hash_const;
- hash_const *= MULT_B;
- dataval *= hash_const;
- dataval ^= dataval >> XSHIFT;
- *first = dataval;
- }
- }
-
- constexpr std::size_t size() const
- {
- return count;
- }
-
- template<typename O>
- RANGES_INTENDED_MODULAR_ARITHMETIC auto param(O dest) const
- -> CPP_ret(void)(
- requires weakly_incrementable<O> &&
- indirectly_copyable<decltype(mixer_.begin()), O>)
- {
- constexpr IntRep INV_A = randutils::fast_exp(MULT_A, IntRep(-1));
- constexpr IntRep MIX_INV_L =
- randutils::fast_exp(MIX_MULT_L, IntRep(-1));
-
- auto mixer_copy = mixer_;
- for(std::size_t round = 0; round < mix_rounds; ++round)
- {
-
- auto hash_const =
- INIT_A * randutils::fast_exp(MULT_A, IntRep(count * count));
-
- for(auto src = mixer_copy.rbegin(); src != mixer_copy.rend();
- ++src)
- for(auto rdest = mixer_copy.rbegin();
- rdest != mixer_copy.rend();
- ++rdest)
- if(src != rdest)
- {
- IntRep revhashed = *src;
- auto mult_const = hash_const;
- hash_const *= INV_A;
- revhashed ^= hash_const;
- revhashed *= mult_const;
- revhashed ^= revhashed >> XSHIFT;
- IntRep unmixed = *rdest;
- unmixed ^= unmixed >> XSHIFT;
- unmixed += MIX_MULT_R * revhashed;
- unmixed *= MIX_INV_L;
- *rdest = unmixed;
- }
-
- for(auto i = mixer_copy.rbegin(); i != mixer_copy.rend(); ++i)
- {
- IntRep unhashed = *i;
- unhashed ^= unhashed >> XSHIFT;
- unhashed *= randutils::fast_exp(hash_const, IntRep(-1));
- hash_const *= INV_A;
- unhashed ^= hash_const;
- *i = unhashed;
- }
- }
- ranges::copy(mixer_copy, dest);
- }
-
- template<typename I, typename S>
- auto seed(I first, S last) -> CPP_ret(void)(
- requires input_iterator<I> && sentinel_for<S, I> &&
- convertible_to<iter_reference_t<I>, IntRep>)
- {
- mix_entropy(first, last);
-
-
- for(std::size_t i = 1; i < mix_rounds; ++i)
- stir();
- }
-
- seed_seq_fe & stir()
- {
- mix_entropy(mixer_.begin(), mixer_.end());
- return *this;
- }
- };
-
- using seed_seq_fe128 = seed_seq_fe<4, std::uint32_t>;
- using seed_seq_fe256 = seed_seq_fe<8, std::uint32_t>;
-
-
-
-
-
-
-
-
-
-
- template<typename SeedSeq>
- struct auto_seeded : public SeedSeq
- {
- auto_seeded()
- : auto_seeded(randutils::get_entropy())
- {}
- template<std::size_t N>
- auto_seeded(std::array<std::uint32_t, N> const & seeds)
- : SeedSeq(seeds.begin(), seeds.end())
- {}
- using SeedSeq::SeedSeq;
-
- const SeedSeq & base() const
- {
- return *this;
- }
- SeedSeq & base()
- {
- return *this;
- }
- };
-
- using auto_seed_128 = auto_seeded<seed_seq_fe128>;
- using auto_seed_256 = auto_seeded<seed_seq_fe256>;
- }
-
- using default_URNG = meta::if_c<(sizeof(void *) >= sizeof(long long)),
- std::mt19937_64, std::mt19937>;
-
- #if !RANGES_CXX_THREAD_LOCAL
- template<typename URNG>
- class sync_URNG : private URNG
- {
- mutable std::mutex mtx_;
-
- public:
- using URNG::URNG;
- sync_URNG() = default;
- using typename URNG::result_type;
- result_type operator()()
- {
- std::lock_guard<std::mutex> guard{mtx_};
- return static_cast<URNG &>(*this)();
- }
- using URNG::max;
- using URNG::min;
- };
- using default_random_engine = sync_URNG<default_URNG>;
- #else
- using default_random_engine = default_URNG;
- #endif
-
- template<typename T = void>
- default_random_engine & get_random_engine()
- {
- using Seeder = meta::if_c<(sizeof(default_URNG) > 16),
- randutils::auto_seed_256,
- randutils::auto_seed_128>;
-
- #if RANGES_CXX_THREAD_LOCAL >= RANGES_CXX_THREAD_LOCAL_11
- static thread_local default_random_engine engine{Seeder{}.base()};
-
- #elif RANGES_CXX_THREAD_LOCAL
- static __thread bool initialized = false;
- static __thread meta::_t<std::aligned_storage<sizeof(default_random_engine),
- alignof(default_random_engine)>>
- storage;
-
- if(!initialized)
- {
- ::new(static_cast<void *>(&storage))
- default_random_engine{Seeder{}.base()};
- initialized = true;
- }
- auto & engine = reinterpret_cast<default_random_engine &>(storage);
- #else
- static default_random_engine engine{Seeder{}.base()};
- #endif
-
- return engine;
- }
- }
-
- }
-
- RANGES_DIAGNOSTIC_POP
-
- #endif
|