pub struct BinaryHeap<T>where
    T: Ord,
{ pub mode: ExpansionMode, /* private fields */ }
Expand description

An implementation of a priority queue using a binary heap. The binary heap is implemented using an array. The binary heap is a complete binary tree, meaning that every level of the tree is filled except for the last level, which is filled from left to right. The binary heap is stored using a function that determines the order of the elements in the heap.

The default heap function is equivalent to a min-heap, meaning that the element with the smallest value is at the top of the heap. For a max-heap, the heap can be created using the max_heap function.

Examples

use trait_based_collection::{prelude::*, BinaryHeap};

let mut heap = BinaryHeap::min_heap(16, ExpansionMode::Expand(2.0));
heap.add(5);
heap.add(3);
heap.add(7);
heap.add(1);

assert_eq!(heap.remove(), Some(1));
assert_eq!(heap.remove(), Some(3));
assert_eq!(heap.remove(), Some(5));
assert_eq!(heap.remove(), Some(7));
assert_eq!(heap.remove(), None);

Fields

mode: ExpansionMode

The current mode of expansion of the deque that determinate the behaviour the collection is full. See ExpansionMode for more information.

Implementations

Creates a new BinaryHeap with the given capacity, expansion mode and function that determines the order of the elements in the heap. The root of the heap is the element that has Ordering::Less when compared to the other elements in the heap.

This a generic constructor, if you want a max_heap or a min_heap, use the corresponding functions.

Panics

This function panics if the capacity is 0.

Examples
use trait_based_collection::{prelude::*, BinaryHeap};

let mut heap: BinaryHeap<i32> = BinaryHeap::new(10,
                                                ExpansionMode::Ignore,
                                                |a, b| a.cmp(b));  // Min-heap.

Creates a MinHeap with the given capacity and expansion mode.

Panics

This function panics if the capacity is 0.

Examples
use trait_based_collection::{prelude::*, BinaryHeap};

let mut heap: BinaryHeap<i32> = BinaryHeap::min_heap(10, ExpansionMode::Overwrite);

Creates a MaxHeap with the given capacity and expansion mode.

Panics

This function panics if the capacity is 0.

Examples
use trait_based_collection::{prelude::*, BinaryHeap};

let mut heap: BinaryHeap<i32> = BinaryHeap::max_heap(10, ExpansionMode::Overwrite);

Trait Implementations

Creates a new Collection with a default capacity. Read more
Creates a new Collection with a specific capacity. Read more
Creates a new Collection with a capacity that is at least the specified capacity. Read more
Adds an item to the Collection. Read more
Removes an item from the Collection. Read more
Clears all items from the Collection while keeping the capacity. Read more
Returns an immutable reference of the item that will be removed next. Read more
Returns a mutable reference of the item that will be removed next. Read more
Returns the number of items in the Collection. Read more
Returns a immutable reference to the n-th item in the Collection. Read more
Returns a mutable reference to the n-th item in the Collection. Read more
Searches for an item in the Collection and returns its index if found. Returns None if the item is not found. Read more
Checks if an item is in the Collection. Read more
Checks if the Collection is empty. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Executes the destructor for this 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 new fixed size Collection with the specified capacity and ExpansionMode. Read more
Returns the maximum amount of items that the collection can hold without expanding. Read more
Expands the capacity of the collection by at least the specified amount. This amount or more will be dded to the capacity. Read more
Returns the ExpansionMode of the collection . This is used to determine how the collection will behave when it is full. Read more
Checks if the number of items in the FixedSizeCollection is equal to the capacity. Read more
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of reference the immutable iterator (Iter) iterates over the items in the Collection. The reference is only valid for the duration of the iteration. Read more
The type of mutable reference the mutable iterator (IterMut) iterates over the items in the Collection. The reference is only valid for the duration of the iteration. Read more
Which kind of iterator is returned by iter. Read more
Which kind of iterator is returned by iter_mut. Read more
Creates an immutable iterator over the items in the Collection without consuming them. Read more
Creates a mutable iterator over the items in the Collection without consuming them. 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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more
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.