pub struct StatisticsWindow<T: Copy + Into<f64>> { /* private fields */ }Expand description
A simple sliding-window buffer that retains the most-recent capacity
values and exposes statistical helpers.
Unlike LockFreeRingBuffer this type is single-threaded and designed
for convenience, not throughput.
§Type bound
T: Copy + Into<f64> so that integer and floating-point scalars can all be
treated uniformly.
§Example
use trustformers_debug::ring_buffer::StatisticsWindow;
let mut w = StatisticsWindow::new(4);
w.push(1u32);
w.push(2u32);
w.push(3u32);
assert_eq!(w.mean(), Some(2.0));Implementations§
Source§impl<T: Copy + Into<f64>> StatisticsWindow<T>
impl<T: Copy + Into<f64>> StatisticsWindow<T>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new window that retains at most capacity values.
Panics if capacity == 0.
Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Push a value; if the window is full the oldest value is evicted.
Sourcepub fn iter_ordered(&self) -> impl Iterator<Item = T> + '_
pub fn iter_ordered(&self) -> impl Iterator<Item = T> + '_
Iterate over the window contents in insertion order (oldest first).
Sourcepub fn std_dev(&self) -> Option<f64>
pub fn std_dev(&self) -> Option<f64>
Population standard deviation of the current window.
Returns None when fewer than 2 values are stored.
Sourcepub fn percentile(&self, p: f64) -> Option<f64>
pub fn percentile(&self, p: f64) -> Option<f64>
Approximate the p-th percentile (0.0–100.0) via a sorted copy.
Uses nearest-rank method.
Sourcepub fn windowed_mean(&self, window: usize) -> Option<f64>
pub fn windowed_mean(&self, window: usize) -> Option<f64>
Mean of the last window values (or all values if fewer available).
Auto Trait Implementations§
impl<T> Freeze for StatisticsWindow<T>
impl<T> RefUnwindSafe for StatisticsWindow<T>where
Vec<T>: RefUnwindSafe,
impl<T> Send for StatisticsWindow<T>
impl<T> Sync for StatisticsWindow<T>
impl<T> Unpin for StatisticsWindow<T>
impl<T> UnsafeUnpin for StatisticsWindow<T>where
Vec<T>: UnsafeUnpin,
impl<T> UnwindSafe for StatisticsWindow<T>where
Vec<T>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.