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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use super::*;
use std::{collections::BTreeMap, fmt::Debug, iter::IntoIterator};
#[derive(Default, Clone, Debug)]
pub struct UniqueMap<K: TName, V>(pub(crate) BTreeMap<K::Key, (K::Loc, V)>);
impl<K: TName, V> UniqueMap<K, V> {
pub fn new() -> Self {
UniqueMap(BTreeMap::new())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn add(&mut self, key: K, value: V) -> Result<(), (K, K::Loc)> {
if let Some(old_loc) = self.get_loc(&key) {
return Err((key, *old_loc));
}
let (loc, key_) = key.drop_loc();
let old_value = self.0.insert(key_, (loc, value));
assert!(old_value.is_none());
Ok(())
}
pub fn contains_key(&self, key: &K) -> bool {
self.contains_key_(key.borrow().1)
}
pub fn contains_key_(&self, key_: &K::Key) -> bool {
self.0.contains_key(key_)
}
pub fn get(&self, key: &K) -> Option<&V> {
self.get_(key.borrow().1)
}
pub fn get_(&self, key_: &K::Key) -> Option<&V> {
self.0.get(key_).map(|loc_value| &loc_value.1)
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.get_mut_(key.borrow().1)
}
pub fn get_mut_(&mut self, key_: &K::Key) -> Option<&mut V> {
self.0.get_mut(key_).map(|loc_value| &mut loc_value.1)
}
pub fn get_loc(&self, key: &K) -> Option<&K::Loc> {
self.get_loc_(key.borrow().1)
}
pub fn get_loc_(&self, key_: &K::Key) -> Option<&K::Loc> {
self.0.get(key_).map(|loc_value| &loc_value.0)
}
pub fn remove(&mut self, key: &K) -> Option<V> {
self.remove_(key.borrow().1)
}
pub fn remove_(&mut self, key_: &K::Key) -> Option<V> {
self.0.remove(key_).map(|loc_value| loc_value.1)
}
pub fn map<V2, F>(self, mut f: F) -> UniqueMap<K, V2>
where
F: FnMut(K, V) -> V2,
{
UniqueMap(
self.0
.into_iter()
.map(|(k_, (loc, v))| {
let v2 = f(K::add_loc(loc, k_.clone()), v);
(k_, (loc, v2))
})
.collect(),
)
}
pub fn filter_map<V2, F>(self, mut f: F) -> UniqueMap<K, V2>
where
F: FnMut(K, V) -> Option<V2>,
{
UniqueMap(
self.0
.into_iter()
.filter_map(|(k_, (loc, v))| {
let v2_opt = f(K::add_loc(loc, k_.clone()), v);
v2_opt.map(|v2| (k_, (loc, v2)))
})
.collect(),
)
}
pub fn ref_map<V2, F>(&self, mut f: F) -> UniqueMap<K, V2>
where
F: FnMut(K, &V) -> V2,
{
UniqueMap(
self.0
.iter()
.map(|(k_, loc_v)| {
let loc = loc_v.0;
let v = &loc_v.1;
let k = K::add_loc(loc, k_.clone());
let v2 = f(k, v);
(k_.clone(), (loc, v2))
})
.collect(),
)
}
pub fn union_with<F>(&self, other: &Self, mut f: F) -> Self
where
V: Clone,
F: FnMut(&K, &V, &V) -> V,
{
let mut joined = Self::new();
for (loc, k_, v1) in self.iter() {
let k = K::add_loc(loc, k_.clone());
let v = match other.get(&k) {
None => v1.clone(),
Some(v2) => f(&k, v1, v2),
};
assert!(joined.add(k, v).is_ok())
}
for (loc, k_, v2) in other.iter() {
if !joined.contains_key_(k_) {
let k = K::add_loc(loc, k_.clone());
assert!(joined.add(k, v2.clone()).is_ok())
}
}
joined
}
pub fn iter(&self) -> Iter<K, V> {
self.into_iter()
}
pub fn key_cloned_iter(&self) -> impl Iterator<Item = (K, &V)> {
self.into_iter()
.map(|(loc, k_, v)| (K::add_loc(loc, k_.clone()), v))
}
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<UniqueMap<K, V>, (K::Key, K::Loc, K::Loc)>> {
Some(Self::maybe_from_iter(
iter.collect::<Option<Vec<_>>>()?.into_iter(),
))
}
pub fn maybe_from_iter(
iter: impl Iterator<Item = (K, V)>,
) -> Result<UniqueMap<K, V>, (K::Key, K::Loc, K::Loc)> {
let mut m = Self::new();
for (k, v) in iter {
if let Err((k, old_loc)) = m.add(k, v) {
let (loc, key_) = k.drop_loc();
return Err((key_, loc, old_loc));
}
}
Ok(m)
}
}
impl<K: TName, V: PartialEq> PartialEq for UniqueMap<K, V> {
fn eq(&self, other: &UniqueMap<K, V>) -> bool {
self.iter()
.all(|(_, k_, v1)| other.get_(k_).map(|v2| v1 == v2).unwrap_or(false))
&& other.iter().all(|(_, k_, _)| self.contains_key_(k_))
}
}
impl<K: TName, V: Eq> Eq for UniqueMap<K, V> {}
impl<K: TName, V: PartialOrd> PartialOrd for UniqueMap<K, V> {
fn partial_cmp(&self, other: &UniqueMap<K, V>) -> Option<std::cmp::Ordering> {
self.0
.iter()
.map(|(k_, loc_v)| {
let v = &loc_v.1;
(k_, v)
})
.partial_cmp(other.0.iter().map(|(k_, loc_v)| {
let v = &loc_v.1;
(k_, v)
}))
}
}
impl<K: TName, V: Ord> Ord for UniqueMap<K, V> {
fn cmp(&self, other: &UniqueMap<K, V>) -> std::cmp::Ordering {
self.0
.iter()
.map(|(k_, loc_v)| {
let v = &loc_v.1;
(k_, v)
})
.cmp(other.0.iter().map(|(k_, loc_v)| {
let v = &loc_v.1;
(k_, v)
}))
}
}
impl<K: TName, V: Hash> Hash for UniqueMap<K, V>
where
K::Key: Hash,
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
for (k_, loc_v) in &self.0 {
let v = &loc_v.1;
k_.hash(state);
v.hash(state);
}
}
}
pub struct IntoIter<K: TName, V>(
std::iter::Map<
std::collections::btree_map::IntoIter<K::Key, (K::Loc, V)>,
fn((K::Key, (K::Loc, V))) -> (K, V),
>,
usize,
);
impl<K: TName, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
if self.1 > 0 {
self.1 -= 1;
}
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.1, Some(self.1))
}
}
impl<K: TName, V> IntoIterator for UniqueMap<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
IntoIter(
self.0.into_iter().map(|(k_, loc_v)| {
let loc = loc_v.0;
let v = loc_v.1;
let k = K::add_loc(loc, k_);
(k, v)
}),
len,
)
}
}
pub struct Iter<'a, K: TName, V>(
std::iter::Map<
std::collections::btree_map::Iter<'a, K::Key, (K::Loc, V)>,
fn((&'a K::Key, &'a (K::Loc, V))) -> (K::Loc, &'a K::Key, &'a V),
>,
usize,
);
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> {
if self.1 > 0 {
self.1 -= 1;
}
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.1, Some(self.1))
}
}
impl<'a, K: TName, V> IntoIterator for &'a UniqueMap<K, V> {
type Item = (K::Loc, &'a K::Key, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
let fix = |(k_, loc_v): (&'a K::Key, &'a (K::Loc, V))| -> (K::Loc, &'a K::Key, &'a V) {
let loc = loc_v.0;
let v = &loc_v.1;
(loc, k_, v)
};
Iter(self.0.iter().map(fix), self.len())
}
}
pub struct IterMut<'a, K: TName, V>(
std::iter::Map<
std::collections::btree_map::IterMut<'a, K::Key, (K::Loc, V)>,
fn((&'a K::Key, &'a mut (K::Loc, V))) -> (K::Loc, &'a K::Key, &'a mut V),
>,
usize,
);
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> {
if self.1 > 0 {
self.1 -= 1;
}
self.0.next()
}
}
impl<'a, K: TName, V> IntoIterator for &'a mut UniqueMap<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 len = self.len();
let fix =
|(k_, loc_v): (&'a K::Key, &'a mut (K::Loc, V))| -> (K::Loc, &'a K::Key, &'a mut V) {
let loc = loc_v.0;
let v = &mut loc_v.1;
(loc, k_, v)
};
IterMut(self.0.iter_mut().map(fix), len)
}
}