Struct heapless::binary_heap::BinaryHeap[][src]

pub struct BinaryHeap<T, N, KIND> where
    T: Ord,
    N: ArrayLength<T>,
    KIND: Kind
{ /* fields omitted */ }

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};
use heapless::consts::*;

let mut heap: BinaryHeap<_, U8, Max> = 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())

Methods

impl<T, N, K> BinaryHeap<T, N, K> where
    T: Ord,
    N: ArrayLength<T>,
    K: Kind
[src]

Creates an empty BinaryHeap as a $K-heap.

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

let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
heap.push(4).unwrap();

Returns the capacity of the binary heap.

Drops all items from the binary heap.

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

let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
heap.push(1).unwrap();
heap.push(3).unwrap();

assert!(!heap.is_empty());

heap.clear();

assert!(heap.is_empty());

Returns the length of the binary heap.

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

let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
heap.push(1).unwrap();
heap.push(3).unwrap();

assert_eq!(heap.len(), 2);

Checks if the binary heap is empty.

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

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

assert!(heap.is_empty());

heap.push(3).unwrap();
heap.push(5).unwrap();
heap.push(1).unwrap();

assert!(!heap.is_empty());

Important traits for Iter<'a, T>

Returns an iterator visiting all values in the underlying vector, in arbitrary order.

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

let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
heap.push(1).unwrap();
heap.push(2).unwrap();
heap.push(3).unwrap();
heap.push(4).unwrap();

// Print 1, 2, 3, 4 in arbitrary order
for x in heap.iter() {
    println!("{}", x);

}

Important traits for IterMut<'a, T>

Returns a mutable iterator visiting all values in the underlying vector, in arbitrary order.

WARNING Mutating the items in the binary heap can leave the heap in an inconsistent state.

Returns the top (greatest if max-heap, smallest if min-heap) item in the binary heap, or None if it is empty.

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

let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
assert_eq!(heap.peek(), None);

heap.push(1).unwrap();
heap.push(5).unwrap();
heap.push(2).unwrap();
assert_eq!(heap.peek(), Some(&5));

Removes the top (greatest if max-heap, smallest if min-heap) item from the binary heap and returns it, or None if it is empty.

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

let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
heap.push(1).unwrap();
heap.push(3).unwrap();

assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), None);

Removes the top (greatest if max-heap, smallest if min-heap) item from the binary heap and returns it, without checking if the binary heap is empty.

Pushes an item onto the binary heap.

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

let mut heap: BinaryHeap<_, U8, Max> = BinaryHeap::new();
heap.push(3).unwrap();
heap.push(5).unwrap();
heap.push(1).unwrap();

assert_eq!(heap.len(), 3);
assert_eq!(heap.peek(), Some(&5));

Pushes an item onto the binary heap without first checking if it's full.

Trait Implementations

impl<'a, T, N, K> IntoIterator for &'a BinaryHeap<T, N, K> where
    N: ArrayLength<T>,
    K: Kind,
    T: Ord
[src]

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

Auto Trait Implementations

impl<T, N, KIND> Send for BinaryHeap<T, N, KIND> where
    KIND: Send,
    T: Send

impl<T, N, KIND> Sync for BinaryHeap<T, N, KIND> where
    KIND: Sync,
    T: Sync