abi_stable/
multikey_map.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#![allow(clippy::missing_const_for_fn)]

use std::{
    borrow::Borrow,
    cmp::{Eq, PartialEq},
    collections::hash_map::{Entry, HashMap},
    fmt::{self, Debug},
    hash::Hash,
    mem,
    ops::{Index, IndexMut},
    ptr,
};

use generational_arena::{Arena, Index as ArenaIndex};

use core_extensions::SelfOps;

/// A Map that maps multiple keys to the same value.
///
/// Every key maps to a value,which is stored at an index.
/// Indices can be used as a proxy for the value.
#[derive(Clone)]
pub struct MultiKeyMap<K, T> {
    map: HashMap<K, MapIndex>,
    arena: Arena<MapValue<K, T>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct MapValue<K, T> {
    keys: Vec<K>,
    value: T,
}

#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct MapIndex {
    index: ArenaIndex,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct IndexValue<T> {
    pub index: MapIndex,
    pub value: T,
}

/// When the element was inserted,now or before the method call.
#[must_use = "call `.into_inner()` to unwrap into the inner value."]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum InsertionTime<T> {
    Now(T),
    Before(T),
}

impl<K, T> MultiKeyMap<K, T>
where
    K: Hash + Eq,
{
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
            map: HashMap::default(),
            arena: Arena::new(),
        }
    }

    pub fn get<Q>(&self, key: &Q) -> Option<&T>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let &i = self.map.get(key)?;
        self.get_with_index(i)
    }

    pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut T>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let &i = self.map.get(key)?;
        self.get_mut_with_index(i)
    }

    #[allow(dead_code)]
    pub fn get2_mut<Q>(&mut self, key0: &Q, key1: &Q) -> (Option<&mut T>, Option<&mut T>)
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let i0 = self.map.get(key0).cloned();
        let i1 = self.map.get(key1).cloned();

        match (i0, i1) {
            (None, None) => (None, None),
            (Some(l), None) => (self.get_mut_with_index(l), None),
            (None, Some(r)) => (None, self.get_mut_with_index(r)),
            (Some(l), Some(r)) => self.get2_mut_with_index(l, r),
        }
    }

    pub fn get_index<Q>(&self, key: &Q) -> Option<MapIndex>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.map.get(key).cloned()
    }

    pub fn get_with_index(&self, i: MapIndex) -> Option<&T> {
        self.arena.get(i.index).map(|x| &x.value)
    }

    pub fn get_mut_with_index(&mut self, i: MapIndex) -> Option<&mut T> {
        self.arena.get_mut(i.index).map(|x| &mut x.value)
    }

    pub fn get2_mut_with_index(
        &mut self,
        i0: MapIndex,
        i1: MapIndex,
    ) -> (Option<&mut T>, Option<&mut T>) {
        let (l, r) = self.arena.get2_mut(i0.index, i1.index);
        fn mapper<K, T>(x: &mut MapValue<K, T>) -> &mut T {
            &mut x.value
        }
        (l.map(mapper), r.map(mapper))
    }

    #[allow(dead_code)]
    pub fn replace_index(&mut self, replace: MapIndex, with: T) -> Option<T> {
        self.get_mut_with_index(replace)
            .map(|x| mem::replace(x, with))
    }

    #[allow(dead_code)]
    /// The amount of keys associated with values.
    pub fn key_len(&self) -> usize {
        self.map.len()
    }

    #[allow(dead_code)]
    /// The amount of values.
    pub fn value_len(&self) -> usize {
        self.arena.len()
    }

    /// Replaces the element at the `replace` index with the one at the `with` index,
    /// mapping all keys to the `with` index to the `replace` index.
    ///
    /// # Return value
    ///
    /// This method returns the previous value at `replace` if all these conditions are satisfied:
    ///
    /// - The `replace` index is not the same as the `with` index.
    ///
    /// - Both `replace` and `with` are valid indices
    ///
    /// If the conditions are not satisfied this method will return None without
    /// modifying the collection.
    ///
    pub fn replace_with_index(&mut self, replace: MapIndex, with: MapIndex) -> Option<T> {
        if replace == with
            || !self.arena.contains(replace.index)
            || !self.arena.contains(with.index)
        {
            return None;
        }
        let with_ = self.arena.remove(with.index)?;
        let replaced = self.arena.get_mut(replace.index)?;
        for key in &with_.keys {
            *self.map.get_mut(key).unwrap() = replace;
        }
        replaced.keys.extend(with_.keys);
        Some(mem::replace(&mut replaced.value, with_.value))
    }

    pub fn get_or_insert(&mut self, key: K, value: T) -> InsertionTime<IndexValue<&mut T>>
    where
        K: Clone,
    {
        match self.map.entry(key.clone()) {
            Entry::Occupied(entry) => {
                let index = *entry.get();
                InsertionTime::Before(IndexValue {
                    index,
                    value: &mut self.arena[index.index].value,
                })
            }
            Entry::Vacant(entry) => {
                let inserted = MapValue {
                    keys: vec![key],
                    value,
                };
                let index = MapIndex::new(self.arena.insert(inserted));
                entry.insert(index);
                // Just inserted the value at the index
                InsertionTime::Now(IndexValue {
                    index,
                    value: &mut self.arena.get_mut(index.index).unwrap().value,
                })
            }
        }
    }

    /// Associates `key` with the value at the `index`.
    /// If the `key` was already associated with a value,this will not do anything.
    ///
    /// # Panic
    ///
    /// Panics if the index is invalid.
    pub fn associate_key(&mut self, key: K, index: MapIndex)
    where
        K: Clone,
    {
        let value = match self.arena.get_mut(index.index) {
            Some(x) => x,
            None => panic!("Invalid index:{:?}", index),
        };
        match self.map.entry(key.clone()) {
            Entry::Occupied(_) => {}
            Entry::Vacant(entry) => {
                entry.insert(index);
                value.keys.push(key);
            }
        }
    }

    /// Associates `key` with the value at the `index`.
    /// If `key` was already associated with a value,
    /// it will be disassociated from that value,
    /// returning that value if it has no keys associated with it.
    ///
    /// # Panic
    ///
    /// Panics if the index is invalid.
    ///
    /// # Index validity
    ///
    /// If `key` was associated with a value,and it was the only key for that value,
    /// the index for the value will be invalidated.
    #[allow(dead_code)]
    pub fn associate_key_forced(&mut self, key: K, index: MapIndex) -> Option<T>
    where
        K: Clone + ::std::fmt::Debug,
    {
        assert!(
            self.arena.contains(index.index),
            "Invalid index:{:?}",
            index,
        );
        let ret = match self.map.entry(key.clone()) {
            Entry::Occupied(mut entry) => {
                let index_before = *entry.get();
                entry.insert(index);
                let slot = &mut self.arena[index_before.index];
                let key_ind = slot.keys.iter().position(|x| *x == key).unwrap();
                slot.keys.swap_remove(key_ind);
                if slot.keys.is_empty() {
                    self.arena
                        .remove(index_before.index)
                        .unwrap()
                        .value
                        .piped(Some)
                } else {
                    None
                }
            }
            Entry::Vacant(_) => None,
        };
        let value = &mut self.arena[index.index];
        self.map.entry(key.clone()).or_insert(index);
        value.keys.push(key);
        ret
    }
}

impl<'a, K, Q: ?Sized, T> Index<&'a Q> for MultiKeyMap<K, T>
where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash,
{
    type Output = T;

    fn index(&self, index: &'a Q) -> &T {
        self.get(index).expect("no entry found for key")
    }
}

impl<'a, K, Q: ?Sized, T> IndexMut<&'a Q> for MultiKeyMap<K, T>
where
    K: Eq + Hash + Borrow<Q>,
    Q: Eq + Hash,
{
    fn index_mut(&mut self, index: &'a Q) -> &mut T {
        self.get_mut(index).expect("no entry found for key")
    }
}

impl<K, T> Debug for MultiKeyMap<K, T>
where
    K: Eq + Hash + Debug,
    T: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MultiKeyMap")
            .field("map", &self.map)
            .field("arena", &self.arena)
            .finish()
    }
}

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

impl<K, T> PartialEq for MultiKeyMap<K, T>
where
    K: Eq + Hash,
    T: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        if self.arena.len() != other.arena.len() || self.map.len() != other.map.len() {
            return false;
        }
        for (_, l_val) in self.arena.iter() {
            let mut keys = l_val.keys.iter();

            let r_val_index = match other.get_index(keys.next().unwrap()) {
                Some(x) => x,
                None => return false,
            };

            let r_val = &other.arena[r_val_index.index];

            if l_val.value != r_val.value {
                return false;
            }

            let all_map_to_r_val = keys.all(|key| match other.get_index(key) {
                Some(r_ind) => ptr::eq(r_val, &other.arena[r_ind.index]),
                None => false,
            });

            if !all_map_to_r_val {
                return false;
            }
        }
        true
    }
}

impl MapIndex {
    #[inline]
    fn new(index: ArenaIndex) -> Self {
        Self { index }
    }
}

impl<T> InsertionTime<T> {
    pub fn into_inner(self) -> T {
        match self {
            InsertionTime::Before(v) | InsertionTime::Now(v) => v,
        }
    }
    #[allow(dead_code)]
    pub fn split(self) -> (T, InsertionTime<()>) {
        let discr = self.discriminant();
        (self.into_inner(), discr)
    }
    #[allow(dead_code)]
    pub fn map<F, U>(self, f: F) -> InsertionTime<U>
    where
        F: FnOnce(T) -> U,
    {
        match self {
            InsertionTime::Before(v) => InsertionTime::Before(f(v)),
            InsertionTime::Now(v) => InsertionTime::Now(f(v)),
        }
    }
    #[allow(dead_code)]
    pub fn discriminant(&self) -> InsertionTime<()> {
        match self {
            InsertionTime::Before { .. } => InsertionTime::Before(()),
            InsertionTime::Now { .. } => InsertionTime::Now(()),
        }
    }
}

#[cfg(all(test, not(feature = "only_new_tests")))]
mod tests {
    use super::*;

    use crate::test_utils::must_panic;

    #[test]
    fn equality() {
        fn insert(map: &mut MultiKeyMap<u32, u32>, key: u32, value: u32) {
            let index = map.get_or_insert(key, value).into_inner().index;
            map.associate_key(key + 1, index);
            map.associate_key(key + 2, index);
        }

        ///////////////////////////////////////////////////
        ////                EQUAL
        ///////////////////////////////////////////////////
        {
            let map_a = MultiKeyMap::<u32, u32>::new();

            let map_b = MultiKeyMap::<u32, u32>::new();

            assert_eq!(map_a, map_b);
        }
        {
            let mut map_a = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_a, 1000, 200);

            let mut map_b = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_b, 1000, 200);

            assert_eq!(map_a, map_b);
        }
        {
            let mut map_a = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_a, 1000, 200);
            insert(&mut map_a, 2000, 400);

            let mut map_b = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_b, 1000, 200);
            insert(&mut map_b, 2000, 400);

            assert_eq!(map_a, map_b);
        }

        ///////////////////////////////////////////////////
        ////             NOT EQUAL
        ///////////////////////////////////////////////////
        {
            let map_a = MultiKeyMap::<u32, u32>::new();

            let mut map_b = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_b, 1000, 200);

            assert_ne!(map_a, map_b);
        }
        {
            let mut map_a = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_a, 1000, 200);

            let map_b = MultiKeyMap::<u32, u32>::new();

            assert_ne!(map_a, map_b);
        }
        {
            let mut map_a = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_a, 1000, 200);
            insert(&mut map_a, 2000, 401);

            let mut map_b = MultiKeyMap::<u32, u32>::new();
            insert(&mut map_b, 1000, 200);
            insert(&mut map_b, 2000, 400);

            assert_ne!(map_a, map_b);
        }
    }

    #[test]
    fn get_or_insert() {
        let mut map = MultiKeyMap::<u32, u32>::new();

        let (ret, ret_discr) = map.get_or_insert(10, 1).split();
        *ret.value = 1234;
        assert_matches!(ret_discr, InsertionTime::Now { .. });

        assert_matches!(
            map.get_or_insert(10, 2).map(|x| x.value).split(),
            (&mut 1234, InsertionTime::Before { .. })
        );
        assert_matches!(
            map.get_or_insert(10, 3).map(|x| x.value).split(),
            (&mut 1234, InsertionTime::Before { .. })
        );
    }

    #[test]
    fn associate_key() {
        let mut map = MultiKeyMap::<u32, u32>::new();

        let (ret, ret_discr) = map.get_or_insert(100, 1).split();
        let index0 = ret.index;
        *ret.value = 1234;
        assert_matches!(ret_discr, InsertionTime::Now { .. });

        let index1 = map.get_or_insert(200, 200).into_inner().index;
        let index2 = map.get_or_insert(300, 300).into_inner().index;

        map.associate_key(20, index0);
        map.associate_key(20, index1);
        map.associate_key(20, index2);
        assert_eq!(map[&20], 1234);

        map.associate_key(30, index0);
        map.associate_key(30, index1);
        map.associate_key(30, index2);
        assert_eq!(map[&30], 1234);

        map.associate_key(50, index2);
        map.associate_key(50, index0);
        map.associate_key(50, index1);
        assert_eq!(map[&50], 300);

        map[&100] = 456;
        assert_eq!(map[&20], 456);
        assert_eq!(map[&30], 456);
    }

    #[test]
    fn associate_key_forced() {
        let mut map = MultiKeyMap::<u32, u32>::new();

        let index0 = map.get_or_insert(100, 1000).into_inner().index;
        let index1 = map.get_or_insert(200, 2000).into_inner().index;
        let index2 = map.get_or_insert(300, 3000).into_inner().index;

        assert_eq!(map.associate_key_forced(20, index2), None);
        assert_eq!(map.associate_key_forced(20, index1), None);
        assert_eq!(map.associate_key_forced(20, index0), None);
        assert_eq!(map[&20], 1000);

        assert_eq!(map.associate_key_forced(30, index2), None);
        assert_eq!(map.associate_key_forced(30, index0), None);
        assert_eq!(map.associate_key_forced(30, index1), None);
        assert_eq!(map[&30], 2000);

        assert_eq!(map.associate_key_forced(50, index1), None);
        assert_eq!(map.associate_key_forced(50, index0), None);
        assert_eq!(map.associate_key_forced(50, index2), None);
        assert_eq!(map[&50], 3000);

        assert_eq!(map.associate_key_forced(100, index2), None);
        assert_eq!(map.associate_key_forced(20, index2), Some(1000));

        assert_eq!(map.associate_key_forced(200, index2), None);
        assert_eq!(map.associate_key_forced(30, index2), Some(2000));

        must_panic(|| map.associate_key_forced(100, index0)).unwrap();
        must_panic(|| map.associate_key_forced(200, index0)).unwrap();
        must_panic(|| map.associate_key_forced(20, index0)).unwrap();
        must_panic(|| map.associate_key_forced(30, index0)).unwrap();
    }

    #[test]
    fn replace_index() {
        let mut map = MultiKeyMap::<u32, u32>::new();

        let index0 = map.get_or_insert(1000, 200).into_inner().index;
        map.associate_key(1001, index0);
        map.associate_key(1002, index0);

        let index1 = map.get_or_insert(2000, 300).into_inner().index;
        map.associate_key(2001, index1);
        map.associate_key(2002, index1);

        let index2 = map.get_or_insert(3000, 400).into_inner().index;
        map.associate_key(3001, index2);
        map.associate_key(3002, index2);

        assert_eq!(map[&1000], 200);
        assert_eq!(map[&1001], 200);
        assert_eq!(map[&1001], 200);

        map.replace_index(index0, 205);
        assert_eq!(map[&1000], 205);
        assert_eq!(map[&1001], 205);
        assert_eq!(map[&1001], 205);

        map.replace_index(index1, 305);
        assert_eq!(map[&2000], 305);
        assert_eq!(map[&2001], 305);
        assert_eq!(map[&2001], 305);

        map.replace_index(index2, 405);
        assert_eq!(map[&3000], 405);
        assert_eq!(map[&3001], 405);
        assert_eq!(map[&3001], 405);
    }

    #[test]
    fn replace_with_index() {
        let mut map = MultiKeyMap::<u32, u32>::new();

        let index0 = map.get_or_insert(1000, 200).into_inner().index;
        map.associate_key(1001, index0);
        map.associate_key(1002, index0);

        let index1 = map.get_or_insert(2000, 300).into_inner().index;
        map.associate_key(2001, index1);
        map.associate_key(2002, index1);

        let index2 = map.get_or_insert(3000, 400).into_inner().index;
        map.associate_key(3001, index2);
        map.associate_key(3002, index2);

        map.replace_with_index(index0, index2);
        assert_eq!(map[&1000], 400);
        assert_eq!(map[&1001], 400);
        assert_eq!(map[&1002], 400);
        assert_eq!(map[&2000], 300);
        assert_eq!(map[&2001], 300);
        assert_eq!(map[&2002], 300);
        assert_eq!(map[&3000], 400);
        assert_eq!(map[&3001], 400);
        assert_eq!(map[&3002], 400);
        map[&1000] = 600;
        assert_eq!(map[&1000], 600);
        assert_eq!(map[&1001], 600);
        assert_eq!(map[&1002], 600);
        assert_eq!(map[&2000], 300);
        assert_eq!(map[&2001], 300);
        assert_eq!(map[&2002], 300);
        assert_eq!(map[&3000], 600);
        assert_eq!(map[&3001], 600);
        assert_eq!(map[&3002], 600);

        map.replace_with_index(index1, index0);
        map[&1000] = 800;
        assert_eq!(map[&1000], 800);
        assert_eq!(map[&1001], 800);
        assert_eq!(map[&1002], 800);
        assert_eq!(map[&2000], 800);
        assert_eq!(map[&2001], 800);
        assert_eq!(map[&2002], 800);
        assert_eq!(map[&3000], 800);
        assert_eq!(map[&3001], 800);
        assert_eq!(map[&3002], 800);
    }

    #[test]
    fn indexing() {
        let mut map = MultiKeyMap::<u32, u32>::new();

        let (index0, it0) = map.get_or_insert(1000, 200).map(|x| x.index).split();
        let (index1, it1) = map.get_or_insert(2000, 300).map(|x| x.index).split();
        let (index2, it2) = map.get_or_insert(3000, 400).map(|x| x.index).split();

        assert_eq!(it0, InsertionTime::Now(()));
        assert_eq!(it1, InsertionTime::Now(()));
        assert_eq!(it2, InsertionTime::Now(()));

        let expected = vec![
            (1000, index0, 200),
            (2000, index1, 300),
            (3000, index2, 400),
        ];
        #[allow(clippy::deref_addrof)]
        for (key, index, val) in expected {
            assert_eq!(*map.get_with_index(index).unwrap(), val);
            assert_eq!(*map.get(&key).unwrap(), val);
            assert_eq!(*(&map[&key]), val);

            assert_eq!(*map.get_mut(&key).unwrap(), val);
            assert_eq!(*map.get_mut_with_index(index).unwrap(), val);
            assert_eq!(*(&mut map[&key]), val);
        }
    }
}