pub struct ReservoirSampler { /* private fields */ }Expand description
Enhanced reservoir sampling implementation based on Vitter’s algorithm
This is a proper implementation of Algorithm R with optimizations:
- True randomness with seedable RNG for reproducibility
- Optimized skip calculation using geometric distribution
- Memory-efficient storage of sample indices
- Support for weighted sampling
Implementations§
Source§impl ReservoirSampler
impl ReservoirSampler
Sourcepub fn seed(capacity: usize, seed: u64) -> Self
pub fn seed(capacity: usize, seed: u64) -> Self
Create a new reservoir sampler with custom seed
Sourcepub fn process_record(&mut self, record_index: usize) -> bool
pub fn process_record(&mut self, record_index: usize) -> bool
Process a new record and decide if it should be included Returns true if the record is selected for the sample
Sourcepub fn replacement_slot(&mut self, seen: usize) -> Option<usize>
pub fn replacement_slot(&mut self, seen: usize) -> Option<usize>
Which reservoir slot row number seen should replace, if any.
Algorithm R over a full reservoir: the seen-th row of the stream
(1-based) replaces a uniformly chosen member with probability
capacity / seen, which leaves every row seen so far equally likely to
be in the sample. Returns None when the row is not selected.
This is the primitive for sampling rows; process_record tracks
indices only and is kept for callers that sample by position.
Sourcepub fn get_sample_indices(&self) -> &[usize]
pub fn get_sample_indices(&self) -> &[usize]
Get current sample as a vector of indices
Sourcepub fn sample_size(&self) -> usize
pub fn sample_size(&self) -> usize
Get current sample size
Sourcepub fn get_stats(&self) -> &ReservoirStats
pub fn get_stats(&self) -> &ReservoirStats
Get sampling statistics
Sourcepub fn sampling_ratio(&self) -> f64
pub fn sampling_ratio(&self) -> f64
Calculate current sampling ratio
Sourcepub fn update_efficiency_stats(&mut self)
pub fn update_efficiency_stats(&mut self)
Update efficiency statistics
Trait Implementations§
Source§impl Clone for ReservoirSampler
impl Clone for ReservoirSampler
Source§fn clone(&self) -> ReservoirSampler
fn clone(&self) -> ReservoirSampler
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more