-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmkmvmap.rs
More file actions
140 lines (121 loc) · 4.13 KB
/
Copy pathmkmvmap.rs
File metadata and controls
140 lines (121 loc) · 4.13 KB
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
use im_rc::{HashMap, HashSet};
use std::fmt;
use std::hash::Hash;
#[derive(Clone, Debug)]
pub struct MKMVMap<K: Eq + Hash + Clone + fmt::Debug, V: Clone> {
current_id: usize,
keys: HashMap<K, HashSet<usize>>,
values: HashMap<usize, Value<K, V>>,
}
#[derive(Clone)]
pub(crate) struct Value<K, V> {
id: usize,
keys: Vec<K>,
data: V,
}
impl<K: Eq, V: PartialEq> PartialEq for Value<K, V> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.keys == other.keys && self.data == other.data
}
}
impl<K: Eq + Hash + Clone + fmt::Debug, V: Clone + PartialEq> PartialEq for MKMVMap<K, V> {
fn eq(&self, other: &Self) -> bool {
self.current_id == other.current_id
&& self.keys == other.keys
&& self.values == other.values
}
}
impl<K: Eq + Hash + Clone + fmt::Debug, V: Clone> MKMVMap<K, V> {
pub fn new() -> MKMVMap<K, V> {
MKMVMap {
current_id: 0,
keys: HashMap::new(),
values: HashMap::new(),
}
}
pub fn add(&mut self, keys: Vec<K>, data: V) {
let id = self.current_id;
self.current_id += 1;
self.keys = keys.iter().fold(self.keys.clone(), |keys, key| {
keys.alter(
|existing| Some(existing.map_or_else(|| HashSet::unit(id), |set| set.update(id))),
key.clone(),
)
});
self.values = self.values.update(id, Value { id, keys, data });
}
pub fn extract(&mut self, key: &K) -> Option<Vec<V>> {
let (ids, keys) = self.keys.extract(key)?;
self.keys = keys;
let mut values = Vec::new();
for id in ids {
if let Some((value, value_map)) = self.values.extract(&id) {
self.values = value_map;
// This attempts to be "correct" by cleaning up all of the ids
// when a value is extracted, but this does mean doing a fair
// amount of work every time. In theory we could one not bother
// and would only pay a minor cost skipping over the garbage,
// except we have some other areas that depend on this being an
// accurate reflection of what is actually being watched.
self.keys = self.keys.alter(
|existing| {
let updated = existing?.without(&value.id);
if updated.is_empty() {
None
} else {
Some(updated)
}
},
key.clone(),
);
values.push(value.data);
}
}
Some(values)
}
pub fn is_empty(&self) -> bool {
self.keys.is_empty()
}
pub fn keys(&self) -> impl Iterator<Item = &K> {
self.keys.keys()
}
}
impl<K: Eq + Hash + Clone + fmt::Debug, V> fmt::Debug for Value<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Value {:?} {:?}", self.id, self.keys)
}
}
#[cfg(test)]
mod tests {
use super::MKMVMap;
#[test]
fn empty() {
let mut map: MKMVMap<usize, usize> = MKMVMap::new();
let values = map.extract(&1);
assert_eq!(values, None);
}
#[test]
fn add_and_extract() {
let mut map = MKMVMap::new();
map.add(vec![1, 2], "12");
let values = map.extract(&1);
assert_eq!(values, Some(vec!["12"]));
assert!(map.values.is_empty());
}
#[test]
fn value_eq() {
let mut a1: MKMVMap<usize, usize> = MKMVMap::new();
a1.add(vec![1], 1);
let mut a2: MKMVMap<usize, usize> = MKMVMap::new();
a2.add(vec![1], 1);
let b: MKMVMap<usize, usize> = MKMVMap::new();
assert_eq!(a1, a2);
assert_ne!(a1, b);
}
#[test]
fn debug_impl() {
let mut a1: MKMVMap<usize, usize> = MKMVMap::new();
a1.add(vec![1], 1);
assert_ne!(format!("{a1:?}"), "");
}
}