1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! [Min heap] more suitable for algorithms such as [Dijkstra] and [A*] than Rust's default
//! max heap. Splits the sorting key and value, so that you can order items without having
//! to implement [`Ord`] on the value type.
//!
//! [Min heap]: https://en.wikipedia.org/wiki/Heap_(data_structure)
//! [Dijkstra]: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
//! [A*]: https://en.wikipedia.org/wiki/A*_search_algorithm
use std::cmp::Ordering;
use std::collections::BinaryHeap;

struct Wrapper<K: Ord, V> {
    key: K,
    value: V,
}

impl<K: Ord, V> PartialEq for Wrapper<K, V> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}

impl<K: Ord, V> Eq for Wrapper<K, V> {}

impl<K: Ord, V> PartialOrd for Wrapper<K, V> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<K: Ord, V> Ord for Wrapper<K, V> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        other.key.cmp(&self.key)
    }
}

#[derive(Default)]
pub struct MinHeap<K: Ord, V> {
    heap: BinaryHeap<Wrapper<K, V>>,
}

impl<K: Ord, V> MinHeap<K, V> {
    pub fn new() -> Self {
        MinHeap { heap: BinaryHeap::new() }
    }

    pub fn with_capacity(capacity: usize) -> Self {
        MinHeap { heap: BinaryHeap::with_capacity(capacity) }
    }

    #[inline]
    pub fn push(&mut self, key: K, value: V) {
        self.heap.push(Wrapper { key, value });
    }

    #[inline]
    pub fn pop(&mut self) -> Option<(K, V)> {
        self.heap.pop().map(|w| (w.key, w.value))
    }
}