m6entry/
lib.rs

1use std::cmp::Ordering;
2
3/// KeyValue Pair
4#[derive(Debug)]
5pub struct Entry<K, V>(pub K, pub V);
6
7
8impl<K: PartialEq, V> PartialEq for Entry<K, V> {
9    fn eq(&self, other: &Self) -> bool {
10        self.0 == other.0
11    }
12}
13
14impl<K: PartialOrd, V> PartialOrd for Entry<K, V> {
15    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
16        self.0.partial_cmp(&other.0)
17    }
18}
19
20impl<K: PartialEq, V> Eq for Entry<K, V> {
21}
22
23impl<K: PartialOrd, V> Ord for Entry<K, V> {
24    fn cmp(&self, other: &Self) -> Ordering {
25        self.partial_cmp(&other).unwrap()
26    }
27}
28
29
30
31
32#[cfg(test)]
33mod test {
34    use super::*;
35
36    #[test]
37    fn test_entry() {
38        use std::collections::BinaryHeap;
39        use std::cmp::Reverse;
40
41        let mut heap = BinaryHeap::new();
42
43        heap.push(Reverse(Entry(2, 3)));
44        heap.push(Reverse(Entry(1, 1)));
45        heap.push(Reverse(Entry(4, 16)));
46        heap.push(Reverse(Entry(2, 1)));
47
48
49        assert_eq!(heap.pop(), Some(Reverse(Entry(1, 1))));
50        assert_eq!(heap.pop(), Some(Reverse(Entry(2, 3))));
51        assert_eq!(heap.pop(), Some(Reverse(Entry(2, 1))));
52        assert_eq!(heap.pop(), Some(Reverse(Entry(4, 16))));
53
54        assert_eq!(heap.pop(), None);
55
56    }
57}