use std::{collections::BinaryHeap, hash::Hash};
use rustc_hash::FxHashMap;
pub struct LazyHeap<T: Ord + Hash + Eq + Clone> {
heap: BinaryHeap<T>,
tombstones: FxHashMap<T, ()>,
}
impl<T: Ord + Hash + Eq + Clone> LazyHeap<T> {
pub fn new(prealloc_size: usize) -> Self {
LazyHeap {
heap: BinaryHeap::with_capacity(prealloc_size),
tombstones: FxHashMap::default(),
}
}
pub fn push(&mut self, item: T) {
self.heap.push(item);
}
pub fn remove(&mut self, item: T) {
self.tombstones.insert(item, ());
}
pub fn peek(&mut self) -> Option<&T> {
self.evict_tombstones();
self.heap.peek()
}
fn evict_tombstones(&mut self) {
loop {
match self.heap.peek() {
None => break,
Some(top) => match self.tombstones.remove(&top) {
Some(_) => {
self.heap.pop();
}
_ => break,
},
}
}
}
}