multi_key_map 0.3.0

a hash table that shares one value across multiple keys.
Documentation
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Provides [MultiKeyMap], an associative array that can share one value
//! across multiple keys without [`Rc`], and provides mutable access
//! that will never panic at runtime, unlike [`RefCell`].
//!
//! ```
//! use multi_key_map::MultiKeyMap;
//!
//! let mut map: MultiKeyMap<i32, String> = MultiKeyMap::from([
//!     (vec![1, 2, 3], "foo".into()),
//!     (vec![4, 5], "bar".into()),
//! ]);
//! map.insert_many(vec![6, 7], "quux".into());
//! map.alias(&7, 8);
//! assert_eq!(map.get(&8), Some(&String::from("quux")));
//! ```
//!
//! [`Rc`]: std::rc::Rc
//!
//! [`RefCell`]: std::cell::RefCell

use core::hash::Hash;
pub use entry::{Entry, OccupiedEntry, VacantEntry};
pub use iter::Iter;
use std::borrow::Borrow;
use std::fmt::Debug;
use std::collections::HashMap;
/// Provides types and methods for the Entry API. for more information, see [`entry`] for more info.
///
/// [`entry`]: MultiKeyMap::entry
pub mod entry;
/// Provides [`Iter`], an iterator over all keys and values. see [`iter`] for more info.
///
/// [`Iter`]: iter::Iter
///
/// [`iter`]: MultiKeyMap::iter
pub mod iter;
#[cfg(test)]
mod tests;

//u128 allows us to not store freed indices and keep removal O(1);
//as many as 10 trillion inserts/removes per second
//would still take ~10^18 years to use up the available index space
#[derive(Hash, PartialEq, Eq, Clone, Copy)]
struct Index(u128);

#[derive(Clone)]
/// A wrapper over [HashMap] that allows for multiple keys to point to a single element,
/// providing some additional utilities to make working with multiple keys easier.
pub struct MultiKeyMap<K, V>
where
    K: Hash + Eq,
{
    /// A wrapper over [HashMap] that allows for multiple keys to point to a single element,
    /// providing some additional utilities to make working with multiple keys easier.
    keys: HashMap<K, Index>,
    data: HashMap<Index, (usize, V)>,
    max_index: Index,
}

impl<K, V> Default for MultiKeyMap<K, V>
where
    K: Hash + Eq,
{
    fn default() -> Self {
        MultiKeyMap {
            keys: HashMap::new(),
            data: HashMap::new(),
            max_index: Index(0),
        }
    }
}
#[allow(dead_code)]
impl<K, V> MultiKeyMap<K, V>
where
    K: Hash + Eq,
{
    ///Creates an empty [MultiKeyMap].
    pub fn new() -> Self {
        Default::default()
    }

    pub(crate) fn next_index(&mut self) -> Index {
        let idx = self.max_index;
        self.max_index = Index(self.max_index.0 + 1);
        idx
    }

    pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.keys.contains_key(k)
    }

    ///inserts a new value at a given key, and returns the value at that key if
    /// there are no other keys to that value. otherwise returns [`None`].
    pub fn insert(&mut self, k: K, v: V) -> Option<V> {
        if self.contains_key(&k) {
            let idx = self.keys.get(&k).unwrap();
            let (count, _) = self.data.get_mut(idx).unwrap();
            if *count <= 1 {
                self.data.insert(*idx, (1, v)).map(|(_, v)| v)
            } else {
                *count -= 1;
                let new_idx = self.max_index;
                self.max_index = Index(self.max_index.0 + 1);
                self.keys.insert(k, new_idx);
                self.data.insert(new_idx, (1, v));
                None
            }
        } else {
            let new_idx = self.max_index;
            self.max_index = Index(self.max_index.0 + 1);
            self.keys.insert(k, new_idx);
            self.data.insert(new_idx, (1, v));
            None
        }
    }
    ///Attempts to add a new key to the element at `k`. Returns the new key if `k` is not
    /// in the map.
    /// ```
    /// use multi_key_map::MultiKeyMap;
    /// let mut map = MultiKeyMap::from([
    ///     (vec!["foo".to_string()], 1),
    ///     (vec!["quux".to_string()], -2),
    /// ]);
    /// let aliased = map.alias("foo", "bar".to_string());
    /// assert_eq!(aliased, Ok(&mut 1));
    /// let aliased = map.alias("baz", "xyz".to_string());
    /// assert_eq!(aliased, Err("xyz".to_string()));
    /// ```
    pub fn alias<Q: ?Sized>(&mut self, k: &Q, alias: K) -> Result<&mut V, K>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        if self.contains_key(k) {
            let idx = *self.keys.get(k).unwrap();
            let (count, v) = self.data.get_mut(&idx).unwrap();
            *count += 1;
            self.keys.insert(alias, idx);
            Ok(v)
        } else {
            Err(alias)
        }
    }
    ///Attempts to add multiple new keys to the element at `k`. Returns the list of keys if `k` is not
    /// in the map.
    /// ```
    /// use multi_key_map::MultiKeyMap;
    /// let mut map = MultiKeyMap::from([
    ///     (vec!["foo".to_string()], 1),
    /// ]);
    /// let aliased = map.alias_many("foo", vec!["bar".to_string(), "quux".to_string()]);
    /// assert_eq!(aliased, Ok(&mut 1));
    /// let aliased = map.alias_many("baz", vec!["xyz".to_string(), "syzygy".to_string()]);
    /// assert_eq!(aliased, Err(vec!["xyz".to_string(), "syzygy".to_string()]));
    /// ```
    pub fn alias_many<Q: ?Sized>(&mut self, k: &Q, aliases: Vec<K>) -> Result<&mut V, Vec<K>>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        if self.contains_key(k) {
            let idx = *self.keys.get(k).unwrap();
            let (count, v) = self.data.get_mut(&idx).unwrap();
            for alias in aliases {
                *count += 1;
                self.keys.insert(alias, idx);
            }
            Ok(v)
        } else {
            Err(aliases)
        }
    }
    ///An iterator visiting all keys in an arbitrary order. Equivalent to [HashMap]::[`keys`].
    ///
    /// [`keys`]: HashMap::keys
    pub fn keys(&self) -> impl Iterator<Item = &K> {
        self.keys.keys()
    }
    ///An iterator visiting all elements in an arbitrary order. Equivalent to [HashMap]::[`values`].
    ///
    /// [`values`]: HashMap::values
    pub fn values(&self) -> impl Iterator<Item = &V> {
        self.data.values().map(|(_, v)| v)
    }
    ///An iterator visiting all elements in an arbitrary order, while allowing mutation. Equivalent to [HashMap]::[`values_mut`].
    ///
    /// [`values_mut`]: HashMap::values_mut
    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
        self.data.values_mut().map(|(_, v)| v)
    }
    ///Consumes the map and provides an iterator over all keys. Equivalent to [HashMap]::[`into_keys`].
    ///
    /// [`into_keys`]: HashMap::into_keys
    pub fn into_keys(self) -> impl Iterator<Item = K> {
        self.keys.into_keys()
    }
    ///Consumes the map and provides an iterator over all values. Equivalent to [HashMap]::[`into_values`].
    ///
    /// [`into_values`]: HashMap::into_values
    pub fn into_values(self) -> impl Iterator<Item = V> {
        self.data.into_values().map(|(_, v)| v)
    }
    ///An iterator visiting all key-value pairs. Due to the nature of [MultiKeyMap], value references
    /// may be shared across multiple keys.
    pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
        iter::Iter::new(self)
    }
    ///Returns a shared reference to the value of the key. Equivalent to [HashMap]::[`get`].
    ///
    /// [`get`]: HashMap::get
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.keys
            .get(k)
            .and_then(|idx| self.data.get(idx))
            .map(|(_, v)| v)
    }
    ///Returns a mutable reference to the value of the key. Equivalent to [HashMap]::[`get_mut`].
    ///
    /// ```
    /// use multi_key_map::MultiKeyMap;
    /// let mut map = MultiKeyMap::new();
    /// map.insert(1, "foo".to_string());
    /// let x = map.get_mut(&1).unwrap();
    /// *x = "bar".to_string();
    /// assert_eq!(Some(&"bar".to_string()), map.get(&1));
    /// ```
    /// [`get_mut`]: HashMap::get_mut
    pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.keys
            .get(k)
            .and_then(|idx| self.data.get_mut(idx))
            .map(|(_, v)| v)
    }
    ///inserts a new value, pairs it to a list of keys, and returns the values that existed
    /// at each key if there are no other keys to that value.
    /// ```
    /// use multi_key_map::MultiKeyMap;
    /// let mut map = MultiKeyMap::new();
    /// map.insert_many(vec![1, 2],"foo".to_string());
    /// assert_eq!(Some(&"foo".to_string()), map.get(&1));
    /// assert_eq!(Some(&"foo".to_string()), map.get(&2));
    /// ```
    pub fn insert_many(&mut self, ks: Vec<K>, v: V) -> Vec<V> {
        let mut bumped = vec![];
        let new_idx = self.max_index;
        self.max_index = Index(self.max_index.0 + 1);
        let mut new_count = 0;
        for k in ks {
            if self.contains_key(&k) {
                let idx = self.keys.get(&k).unwrap();
                let (count, _) = self.data.get_mut(idx).unwrap();
                if *count <= 1 {
                    if let Some((_, v)) = self.data.remove(idx) {
                        bumped.push(v);
                    }
                } else {
                    *count -= 1;
                    new_count += 1;
                    self.keys.insert(k, new_idx);
                }
            } else {
                new_count += 1;
                self.keys.insert(k, new_idx);
            }
        }
        self.data.insert(new_idx, (new_count, v));
        bumped
    }
    ///Removes a key from the map, returning the value at that key if it existed in the map
    /// and no other keys share that value.
    pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        if self.contains_key(k) {
            let idx = self.keys.get(k).unwrap();
            let (count, _) = self.data.get_mut(idx).unwrap();
            if *count == 1 {
                let result = self.data.remove(idx).map(|(_, v)| v);
                self.keys.remove(k);
                result
            } else {
                *count -= 1;
                self.keys.remove(k);
                None
            }
        } else {
            None
        }
    }
    ///Removes a list of keys from the map, returning the values at each key if they existed in the map
    /// and no other keys shared that value.
    pub fn remove_many<Q: ?Sized, const N: usize>(&mut self, ks: [&Q; N]) -> Vec<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let mut bumped = vec![];
        for k in ks {
            if let Some(v) = self.remove(k) {
                bumped.push(v);
            }
        }
        bumped
    }
    ///Equivalent to [HashMap]::[`entry`].
    ///
    /// [`entry`]: HashMap::entry
    pub fn entry(&mut self, k: K) -> Entry<K, V> {
        if let Some(idx) = self.keys.get(&k) {
            Entry::Occupied(OccupiedEntry {
                key: k,
                idx: *idx,
                map: self,
            })
        } else {
            Entry::Vacant(VacantEntry { key: k, map: self })
        }
    }
}

impl<K, V, const N: usize> From<[(Vec<K>, V); N]> for MultiKeyMap<K, V>
where
    K: Hash + Eq,
{
    fn from(arr: [(Vec<K>, V); N]) -> Self {
        Self::from_iter(arr)
    }
}

impl<K, V> FromIterator<(Vec<K>, V)> for MultiKeyMap<K, V>
where
    K: Hash + Eq,
{
    fn from_iter<T: IntoIterator<Item = (Vec<K>, V)>>(iter: T) -> Self {
        let mut map = Self::new();
        for (keys, value) in iter {
            map.insert_many(keys, value);
        }
        map
    }
}

impl<K, V> PartialEq for MultiKeyMap<K, V>
where
    K: Hash + Eq,
    V: PartialEq,
{
    fn eq(&self, rhs: &Self) -> bool {
        if self.keys.len() != rhs.keys.len() || self.data.len() != rhs.data.len() {
            return false;
        }
        self.iter()
            .all(|(key, value)| rhs.get(key).map_or(false, |v| *value == *v))
    }
}

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

impl<K, V> Debug for MultiKeyMap<K, V> 
where 
    K: Hash + Eq + Debug, 
    V: Debug 
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

impl<'a, K, V> Extend<(&'a [K], &'a V)> for MultiKeyMap<K, V>
where
    K: Hash + Eq + Copy,
    V: Copy,
{
    fn extend<T: IntoIterator<Item = (&'a [K], &'a V)>>(&mut self, iter: T) {
        for (ks, v) in iter {
            self.insert_many(ks.to_vec(), *v);
        }
    }
}

impl<K, V> Extend<(Vec<K>, V)> for MultiKeyMap<K, V>
where
    K: Hash + Eq,
{
    fn extend<T: IntoIterator<Item = (Vec<K>, V)>>(&mut self, iter: T) {
        for (ks, v) in iter {
            self.insert_many(ks, v);
        }
    }
}