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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#[cfg(test)]
mod tests;

use malloc_size_of_derive::MallocSizeOf as DeriveMallocSizeOf;
use std::{cmp::Ordering, collections::HashMap, fmt::Debug, hash};

/// The `HeapMap` maintain a max heap along with a hash map to support
/// additional `remove` and `update` operations.
#[derive(DeriveMallocSizeOf)]
pub struct HeapMap<K: hash::Hash + Eq + Copy + Debug, V: Eq + Ord + Clone> {
    data: Vec<Node<K, V>>,
    mapping: HashMap<K, usize>,
}

#[derive(Clone, DeriveMallocSizeOf)]
pub struct Node<K, V: Eq + Ord> {
    key: K,
    value: V,
}

impl<K, V: Eq + Ord> Node<K, V> {
    pub fn new(key: K, value: V) -> Self { Node { key, value } }
}

impl<K, V: Eq + Ord> PartialEq for Node<K, V> {
    fn eq(&self, other: &Self) -> bool { self.value.eq(&other.value) }
}

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

impl<K, V: Eq + Ord> PartialOrd for Node<K, V> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<K, V: Eq + Ord> Ord for Node<K, V> {
    fn cmp(&self, other: &Self) -> Ordering { self.value.cmp(&other.value) }
}

impl<K: hash::Hash + Eq + Copy + Debug, V: Eq + Ord + Clone> Default
    for HeapMap<K, V>
{
    fn default() -> Self { Self::new() }
}

impl<K: hash::Hash + Eq + Copy + Debug, V: Eq + Ord + Clone> HeapMap<K, V> {
    pub fn new() -> Self {
        Self {
            data: vec![],
            mapping: HashMap::new(),
        }
    }

    /// Insert a K-V into the HeapMap.
    /// Return the old value if `key` already exist. Return `None` otherwise.
    pub fn insert(&mut self, key: &K, value: V) -> Option<V> {
        if self.mapping.contains_key(key) {
            let old_value = self.update(key, value);
            Some(old_value)
        } else {
            self.append(key, value);
            None
        }
    }

    /// Remove `key` from the HeapMap.
    pub fn remove(&mut self, key: &K) -> Option<V> {
        let index = self.mapping.remove(key)?;
        let removed_node = self.data.swap_remove(index);
        if index != self.data.len() {
            // The last node has been swapped to index
            match self.data[index].cmp(&removed_node) {
                Ordering::Less => self.sift_down(index),
                Ordering::Greater => self.sift_up(index),
                Ordering::Equal => {
                    self.mapping.insert(self.data[index].key, index);
                }
            }
        }
        Some(removed_node.value)
    }

    /// In-place update some fields of a node's value.
    pub fn update_with<F>(&mut self, key: &K, mut update_fn: F)
    where F: FnMut(&mut V) -> () {
        let index = match self.mapping.get(&key) {
            None => {
                return;
            }
            Some(i) => *i,
        };
        let origin_node = self.data[index].clone();
        update_fn(&mut self.data[index].value);
        // The order of node is the opposite of the order of this tuple.
        match self.data[index].cmp(&origin_node) {
            Ordering::Less => self.sift_down(index),
            Ordering::Greater => self.sift_up(index),
            _ => {}
        }
    }

    /// Return the top K-V reference tuple.
    pub fn top(&self) -> Option<(&K, &V)> {
        self.data.get(0).map(|node| (&node.key, &node.value))
    }

    /// Pop the top node and return it as a K-V tuple.
    pub fn pop(&mut self) -> Option<(K, V)> {
        if self.is_empty() {
            return None;
        }
        let item = self.data.swap_remove(0);
        if !self.is_empty() {
            self.sift_down(0);
        }
        self.mapping.remove(&item.key);
        Some((item.key, item.value))
    }

    /// Get the value reference of `key`.
    pub fn get(&self, key: &K) -> Option<&V> {
        let index = *self.mapping.get(key)?;
        self.data.get(index).map(|node| &node.value)
    }

    /// Clear all key-values of the HeapMap.
    pub fn clear(&mut self) {
        self.mapping.clear();
        self.data.clear();
    }

    #[inline]
    pub fn is_empty(&self) -> bool { self.data.is_empty() }

    #[inline]
    pub fn len(&self) -> usize { self.data.len() }

    pub fn iter(&self) -> impl Iterator<Item = V> + '_ {
        self.data.iter().map(|f| f.value.clone())
    }

    fn update(&mut self, key: &K, value: V) -> V {
        let index = *self.mapping.get(key).unwrap();
        let origin_node = self.data[index].clone();
        self.data[index] = Node::new(*key, value);
        match self.data[index].cmp(&origin_node) {
            Ordering::Less => self.sift_down(index),
            Ordering::Greater => self.sift_up(index),
            _ => {}
        }
        origin_node.value
    }

    fn append(&mut self, key: &K, value: V) {
        self.data.push(Node::new(*key, value));
        self.sift_up(self.data.len() - 1);
    }

    fn sift_up(&mut self, index: usize) {
        let val = self.data[index].clone();
        let mut pos = index;
        while pos > 0 {
            let parent = (pos - 1) / 2;
            if self.data[parent] >= val {
                break;
            }
            self.data[pos] = self.data[parent].clone();
            self.mapping.insert(self.data[pos].key, pos);
            pos = parent;
        }

        self.mapping.insert(val.key, pos);
        self.data[pos] = val;
    }

    fn sift_down(&mut self, index: usize) {
        let val = self.data[index].clone();
        let mut pos = index;
        let mut child = pos * 2 + 1;
        while child < self.data.len() {
            let right = child + 1;
            if right < self.data.len() && self.data[right] > self.data[child] {
                child = right;
            }
            if val >= self.data[child] {
                break;
            }
            self.data[pos] = self.data[child].clone();
            self.mapping.insert(self.data[pos].key, pos);
            pos = child;
            child = pos * 2 + 1;
        }
        self.mapping.insert(val.key, pos);
        self.data[pos] = val;
    }
}