31# ifndef RESERVOIR_SAMPLING_H
32# define RESERVOIR_SAMPLING_H
94 const unsigned long seed =
static_cast<unsigned long>(
95 std::chrono::high_resolution_clock::now().time_since_epoch().
count()))
138 std::swap(
k_, other.k_);
139 std::swap(
n_seen_, other.n_seen_);
140 std::swap(
seed_, other.seed_);
141 std::swap(
rng_min_, other.rng_min_);
142 std::swap(
rng_max_, other.rng_max_);
145 std::swap(
rng_, other.rng_);
160 <<
"Reservoir_Sampler::update: stream length exceeded size_t capacity";
174 <<
"Reservoir_Sampler::update: stream length exceeds unsigned long capacity";
179 <<
"Reservoir_Sampler::update: stream length exceeds RNG generator range (" <<
rng_range_ <<
")";
199 template <std::input_iterator Itor, std::sentinel_for<Itor> Sent>
247 <<
"set_n_seen_for_testing: inconsistent state: "
248 <<
"n=" << n <<
", k=" <<
k_
250 <<
" (expected " <<
expected <<
")";
285 template <std::input_iterator Itor, std::sentinel_for<Itor> Sent>
286 [[nodiscard]]
inline auto
288 unsigned long seed =
static_cast<unsigned long>(
289 std::chrono::high_resolution_clock::now().time_since_epoch().
count()))
291 using T =
typename std::iterator_traits<Itor>::value_type;
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Simple dynamic array with automatic resizing and functional operations.
A Gsl_Compatible_Rng in dynamic memory, so that what holds it grows only by a pointer,...
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
static constexpr result_type min() noexcept
The least number of operator()().
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Incremental reservoir sampler (Algorithm R).
size_t sample_size() const noexcept
Target sample size.
uint64_t rng_range_
Cached range of the RNG (max - min + 1).
void update(const T &val)
Process a new element from the stream.
void swap(Reservoir_Sampler &other) noexcept
Exchange in O(1) the state of this sampler and another.
size_t n_seen_
Total elements seen so far.
void clear()
Reset the sampler while keeping k and seed.
const Array< T > & get_sample() const noexcept
Returns the current random sample.
Reservoir_Sampler(const size_t k, const unsigned long seed=static_cast< unsigned long >(std::chrono::high_resolution_clock::now().time_since_epoch().count()))
Initialize sampler.
Reservoir_Sampler(Reservoir_Sampler &&other)
Take the sample and the generator of another sampler, which is left as a new sampler of the same size...
void update(Itor beg, Sent end)
Process multiple elements.
uint64_t rng_min_
Minimum value produced by RNG.
uint64_t rng_range() const noexcept
RNG output range (max - min + 1).
Gsl_Compatible_Rng_Owner rng_
Random number generator.
Array< T > reservoir_
Current random sample.
uint64_t rng_max_
Maximum value produced by RNG.
void set_n_seen_for_testing(const size_t n)
Forcibly set the internal stream counter (for testing only).
Reservoir_Sampler & operator=(Reservoir_Sampler &&other) noexcept
Exchange this sampler with another, which is left with the previous state of this one.
size_t total_seen() const noexcept
Total elements processed so far.
unsigned long seed_
Stored seed for RNG reset.
Minimal std::expected-style result type for C++20.
Main namespace for Aleph-w library functions.
auto reservoir_sample(Itor beg, Sent end, size_t k, unsigned long seed=static_cast< unsigned long >(std::chrono::high_resolution_clock::now().time_since_epoch().count()))
Convenience function to sample k elements from a range.
std::decay_t< typename HeadC::Item_Type > T
Itor upper_bound(Itor beg, Itor end, const T &value)
Find upper bound in a sorted range.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Dynamic array container with automatic resizing.