Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,163 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Random
|
||||
{
|
||||
|
||||
// ── Xoshiro256** PRNG (fast, good quality, no stdlib overhead) ───────────────
|
||||
|
||||
class Xoshiro256
|
||||
{
|
||||
public:
|
||||
using result_type = std::uint64_t;
|
||||
|
||||
explicit Xoshiro256(std::uint64_t seed = 0xdeadbeefcafe1234ull) noexcept
|
||||
{
|
||||
// SplitMix64 seeder
|
||||
auto splitmix = [](std::uint64_t& s) noexcept -> std::uint64_t {
|
||||
s += 0x9e3779b97f4a7c15ull;
|
||||
std::uint64_t z = s;
|
||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
|
||||
z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
|
||||
return z ^ (z >> 31);
|
||||
};
|
||||
s_[0] = splitmix(seed);
|
||||
s_[1] = splitmix(seed);
|
||||
s_[2] = splitmix(seed);
|
||||
s_[3] = splitmix(seed);
|
||||
}
|
||||
|
||||
static constexpr result_type min() noexcept { return 0; }
|
||||
static constexpr result_type max() noexcept
|
||||
{
|
||||
return std::numeric_limits<result_type>::max();
|
||||
}
|
||||
|
||||
result_type operator()() noexcept
|
||||
{
|
||||
const std::uint64_t r = rotl(s_[1] * 5, 7) * 9;
|
||||
const std::uint64_t t = s_[1] << 17;
|
||||
s_[2] ^= s_[0];
|
||||
s_[3] ^= s_[1];
|
||||
s_[1] ^= s_[2];
|
||||
s_[0] ^= s_[3];
|
||||
s_[2] ^= t;
|
||||
s_[3] = rotl(s_[3], 45);
|
||||
return r;
|
||||
}
|
||||
|
||||
void discard(unsigned long long n) noexcept
|
||||
{
|
||||
while (n--) operator()();
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr std::uint64_t rotl(std::uint64_t x, int k) noexcept
|
||||
{
|
||||
return (x << k) | (x >> (64 - k));
|
||||
}
|
||||
std::uint64_t s_[4];
|
||||
};
|
||||
|
||||
// ── Thread-local default generator ───────────────────────────────────────────
|
||||
|
||||
inline Xoshiro256& global_rng() noexcept
|
||||
{
|
||||
static thread_local Xoshiro256 rng{std::random_device{}()};
|
||||
return rng;
|
||||
}
|
||||
|
||||
// ── Distributions ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Uniform integer in [lo, hi] (inclusive).
|
||||
template <std::integral T>
|
||||
[[nodiscard]] T uniform_int(T lo, T hi) noexcept
|
||||
{
|
||||
std::uniform_int_distribution<T> d{lo, hi};
|
||||
return d(global_rng());
|
||||
}
|
||||
|
||||
/// Uniform real in [lo, hi).
|
||||
template <std::floating_point T = double>
|
||||
[[nodiscard]] T uniform_real(T lo = T{0}, T hi = T{1}) noexcept
|
||||
{
|
||||
std::uniform_real_distribution<T> d{lo, hi};
|
||||
return d(global_rng());
|
||||
}
|
||||
|
||||
/// Bernoulli trial: true with probability p.
|
||||
[[nodiscard]] inline bool coin_flip(double p = 0.5) noexcept
|
||||
{
|
||||
std::bernoulli_distribution d{p};
|
||||
return d(global_rng());
|
||||
}
|
||||
|
||||
/// Normal (Gaussian) sample.
|
||||
[[nodiscard]] inline double normal(double mean = 0.0,
|
||||
double stddev = 1.0) noexcept
|
||||
{
|
||||
std::normal_distribution<double> d{mean, stddev};
|
||||
return d(global_rng());
|
||||
}
|
||||
|
||||
/// Pick a random element from a non-empty span. UB if span is empty.
|
||||
template <typename T>
|
||||
[[nodiscard]] const T& choice(std::span<const T> items) noexcept
|
||||
{
|
||||
return items[uniform_int<std::size_t>(0, items.size() - 1)];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] T& choice(std::span<T> items) noexcept
|
||||
{
|
||||
return items[uniform_int<std::size_t>(0, items.size() - 1)];
|
||||
}
|
||||
|
||||
/// Shuffle a container in place using Fisher-Yates.
|
||||
template <typename Container>
|
||||
void shuffle(Container& c) noexcept
|
||||
{
|
||||
std::ranges::shuffle(c, global_rng());
|
||||
}
|
||||
|
||||
/// Fill a span with random bytes.
|
||||
inline void fill_random(std::span<std::byte> buf) noexcept
|
||||
{
|
||||
for (auto& b : buf) {
|
||||
b = static_cast<std::byte>(uniform_int<int>(0, 255));
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a random alphanumeric string of length n.
|
||||
[[nodiscard]] inline std::string random_string(std::size_t n)
|
||||
{
|
||||
constexpr std::string_view chars =
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"0123456789";
|
||||
std::string s;
|
||||
s.reserve(n);
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
s += chars[uniform_int<std::size_t>(0, chars.size() - 1)];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/// Sample k distinct indices from [0, n).
|
||||
[[nodiscard]] inline std::vector<std::size_t> sample_indices(std::size_t n,
|
||||
std::size_t k)
|
||||
{
|
||||
std::vector<std::size_t> pool(n);
|
||||
std::iota(pool.begin(), pool.end(), std::size_t{0});
|
||||
shuffle(pool);
|
||||
pool.resize(k);
|
||||
return pool;
|
||||
}
|
||||
|
||||
} // namespace Void::Random
|
||||
Reference in new issue
Block a user