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
240
241
242
243
244
245
246
247
248
249
use super::{unique_map::UniqueMap, *};
use std::{collections::BTreeSet, fmt, iter::IntoIterator};
#[derive(Clone)]
pub struct RememberingUniqueMap<K: TName + Ord, V> {
map: UniqueMap<K, V>,
gotten_keys: BTreeSet<K>,
}
#[allow(clippy::new_without_default)]
impl<K: TName, V> RememberingUniqueMap<K, V> {
pub fn new() -> Self {
RememberingUniqueMap {
map: UniqueMap::new(),
gotten_keys: BTreeSet::new(),
}
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn add(&mut self, key: K, value: V) -> Result<(), (K, K::Loc)> {
self.map.add(key, value)
}
pub fn key_gotten(&self, key: &K) -> bool {
self.gotten_keys.contains(key)
}
pub fn contains_key(&self, key: &K) -> bool {
self.map.contains_key(key)
}
pub fn contains_key_(&self, key_: &K::Key) -> bool {
self.map.contains_key_(key_)
}
pub fn get(&mut self, key: &K) -> Option<&V> {
self.gotten_keys.insert(key.clone());
self.map.get(key)
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.gotten_keys.insert(key.clone());
self.map.get_mut(key)
}
pub fn get_loc(&mut self, key: &K) -> Option<&K::Loc> {
self.gotten_keys.insert(key.clone());
self.map.get_loc(key)
}
pub fn remove(&mut self, key: &K) -> Option<V> {
self.gotten_keys.remove(key);
self.map.remove(key)
}
pub fn map<V2, F>(self, f: F) -> RememberingUniqueMap<K, V2>
where
F: FnMut(K, V) -> V2,
{
RememberingUniqueMap {
map: self.map.map(f),
gotten_keys: self.gotten_keys,
}
}
pub fn ref_map<V2, F>(&self, f: F) -> RememberingUniqueMap<K, V2>
where
F: FnMut(K, &V) -> V2,
{
RememberingUniqueMap {
map: self.map.ref_map(f),
gotten_keys: self.gotten_keys.clone(),
}
}
pub fn union_with<F>(&self, other: &Self, f: F) -> Self
where
V: Clone,
F: FnMut(&K, &V, &V) -> V,
{
RememberingUniqueMap {
map: self.map.union_with(&other.map, f),
gotten_keys: self
.gotten_keys
.union(&other.gotten_keys)
.cloned()
.collect(),
}
}
pub fn iter(&self) -> Iter<K, V> {
self.into_iter()
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
self.into_iter()
}
pub fn maybe_from_opt_iter(
iter: impl Iterator<Item = Option<(K, V)>>,
) -> Option<Result<RememberingUniqueMap<K, V>, (K::Key, K::Loc, K::Loc)>> {
let map_res = UniqueMap::maybe_from_opt_iter(iter)?;
Some(map_res.map(|map| RememberingUniqueMap {
map,
gotten_keys: BTreeSet::new(),
}))
}
pub fn maybe_from_iter(
iter: impl Iterator<Item = (K, V)>,
) -> Result<RememberingUniqueMap<K, V>, (K::Key, K::Loc, K::Loc)> {
let map = UniqueMap::maybe_from_iter(iter)?;
Ok(RememberingUniqueMap {
map,
gotten_keys: BTreeSet::new(),
})
}
pub fn into_inner(self) -> UniqueMap<K, V> {
self.map
}
pub fn remember(self) -> BTreeSet<K> {
self.gotten_keys
}
}
impl<K: TName, V: PartialEq> PartialEq for RememberingUniqueMap<K, V> {
fn eq(&self, other: &RememberingUniqueMap<K, V>) -> bool {
self.map == other.map && self.gotten_keys == other.gotten_keys
}
}
impl<K: TName, V: Eq> Eq for RememberingUniqueMap<K, V> {}
impl<K: TName + fmt::Debug, V: fmt::Debug> fmt::Debug for RememberingUniqueMap<K, V>
where
K::Key: fmt::Debug,
K::Loc: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"RememberingUniqueMap {{ map: {:#?}, gotten_keys: {:#?} }}",
self.map, self.gotten_keys
)
}
}
pub struct IntoIter<K: TName, V>(unique_map::IntoIter<K, V>);
impl<K: TName, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<K: TName, V> IntoIterator for RememberingUniqueMap<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.map.into_iter())
}
}
pub struct Iter<'a, K: TName, V>(unique_map::Iter<'a, K, V>);
impl<'a, K: TName, V> Iterator for Iter<'a, K, V> {
type Item = (K::Loc, &'a K::Key, &'a V);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<'a, K: TName, V> IntoIterator for &'a RememberingUniqueMap<K, V> {
type Item = (K::Loc, &'a K::Key, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
let m = &self.map;
Iter(m.into_iter())
}
}
pub struct IterMut<'a, K: TName, V>(unique_map::IterMut<'a, K, V>);
impl<'a, K: TName, V> Iterator for IterMut<'a, K, V> {
type Item = (K::Loc, &'a K::Key, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<'a, K: TName, V> IntoIterator for &'a mut RememberingUniqueMap<K, V> {
type Item = (K::Loc, &'a K::Key, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
let m = &mut self.map;
IterMut(m.into_iter())
}
}