use std::cmp::Reverse;
use std::collections::BinaryHeap;
use crate::dijkstra::heap_trait::PriorityQueue;
pub struct BinaryHeapPQ {
heap: BinaryHeap<Reverse<(u32, usize)>>,
}
impl Default for BinaryHeapPQ {
fn default() -> Self {
Self::new()
}
}
impl BinaryHeapPQ {
pub fn new() -> Self {
BinaryHeapPQ {
heap: BinaryHeap::new(),
}
}
}
impl PriorityQueue for BinaryHeapPQ {
type Handle = ();
fn insert(&mut self, key: u32, node_id: usize) -> Self::Handle {
self.heap.push(Reverse((key, node_id)));
}
fn extract_min(&mut self) -> Option<(u32, usize)> {
self.heap
.pop()
.map(|Reverse((key, node_id))| (key, node_id))
}
fn supports_decrease_key(&self) -> bool {
false
}
fn decrease_key(&mut self, _handle: &Self::Handle, _new_key: u32) {
unreachable!("BinaryHeap doesn't support decrease_key")
}
}
pub fn dijkstra_binary(
start: usize,
end: usize,
graph: &[Vec<(usize, u32)>],
) -> (Vec<usize>, Option<u32>) {
crate::dijkstra::dijkstra(start, end, graph, BinaryHeapPQ::new())
}