Skip to main content

Window

Struct Window 

Source
pub struct Window<const N: usize> { /* private fields */ }
Expand description

A fixed-capacity window over the most recent N readings.

Many field decisions look not at the latest reading but at the recent run of them: the lowest battery voltage in the last minute, the average flow over the last ten samples, how widely a tank level is bouncing. A Window keeps the last N readings in a ring buffer - no allocation, so it runs on a microcontroller - and reports their spread. It is the base the forecasting helpers build on.

The population variance is given directly; the standard deviation is its square root, left to the caller so the type stays dependency-free. Capacity N should be at least one; a zero-capacity window simply holds nothing.

§Examples

use pamoja_kit::Window;

// Keep the last four tank-level readings and read their spread.
let mut levels = Window::<4>::new();
for reading in [40.0, 42.0, 38.0, 41.0] {
    levels.push(reading);
}
assert!(levels.is_full());
assert_eq!(levels.min(), Some(38.0));
assert_eq!(levels.max(), Some(42.0));
assert_eq!(levels.range(), Some(4.0));
assert_eq!(levels.latest(), Some(41.0));

Implementations§

Source§

impl<const N: usize> Window<N>

Source

pub fn new() -> Self

Creates an empty window with capacity N.

§Returns

A window holding no readings yet.

Source

pub fn push(&mut self, reading: f32)

Adds a reading, evicting the oldest once the window is full.

§Arguments
  • reading - the value to record.
Source

pub fn len(&self) -> usize

Returns the number of readings currently held, at most N.

Source

pub fn is_empty(&self) -> bool

Returns true if the window holds no readings.

Source

pub fn is_full(&self) -> bool

Returns true if the window holds its full capacity of N readings.

Source

pub fn capacity(&self) -> usize

Returns the window’s capacity, N.

Source

pub fn latest(&self) -> Option<f32>

Returns the most recent reading, or None if the window is empty.

Source

pub fn oldest(&self) -> Option<f32>

Returns the oldest reading still held, or None if the window is empty.

Source

pub fn min(&self) -> Option<f32>

Returns the smallest reading in the window, or None if it is empty.

Source

pub fn max(&self) -> Option<f32>

Returns the largest reading in the window, or None if it is empty.

Source

pub fn range(&self) -> Option<f32>

Returns the spread (largest minus smallest), or None if the window is empty.

Source

pub fn mean(&self) -> Option<f32>

Returns the mean of the readings, or None if the window is empty.

Source

pub fn variance(&self) -> Option<f32>

Returns the population variance of the readings, or None if the window is empty.

§Returns

The mean squared deviation from the mean. The standard deviation is its square root.

Trait Implementations§

Source§

impl<const N: usize> Clone for Window<N>

Source§

fn clone(&self) -> Window<N>

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<const N: usize> Copy for Window<N>

Source§

impl<const N: usize> Debug for Window<N>

Source§

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

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

impl<const N: usize> Default for Window<N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Window<N>
where [f32; N]: Freeze,

§

impl<const N: usize> RefUnwindSafe for Window<N>
where [f32; N]: RefUnwindSafe,

§

impl<const N: usize> Send for Window<N>
where [f32; N]: Send,

§

impl<const N: usize> Sync for Window<N>
where [f32; N]: Sync,

§

impl<const N: usize> Unpin for Window<N>
where [f32; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for Window<N>
where [f32; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for Window<N>
where [f32; N]: 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.