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
pub trait Intersection<Rhs = Self> {
type Output;
fn intersection(self, that: Rhs) -> Self::Output;
}
pub trait Union<Rhs = Self> {
type Output;
fn union(self, that: Rhs) -> Self::Output;
}
pub trait Difference<Rhs = Self> {
type Output;
fn difference(self, that: Rhs) -> Self::Output;
}
pub trait SymmetricDifference<Rhs = Self> {
type Output;
fn symmetric_difference(self, that: Rhs) -> Self::Output;
}
impl<T> Intersection<Option<T>> for Option<T>
where
T: Intersection<T, Output = T>,
{
type Output = Option<T>;
fn intersection(self, that: Option<T>) -> Self::Output {
match (self, that) {
(Some(t1), Some(t2)) => Some(t1.intersection(t2)),
_ => None,
}
}
}
impl<T> Union<Option<T>> for Option<T>
where
T: Union<T, Output = T>,
{
type Output = Option<T>;
fn union(self, that: Option<T>) -> Self::Output {
match (self, that) {
(Some(t1), Some(t2)) => Some(t1.union(t2)),
(Some(t1), None) => Some(t1),
(None, Some(t2)) => Some(t2),
(None, None) => None,
}
}
}
impl<'a, T> Difference<Option<T>> for Option<T>
where
T: Difference<T, Output = T>,
{
type Output = Option<T>;
fn difference(self, that: Option<T>) -> Self::Output {
match (self, that) {
(Some(t1), Some(t2)) => Some(t1.difference(t2)),
(Some(t1), None) => Some(t1),
_ => None,
}
}
}
impl<'a, T> SymmetricDifference<Option<T>> for Option<T>
where
T: SymmetricDifference<T, Output = T>,
{
type Output = Option<T>;
fn symmetric_difference(self, that: Option<T>) -> Self::Output {
match (self, that) {
(Some(t1), Some(t2)) => Some(t1.symmetric_difference(t2)),
(Some(t1), None) => Some(t1),
(None, Some(t2)) => Some(t2),
_ => None,
}
}
}
pub trait IntersectionWith<Rhs = Self> {
fn intersection_with(&mut self, that: Rhs);
}
pub trait UnionWith<Rhs = Self> {
fn union_with(&mut self, that: Rhs);
}
pub trait DifferenceWith<Rhs = Self> {
fn difference_with(&mut self, that: Rhs);
}
pub trait SymmetricDifferenceWith<Rhs = Self> {
fn symmetric_difference_with(&mut self, that: Rhs);
}
impl<T> ::std::ops::BitAndAssign<T> for IntersectionWith<T> {
fn bitand_assign(&mut self, that: T) {
self.intersection_with(that)
}
}
impl<T> ::std::ops::BitOrAssign<T> for UnionWith<T> {
fn bitor_assign(&mut self, that: T) {
self.union_with(that)
}
}
impl<T> ::std::ops::SubAssign<T> for DifferenceWith<T> {
fn sub_assign(&mut self, that: T) {
self.difference_with(that);
}
}
impl<T> ::std::ops::BitXorAssign<T> for SymmetricDifferenceWith<T> {
fn bitxor_assign(&mut self, that: T) {
self.symmetric_difference_with(that);
}
}
macro_rules! impl_pairwise {
( $( $type:ty ),* ) => ($(
impl IntersectionWith for $type {
fn intersection_with(&mut self, rhs: $type) {*self &= rhs;}
}
impl UnionWith for $type {
fn union_with(&mut self, rhs: $type) {*self |= rhs;}
}
impl DifferenceWith for $type {
fn difference_with(&mut self, rhs: $type) {*self &= !rhs;}
}
impl SymmetricDifferenceWith for $type {
fn symmetric_difference_with(&mut self, rhs: $type) {*self ^= rhs;}
}
)*)
}
impl_pairwise!(u8, u16, u32, u64, usize);
use std::iter::{Fuse, Peekable, ExactSizeIterator};
use std::cmp::{self, Ordering};
macro_rules! define_pair {
( $( ( $fn:ident, $op:ident ) ),* ) => ($(
pub(crate) struct $op<I1, I2, T>
where I1: Iterator<Item = T>,
I2: Iterator<Item = T>
{
lhs: Peekable<Fuse<I1>>,
rhs: Peekable<Fuse<I2>>,
}
pub(crate) fn $fn<I1, I2, T>(x: I1, y: I2) -> $op<I1, I2, T>
where I1: Iterator<Item = T> + ExactSizeIterator,
I2: Iterator<Item = T> + ExactSizeIterator,
T: Ord
{
$op {lhs: x.fuse().peekable(), rhs: y.fuse().peekable()}
}
)*);
}
define_pair!(
(intersection, IntersectionIter),
(union, UnionIter),
(difference, DifferenceIter),
(symmetric_difference, SymmetricDifferenceIter)
);
fn comparing<T: Ord>(
a: Option<T>,
b: Option<T>,
x: cmp::Ordering,
y: cmp::Ordering,
) -> cmp::Ordering {
match (a, b) {
(None, _) => x,
(_, None) => y,
(Some(ref a1), Some(ref b1)) => a1.cmp(b1),
}
}
impl<I1, I2, T> Iterator for IntersectionIter<I1, I2, T>
where
I1: Iterator<Item = T> + ExactSizeIterator,
I2: Iterator<Item = T> + ExactSizeIterator,
T: Ord,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
use self::Ordering::{Less, Equal, Greater};
loop {
let compared = {
let x = self.lhs.peek();
let y = self.rhs.peek();
x.and_then(|x1| y.map(|y1| x1.cmp(y1)))
};
match compared {
None => return None,
Some(Less) => {
self.lhs.next();
}
Some(Equal) => {
self.rhs.next();
return self.lhs.next();
}
Some(Greater) => {
self.rhs.next();
}
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(cmp::min(self.lhs.len(), self.rhs.len())))
}
}
impl<I1, I2, T> Iterator for UnionIter<I1, I2, T>
where
I1: Iterator<Item = T> + ExactSizeIterator,
I2: Iterator<Item = T> + ExactSizeIterator,
T: Ord,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
use self::Ordering::{Less, Equal, Greater};
match comparing(self.lhs.peek(), self.rhs.peek(), Greater, Less) {
Less => self.lhs.next(),
Equal => {
self.rhs.next();
self.lhs.next()
}
Greater => self.rhs.next(),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let x_len = self.lhs.len();
let y_len = self.rhs.len();
(cmp::max(x_len, y_len), Some(x_len + y_len))
}
}
impl<I1, I2, T> Iterator for DifferenceIter<I1, I2, T>
where
I1: Iterator<Item = T> + ExactSizeIterator,
I2: Iterator<Item = T> + ExactSizeIterator,
T: Ord,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
use self::Ordering::{Less, Equal, Greater};
loop {
let compaed = comparing(self.lhs.peek(), self.rhs.peek(), Less, Less);
match compaed {
Less => return self.lhs.next(),
Equal => {
self.lhs.next();
self.rhs.next();
}
Greater => {
self.rhs.next();
}
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let x_len = self.lhs.len();
let y_len = self.rhs.len();
(x_len.saturating_sub(y_len), Some(x_len))
}
}
impl<I1, I2, T> Iterator for SymmetricDifferenceIter<I1, I2, T>
where
I1: Iterator<Item = T> + ExactSizeIterator,
I2: Iterator<Item = T> + ExactSizeIterator,
T: Ord,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
use self::Ordering::{Less, Equal, Greater};
loop {
match comparing(self.lhs.peek(), self.rhs.peek(), Greater, Less) {
Less => return self.lhs.next(),
Equal => {
self.lhs.next();
self.rhs.next();
}
Greater => return self.rhs.next(),
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.lhs.len() + self.rhs.len()))
}
}