summaryrefslogtreecommitdiff
path: root/utils/data_struct/src/bi_map.rs
blob: c21a9c867d0b023910e5fa78827546711e7137a1 (plain)
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
use ahash::AHasher;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hash};

type FastHashMap<K, V> = HashMap<K, V, BuildHasherDefault<AHasher>>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BiMap<A, B>
where
    A: Eq + Hash + Clone,
    B: Eq + Hash + Clone,
{
    #[serde(flatten)]
    a_to_b: FastHashMap<A, B>,
    #[serde(skip)]
    b_to_a: FastHashMap<B, A>,
}

pub struct Entry<'a, A, B>
where
    A: Eq + Hash + Clone,
    B: Eq + Hash + Clone,
{
    bimap: &'a mut BiMap<A, B>,
    key: A,
    value: Option<B>,
}

impl<A, B> BiMap<A, B>
where
    A: Eq + Hash + Clone,
    B: Eq + Hash + Clone,
{
    pub fn new() -> Self {
        Self {
            a_to_b: FastHashMap::default(),
            b_to_a: FastHashMap::default(),
        }
    }

    pub fn entry(&mut self, a: A) -> Entry<'_, A, B> {
        let value = self.a_to_b.get(&a).cloned();
        Entry {
            bimap: self,
            key: a,
            value,
        }
    }

    #[inline(always)]
    pub fn insert(&mut self, a: A, b: B) {
        if let Some(old_b) = self.a_to_b.insert(a.clone(), b.clone()) {
            self.b_to_a.remove(&old_b);
        }
        if let Some(old_a) = self.b_to_a.insert(b.clone(), a.clone()) {
            self.a_to_b.remove(&old_a);
        }
    }

    #[inline(always)]
    pub fn get_by_a(&self, key: &A) -> Option<&B> {
        self.a_to_b.get(key)
    }

    #[inline(always)]
    pub fn get_by_b(&self, key: &B) -> Option<&A> {
        self.b_to_a.get(key)
    }

    pub fn remove_by_a(&mut self, key: &A) -> Option<(A, B)> {
        if let Some(b) = self.get_by_a(key).cloned() {
            let a = self.get_by_b(&b).cloned().unwrap();
            self.a_to_b.remove(key);
            self.b_to_a.remove(&b);
            Some((a, b))
        } else {
            None
        }
    }

    pub fn remove_by_b(&mut self, key: &B) -> Option<(A, B)> {
        if let Some(a) = self.get_by_b(key).cloned() {
            let b = self.get_by_a(&a).cloned().unwrap();
            self.b_to_a.remove(key);
            self.a_to_b.remove(&a);
            Some((a, b))
        } else {
            None
        }
    }

    pub fn reserve(&mut self, additional: usize) {
        self.a_to_b.reserve(additional);
        self.b_to_a.reserve(additional);
    }

    pub fn len(&self) -> usize {
        self.a_to_b.len()
    }

    pub fn is_empty(&self) -> bool {
        self.a_to_b.is_empty()
    }

    pub fn clear(&mut self) {
        self.a_to_b.clear();
        self.b_to_a.clear();
    }

    pub fn contains_a(&self, key: &A) -> bool {
        self.a_to_b.contains_key(key)
    }

    pub fn contains_b(&self, key: &B) -> bool {
        self.b_to_a.contains_key(key)
    }

    pub fn keys_a(&self) -> impl Iterator<Item = &A> {
        self.a_to_b.keys()
    }

    pub fn keys_b(&self) -> impl Iterator<Item = &B> {
        self.b_to_a.keys()
    }

    pub fn iter_a_to_b(&self) -> impl Iterator<Item = (&A, &B)> {
        self.a_to_b.iter()
    }

    pub fn iter_b_to_a(&self) -> impl Iterator<Item = (&B, &A)> {
        self.b_to_a.iter()
    }
}

impl<'a, A, B> Entry<'a, A, B>
where
    A: Eq + Hash + Clone,
    B: Eq + Hash + Clone,
{
    pub fn and_modify<F>(mut self, f: F) -> Self
    where
        F: FnOnce(&mut B),
    {
        if let Some(ref mut value) = self.value {
            f(value);
        }
        self
    }

    pub fn or_insert(self, default: B) -> Result<&'a mut B, &'static str> {
        self.or_insert_with(|| default)
    }

    pub fn or_insert_with<F>(mut self, default: F) -> Result<&'a mut B, &'static str>
    where
        F: FnOnce() -> B,
    {
        if self.value.is_none() {
            self.value = Some(default());
        }

        let value = self.value.as_ref().ok_or("Value is None")?.clone();
        self.bimap.insert(self.key.clone(), value);

        self.bimap
            .a_to_b
            .get_mut(&self.key)
            .ok_or("Key not found in a_to_b map")
    }
}

impl<A, B> Default for BiMap<A, B>
where
    A: Eq + Hash + Clone,
    B: Eq + Hash + Clone,
{
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_bimap_basic_operations() {
        let mut bimap = BiMap::new();
        bimap.insert("key1", "value1");

        assert_eq!(bimap.get_by_a(&"key1"), Some(&"value1"));
        assert_eq!(bimap.get_by_b(&"value1"), Some(&"key1"));
        assert!(bimap.contains_a(&"key1"));
        assert!(bimap.contains_b(&"value1"));
    }

    #[test]
    fn test_bimap_remove() {
        let mut bimap = BiMap::new();
        bimap.insert(1, "one");

        assert_eq!(bimap.remove_by_a(&1), Some((1, "one")));
        assert!(bimap.is_empty());
    }

    #[test]
    fn test_bimap_entry() {
        let mut bimap = BiMap::new();
        bimap.entry("key1").or_insert("value1").unwrap();

        assert_eq!(bimap.get_by_a(&"key1"), Some(&"value1"));
    }

    #[test]
    fn test_bimap_iterators() {
        let mut bimap = BiMap::new();
        bimap.insert(1, "one");
        bimap.insert(2, "two");

        let a_keys: Vec<_> = bimap.keys_a().collect();
        assert!(a_keys.contains(&&1) && a_keys.contains(&&2));

        let b_keys: Vec<_> = bimap.keys_b().collect();
        assert!(b_keys.contains(&&"one") && b_keys.contains(&&"two"));
    }

    #[test]
    fn test_bimap_duplicate_insert() {
        let mut bimap = BiMap::new();
        bimap.insert(1, "one");
        bimap.insert(1, "new_one");
        bimap.insert(2, "one");

        assert_eq!(bimap.get_by_a(&1), Some(&"new_one"));
        assert_eq!(bimap.get_by_b(&"one"), Some(&2));
        assert_eq!(bimap.get_by_a(&2), Some(&"one"));
    }
}