1use std::borrow::Borrow;
2use std::collections::{HashMap, HashSet};
3use std::collections::hash_map::{IntoIter, Keys, RandomState};
4use std::collections::hash_map::Iter;
5use std::collections::hash_map::IterMut;
6use std::fmt::{self, Debug};
7use std::hash::{BuildHasher, Hash};
8use std::iter::{FromIterator, IntoIterator, Iterator};
9
10#[macro_export]
11macro_rules! hashsetmultimap {
15 ($($key:expr => $value:expr),*)=>{
16 {
17 let mut _map = HashsetMultiMap::new();
18 $(
19 map.insert($key,$value);
20 )*
21 _map
22 }
23 }
24}
25
26#[derive(Clone)]
27pub struct HashsetMultiMap<K, V, S = RandomState> {
28 _inner_map: HashMap<K, HashSet<V>, S>,
29}
30
31impl<K, V> HashsetMultiMap<K, V>
32where K: Eq + Hash,
33 V: Eq + Hash
34{
35 pub fn new() -> HashsetMultiMap<K, V> {
39 HashsetMultiMap { _inner_map: HashMap::new() }
40 }
41
42 pub fn with_capacity(capacity: usize) -> HashsetMultiMap<K, V> {
46 HashsetMultiMap { _inner_map: HashMap::with_capacity(capacity) }
47 }
48}
49
50impl<K, V, S> HashsetMultiMap<K, V, S>
51where K: Eq + Hash,
52 V: Eq + Hash,
53 S: BuildHasher,
54{
55 pub fn with_hasher(hash_builder: S) -> HashsetMultiMap<K, V, S> {
59 HashsetMultiMap {
60 _inner_map: HashMap::with_hasher(hash_builder)
61 }
62 }
63
64 pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashsetMultiMap<K, V, S> {
68 HashsetMultiMap {
69 _inner_map: HashMap::with_capacity_and_hasher(capacity, hash_builder)
70 }
71 }
72
73 pub fn insert(&mut self, key: K, value: V) {
77
78 match self._inner_map.get_mut(&key) {
79 Some(set) => {
80 set.insert(value);
81 },
82 None => {
83 let mut hashset = HashSet::new();
84 hashset.insert(value);
85 self._inner_map.insert(key, hashset);
86 }
87 };
88 }
89
90 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
94 where K: Borrow<Q>,
95 Q: Eq + Hash
96 {
97 self._inner_map.contains_key(k)
98 }
99
100 pub fn len(&self) -> usize {
104 self._inner_map.len()
105 }
106
107 pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<HashSet<V>>
111 where K: Borrow<Q>,
112 Q: Eq + Hash
113 {
114 self._inner_map.remove(k)
115 }
116
117 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&HashSet<V>>
121 where K: Borrow<Q>,
122 Q: Eq + Hash
123 {
124 self._inner_map.get(k)
125 }
126
127 pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut HashSet<V>>
131 where K: Borrow<Q>,
132 Q: Eq + Hash
133 {
134 self._inner_map.get_mut(k)
135 }
136
137 pub fn capacity(&self) -> usize {
141 self._inner_map.capacity()
142 }
143
144 pub fn is_empty(&self) -> bool {
148 self._inner_map.is_empty()
149 }
150
151 pub fn clear(&mut self) {
156 self._inner_map.clear();
157 }
158
159 pub fn keys(&self) -> Keys<K, HashSet<V>> {
163 self._inner_map.keys()
164 }
165
166 pub fn iter(&self) -> Iter<K, HashSet<V>> {
169 self._inner_map.iter()
170 }
171
172 pub fn iter_mut(&mut self) -> IterMut<K, HashSet<V>> {
175 self._inner_map.iter_mut()
176 }
177
178 pub fn retain<F>(&mut self, mut f: F)
184 where F: FnMut(&K, &V) -> bool
185 {
186 for (key, vector) in &mut self._inner_map {
187 vector.retain(|ref value| f(key, value));
188 }
189 self._inner_map.retain(|&_, ref v| !v.is_empty());
190 }
191}
192
193impl<K, V, S> Debug for HashsetMultiMap<K, V, S>
194where K: Eq + Hash + Debug,
195 V: Eq + Hash + Debug,
196 S: BuildHasher
197{
198 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199
200 let items: Vec<(&K, &HashSet<V>)> = self.iter().map(|(key, value)| (key, value)).collect();
201 let mut debug_map = f.debug_map();
202
203 for item in items {
204 debug_map.entry(&item.0, &item.1);
205 }
206
207 return debug_map.finish();
208 }
209}
210
211impl<K, V, S> PartialEq for HashsetMultiMap<K, V, S>
212where K: Eq + Hash,
213 V: PartialEq + Eq + Hash,
214 S: BuildHasher
215{
216 fn eq(&self, other: &HashsetMultiMap<K, V, S>) -> bool {
217 if self.len() != other.len() {
218 return false;
219 }
220
221 self.iter().all(|(key, value)| other.get(key).map_or(false, |v| value == v))
222 }
223}
224
225impl<K, V, S> Eq for HashsetMultiMap<K, V, S>
226where K: Eq + Hash,
227 V: Eq + Hash,
228 S: BuildHasher
229{
230}
231
232impl<K, V, S> Default for HashsetMultiMap<K, V, S>
233where K: Eq + Hash,
234 V: Eq + Hash,
235 S: BuildHasher + Default
236{
237 fn default() -> HashsetMultiMap<K, V, S> {
238 HashsetMultiMap { _inner_map: Default::default() }
239 }
240}
241
242impl<K, V, S> FromIterator<(K, V)> for HashsetMultiMap<K, V, S>
243where K: Eq + Hash,
244 V: Eq + Hash,
245 S: BuildHasher + Default
246{
247 fn from_iter<T: IntoIterator<Item = (K, V)>>(iterable: T) -> HashsetMultiMap<K, V, S> {
248 let iter = iterable.into_iter();
249 let hint = iter.size_hint().0;
250
251 let mut map = HashsetMultiMap::with_capacity_and_hasher(hint, S::default());
252 for (k, v) in iter {
253 map.insert(k, v);
254 }
255
256 map
257 }
258}
259
260impl<'a, K, V, S> IntoIterator for &'a HashsetMultiMap<K, V, S>
261where K: Eq + Hash,
262 V: Eq + Hash,
263 S: BuildHasher
264{
265 type Item = (&'a K, &'a HashSet<V>);
266 type IntoIter = Iter<'a, K, HashSet<V>>;
267
268 fn into_iter(self) -> Iter<'a, K, HashSet<V>> {
269 self.iter()
270 }
271}
272
273impl<'a, K, V, S> IntoIterator for &'a mut HashsetMultiMap<K, V, S>
274where K: Eq + Hash,
275 V: Eq + Hash,
276 S: BuildHasher
277{
278 type Item = (&'a K, &'a mut HashSet<V>);
279 type IntoIter = IterMut<'a, K, HashSet<V>>;
280
281 fn into_iter(self) -> Self::IntoIter {
282 self._inner_map.iter_mut()
283 }
284}
285
286impl<K, V, S> IntoIterator for HashsetMultiMap<K, V, S>
287where K: Eq + Hash,
288 V: Eq + Hash,
289 S: BuildHasher
290{
291 type Item = (K, HashSet<V>);
292 type IntoIter = IntoIter<K, HashSet<V>>;
293
294 fn into_iter(self) -> Self::IntoIter {
295 self._inner_map.into_iter()
296 }
297}
298
299impl<K, V, S> Extend<(K, V)> for HashsetMultiMap<K, V, S>
300where K: Eq + Hash,
301 V: Eq + Hash,
302 S: BuildHasher
303{
304 fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
305 for (k, v) in iter {
306 self.insert(k, v);
307 }
308 }
309}
310
311impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashsetMultiMap<K, V, S>
312where K: Eq + Hash + Copy,
313 V: Eq + Hash + Copy,
314 S: BuildHasher
315{
316 fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
317 self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
318 }
319}