SamplingReservoir

Struct SamplingReservoir 

Source
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>

Source

pub const fn new() -> Self

Creates a empty reservoir, allocating an uninitialized buffer. Panics if N is not a power of two.

Source

pub const fn capacity(&self) -> usize

Returns N, the capacity of the internal buffer.

Source

pub fn len(&self) -> usize

Get the number of currently stored items. Can be from 0 to N-1 and never decreases.

Source

pub fn is_empty(&self) -> bool

Source

pub fn into_inner(self) -> Vec<T, N>

Consume self and return the internal item buffer.

Source

pub fn as_unordered_slice(&self) -> &[T]

Get a view into the occupied part of the internal buffer.

Source

pub fn ordered_iter(&self) -> impl Iterator<Item = &T>

Return an iterator over the items in chronological order - O(N).

Source

pub fn into_ordered_iter(self) -> impl Iterator<Item = T>

This is irreversible and consumes the reservoir.

Source

pub fn sampling_rate(&self) -> &SamplingRate

Returns a reference to the current sampling rate.

Source

pub fn samples_stored(&self) -> usize

Returns the total number of samples written into the buffer since the beginning.

Source

pub fn samples_seen(&self) -> usize

Returns the total number of samples observed by the sampler since the beginning.

Source

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>

Source§

fn clone(&self) -> SamplingReservoir<T, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for SamplingReservoir<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for SamplingReservoir<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for SamplingReservoir<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for SamplingReservoir<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for SamplingReservoir<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for SamplingReservoir<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.