diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/random/lcg_engine.h | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/random/lcg_engine.h')
-rw-r--r-- | util/random/lcg_engine.h | 82 |
1 files changed, 41 insertions, 41 deletions
diff --git a/util/random/lcg_engine.h b/util/random/lcg_engine.h index 08cc93c845..fcf8ab7166 100644 --- a/util/random/lcg_engine.h +++ b/util/random/lcg_engine.h @@ -1,66 +1,66 @@ -#pragma once - +#pragma once + #include <utility> -#include <util/generic/typetraits.h> - -// common engine for lcg-based RNG's -// http://en.wikipedia.org/wiki/Linear_congruential_generator - +#include <util/generic/typetraits.h> + +// common engine for lcg-based RNG's +// http://en.wikipedia.org/wiki/Linear_congruential_generator + namespace NPrivate { template <typename T> T LcgAdvance(T seed, T lcgBase, T lcgAddend, T delta) noexcept; }; -template <typename T, T A, T C> -struct TFastLcgIterator { - static_assert(C % 2 == 1, "C must be odd"); - +template <typename T, T A, T C> +struct TFastLcgIterator { + static_assert(C % 2 == 1, "C must be odd"); + static constexpr T Iterate(T x) noexcept { - return x * A + C; - } + return x * A + C; + } static inline T IterateMultiple(T x, T delta) noexcept { return ::NPrivate::LcgAdvance(x, A, C, delta); } -}; - -template <typename T, T A> -struct TLcgIterator { +}; + +template <typename T, T A> +struct TLcgIterator { inline TLcgIterator(T seq) noexcept - : C((seq << 1u) | (T)1) // C must be odd - { - } - + : C((seq << 1u) | (T)1) // C must be odd + { + } + inline T Iterate(T x) noexcept { - return x * A + C; - } - + return x * A + C; + } + inline T IterateMultiple(T x, T delta) noexcept { return ::NPrivate::LcgAdvance(x, A, C, delta); } - const T C; -}; - -template <class TIterator, class TMixer> -struct TLcgRngBase: public TIterator, public TMixer { + const T C; +}; + +template <class TIterator, class TMixer> +struct TLcgRngBase: public TIterator, public TMixer { using TStateType = decltype(std::declval<TIterator>().Iterate(0)); using TResultType = decltype(std::declval<TMixer>().Mix(TStateType())); - - template <typename... Args> - inline TLcgRngBase(TStateType seed, Args&&... args) + + template <typename... Args> + inline TLcgRngBase(TStateType seed, Args&&... args) : TIterator(std::forward<Args>(args)...) - , X(seed) - { - } - + , X(seed) + { + } + inline TResultType GenRand() noexcept { - return this->Mix(X = this->Iterate(X)); - } - + return this->Mix(X = this->Iterate(X)); + } + inline void Advance(TStateType delta) noexcept { X = this->IterateMultiple(X, delta); } - TStateType X; -}; + TStateType X; +}; |