A flexible binary heap implementation for Rust with more design flexibility than the standard library's BinaryHeap.
Overview
This crate provides classical binary heaps similar to [std::collections::BinaryHeap], but with a more flexible, modular design that allows you to choose both the storage mechanism and the ordering behavior.
Quick Start
use ;
let mut heap = new;
heap.push;
heap.push;
heap.push;
let mut data = Vecnew;
while let Some = heap.pop
assert_eq!;
Usage
Choose Storage
Select how the heap is stored in memory:
VecHeap- Stores elements in a plainVec, analogous tostd::collections::BinaryHeapIndexableHeap- Similar toVecHeap, but allows accessing elements by an opaqueIdx
Choose Ordering
Select how elements should be sorted:
MaxHeap- Puts the largest element on top (likestd::collections::BinaryHeap)MinHeap- Puts the smallest element on top (likestd::collections::BinaryHeapwithReversewrapper)
3. Custom Orderings
You can compare elements using custom orderings:
use ;
let mut heap = with_ordering;
heap.push;
heap.push;
heap.push;
let mut data = Vecnew;
while let Some = heap.pop
assert_eq!;
Examples
Basic Max Heap
use ;
let mut heap = new;
heap.push;
heap.push;
heap.push;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Min Heap
use ;
let mut heap = new;
heap.push;
heap.push;
heap.push;
assert_eq!;
assert_eq!;
assert_eq!;
Indexable Heap
use ;
let mut heap = new;
let idx1 = heap.push;
let idx2 = heap.push;
let idx3 = heap.push;
// Access elements by index
assert_eq!;
assert_eq!;
assert_eq!;
// Update elements
*heap.by_index_mut = 150;
assert_eq!;