Struct PairingHeap

Source
pub struct PairingHeap<T: Ord> { /* private fields */ }
Expand description

A priority queue based on a pairing heap.

Like BinaryHeap, PairingHeap is an implementation of a priority queue. Unlike BinaryHeap, PairingHeap provides an append method whose runtime is asymptotically optimal, at the cost of greater memory usage, slower iteration, and poor cache locality.

§Time Complexity

OperationTime Complexity
appendO(1)
peekO(1)
popO(log n) (amortized)
pushO(1)
push_popO(log n) (amortized)
replaceO(log n) (amortized)

Implementations§

Source§

impl<T: Ord> PairingHeap<T>

Source

pub fn new() -> Self

Returns a new heap.

Source

pub fn is_empty(&self) -> bool

Checks if the heap is empty.

Source

pub fn len(&self) -> usize

Returns the number of items in the heap.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator that yields references to the items in the heap in arbitrary order.

Source

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

Returns a reference to the greatest item in the heap.

Returns None if the heap is empty.

Source

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

Pushes the given item onto the heap.

Source

pub fn append(&mut self, other: &mut Self)

Moves the given heap’s items into the heap, leaving the given heap empty.

This is equivalent to, but likely to be faster than, the following:

heap.extend(heap2.drain());
Source

pub fn push_pop(&mut self, item: T) -> T

Pushes the given item onto the heap, then removes the greatest item in the heap and returns it.

This method is equivalent to, but likely faster than, the following:

heap.push(item);
let max = heap.pop().unwrap();
Source

pub fn replace(&mut self, item: T) -> Option<T>

Removes the greatest item in the heap, then pushes the given item onto the heap.

Returns the item that was removed, or None if the heap was empty.

This method is equivalent to, but likely faster than, the following:

let max = heap.pop();
heap.push(item);
Source

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

Removes the greatest item in the heap and returns it.

Returns None if the heap was empty.

If a call to this method is immediately preceded by a call to push, consider using push_pop instead. If a call to this method is immediately followed by a call to push, consider using replace instead.

Source

pub fn clear(&mut self)

Removes all items from the heap.

Source

pub fn drain(&mut self) -> Drain<'_, T>

Removes all items from the heap and returns an iterator that yields them in arbitrary order.

All items are removed even if the iterator is not exhausted. However, the behavior of this method is unspecified if the iterator is leaked (e.g. via mem::forget).

Trait Implementations§

Source§

impl<T: Clone + Ord> Clone for PairingHeap<T>

Source§

fn clone(&self) -> PairingHeap<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: Ord + Debug> Debug for PairingHeap<T>

Source§

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

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

impl<T: Ord> Default for PairingHeap<T>

Source§

fn default() -> Self

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

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

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, items: 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> FromIterator<T> for PairingHeap<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(items: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: Ord> IntoIterator for &'a PairingHeap<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) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T: Ord> IntoIterator for PairingHeap<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) -> IntoIter<T>

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for PairingHeap<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for PairingHeap<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.