46#if KBLIB_DEBUG_SEED_SEQ
62template <
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;
158template <
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>
161struct 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)> {};
165template <
typename UIntType, UIntType a, UIntType c, UIntType m>
167 : std::integral_constant<std::size_t, (filg2(m) + 31) / 32> {
173template <
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)> {};
177template <
typename Engine, std::
size_t P, std::
size_t R>
181template <
typename Engine, std::
size_t W,
typename UIntType>
185template <
typename Engine, std::
size_t K>
191static_assert(state_size_v<std::mt19937> == std::mt19937::state_size,
"");
192static_assert(state_size_v<std::mt19937_64> == std::mt19937_64::state_size * 2,
195template <
typename T,
typename =
void>
202static_assert(seed_discard_v<std::minstd_rand> == 3,
203 "linear_congruential_engines discard 3 words of seed data");
205template <
typename Gen,
typename Source>
212template <
typename Gen>
214 return seeded<Gen>(std::random_device{});
217template <
typename URBG,
typename Transform>
221 static_assert(std::is_default_constructible<Transform>::value,
"");
224 return static_cast<E&
>(*this);
227 return static_cast<const E&
>(*this);
235 std::is_nothrow_copy_constructible<URBG>::value)
238 std::is_nothrow_move_constructible<URBG>::value)
242 template <
typename SSeq,
256 return Transform{}(engine()());
273 return lhs.engine() == rhs.engine();
278 return ! (lhs == rhs);
283 return os <<
e.engine();
287 return is >>
e.engine();
291template <
typename Engine,
typename Transform>
294template <
typename UIntType, UIntType shift, UIntType mask = max>
298 template <
typename UIntInput>
300 return static_cast<UIntType
>(in >> shift) & mask;
312 UIntType
max)
noexcept
318template <
typename UIntType>
320 if (b == std::numeric_limits<UIntType>::digits) {
323 return UIntType{1} << b;
327inline namespace lcgs {
329 template <
typename UIntType, UIntType a, UIntType c, UIntType b>
330 using lcg_p2 = std::linear_congruential_engine<UIntType,
a, c,
ipow2(b)>;
332 inline namespace common_lcgs {
346 uint64_t, 6364136223846793005U, 1442695040888963407U,
351 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
transform_engine< lcg_p2< std::uint_fast64_t, 25214903917u, 11u, 48u >, shift_mask< std::uint_fast32_t, 16u > > rand48
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
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.
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(UIntType min, UIntType max) noexcept -> UIntType
static constexpr auto min(UIntType min, 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.