datafusion_functions_aggregate::min_max

Struct MovingMin

Source
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>

Source

pub fn new() -> Self

Creates a new MovingMin to keep track of the minimum in a sliding window.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new MovingMin to keep track of the minimum in a sliding window with capacity allocated slots.

Source

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

Returns the minimum of the sliding window or None if the window is empty.

Source

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

Pushes a new element into the sliding window.

Source

pub fn pop(&mut self) -> Option<T>

Removes and returns the last value of the sliding window.

Source

pub fn len(&self) -> usize

Returns the number of elements stored in the sliding window.

Source

pub fn is_empty(&self) -> bool

Returns true if the moving window contains no elements.

Trait Implementations§

Source§

impl<T: Debug> Debug for MovingMin<T>

Source§

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

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

impl<T: Clone + PartialOrd> Default for MovingMin<T>

Source§

fn default() -> Self

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

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T