pub struct BinaryHeap<T> where
    T: PackedLayout + Ord
{ /* private fields */ }
Expand description

A priority queue implemented with a binary heap.

Note

The heap is a max-heap by default, i.e. the first element is the largest. Either Reverse or a custom Ord implementation can be used to make BinaryHeap a min-heap. This then makes heap.pop() return the smallest value instead of the largest one.

Implementations

Creates a new empty storage heap.

Returns the number of elements in the heap, also referred to as its length.

Returns true if the heap contains no elements.

Returns an iterator yielding shared references to all elements of the heap.

Note

Avoid unbounded iteration over large heaps. Prefer using methods like Iterator::take in order to limit the number of yielded elements.

Returns a shared reference to the greatest element of the heap

Returns None if the heap is empty

Returns an exclusive reference to the greatest element of the heap

Returns None if the heap is empty

Note:

If the PeekMut value is leaked, the heap may be in an inconsistent state.

Example
use ink_storage::collections::BinaryHeap;
let mut heap = BinaryHeap::new();
assert!(heap.peek_mut().is_none());

heap.push(1);
heap.push(5);
heap.push(2);
{
    let mut val = heap.peek_mut().unwrap();
    *val = 0;
}
assert_eq!(heap.peek(), Some(&2));

Pops greatest element from the heap and returns it

Returns None if the heap is empty

Removes all elements from this heap.

Note

Use this method to clear the heap instead of e.g. iterative pop(). This method performs significantly better and does not actually read any of the elements (whereas pop() does).

Pushes the given element to the binary heap.

Trait Implementations

Formats the value using the given formatter. Read more

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

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Creates a value from an iterator. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Default initializes the implementing type using spread layout. Read more

The footprint of the type. Read more

Pulls an instance of Self from the contract storage. Read more

Pushes an instance of Self to the contract storage. Read more

Clears an instance of Self from the contract storage. Read more

Indicates whether a type requires deep clean-up of its state meaning that a clean-up routine has to decode an entity into an instance in order to eventually recurse upon its tear-down. This is not required for the majority of primitive data types such as i32, however types such as storage::Box that might want to forward the clean-up procedure to their inner T require a deep clean-up. Read more

Returns the static storage layout of Self. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.