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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/// A sorted map that allows multiple values for each key.
pub use dup_tree_map::DupTreeMap;
pub use dup_tree_set::DupTreeSet;
mod one_or_more {
use either::Either;
use std::{iter, slice, vec};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum OneOrMore<T> {
One(T),
More(Vec<T>),
}
pub type Iter<'a, T> = Either<slice::Iter<'a, T>, iter::Once<&'a T>>;
pub type IterMut<'a, T> =
Either<slice::IterMut<'a, T>, iter::Once<&'a mut T>>;
pub type IntoIter<T> = Either<vec::IntoIter<T>, iter::Once<T>>;
impl<T> OneOrMore<T> {
pub fn new(x: T) -> Self {
OneOrMore::One(x)
}
pub fn push(&mut self, x: T) {
take_mut::take(self, |sef| match sef {
OneOrMore::More(mut vec) => {
vec.push(x);
OneOrMore::More(vec)
}
OneOrMore::One(one) => OneOrMore::More(vec![one, x]),
})
}
pub fn iter(&self) -> Iter<'_, T> {
match self {
OneOrMore::More(vec) => Either::Left(vec.iter()),
OneOrMore::One(x) => Either::Right(iter::once(x)),
}
}
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
match self {
OneOrMore::More(vec) => Either::Left(vec.iter_mut()),
OneOrMore::One(x) => Either::Right(iter::once(x)),
}
}
}
impl<T> IntoIterator for OneOrMore<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
match self {
OneOrMore::More(vec) => Either::Left(vec.into_iter()),
OneOrMore::One(x) => Either::Right(iter::once(x)),
}
}
}
}
mod dup_tree_map {
use super::one_or_more;
use super::one_or_more::OneOrMore;
use std::collections::{btree_map, BTreeMap};
use std::iter;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DupTreeMap<K, V> {
map: BTreeMap<K, OneOrMore<V>>,
size: usize,
}
pub type IterInternalIntermediate<'a, K, V> =
iter::Zip<iter::Repeat<&'a K>, one_or_more::Iter<'a, V>>;
pub type IterInternal<'a, K, V> = iter::FlatMap<
btree_map::Iter<'a, K, OneOrMore<V>>,
IterInternalIntermediate<'a, K, V>,
for<'b> fn(
(&'b K, &'b OneOrMore<V>),
) -> IterInternalIntermediate<'b, K, V>,
>;
pub struct Iter<'a, K, V> {
iter: IterInternal<'a, K, V>,
size: usize,
}
pub type IterMutInternalIntermediate<'a, K, V> =
iter::Zip<iter::Repeat<&'a K>, one_or_more::IterMut<'a, V>>;
pub type IterMutInternal<'a, K, V> = iter::FlatMap<
btree_map::IterMut<'a, K, OneOrMore<V>>,
IterMutInternalIntermediate<'a, K, V>,
for<'b> fn(
(&'b K, &'b mut OneOrMore<V>),
) -> IterMutInternalIntermediate<'b, K, V>,
>;
pub struct IterMut<'a, K, V> {
iter: IterMutInternal<'a, K, V>,
size: usize,
}
pub type IntoIterInternalIntermediate<K, V> =
iter::Zip<iter::Repeat<K>, one_or_more::IntoIter<V>>;
pub type IntoIterInternal<K, V> = iter::FlatMap<
btree_map::IntoIter<K, OneOrMore<V>>,
IntoIterInternalIntermediate<K, V>,
fn((K, OneOrMore<V>)) -> IntoIterInternalIntermediate<K, V>,
>;
pub struct IntoIter<K: Clone, V> {
iter: IntoIterInternal<K, V>,
size: usize,
}
impl<K, V> DupTreeMap<K, V> {
pub fn new() -> Self
where
K: Ord,
{
DupTreeMap {
map: BTreeMap::new(),
size: 0,
}
}
pub fn insert(&mut self, key: K, value: V)
where
K: Ord,
{
use std::collections::btree_map::Entry;
match self.map.entry(key) {
Entry::Vacant(e) => {
e.insert(OneOrMore::new(value));
}
Entry::Occupied(mut e) => e.get_mut().push(value),
}
self.size += 1;
}
pub fn len(&self) -> usize {
self.size
}
pub fn is_empty(&self) -> bool {
self.size == 0
}
pub fn iter(&self) -> Iter<'_, K, V>
where
K: Ord,
{
fn foo<'a, K, V>(
(k, oom): (&'a K, &'a OneOrMore<V>),
) -> IterInternalIntermediate<'a, K, V> {
iter::repeat(k).zip(oom.iter())
}
Iter {
iter: self.map.iter().flat_map(foo),
size: self.size,
}
}
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
where
K: Ord,
{
fn foo<'a, K, V>(
(k, oom): (&'a K, &'a mut OneOrMore<V>),
) -> IterMutInternalIntermediate<'a, K, V> {
iter::repeat(k).zip(oom.iter_mut())
}
IterMut {
iter: self.map.iter_mut().flat_map(foo),
size: self.size,
}
}
}
impl<K, V> Default for DupTreeMap<K, V>
where
K: Ord,
{
fn default() -> Self {
Self::new()
}
}
impl<K, V> IntoIterator for DupTreeMap<K, V>
where
K: Ord + Clone,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
fn foo<K, V>(
(k, oom): (K, OneOrMore<V>),
) -> IntoIterInternalIntermediate<K, V>
where
K: Clone,
{
iter::repeat(k).zip(oom.into_iter())
}
IntoIter {
iter: self.map.into_iter().flat_map(foo),
size: self.size,
}
}
}
impl<'a, K, V> IntoIterator for &'a DupTreeMap<K, V>
where
K: Ord,
{
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, K, V> IntoIterator for &'a mut DupTreeMap<K, V>
where
K: Ord,
{
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<K, V> iter::FromIterator<(K, V)> for DupTreeMap<K, V>
where
K: Ord,
{
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (K, V)>,
{
let mut map = DupTreeMap::new();
for (k, v) in iter {
map.insert(k, v);
}
map
}
}
impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
let next = self.iter.next();
if next.is_some() {
self.size -= 1;
}
next
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.size, Some(self.size))
}
}
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
let next = self.iter.next();
if next.is_some() {
self.size -= 1;
}
next
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.size, Some(self.size))
}
}
impl<K, V> Iterator for IntoIter<K, V>
where
K: Clone,
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
let next = self.iter.next();
if next.is_some() {
self.size -= 1;
}
next
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.size, Some(self.size))
}
}
// unsafe impl<K, V> iter::TrustedLen for IntoIter<K, V> {}
}
mod dup_tree_set {
use super::DupTreeMap;
use std::iter;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DupTreeSet<K> {
map: DupTreeMap<K, ()>,
}
pub type Iter<'a, K> = iter::Map<
super::dup_tree_map::Iter<'a, K, ()>,
for<'b> fn((&'b K, &'b ())) -> &'b K,
>;
pub type IntoIter<K> =
iter::Map<super::dup_tree_map::IntoIter<K, ()>, fn((K, ())) -> K>;
impl<K> DupTreeSet<K> {
pub fn new() -> Self
where
K: Ord,
{
DupTreeSet {
map: DupTreeMap::new(),
}
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn iter(&self) -> Iter<'_, K>
where
K: Ord,
{
fn foo<'a, K>((k, ()): (&'a K, &'a ())) -> &'a K {
k
}
self.map.iter().map(foo)
}
}
impl<K> Default for DupTreeSet<K>
where
K: Ord,
{
fn default() -> Self {
Self::new()
}
}
impl<K> IntoIterator for DupTreeSet<K>
where
K: Ord + Clone,
{
type Item = K;
type IntoIter = IntoIter<K>;
fn into_iter(self) -> Self::IntoIter {
fn foo<K>((k, ()): (K, ())) -> K {
k
}
self.map.into_iter().map(foo)
}
}
impl<'a, K> IntoIterator for &'a DupTreeSet<K>
where
K: Ord,
{
type Item = &'a K;
type IntoIter = Iter<'a, K>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<K> iter::FromIterator<K> for DupTreeSet<K>
where
K: Ord,
{
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = K>,
{
let map = iter.into_iter().map(|k| (k, ())).collect();
DupTreeSet { map }
}
}
}
|