Skip to main content

RollingMedian

Struct RollingMedian 

Source
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>
where T: Ord + PartialOrd + Add<Output = T> + Sub<Output = T> + Div<Output = T> + Mul<Output = T> + Copy + From<u8>,

Source

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.

Source

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.

Source

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.

Source

pub fn pop_back(&mut self)

Pops the back of the window, i.e. the youngest item.

Source

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.

Source

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.

Source

pub fn window_len(&self) -> usize

Returns the number of items currently in the RollingMedian.

Source

pub fn median(&self) -> T

Calculates the median of the values currently in the RollingMedian.

Source

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>

Source§

fn clone(&self) -> RollingMedian<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for RollingMedian<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Eq> Eq for RollingMedian<T>

Source§

impl<T: Ord> Ord for RollingMedian<T>

Source§

fn cmp(&self, other: &RollingMedian<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for RollingMedian<T>

Source§

fn eq(&self, other: &RollingMedian<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialOrd> PartialOrd for RollingMedian<T>

Source§

fn partial_cmp(&self, other: &RollingMedian<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.

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