37 lines
1002 B
Rust
37 lines
1002 B
Rust
|
|
use std::collections::HashMap;
|
||
|
|
use std::hash::Hash;
|
||
|
|
|
||
|
|
pub struct HashList<K: Eq + Hash, V>(HashMap<K, Vec<V>>);
|
||
|
|
|
||
|
|
impl<K: Eq + Hash + core::fmt::Debug, V: core::fmt::Debug> core::fmt::Debug for HashList<K, V> {
|
||
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
|
|
f.debug_tuple("HashList").field(&self.0).finish()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<K: Eq + Hash + Clone, V: Clone> Clone for HashList<K, V> {
|
||
|
|
fn clone(&self) -> Self {
|
||
|
|
Self(self.0.clone())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<K: Eq + Hash, V> HashList<K, V> {
|
||
|
|
pub fn new() -> Self {
|
||
|
|
Self(HashMap::new())
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn add(&mut self, key: K, value: V) {
|
||
|
|
match self.0.get_mut(&key) {
|
||
|
|
Some(values) => values.push(value),
|
||
|
|
None => {
|
||
|
|
self.0.insert(key, vec![value]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
pub fn decompose(&mut self) -> Box<[(K, Vec<V>)]> {
|
||
|
|
let mut body = HashMap::new();
|
||
|
|
core::mem::swap(&mut self.0, &mut body);
|
||
|
|
body.into_iter().collect()
|
||
|
|
}
|
||
|
|
}
|