infinity-sampler

Crates.ioinfinity-sampler
lib.rsinfinity-sampler
version0.3.0
sourcesrc
created_at2024-01-20 01:03:16.800853
updated_at2024-01-21 14:06:00.641703
descriptionDeterministic reservoir sampling for Rust
homepage
repositoryhttps://github.com/Eugeny/infinity-sampler
max_upload_size
id1105955
size21,162
Eugene (Eugeny)

documentation

README

Infinity Sampler

✨ Allocation free ✨ Minimal dependencies ✨ no_std compatible ✨

The Infinity Sampler lets you automatically sample an infinite stream of values into a fixed size buffer, while keeping an even spread of samples.

It's a deterministic variation of the Reservoir Sampling algorithm. Writes are O(1), iteration is O(N). See [math] for an illustrated explainer.

Your primary interface is the [SamplingReservoir] struct:

use infinity_sampler::SamplingReservoir;

let mut reservoir = SamplingReservoir::<u32, 8>::new();
for i in 0..256 {
   reservoir.sample(i);
}
let samples: Vec<_> = reservoir.into_ordered_iter().collect();

assert_eq!(samples, vec![0, 32, 64, 96, 128, 160, 192, 224]);
Commit count: 0

cargo fmt