pub struct BinaryHeapInner<T, K, S: VecStorage<T> + ?Sized> { /* private fields */ }
Expand description
Base struct for BinaryHeap
and BinaryHeapView
, generic over the VecStorage
.
In most cases you should use BinaryHeap
or BinaryHeapView
directly. Only use this
struct if you want to write code that’s generic over both.
Implementations§
Source§impl<T, K, const N: usize> BinaryHeapInner<T, K, VecStorageInner<[MaybeUninit<T>; N]>>
impl<T, K, const N: usize> BinaryHeapInner<T, K, VecStorageInner<[MaybeUninit<T>; 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();
Source§impl<T, K, const N: usize> BinaryHeapInner<T, K, VecStorageInner<[MaybeUninit<T>; N]>>
impl<T, K, const N: usize> BinaryHeapInner<T, K, VecStorageInner<[MaybeUninit<T>; N]>>
Source§impl<T, K, S: VecStorage<T>> BinaryHeapInner<T, K, S>
impl<T, K, S: VecStorage<T>> BinaryHeapInner<T, K, S>
Sourcepub fn as_view(&self) -> &BinaryHeapView<T, K>
pub fn as_view(&self) -> &BinaryHeapView<T, K>
Get a reference to the BinaryHeap
, erasing the N
const-generic.
Sourcepub fn as_mut_view(&mut self) -> &mut BinaryHeapView<T, K>
pub fn as_mut_view(&mut self) -> &mut BinaryHeapView<T, K>
Get a mutable reference to the BinaryHeap
, erasing the N
const-generic.
Source§impl<T, K, S: VecStorage<T> + ?Sized> BinaryHeapInner<T, K, S>
impl<T, K, S: VecStorage<T> + ?Sized> BinaryHeapInner<T, K, S>
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Drops all items from the binary heap.
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
heap.push(1).unwrap();
heap.push(3).unwrap();
assert!(!heap.is_empty());
heap.clear();
assert!(heap.is_empty());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the binary heap.
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
heap.push(1).unwrap();
heap.push(3).unwrap();
assert_eq!(heap.len(), 2);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the binary heap is empty.
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
assert!(heap.is_empty());
heap.push(3).unwrap();
heap.push(5).unwrap();
heap.push(1).unwrap();
assert!(!heap.is_empty());
Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Checks if the binary heap is full.
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 4> = BinaryHeap::new();
assert!(!heap.is_full());
heap.push(1).unwrap();
heap.push(2).unwrap();
heap.push(3).unwrap();
heap.push(4).unwrap();
assert!(heap.is_full());
Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns an iterator visiting all values in the underlying vector, in arbitrary order.
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = 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);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, 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.
Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
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};
let mut heap: BinaryHeap<_, Max, 8> = 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));
Sourcepub fn peek_mut(&mut self) -> Option<PeekMutInner<'_, T, K, S>>
pub fn peek_mut(&mut self) -> Option<PeekMutInner<'_, T, K, S>>
Returns a mutable reference to the greatest item in the binary heap, or
None
if it is empty.
Note: If the PeekMut
value is leaked, the heap may be in an
inconsistent state.
§Examples
Basic usage:
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = 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));
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
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};
let mut heap: BinaryHeap<_, Max, 8> = 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);
Sourcepub unsafe fn pop_unchecked(&mut self) -> T
pub unsafe fn pop_unchecked(&mut self) -> T
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.
§Safety
The binary heap must not be empty.
§Example
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
heap.push(42)?;
// SAFETY: We just pushed a number onto the heap, so it cannot be empty.
let val = unsafe { heap.pop_unchecked() };
assert_eq!(val, 42);
Sourcepub fn push(&mut self, item: T) -> Result<(), T>
pub fn push(&mut self, item: T) -> Result<(), T>
Pushes an item onto the binary heap.
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = 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));
Sourcepub unsafe fn push_unchecked(&mut self, item: T)
pub unsafe fn push_unchecked(&mut self, item: T)
Pushes an item onto the binary heap without first checking if it’s full.
§Safety
The binary heap must not be full.
§Example
use heapless::binary_heap::{BinaryHeap, Max};
let mut heap: BinaryHeap<_, Max, 8> = BinaryHeap::new();
// SAFETY: We just created an empty heap of size 8, so it cannot be full.
unsafe { heap.push_unchecked(42) };
assert_eq!(heap.len(), 1);
assert_eq!(heap.peek(), Some(&42));