#sliding-window #min-max #sliding #moving #max #min #window

moving_min_max

Tracking minimum or maximum of sliding windows

10 releases (4 stable)

1.3.0 Jul 4, 2020
1.2.0 Jun 24, 2020
1.1.0 Apr 11, 2020
0.1.5 Feb 3, 2020
0.1.2 Jan 15, 2020

#1059 in Data structures

Download history 295/week @ 2024-07-24 654/week @ 2024-07-31 589/week @ 2024-08-07 330/week @ 2024-08-14 274/week @ 2024-08-21 75/week @ 2024-08-28 65/week @ 2024-09-04 77/week @ 2024-09-11 172/week @ 2024-09-18 123/week @ 2024-09-25 115/week @ 2024-10-02 32/week @ 2024-10-09 96/week @ 2024-10-16 85/week @ 2024-10-23 31/week @ 2024-10-30 36/week @ 2024-11-06

250 downloads per month

MIT/Apache

14KB
212 lines

moving min max

Crates.io docs.rs docs ci

Keep track of the minimum or maximum value in a sliding window.

moving min max provides one data structure for keeping track of the minimum value and one for keeping track of the maximum value in a sliding window.

The algorithm is based on the description here.

The complexity of the operations are

  • O(1) for getting the minimum/maximum
  • O(1) for push
  • amortized O(1) for pop
use moving_min_max::MovingMin;

let mut moving_min = MovingMin::<i32>::new();
moving_min.push(2);
moving_min.push(1);
moving_min.push(3);

assert_eq!(moving_min.min(), Some(&1));
assert_eq!(moving_min.pop(), Some(2));

assert_eq!(moving_min.min(), Some(&1));
assert_eq!(moving_min.pop(), Some(1));

assert_eq!(moving_min.min(), Some(&3));
assert_eq!(moving_min.pop(), Some(3));

assert_eq!(moving_min.min(), None);
assert_eq!(moving_min.pop(), None);

or

use moving_min_max::MovingMax;

let mut moving_max = MovingMax::<i32>::new();
moving_max.push(2);
moving_max.push(3);
moving_max.push(1);

assert_eq!(moving_max.max(), Some(&3));
assert_eq!(moving_max.pop(), Some(2));

assert_eq!(moving_max.max(), Some(&3));
assert_eq!(moving_max.pop(), Some(3));

assert_eq!(moving_max.max(), Some(&1));
assert_eq!(moving_max.pop(), Some(1));

assert_eq!(moving_max.max(), None);
assert_eq!(moving_max.pop(), None);

No runtime deps