pub struct RollingMedian<T> { /* private fields */ }Expand description
A rolling median type.
This keeps track of a window of items and allows calculating the RollingMedian::median of them.
Example:
let mut rolling_median = RollingMedian::new(2);
rolling_median.push(1);
assert_eq!(rolling_median.median(), 1);
assert_eq!(rolling_median.window_len(), 1);
rolling_median.push(3);
assert_eq!(rolling_median.median(), 2);
assert_eq!(rolling_median.window_len(), 2);
rolling_median.push(5);
assert_eq!(rolling_median.median(), 4);
assert_eq!(rolling_median.window_len(), 2);Implementations§
Source§impl<T> RollingMedian<T>
impl<T> RollingMedian<T>
Sourcepub fn new(target_window: usize) -> Self
pub fn new(target_window: usize) -> Self
Creates a new RollingMedian with a certain target window length.
target_window is the maximum amount of items to keep in the rolling window.
Sourcepub fn from_vec(vec: Vec<T>, target_window: usize) -> Self
pub fn from_vec(vec: Vec<T>, target_window: usize) -> Self
Creates a new RollingMedian from a Vec with a certain target window length.
target_window is the maximum amount of items to keep in the rolling window.
§Panics
This function panics if vec.len() > target_window.
Sourcepub fn pop_front(&mut self)
pub fn pop_front(&mut self)
Pops the front of the window, i.e. the oldest item.
This is often not needed as RollingMedian::push will handle popping old values when they fall
out of the window.
Sourcepub fn push(&mut self, item: T)
pub fn push(&mut self, item: T)
Push an item to the back of the window.
This will pop the oldest item in the window if the target length has been exceeded.
Sourcepub fn append_front(&mut self, items: Vec<T>)
pub fn append_front(&mut self, items: Vec<T>)
Append some values to the front of the window.
These new values will be the oldest items in the window. The order of the inputted items will be
kept, i.e. the first item in the Vec will be the oldest item in the queue.
Sourcepub fn window_len(&self) -> usize
pub fn window_len(&self) -> usize
Returns the number of items currently in the RollingMedian.
Sourcepub fn median(&self) -> T
pub fn median(&self) -> T
Calculates the median of the values currently in the RollingMedian.
Sourcepub fn median_with_grace(&self, grace: usize) -> T
pub fn median_with_grace(&self, grace: usize) -> T
Calculates a median value with a set amount of grace values.
grace values are minimum values added to the back of the RollingMedian. The median is then
got as if these values had been added and replaced any values at the front, if the capacity is
reached.
Trait Implementations§
Source§impl<T: Clone> Clone for RollingMedian<T>
impl<T: Clone> Clone for RollingMedian<T>
Source§fn clone(&self) -> RollingMedian<T>
fn clone(&self) -> RollingMedian<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for RollingMedian<T>
impl<T: Debug> Debug for RollingMedian<T>
impl<T: Eq> Eq for RollingMedian<T>
Source§impl<T: Ord> Ord for RollingMedian<T>
impl<T: Ord> Ord for RollingMedian<T>
Source§fn cmp(&self, other: &RollingMedian<T>) -> Ordering
fn cmp(&self, other: &RollingMedian<T>) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for RollingMedian<T>
impl<T: PartialEq> PartialEq for RollingMedian<T>
Source§impl<T: PartialOrd> PartialOrd for RollingMedian<T>
impl<T: PartialOrd> PartialOrd for RollingMedian<T>
impl<T: PartialEq> StructuralPartialEq for RollingMedian<T>
Auto Trait Implementations§
impl<T> Freeze for RollingMedian<T>
impl<T> RefUnwindSafe for RollingMedian<T>where
T: RefUnwindSafe,
impl<T> Send for RollingMedian<T>where
T: Send,
impl<T> Sync for RollingMedian<T>where
T: Sync,
impl<T> Unpin for RollingMedian<T>where
T: Unpin,
impl<T> UnsafeUnpin for RollingMedian<T>
impl<T> UnwindSafe for RollingMedian<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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 more§impl<T> Pointable for T
impl<T> Pointable for T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 64 bytes