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>
impl<const N: usize> Window<N>
Sourcepub fn push(&mut self, reading: f32)
pub fn push(&mut self, reading: f32)
Adds a reading, evicting the oldest once the window is full.
§Arguments
reading- the value to record.
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true if the window holds its full capacity of N readings.
Sourcepub fn latest(&self) -> Option<f32>
pub fn latest(&self) -> Option<f32>
Returns the most recent reading, or None if the window is empty.
Sourcepub fn oldest(&self) -> Option<f32>
pub fn oldest(&self) -> Option<f32>
Returns the oldest reading still held, or None if the window is empty.
Sourcepub fn min(&self) -> Option<f32>
pub fn min(&self) -> Option<f32>
Returns the smallest reading in the window, or None if it is empty.
Sourcepub fn max(&self) -> Option<f32>
pub fn max(&self) -> Option<f32>
Returns the largest reading in the window, or None if it is empty.
Sourcepub fn range(&self) -> Option<f32>
pub fn range(&self) -> Option<f32>
Returns the spread (largest minus smallest), or None if the window is empty.