pub struct MmHeap<T> { /* private fields */ }
Expand description
An implicit binary heap that can efficiently find the min or max of its contained elements.
- Find min / find max:
O(1)
- Pop min / pop max:
O(log(n))
- Push:
O(log(n))
- Heapify:
O(n)
The comparison function can be changed after creation by just callingMmHeap::ify_by
again. Rust’s standardstd::collections::BinaryHeap
claims anO(1)
average complexity for push, but as it notes in the documentation this is amortized over a randomly ordered sequence. This heap is also a binary heap so for a randomly ordered sequence it could also be consideredO(1)
, but for this, the standard binary heap, and indeed any binary heap, the worst case insertion time isO(logn)
. Also, when a min (or minmax) heap is used to find them
largest elements in a large sequence ofn > m
elements or visa versa, the expected cost of insertion drops from1 - m/2^m
to … even lower.
Implementations§
source§impl<T> MmHeap<T>
impl<T> MmHeap<T>
sourcepub fn make(buf: Vec<T>, cmp: &impl Fn(&T, &T) -> Ordering) -> Self
pub fn make(buf: Vec<T>, cmp: &impl Fn(&T, &T) -> Ordering) -> Self
Create an MmHeap out of a vector and immediately heapify it according to cmp Note that cmp takes references, which means the common case of storing references in the heap can require wrapping the comparison function since it will have to take references to references
sourcepub fn ify_by(&mut self, cmp: &impl Fn(&T, &T) -> Ordering)
pub fn ify_by(&mut self, cmp: &impl Fn(&T, &T) -> Ordering)
Reorder the heap according to a new comparison function.
It’s impossible to add to the heap’s private buffer directly, so this is only necessary
when changing the comparison function. When calling Self::push_by
etc with the same comparison
function, the heap will maintain its invariant
sourcepub fn peek_max_by(&self, cmp: &impl Fn(&T, &T) -> Ordering) -> Option<&T>
pub fn peek_max_by(&self, cmp: &impl Fn(&T, &T) -> Ordering) -> Option<&T>
Get the maximum element without removing it
sourcepub fn push_by(&mut self, e: T, cmp: &impl Fn(&T, &T) -> Ordering)
pub fn push_by(&mut self, e: T, cmp: &impl Fn(&T, &T) -> Ordering)
Insert an element into the heap Elements that compare equal are fine, but their order will be unspecified
sourcepub fn pop_min_by(&mut self, cmp: &impl Fn(&T, &T) -> Ordering) -> Option<T>
pub fn pop_min_by(&mut self, cmp: &impl Fn(&T, &T) -> Ordering) -> Option<T>
Get the minimal element and remove it
sourcepub fn pop_max_by(&mut self, cmp: &impl Fn(&T, &T) -> Ordering) -> Option<T>
pub fn pop_max_by(&mut self, cmp: &impl Fn(&T, &T) -> Ordering) -> Option<T>
Get the maximal element and remove it
sourcepub fn pushpop_min_by(&mut self, e: T, cmp: &impl Fn(&T, &T) -> Ordering) -> T
pub fn pushpop_min_by(&mut self, e: T, cmp: &impl Fn(&T, &T) -> Ordering) -> T
Insert a new element and remove the min element in the resulting heap in a single operation
Could be faster than Self::push_by
+ [
Self::pop_min_by`] separately
(the current implementation is only more efficient if the heap is empty, but it will always
be at least as good)