Expand description
This crate provides classical binary heaps.
Like std::collections::BinaryHeap, but with more flexibility oriented design.
General api is similar to std::collections::BinaryHeap:
let mut heap = VecHeap::<_, MaxHeap>::new();
heap.push(3);
heap.push(15);
heap.push(1);
let mut data = Vec::new();
while let Some(x) = heap.pop() {
data.push(x);
}
assert_eq!(data, vec![15, 3, 1]);To use the crate, you need to select a few options:
First you select the heap storage.
It represents how the heap is stored in memory and what additional operations are needed.
Currently there are two storages:
VecHeap- stores elements in a plainVecand nothing else. Analogous tostd::collections::BinaryHeap.IndexableHeap- similar toVecHeap, but allows to access elements by an opaqueIdx
Then you select how the elements should be sorted - an Ordering.
Two primary orderings are:
MaxHeap- puts largest element on top of the heap. Like thestd::collections::BinaryHeap.MinHeap- puts smallest element on top of the heap. Like thestd::collections::BinaryHeapwithReversewrapper.
You can also compare elements by ad-hoc orderings. For example:
let mut heap = VecHeap::with_ordering(MaxHeap::by_key(|it: &(_, _)| it.0));
heap.push((3, 1));
heap.push((15, 2));
heap.push((1, 3));
let mut data = Vec::new();
while let Some(x) = heap.pop() {
data.push(x);
}
assert_eq!(data, vec![(15, 2), (3, 1), (1, 3)]);Re-exports§
pub use crate::indexable_heap::IndexableHeap;pub use crate::ordering::MaxHeap;pub use crate::ordering::MinHeap;pub use crate::vec_heap::VecHeap;
Modules§
- indexable_
heap - A heap that tracks where elements move and allows access by index.
- ordering
- This module defines how the heap elements should ordered relative to each other.
- vec_
heap - A simple heap stored in a
Vec. Analogous tostd::collections::BinaryHeap.
Traits§
- Const
Default - A hack to have
Defaulttrait in const contexts. Used forOrderingimpls.