https://github.com/eugeny/infinity-sampler
Deterministic reservoir sampling for Rust
https://github.com/eugeny/infinity-sampler
Last synced: about 1 month ago
JSON representation
Deterministic reservoir sampling for Rust
- Host: GitHub
- URL: https://github.com/eugeny/infinity-sampler
- Owner: Eugeny
- Created: 2024-01-20T01:02:10.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-21T14:05:57.000Z (over 1 year ago)
- Last Synced: 2025-04-13T23:13:42.406Z (about 1 month ago)
- Language: Rust
- Homepage: https://docs.rs/infinity-sampler
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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](https://en.wikipedia.org/wiki/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::::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]);
```