pub struct MinMaxHeap<T>(/* private fields */);
Expand description
A double-ended priority queue.
Most operations are O(log n).
Implementations§
Source§impl<T> MinMaxHeap<T>
impl<T> MinMaxHeap<T>
Sourcepub fn with_capacity(len: usize) -> Self
pub fn with_capacity(len: usize) -> Self
Creates a new, empty MinMaxHeap
with space allocated to hold
len
elements.
O(n).
Source§impl<T: Ord> MinMaxHeap<T>
impl<T: Ord> MinMaxHeap<T>
Sourcepub fn push(&mut self, element: T)
pub fn push(&mut self, element: T)
Adds an element to the heap.
Amortized O(log n); worst-case O(n) when the backing vector needs to grow.
Sourcepub fn peek_min_mut(&mut self) -> Option<PeekMinMut<'_, T>>
pub fn peek_min_mut(&mut self) -> Option<PeekMinMut<'_, T>>
Returns a mutable reference to the minimum element, if any. Once this reference is dropped, the heap is adjusted if necessary.
Note: If the PeekMinMut
value is leaked, the heap may be in an
inconsistent state.
O(1) for the peek; O(log n) when the reference is dropped.
Sourcepub fn peek_max_mut(&mut self) -> Option<PeekMaxMut<'_, T>>
pub fn peek_max_mut(&mut self) -> Option<PeekMaxMut<'_, T>>
Returns a mutable reference to the maximum element, if any. Once this reference is dropped, the heap is adjusted if necessary.
Note: If the PeekMaxMut
value is leaked, the heap may be in an
inconsistent state.
O(1) for the peek; O(log n) when the reference is dropped.
Sourcepub fn push_pop_min(&mut self, element: T) -> T
pub fn push_pop_min(&mut self, element: T) -> T
Pushes an element, then pops the minimum element.
Unlike a push followed by a pop, this combined operation will not allocate.
O(log n).
Sourcepub fn push_pop_max(&mut self, element: T) -> T
pub fn push_pop_max(&mut self, element: T) -> T
Pushes an element, then pops the maximum element in an optimized fashion.
Unlike a push followed by a pop, this combined operation will not allocate.
O(log n).
Sourcepub fn replace_min(&mut self, element: T) -> Option<T>
pub fn replace_min(&mut self, element: T) -> Option<T>
Pops the minimum, then pushes an element in an optimized fashion.
O(log n).
Sourcepub fn replace_max(&mut self, element: T) -> Option<T>
pub fn replace_max(&mut self, element: T) -> Option<T>
Pops the maximum, then pushes an element in an optimized fashion.
O(log n).
Sourcepub fn into_vec_asc(self) -> Vec<T>
pub fn into_vec_asc(self) -> Vec<T>
Returns an ascending (sorted) vector, reusing the heap’s storage.
O(n log n).
Sourcepub fn into_vec_desc(self) -> Vec<T>
pub fn into_vec_desc(self) -> Vec<T>
Returns an descending (sorted) vector, reusing the heap’s storage.
O(n log n).
Source§impl<T> MinMaxHeap<T>
impl<T> MinMaxHeap<T>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
The number of elements the heap can hold without reallocating.
O(1)
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional
more
elements to be inserted in the given MinMaxHeap
.
O(n)
§Panics
Panics if the new capacity overflows usize
.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves the minimum capacity for at least additional
more
elements to be inserted in the given MinMaxHeap
.
O(n)
§Panics
Panics if the new capacity overflows usize
.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Discards extra capacity.
O(n)
Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Consumes the MinMaxHeap
and returns its elements in a vector
in arbitrary order.
O(n)
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns a borrowing iterator over the min-max-heap’s elements in arbitrary order.
O(1) on creation, and O(1) for each next()
operation.
Sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Returns a draining iterator over the min-max-heap’s elements in arbitrary order.
O(1) on creation, and O(1) for each next()
operation.
Sourcepub fn drain_asc(&mut self) -> DrainAsc<'_, T> ⓘ
pub fn drain_asc(&mut self) -> DrainAsc<'_, T> ⓘ
Returns a draining iterator over the min-max-heap’s elements in ascending (min-first) order.
O(1) on creation, and O(log n) for each next()
operation.
Sourcepub fn drain_desc(&mut self) -> DrainDesc<'_, T> ⓘ
pub fn drain_desc(&mut self) -> DrainDesc<'_, T> ⓘ
Returns a draining iterator over the min-max-heap’s elements in descending (max-first) order.
O(1) on creation, and O(log n) for each next()
operation.
Trait Implementations§
Source§impl<T: Clone> Clone for MinMaxHeap<T>
impl<T: Clone> Clone for MinMaxHeap<T>
Source§fn clone(&self) -> MinMaxHeap<T>
fn clone(&self) -> MinMaxHeap<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Debug> Debug for MinMaxHeap<T>
impl<T: Debug> Debug for MinMaxHeap<T>
Source§impl<T> Default for MinMaxHeap<T>
impl<T> Default for MinMaxHeap<T>
Source§impl<'de, T> Deserialize<'de> for MinMaxHeap<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for MinMaxHeap<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'a, T: Ord + Clone + 'a> Extend<&'a T> for MinMaxHeap<T>
impl<'a, T: Ord + Clone + 'a> Extend<&'a T> for MinMaxHeap<T>
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T: Ord> Extend<T> for MinMaxHeap<T>
impl<T: Ord> Extend<T> for MinMaxHeap<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)