46 #if KBLIB_DEBUG_SEED_SEQ
62 template <
typename Array,
typename RandomGenerator,
typename freqtype =
double>
64 [[deprecated(
"Use std::discrete_distribution instead")]] constexpr
auto
66 -> decltype(cats.size()) {
67 std::uniform_real_distribution<freqtype> uniform(
69 freqtype choose = uniform(r);
70 for (decltype(cats.size()) stop = 0; stop != cats.size(); ++stop) {
77 # if __has_builtin(__builtin_unreachable)
78 __builtin_unreachable();
80 return cats.size() - 1;
83 return cats.size() - 1;
92 template <
typename InputIt>
98 template <
typename Source>
105 template <
typename Source>
107 : data(count + discard, 0x8b8b8b8bu) {
112 template <
typename RandomAccessIt>
113 auto generate(RandomAccessIt begin, RandomAccessIt end)
const ->
void {
114 auto o_size = end - begin;
116 #if KBLIB_DEBUG_SEED_SEQ
117 if (o_size < d_size) {
118 std::clog <<
"trivial_seed_seq: overseeded for output size of "
119 << o_size <<
", have data size " << d_size <<
'\n';
123 std::fill(begin, end, 0x8b8b8b8bu);
125 #if KBLIB_DEBUG_SEED_SEQ
126 if (o_size > d_size) {
127 std::clog <<
"trivial_seed_seq: unexpectedly wrapping, output size "
128 << o_size <<
" greater than data size " << d_size <<
'\n';
134 = saturating_cast<std::size_t>(
std::min(d_size, o_size));
136 }
while ((o_size -= d_size) > 0);
145 template <
typename OutputIt>
146 auto param(OutputIt dest)
const ->
void {
147 std::copy(data.begin(), data.end(), dest);
152 std::vector<std::uint32_t> data;
155 template <
typename T>
158 template <
typename UIntType,
size_t w,
size_t n,
size_t m,
size_t r, UIntType
a,
159 size_t u, UIntType d,
size_t s, UIntType b,
size_t t, UIntType c,
160 size_t l, UIntType f>
161 struct state_size<
std::mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s,
163 : std::integral_constant<std::size_t, n*((w + 31) / 32)> {};
165 template <
typename UIntType, UIntType a, UIntType c, UIntType m>
167 : std::integral_constant<std::size_t, (filg2(m) + 31) / 32> {
173 template <
typename UIntType, std::
size_t w, std::
size_t s, std::
size_t r>
175 : std::integral_constant<std::size_t, r*((w + 31) / 32)> {};
177 template <
typename Engine, std::
size_t P, std::
size_t R>
181 template <
typename Engine, std::
size_t W,
typename UIntType>
185 template <
typename Engine, std::
size_t K>
188 template <
typename T>
191 static_assert(state_size_v<std::mt19937> == std::mt19937::state_size);
192 static_assert(state_size_v<std::mt19937_64> == std::mt19937_64::state_size * 2);
194 template <
typename T,
typename =
void>
196 template <
typename T>
201 static_assert(seed_discard_v<std::minstd_rand> == 3,
202 "linear_congruential_engines discard 3 words of seed data");
204 template <
typename Gen,
typename Source>
211 template <
typename Gen>
213 return seeded<Gen>(std::random_device{});
216 template <
typename URBG,
typename Transform>
220 static_assert(std::is_default_constructible<Transform>::value,
"");
223 return static_cast<E&
>(*this);
226 return static_cast<const E&
>(*this);
234 std::is_nothrow_copy_constructible<URBG>::value)
237 std::is_nothrow_move_constructible<URBG>::value)
241 template <
typename SSeq,
255 return Transform{}(engine()());
272 return lhs.engine() == rhs.engine();
277 return ! (lhs == rhs);
282 return os <<
e.engine();
286 return is >>
e.engine();
290 template <
typename Engine,
typename Transform>
293 template <
typename UIntType, UIntType shift, UIntType mask = max>
297 template <
typename UIntInput>
299 return static_cast<UIntType
>(in >> shift) & mask;
311 UIntType
max) noexcept
317 template <
typename UIntType>
319 if (b == std::numeric_limits<UIntType>::digits) {
322 return UIntType{1} << b;
326 inline namespace lcgs {
327 template <
typename UIntType, UIntType a, UIntType c, UIntType b>
328 using lcg_p2 = std::linear_congruential_engine<UIntType,
a, c,
ipow2(b)>;
330 inline namespace common_lcgs {
335 = std::linear_congruential_engine<std::uint_fast64_t, 0x5DEECE66D, 11,
339 uint64_t, 6364136223846793005U, 1442695040888963407U,
344 inline namespace best_lcgs {
Provides general-purpose algorithms, similar to the <algorithms> header.
trivial_seed_seq(Source gen, std::size_t count, std::size_t discard)
trivial_seed_seq()=default
trivial_seed_seq(std::initializer_list< T > il)
auto generate(RandomAccessIt begin, RandomAccessIt end) const -> void
trivial_seed_seq(InputIt begin, InputIt end)
auto param(OutputIt dest) const -> void
auto size() const noexcept -> std::size_t
trivial_seed_seq(Source gen, std::size_t count)
std::uint32_t result_type
This file provides some iterators, ranges, iterator/range adapters, and operations that can be perfor...
Provides utilities to enable safe and expressive memory management and low-level memory manipulation.
typename make_void< Ts... >::type void_t
lcg_p2< std::uint_fast64_t, 0xbdcdbb079f8du, 0u, 48u > mcg48
lcg_p2< std::uint_fast64_t, 0xf1357aea2e62a9c5u, 0u, 64u > mcg64
lcg_p2< std::uint_fast64_t, 0xb67a49a5466du, 1u, 48u > lcg48
lcg_p2< std::uint_fast32_t, 0x93d765ddu, 0u, 32u > mcg32
lcg_p2< std::uint_fast32_t, 0xa13fc965u, 1u, 32u > lcg32
lcg_p2< std::uint_fast64_t, 0xaf251af3b0f025b5u, 1u, 64u > lcg64
std::linear_congruential_engine< uint64_t, 6364136223846793005U, 1442695040888963407U, 0U > knuth_lcg
std::linear_congruential_engine< std::uint_fast64_t, 0x5DEECE66D, 11, 1ull<< 48u > java_rand
std::linear_congruential_engine< UIntType, a, c, ipow2(b)> lcg_p2
constexpr struct kblib::nums::min_t min
constexpr struct kblib::nums::max_t max
The main namespace in which all entities from kblib are defined.
constexpr auto to_signed(I x) -> std::make_signed_t< I >
Cast integral argument to corresponding signed type.
constexpr auto a(const std::initializer_list< T > &a) -> auto
Index an array literal without naming its type.
typename std::enable_if< B, T >::type enable_if_t
constexpr auto copy_n(InputIt first, Size count, OutputIt out) -> OutputIt
Copies all elements of [first, std::advance(first, n)) to out.
constexpr auto generate_n(OutputIt first, Size count, Generator g) noexcept(noexcept(*first++=g())) -> OutputIt
Like std::generate_n except that it is constexpr.
auto seeded(Source &&s) -> Gen
constexpr auto ipow2(UIntType b) noexcept -> UIntType
constexpr auto accumulate(InputIt first, InputIt last, T init) -> T
A constexpr version of std::accumulate.
constexpr std::size_t state_size_v
constexpr auto copy(InputIt first, EndIt last, OutputIt out) -> OutputIt
Copies all elements of [first, last) to out. It also allows for a sentinel end iterator.
constexpr std::size_t seed_discard_v
constexpr auto chooseCategorical(Array &&cats, RandomGenerator &r) -> decltype(cats.size())
Given a categorical distribution cats, selects one category.
Provides general utilities which do not fit in any more specific header.
Provides numerical and mathematical utilities.
static constexpr auto max([[maybe_unused]] UIntType min, UIntType max) noexcept -> UIntType
static constexpr auto min(UIntType min, [[maybe_unused]] UIntType max) noexcept -> UIntType
constexpr auto operator()(UIntType in) const noexcept -> UIntType
static constexpr auto g(UIntInput in) noexcept -> UIntType
Provides macros and basic templates used by the rest of kblib.
#define KBLIB_UNUSED
This internal macro is used to provide a fallback for [[maybe_unused]] in C++14.
#define KBLIB_NODISCARD
This internal macro is used to provide a fallback for [[nodiscard]] in C++14.