pub struct SamplingReservoir<T, const N: usize> { /* private fields */ }Expand description
§Infinity Sampler
See the top-level doc for an example.
The sampling rate gets halved after every N/2 stored values, which is the same
as every N*2^X values observed by the sampler.
Feed the values into the reservoir using sample() and then turn it into an ordered iterator with into_ordered_iter().
The buffer size must be a power of two.
Implementations§
Source§impl<T, const N: usize> SamplingReservoir<T, N>
impl<T, const N: usize> SamplingReservoir<T, N>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a empty reservoir, allocating an uninitialized buffer.
Panics if N is not a power of two.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of currently stored items. Can be from 0 to N-1 and never decreases.
pub fn is_empty(&self) -> bool
Sourcepub fn into_inner(self) -> Vec<T, N>
pub fn into_inner(self) -> Vec<T, N>
Consume self and return the internal item buffer.
Sourcepub fn as_unordered_slice(&self) -> &[T]
pub fn as_unordered_slice(&self) -> &[T]
Get a view into the occupied part of the internal buffer.
Sourcepub fn ordered_iter(&self) -> impl Iterator<Item = &T>
pub fn ordered_iter(&self) -> impl Iterator<Item = &T>
Return an iterator over the items in chronological order - O(N).
Sourcepub fn into_ordered_iter(self) -> impl Iterator<Item = T>
pub fn into_ordered_iter(self) -> impl Iterator<Item = T>
This is irreversible and consumes the reservoir.
Sourcepub fn sampling_rate(&self) -> &SamplingRate
pub fn sampling_rate(&self) -> &SamplingRate
Returns a reference to the current sampling rate.
Sourcepub fn samples_stored(&self) -> usize
pub fn samples_stored(&self) -> usize
Returns the total number of samples written into the buffer since the beginning.
Sourcepub fn samples_seen(&self) -> usize
pub fn samples_seen(&self) -> usize
Returns the total number of samples observed by the sampler since the beginning.
Sourcepub fn sample(&mut self, value: T) -> SamplingOutcome<T>
pub fn sample(&mut self, value: T) -> SamplingOutcome<T>
Observe a value and possibly store it - O(1).
Performs a sampling “step”, consuming the value and storing it into the buffer, or returning it back if it’s discarded due to the sampling rate.
Trait Implementations§
Source§impl<T: Clone, const N: usize> Clone for SamplingReservoir<T, N>
impl<T: Clone, const N: usize> Clone for SamplingReservoir<T, N>
Source§fn clone(&self) -> SamplingReservoir<T, N>
fn clone(&self) -> SamplingReservoir<T, N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more