orderedmap/
lib.rs

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use std::borrow::Borrow;
use std::collections::HashMap;
use std::convert::Infallible;
use std::fmt::Debug;
use std::hash::Hash;

/// A map that preserves the insertion order of keys.
#[derive(Clone)]
pub struct OrderedMap<K, V> {
    map: HashMap<K, V>,
    keys: Vec<K>,
}

impl<K, V> OrderedMap<K, V> {
    /// Creates a new, empty `OrderedMap`.
    pub fn new() -> Self {
        OrderedMap {
            map: HashMap::new(),
            keys: Vec::new(),
        }
    }

    /// Returns the number of key-value pairs in the map.
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Checks if the map is empty.
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    /// Returns an iterator over the keys.
    pub fn keys(&self) -> std::slice::Iter<K> {
        self.keys.iter()
    }

    /// Remove all the entries in the map.
    pub fn clear(&mut self) {
        self.keys.clear();
        self.map.clear();
    }
}

impl<K, V> OrderedMap<K, V>
where
    K: Eq + Hash,
{
    /// Retrieves a reference to the value corresponding to the key.
    pub fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        Q: Hash + Eq + ?Sized,
        K: Borrow<Q>,
    {
        self.map.get(key)
    }

    /// Retrieves a mutable reference to the value corresponding to the key.
    pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        Q: Hash + Eq + ?Sized,
        K: Borrow<Q>,
    {
        self.map.get_mut(key)
    }

    /// Returns the reference to the element with the key at the given position.
    pub fn get_index(&self, index: usize) -> Option<&V> {
        match self.keys.get(index) {
            Some(key) => self.get(key),
            None => None,
        }
    }

    /// Returns the mutable reference to the element with the key at the given position.
    pub fn get_index_mut(&mut self, index: usize) -> Option<&mut V> {
        match self.keys.get(index) {
            Some(key) => self.map.get_mut(key),
            None => None,
        }
    }

    /// Returns `true` if the map contains the given key.
    pub fn contains_key<Q>(&self, key: &Q) -> bool
    where
        Q: Hash + Eq + ?Sized,
        K: Borrow<Q>,
    {
        self.map.contains_key(key)
    }

    /// Removes a key from the map, returning its value if it was present.
    pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        Q: Hash + Eq + ?Sized,
        K: Borrow<Q>,
    {
        match self.map.remove_entry(key) {
            Some((k, v)) => {
                self.keys.retain(|x| x != &k);
                Some(v)
            }
            None => None,
        }
    }

    /// Removes the element at the given position.
    pub fn remove_index(&mut self, index: usize) -> Option<V> {
        let key = self.keys.get(index)?;
        match self.map.remove_entry(key) {
            Some((k, v)) => {
                self.keys.retain(|x| x != &k);
                Some(v)
            }
            None => None,
        }
    }

    /// Remove all elements that does not matches the predicate.
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&K, &mut V) -> bool,
    {
        self.try_retain(|k, v| Ok::<bool, Infallible>(f(k, v)))
            .expect("no error");
    }

    /// Remove all elements that does not matches the predicate.
    /// If the predicate returns an error, the operation is stopped and the error is returned.
    pub fn try_retain<F, E>(&mut self, mut f: F) -> Result<(), E>
    where
        F: FnMut(&K, &mut V) -> Result<bool, E>,
    {
        let mut i = 0;

        while i < self.keys.len() {
            let key = &self.keys[i];
            let value = self.map.get_mut(key).expect("expected value");

            match f(key, value) {
                Ok(keep) => {
                    if !keep {
                        self.map.remove(key);
                        self.keys.remove(i);
                    } else {
                        i += 1;
                    }
                }
                Err(e) => return Err(e),
            }
        }

        Ok(())
    }

    /// Returns an iterator over the key-value pairs in insertion order.
    pub fn iter(&self) -> Iter<K, V> {
        Iter {
            keys: self.keys.iter(),
            map: &self.map,
        }
    }
}

impl<K, V> OrderedMap<K, V>
where
    K: Eq + Hash + Ord,
{
    /// Sort the keys of this map.
    pub fn sort_keys(&mut self) {
        self.keys.sort();
    }
}

impl<K, V> OrderedMap<K, V>
where
    K: Eq + Hash + Clone,
{
    /// Inserts a key-value pair into the map.
    /// If the key is new, it is added to the `keys` vector to preserve order.
    /// If the key already exists, its value is updated.
    pub fn insert(&mut self, key: K, value: V) {
        if !self.map.contains_key(&key) {
            self.keys.push(key.clone());
        }
        self.map.insert(key, value);
    }
}

impl<K, V> Default for OrderedMap<K, V> {
    fn default() -> Self {
        Self {
            map: Default::default(),
            keys: Default::default(),
        }
    }
}

impl<K, V> Debug for OrderedMap<K, V>
where
    K: Debug + Eq + Hash,
    V: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut map = f.debug_map();

        for k in self.keys() {
            let v = self.map.get(k).expect("value should exists");
            map.entry(k, v);
        }

        map.finish()
    }
}

pub struct Iter<'a, K, V> {
    keys: std::slice::Iter<'a, K>,
    map: &'a HashMap<K, V>,
}

impl<'a, K, V> Iterator for Iter<'a, K, V>
where
    K: Eq + Hash,
{
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        self.keys.next().and_then(|k| {
            let v = self.map.get(k)?;
            Some((k, v))
        })
    }
}

impl<'a, K, V> IntoIterator for &'a OrderedMap<K, V>
where
    K: Eq + Hash,
{
    type Item = (&'a K, &'a V);
    type IntoIter = Iter<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

pub struct IntoIter<K, V> {
    keys: std::vec::IntoIter<K>,
    map: HashMap<K, V>,
}

impl<K, V> Iterator for IntoIter<K, V>
where
    K: Eq + Hash,
{
    type Item = (K, V);

    fn next(&mut self) -> Option<Self::Item> {
        self.keys.next().and_then(|k| {
            let v = self.map.remove(&k)?;
            Some((k, v))
        })
    }
}

impl<K, V> IntoIterator for OrderedMap<K, V>
where
    K: Eq + Hash,
{
    type Item = (K, V);
    type IntoIter = IntoIter<K, V>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            keys: self.keys.into_iter(),
            map: self.map,
        }
    }
}

impl<K, V> PartialEq for OrderedMap<K, V>
where
    K: Eq + Hash,
    V: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.map == other.map && self.keys == other.keys
    }
}

impl<K, V> Eq for OrderedMap<K, V>
where
    K: Eq + Hash,
    V: PartialEq,
{
}

#[cfg(test)]
mod tests {
    use super::OrderedMap;

    #[test]
    fn should_get_entries_in_order() {
        let mut map = OrderedMap::new();
        map.insert("first", 1);
        map.insert("second", 2);
        map.insert("third", 3);

        // Should get keys in order
        let keys = map.keys().cloned().collect::<Vec<_>>();
        assert_eq!(keys, vec!["first", "second", "third"]);

        // Entries
        let mut iter = map.into_iter();
        assert_eq!(iter.next(), Some(("first", 1)));
        assert_eq!(iter.next(), Some(("second", 2)));
        assert_eq!(iter.next(), Some(("third", 3)));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_retain() {
        let mut map = OrderedMap::new();
        map.insert("a", 1);
        map.insert("b", 2);
        map.insert("c", 3);

        // Retain only the entries where the value is even
        map.retain(|_k, v| *v % 2 == 0);

        // After retain, only the entry with key "b" should remain
        let keys = map.keys().cloned().collect::<Vec<_>>();
        assert_eq!(keys, vec!["b"]);

        // The value for "b" should be 2
        assert_eq!(map.get("b"), Some(&2));
        assert_eq!(map.get("a"), None);
        assert_eq!(map.get("c"), None);
    }

    #[test]
    fn test_sort_keys() {
        let mut map = OrderedMap::new();
        map.insert("banana", 1);
        map.insert("apple", 2);
        map.insert("cherry", 3);

        // Sort the keys
        map.sort_keys();

        // Keys should be in lexicographical order: ["apple", "banana", "cherry"]
        let keys = map.keys().cloned().collect::<Vec<_>>();
        assert_eq!(keys, vec!["apple", "banana", "cherry"]);
    }
}