BinaryHeap

Type Alias BinaryHeap 

Source
pub type BinaryHeap<T, K, const N: usize> = BinaryHeapInner<T, K, OwnedVecStorage<T, N>>;
Expand description

A priority queue implemented with a binary heap.

This can be either a min-heap or a max-heap.

It is a logic error for an item to be modified in such a way that the item’s ordering relative to any other item, as determined by the Ord trait, changes while it is in the heap. This is normally only possible through Cell, RefCell, global state, I/O, or unsafe code.

use heapless::binary_heap::{BinaryHeap, Max};

let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();

// We can use peek to look at the next item in the heap. In this case,
// there's no items in there yet so we get None.
assert_eq!(heap.peek(), None);

// Let's add some scores...
heap.push(1).unwrap();
heap.push(5).unwrap();
heap.push(2).unwrap();

// Now peek shows the most important item in the heap.
assert_eq!(heap.peek(), Some(&5));

// We can check the length of a heap.
assert_eq!(heap.len(), 3);

// We can iterate over the items in the heap, although they are returned in
// a random order.
for x in &heap {
    println!("{}", x);
}

// If we instead pop these scores, they should come back in order.
assert_eq!(heap.pop(), Some(5));
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), None);

// We can clear the heap of any remaining items.
heap.clear();

// The heap should now be empty.
assert!(heap.is_empty())

Aliased Type§

pub struct BinaryHeap<T, K, const N: usize> { /* private fields */ }

Implementations§

Source§

impl<T, K, const N: usize> BinaryHeap<T, K, N>

Source

pub const fn new() -> Self

Creates an empty BinaryHeap as a $K-heap.

use heapless::binary_heap::{BinaryHeap, Max};

// allocate the binary heap on the stack
let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
heap.push(4).unwrap();

// allocate the binary heap in a static variable
static mut HEAP: BinaryHeap<i32, Max, 8> = BinaryHeap::new();
Source§

impl<T, K, const N: usize> BinaryHeap<T, K, N>

Source

pub fn into_vec(self) -> Vec<T, N, usize>

Returns the underlying Vec<T,N>. Order is arbitrary and time is O(1).

Trait Implementations§

Source§

impl<T, K, const N: usize> Clone for BinaryHeap<T, K, N>
where K: Kind, T: Ord + Clone,

Source§

fn clone(&self) -> Self

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, K, const N: usize> Default for BinaryHeap<T, K, N>
where T: Ord, K: Kind,

Source§

fn default() -> Self

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