Struct MinMaxHeap

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

Source

pub fn new() -> Self

Creates a new, empty MinMaxHeap.

O(1).

Source

pub fn with_capacity(len: usize) -> Self

Creates a new, empty MinMaxHeap with space allocated to hold len elements.

O(n).

Source

pub fn len(&self) -> usize

The number of elements in the heap.

O(1).

Source

pub fn is_empty(&self) -> bool

Is the heap empty?

O(1).

Source§

impl<T: Ord> MinMaxHeap<T>

Source

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.

Source

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

Gets a reference to the minimum element, if any.

O(1).

Source

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.

Source

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

Gets a reference to the maximum element, if any.

O(1).

Source

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.

Source

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

Removes the minimum element, if any.

O(log n).

Source

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

Removes the maximum element, if any.

O(log n).

Source

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).

Source

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).

Source

pub fn replace_min(&mut self, element: T) -> Option<T>

Pops the minimum, then pushes an element in an optimized fashion.

O(log n).

Source

pub fn replace_max(&mut self, element: T) -> Option<T>

Pops the maximum, then pushes an element in an optimized fashion.

O(log n).

Source

pub fn into_vec_asc(self) -> Vec<T>

Returns an ascending (sorted) vector, reusing the heap’s storage.

O(n log n).

Source

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>

Source

pub fn clear(&mut self)

Drops all items from the heap.

O(n)

Source

pub fn capacity(&self) -> usize

The number of elements the heap can hold without reallocating.

O(1)

Source

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.

Source

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.

Source

pub fn shrink_to_fit(&mut self)

Discards extra capacity.

O(n)

Source

pub fn into_vec(self) -> Vec<T>

Consumes the MinMaxHeap and returns its elements in a vector in arbitrary order.

O(n)

Source

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.

Source

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.

Source

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.

Source

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>

Source§

fn clone(&self) -> MinMaxHeap<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for MinMaxHeap<T>

Source§

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

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

impl<T> Default for MinMaxHeap<T>

Source§

fn default() -> Self

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

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

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, T: Ord + Clone + 'a> Extend<&'a T> for MinMaxHeap<T>

Source§

fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Ord> Extend<T> for MinMaxHeap<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Ord> From<Vec<T>> for MinMaxHeap<T>

Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Ord> FromIterator<T> for MinMaxHeap<T>

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<'a, T> IntoIterator for &'a MinMaxHeap<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for MinMaxHeap<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Serialize for MinMaxHeap<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MinMaxHeap<T>

§

impl<T> RefUnwindSafe for MinMaxHeap<T>
where T: RefUnwindSafe,

§

impl<T> Send for MinMaxHeap<T>
where T: Send,

§

impl<T> Sync for MinMaxHeap<T>
where T: Sync,

§

impl<T> Unpin for MinMaxHeap<T>
where T: Unpin,

§

impl<T> UnwindSafe for MinMaxHeap<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,