I've given up on using the BinaryHeap
because I need to be able to update and view into the contents of the queue. I've replaced it with BTreeMap
and other than my Ord implementation being a bit weird, it's working great.
This is the minimal code as a proof-of-concept:
use std::collections::BTreeSet;
fn main() {
let mut pq = BTreeSet::new();
pq.insert(3);
pq.insert(1);
pq.insert(0);
pq.insert(2);
pq.insert(4);
loop {
// My pretend 'pop' function looks up the element, then calls take() below.
let item = match pq.iter().next() {
None => return,
Some(x) => x.clone()
};
let item = pq.take(&item)
.expect("Should be covered by match arm above");
println!("Value: {}", item);
}
}
This is bad code for multiple reasons, but it begs the question: Why isn't pop() on BTreeMap
part of the standard library? It should be more useful and efficient than the BinaryHeap
implementation presuming the BinaryHeap
is in fact a binary heap underneath.
If I were to implement pop(), what needs to happen?