use std::hash::Hash;
use ordered_float::OrderedFloat;
use priority_queue::PriorityQueue;
#[derive(Debug)]
pub struct PQueue<T, W>
where
T: Eq + Hash,
W: ordered_float::FloatCore,
{
queue: PriorityQueue<T, OrderedFloat<W>>,
}
impl<T, W> PQueue<T, W>
where
T: Eq + Hash,
W: ordered_float::FloatCore,
{
pub fn with_capacity(capacity: usize) -> Self {
PQueue {
queue: PriorityQueue::with_capacity(capacity),
}
}
pub fn push(&mut self, weight: W, item: T) -> Option<OrderedFloat<W>> {
self.queue.push(item, OrderedFloat(-weight))
}
pub fn pop(&mut self) -> Option<(W, T)> {
if let Some(item) = self.queue.pop() {
Some((-item.1 .0, item.0))
} else {
None
}
}
pub fn remove(&mut self, item: T) -> Option<(T, W)> {
self.queue.remove(&item).map(|(i, OrderedFloat(w))| (i, w))
}
pub fn is_empty(&self) -> bool {
self.queue.is_empty()
}
}