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>
impl<T, K, const N: usize> BinaryHeap<T, K, N>
Sourcepub const fn new() -> Self
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();