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
use std::collections::HashMap;
use std::collections::hash_map::Iter;
use std::hash::Hash;
use std::borrow::Borrow;
pub struct MultiMap<K1: Eq + Hash, K2: Eq + Hash, V> {
value_map: HashMap<K1, (K2, V)>,
key_map: HashMap<K2, K1>,
}
impl<K1: Eq + Hash + Clone, K2: Eq + Hash + Clone, V> MultiMap<K1, K2, V> {
pub fn new() -> MultiMap<K1, K2, V> {
MultiMap {
value_map: HashMap::new(),
key_map: HashMap::new(),
}
}
pub fn insert(&mut self, key_one: K1, key_two: K2, value: V) {
self.key_map.insert(key_two.clone(), key_one.clone());
self.value_map.insert(key_one, (key_two, value));
}
pub fn get(&self, key: &K1) -> Option<&V> {
let mut result = None;
if let Some(pair) = self.value_map.get(key) {
result = Some(&pair.1)
}
result
}
pub fn get_mut(&mut self, key: &K1) -> Option<&mut V> {
let mut result = None;
if let Some(pair) = self.value_map.get_mut(key) {
result = Some(&mut pair.1)
}
result
}
pub fn get_alt(&self, key: &K2) -> Option<&V> {
let mut result = None;
if let Some(key_a) = self.key_map.get(key) {
if let Some(pair) = self.value_map.get(&key_a) {
result = Some(&pair.1)
}
}
result
}
pub fn get_mut_alt(&mut self, key: &K2) -> Option<&mut V> {
let mut result = None;
if let Some(key_a) = self.key_map.get(key) {
if let Some(pair) = self.value_map.get_mut(&key_a) {
result = Some(&mut pair.1)
}
}
result
}
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where K1: Borrow<Q>,
Q: Hash + Eq
{
let mut result = None;
if let Some(pair) = self.value_map.remove(key) {
self.key_map.remove(&pair.0);
result = Some(pair.1)
}
result
}
pub fn remove_alt<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where K2: Borrow<Q>,
Q: Hash + Eq
{
let mut result = None;
if let Some(key_a) = self.key_map.remove(key) {
if let Some(pair) = self.value_map.remove(&key_a) {
result = Some(pair.1)
}
}
result
}
pub fn iter(&self) -> Iter<K1, (K2, V)> {
self.value_map.iter()
}
}
mod test {
#[test]
fn big_test() {
use ::MultiMap;
let mut map = MultiMap::new();
map.insert(1, "One", String::from("Ein"));
map.insert(2, "Two", String::from("Zwei"));
map.insert(3, "Three", String::from("Drei"));
assert!(*map.get(&1).unwrap() == String::from("Ein"));
assert!(*map.get(&2).unwrap() == String::from("Zwei"));
assert!(*map.get(&3).unwrap() == String::from("Drei"));
map.get_mut_alt(&"One").unwrap().push_str("s");
assert!(*map.get_alt(&"One").unwrap() == String::from("Eins"));
assert!(*map.get_alt(&"Two").unwrap() == String::from("Zwei"));
assert!(*map.get_alt(&"Three").unwrap() == String::from("Drei"));
map.remove(&3);
assert!(*map.get_alt(&"One").unwrap() == String::from("Eins"));
assert!(*map.get_alt(&"Two").unwrap() == String::from("Zwei"));
assert!(map.get_alt(&"Three") == None);
assert!(map.get(&3) == None);
assert!(map.remove_alt(&"Three") == None);
assert!(*map.remove_alt(&"One").unwrap() == String::from("Eins"));
map.get_mut(&2).unwrap().push_str("!");
assert!(map.get(&1) == None);
assert!(*map.get(&2).unwrap() == String::from("Zwei!"));
assert!(map.get_alt(&"Three") == None);
assert!(map.get(&3) == None);
}
}