Skip to main content

RollingMax

Struct RollingMax 

Source
pub struct RollingMax<T, const WINDOW: usize> { /* private fields */ }
Expand description

§Rolling Max

Tracks the largest value in a fixed-size window.

§Design

The algorithm for this is well-known but not formalized anywhere I found easily accessible. The constraint of accumulating values internally is also a slight divergence from how this problem is typically presented.

Implementations§

Source§

impl<T, const W: usize> RollingMax<T, W>
where T: PartialOrd,

Source

pub const fn new() -> Self

Constructs a new empty RollingMax.

This type is stored entirely on the stack, so be aware of window size. Boxing might be a good idea. Doing so yourself enables cache-friendlier patterns than if each RollingMax were unconditionally allocated on the heap.

use core::cmp::Reverse;
use high_roller::rolling_max::RollingMax;

const WINDOW: usize = 6000;

#[derive(Default)]
struct MyTelemetry {
    max_latency: RollingMax<u32, WINDOW>,
    min_latency: RollingMax<Reverse<u32>, WINDOW>,
    largest_batch: RollingMax<usize, WINDOW>
}

// `MyTelemetry` is too big to live comfortably on the stack.
// But keeping everything in one allocation may yield friendlier
// cache access patterns.
const _: () = assert!(core::mem::size_of::<MyTelemetry>() == 240120);
let _telemetry = Box::new(MyTelemetry::default());
Source

pub fn push(&mut self, entry: T)

Adds an entry to the rolling window. If the window is full, the oldest member is evicted.

use high_roller::rolling_max::RollingMax;

let mut window: RollingMax<usize, 3> = RollingMax::new();

window.push(usize::MAX);
window.push(1);
window.push(2);

assert_eq!(window.max().copied(), Some(usize::MAX));

// Evicts the first entry, which was previously the max.
window.push(0);
assert_eq!(window.max().copied(), Some(2));
Source

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

Returns the maximum entry in the window or None if the window is empty.

use high_roller::rolling_max::RollingMax;

let mut window: RollingMax<usize, 3> = RollingMax::new();
assert_eq!(window.max(), None);

window.push(5);
// Once any entry has been pushed, max will never again
// return None.
assert_eq!(window.max().copied(), Some(5));

Trait Implementations§

Source§

impl<T: Debug, const WINDOW: usize> Debug for RollingMax<T, WINDOW>

Source§

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

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

impl<T: Default, const WINDOW: usize> Default for RollingMax<T, WINDOW>

Source§

fn default() -> RollingMax<T, WINDOW>

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

Auto Trait Implementations§

§

impl<T, const WINDOW: usize> Freeze for RollingMax<T, WINDOW>
where T: Freeze,

§

impl<T, const WINDOW: usize> RefUnwindSafe for RollingMax<T, WINDOW>
where T: RefUnwindSafe,

§

impl<T, const WINDOW: usize> Send for RollingMax<T, WINDOW>
where T: Send,

§

impl<T, const WINDOW: usize> Sync for RollingMax<T, WINDOW>
where T: Sync,

§

impl<T, const WINDOW: usize> Unpin for RollingMax<T, WINDOW>
where T: Unpin,

§

impl<T, const WINDOW: usize> UnsafeUnpin for RollingMax<T, WINDOW>
where T: UnsafeUnpin,

§

impl<T, const WINDOW: usize> UnwindSafe for RollingMax<T, WINDOW>
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> 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 = 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.