pub struct MovingWindow<T: MovingWindowValue> { /* private fields */ }Expand description
A moving window that maintains the last N values of type T and calculates
their arithmetic mean. All values in the window have equal weight and the
oldest value is dropped when the window exceeds its configured capacity.
Implementations§
Source§impl<T: MovingWindowValue> MovingWindow<T>
impl<T: MovingWindowValue> MovingWindow<T>
Sourcepub fn new(init_value: T, max_size: usize) -> Self
pub fn new(init_value: T, max_size: usize) -> Self
Creates a new MovingWindow with the specified maximum size and an
init_value. The provided max_size must be greater than 0.
Sourcepub fn add_value(&mut self, value: T)
pub fn add_value(&mut self, value: T)
Adds a new value to the window. If the window is at capacity, the oldest value is removed before adding the new value.
Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Get the current average of all values in the window. Returns the value’s zero if the window is empty.
pub fn is_empty(&self) -> bool
Trait Implementations§
Source§impl<T: Clone + MovingWindowValue> Clone for MovingWindow<T>
impl<T: Clone + MovingWindowValue> Clone for MovingWindow<T>
Source§fn clone(&self) -> MovingWindow<T>
fn clone(&self) -> MovingWindow<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<T> Freeze for MovingWindow<T>where
T: Freeze,
impl<T> RefUnwindSafe for MovingWindow<T>where
T: RefUnwindSafe,
impl<T> Send for MovingWindow<T>where
T: Send,
impl<T> Sync for MovingWindow<T>where
T: Sync,
impl<T> Unpin for MovingWindow<T>where
T: Unpin,
impl<T> UnsafeUnpin for MovingWindow<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for MovingWindow<T>where
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
Mutably borrows from an owned value. Read more