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
use std::{ u16 };
use std::slice;
use util::Either;
use util::Either::{ Left, Right };
use container::{ Container };
pub struct Iter<'a> {
inner_iter: Option<(u16, Box<Iterator<Item = u16> + 'a>)>,
container_iter: slice::Iter<'a, Container>,
}
#[inline]
fn calc(key: u16, value: u16) -> u32 {
((key as u32) << u16::BITS) + (value as u32)
}
#[inline]
fn next_iter<'a>(container_iter: &mut slice::Iter<'a, Container>) -> Option<(u16, Box<Iterator<Item = u16> + 'a>)> {
container_iter.next().map(|container| (container.key(), container.iter()))
}
#[inline]
pub fn new<'a>(mut container_iter: slice::Iter<'a, Container>) -> Iter<'a> {
Iter {
inner_iter: next_iter(&mut container_iter),
container_iter: container_iter
}
}
impl<'a> Iter<'a> {
#[inline]
fn choose_next(&mut self) -> Option<Either<u32, Option<(u16, Box<Iterator<Item = u16> + 'a>)>>> {
match self.inner_iter {
Some((key, ref mut iter)) => Some(match iter.next() {
Some(value) => Left(calc(key, value)),
None => Right(next_iter(&mut self.container_iter)),
}),
None => None,
}
}
}
impl<'a> Iterator for Iter<'a> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
match self.choose_next() {
None => None,
Some(Left(val)) => Some(val),
Some(Right(new_iter)) => {
self.inner_iter = new_iter;
self.next()
},
}
}
}
pub struct UnionIter<'a> {
current1: Option<u32>,
current2: Option<u32>,
iter1: Iter<'a>,
iter2: Iter<'a>,
}
pub mod union {
use super::{ Iter, UnionIter };
#[inline]
pub fn new<'a>(mut iter1: Iter<'a>, mut iter2: Iter<'a>) -> UnionIter<'a> {
UnionIter {
current1: iter1.next(),
current2: iter2.next(),
iter1: iter1,
iter2: iter2,
}
}
}
impl<'a> Iterator for UnionIter<'a> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
match (self.current1, self.current2) {
(None, None) => None,
(val, None) => { self.current1 = self.iter1.next(); val },
(None, val) => { self.current2 = self.iter2.next(); val },
(val1, val2) if val1 < val2 => { self.current1 = self.iter1.next(); val1 },
(val1, val2) if val1 > val2 => { self.current2 = self.iter2.next(); val2 },
(val1, val2) if val1 == val2 => {
self.current1 = self.iter1.next();
self.current2 = self.iter2.next();
val1
},
_ => panic!("Should not be possible to get here"),
}
}
}
pub struct IntersectionIter<'a> {
current1: Option<u32>,
current2: Option<u32>,
iter1: Iter<'a>,
iter2: Iter<'a>,
}
pub mod intersection {
use super::{ Iter, IntersectionIter };
#[inline]
pub fn new<'a>(mut iter1: Iter<'a>, mut iter2: Iter<'a>) -> IntersectionIter<'a> {
IntersectionIter {
current1: iter1.next(),
current2: iter2.next(),
iter1: iter1,
iter2: iter2,
}
}
}
impl<'a> Iterator for IntersectionIter<'a> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
match (self.current1, self.current2) {
(None, _) | (_, None) => None,
(val1, val2) if val1 < val2 => { self.current1 = self.iter1.next(); self.next() },
(val1, val2) if val1 > val2 => { self.current2 = self.iter2.next(); self.next() },
(val1, val2) if val1 == val2 => {
self.current1 = self.iter1.next();
self.current2 = self.iter2.next();
val1
},
_ => panic!("Should not be possible to get here"),
}
}
}
pub struct DifferenceIter<'a> {
current1: Option<u32>,
current2: Option<u32>,
iter1: Iter<'a>,
iter2: Iter<'a>,
}
pub mod difference {
use super::{ Iter, DifferenceIter };
#[inline]
pub fn new<'a>(mut iter1: Iter<'a>, mut iter2: Iter<'a>) -> DifferenceIter<'a> {
DifferenceIter {
current1: iter1.next(),
current2: iter2.next(),
iter1: iter1,
iter2: iter2,
}
}
}
impl<'a> Iterator for DifferenceIter<'a> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
loop {
match (self.current1, self.current2) {
(None, _) | (_, None) => return None,
(val1, val2) if val1 < val2 => { self.current1 = self.iter1.next(); return val1; },
(val1, val2) if val1 > val2 => self.current2 = self.iter2.next(),
(val1, val2) if val1 == val2 => {
self.current1 = self.iter1.next();
self.current2 = self.iter2.next();
},
_ => panic!("Should not be possible to get here"),
}
}
}
}
pub struct SymmetricDifferenceIter<'a> {
current1: Option<u32>,
current2: Option<u32>,
iter1: Iter<'a>,
iter2: Iter<'a>,
}
pub mod symmetric_difference {
use super::{ Iter, SymmetricDifferenceIter };
#[inline]
pub fn new<'a>(mut iter1: Iter<'a>, mut iter2: Iter<'a>) -> SymmetricDifferenceIter<'a> {
SymmetricDifferenceIter {
current1: iter1.next(),
current2: iter2.next(),
iter1: iter1,
iter2: iter2,
}
}
}
impl<'a> Iterator for SymmetricDifferenceIter<'a> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
match (self.current1, self.current2) {
(None, _) | (_, None) => None,
(val1, val2) if val1 < val2 => { self.current1 = self.iter1.next(); val1 },
(val1, val2) if val1 > val2 => { self.current2 = self.iter2.next(); val2 },
(val1, val2) if val1 == val2 => {
self.current1 = self.iter1.next();
self.current2 = self.iter2.next();
self.next()
},
_ => panic!("Should not be possible to get here"),
}
}
}