pub struct MovingMin<T> { /* private fields */ }
Expand description
Keep track of the minimum value in a sliding window.
The implementation is taken from https://github.com/spebern/moving_min_max/blob/master/src/lib.rs
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.
Each element is stored with the current min/max. One stack to push and another one for pop. If pop stack is empty, push to this stack all elements popped from first stack while updating their current min/max. Now pop from the second stack (MovingMin/Max struct works as a queue). To find the minimum element of the queue, look at the smallest/largest two elements of the individual stacks, then take the minimum of those two values.
The complexity of the operations are
- O(1) for getting the minimum/maximum
- O(1) for push
- amortized O(1) for pop
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);
Implementations§
Source§impl<T: Clone + PartialOrd> MovingMin<T>
impl<T: Clone + PartialOrd> MovingMin<T>
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for MovingMin<T>
impl<T> RefUnwindSafe for MovingMin<T>where
T: RefUnwindSafe,
impl<T> Send for MovingMin<T>where
T: Send,
impl<T> Sync for MovingMin<T>where
T: Sync,
impl<T> Unpin for MovingMin<T>where
T: Unpin,
impl<T> UnwindSafe for MovingMin<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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