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
395
396
397
398
399
400
use core::cmp::Ordering;

use crate::set::SgSet;
use crate::tree::{Idx, IntoIter as TreeIntoIter, Iter as TreeIter};

use smallnum::SmallUnsigned;
use tinyvec::{ArrayVec, ArrayVecIterator};

// General Iterators ---------------------------------------------------------------------------------------------------

/// An iterator over the items of a [`SgSet`][crate::set::SgSet].
///
/// This `struct` is created by the [`iter`][crate::set::SgSet::iter] method on [`SgSet`][crate::set::SgSet].
/// See its documentation for more.
pub struct Iter<'a, T: Ord + Default, const N: usize> {
    ref_iter: TreeIter<'a, T, (), N>,
}

impl<'a, T: Ord + Default, const N: usize> Iter<'a, T, N> {
    /// Construct reference iterator.
    pub(crate) fn new(set: &'a SgSet<T, N>) -> Self {
        Iter {
            ref_iter: TreeIter::new(&set.bst),
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> Iterator for Iter<'a, T, N> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.ref_iter.next().map(|(k, _)| k)
    }
}

impl<'a, T: Ord + Default, const N: usize> ExactSizeIterator for Iter<'a, T, N> {
    fn len(&self) -> usize {
        self.ref_iter.len()
    }
}

/// An owning iterator over the items of a [`SgSet`][crate::set::SgSet].
///
/// This `struct` is created by the [`into_iter`][crate::set::SgSet::into_iter] method on [`SgSet`][crate::set::SgSet]
/// (provided by the IntoIterator trait). See its documentation for more.
pub struct IntoIter<T: Ord + Default, const N: usize> {
    cons_iter: TreeIntoIter<T, (), N>,
}

impl<T: Ord + Default, const N: usize> IntoIter<T, N> {
    /// Construct owning iterator.
    pub(crate) fn new(set: SgSet<T, N>) -> Self {
        IntoIter {
            cons_iter: TreeIntoIter::new(set.bst),
        }
    }
}

impl<T: Ord + Default, const N: usize> Iterator for IntoIter<T, N> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.cons_iter.next().map(|(k, _)| k)
    }
}

impl<T: Ord + Default, const N: usize> ExactSizeIterator for IntoIter<T, N> {
    fn len(&self) -> usize {
        self.cons_iter.len()
    }
}

/*
Workaround Note:

The remaining iterators in this file only store indexes into the input set(s) iterator(s) and have to
recover set elements with `set.iter().nth(idx)`. Rather inefficient, solves a blocking problem:
in `ArrayVecIterator<[&'a T; N]>` `Default` is not implemented for `&'a T`.

TODO: faster solution?
*/

// TODO: without `feature(generic_const_exprs)`, `Union` and `SymmetricDifference` cannot compute `2 * N` length
// iterator to support disjoint sets. This is a temporary workaround, documented in external API docs.
const PLACEHOLDER_2N: usize = 4096;

// Intersection Iterator -----------------------------------------------------------------------------------------------

// TODO: these need more trait implementations for full compatibility
// TODO: make this a lazy iterator like `std::collections::btree_set::Intersection`

/// An iterator producing elements in the intersection of [`SgSet`][crate::set::SgSet]s.
///
/// This `struct` is created by the [`intersection`][crate::set::SgSet::difference] method on [`SgSet`][crate::set::SgSet].
/// See its documentation for more.
pub struct Intersection<'a, T: Ord + Default, const N: usize> {
    pub(crate) inner: ArrayVecIterator<[Idx; N]>,
    set_this: &'a SgSet<T, N>,
    total_cnt: usize,
    spent_cnt: usize,
}

impl<'a, T: Ord + Default, const N: usize> Intersection<'a, T, N> {
    /// Construct `Intersection` iterator.
    /// Values that are both in `this` and `other`.
    pub(crate) fn new(this: &'a SgSet<T, N>, other: &SgSet<T, N>) -> Self {
        let mut self_enum_iter = this.iter().enumerate();
        let mut other_enum_iter = other.iter().enumerate();

        let mut opt_self = self_enum_iter.next();
        let mut opt_other = other_enum_iter.next();

        let mut inter = ArrayVec::default();
        let mut len = 0;

        // If either is shorter, short-circuit.
        while let (Some((self_idx, self_val)), Some((_, other_val))) = (opt_self, opt_other) {
            match self_val.cmp(other_val) {
                Ordering::Less => {
                    opt_self = self_enum_iter.next();
                }
                Ordering::Equal => {
                    inter.push(Idx::checked_from(self_idx));
                    len += 1;
                    opt_self = self_enum_iter.next();
                    opt_other = other_enum_iter.next();
                }
                Ordering::Greater => {
                    opt_other = other_enum_iter.next();
                }
            }
        }

        Intersection {
            inner: inter.into_iter(),
            set_this: this,
            total_cnt: len,
            spent_cnt: 0,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> Iterator for Intersection<'a, T, N> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        match self.inner.next() {
            Some(idx) => match self.set_this.iter().nth(idx.usize()) {
                Some(item) => {
                    self.spent_cnt += 1;
                    Some(item)
                }
                None => None,
            },
            None => None,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> ExactSizeIterator for Intersection<'a, T, N> {
    fn len(&self) -> usize {
        debug_assert!(self.spent_cnt <= self.total_cnt);
        self.total_cnt - self.spent_cnt
    }
}

// Difference Iterator -------------------------------------------------------------------------------------------------

// TODO: these need more trait implementations for full compatibility
// TODO: make this a lazy iterator like `std::collections::btree_set::Difference`

/// An iterator producing elements in the difference of [`SgSet`][crate::set::SgSet]s.
///
/// This `struct` is created by the [`difference`][crate::set::SgSet::difference] method
/// on [`SgSet`][crate::set::SgSet]. See its documentation for more.
pub struct Difference<'a, T: Ord + Default, const N: usize> {
    pub(crate) inner: ArrayVecIterator<[Idx; N]>,
    set_this: &'a SgSet<T, N>,
    total_cnt: usize,
    spent_cnt: usize,
}

impl<'a, T: Ord + Default, const N: usize> Difference<'a, T, N> {
    /// Construct `Difference` iterator.
    /// Values that are in `this` but not in `other`.
    pub(crate) fn new(this: &'a SgSet<T, N>, other: &SgSet<T, N>) -> Self {
        let mut diff = ArrayVec::default();
        let mut len = 0;

        for (idx, val) in this.iter().enumerate() {
            if !other.contains(val) {
                diff.push(Idx::checked_from(idx));
                len += 1;
            }
        }

        Difference {
            inner: diff.into_iter(),
            set_this: this,
            total_cnt: len,
            spent_cnt: 0,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> Iterator for Difference<'a, T, N> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        match self.inner.next() {
            Some(idx) => match self.set_this.iter().nth(idx.usize()) {
                Some(item) => {
                    self.spent_cnt += 1;
                    Some(item)
                }
                None => None,
            },
            None => None,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> ExactSizeIterator for Difference<'a, T, N> {
    fn len(&self) -> usize {
        debug_assert!(self.spent_cnt <= self.total_cnt);
        self.total_cnt - self.spent_cnt
    }
}

// Symmetric Difference Iterator ---------------------------------------------------------------------------------------

// TODO: these need more trait implementations for full compatibility
// TODO: make this a lazy iterator like `std::collections::btree_set::Difference`

/// An iterator producing elements in the symmetric difference of [`SgSet`][crate::set::SgSet]s.
///
/// This `struct` is created by the [`symmetric_difference`][crate::set::SgSet::symmetric_difference]
/// method on [`SgSet`][crate::set::SgSet]. See its documentation for more.
pub struct SymmetricDifference<'a, T: Ord + Default, const N: usize> {
    pub(crate) inner: ArrayVecIterator<[(Idx, bool); PLACEHOLDER_2N]>, // TODO: placeholder
    set_this: &'a SgSet<T, N>,
    set_other: &'a SgSet<T, N>,
    total_cnt: usize,
    spent_cnt: usize,
}

impl<'a, T: Ord + Default, const N: usize> SymmetricDifference<'a, T, N> {
    /// Construct `SymmetricDifference` iterator.
    /// Values that are in `this` or in `other` but not in both.
    pub(crate) fn new(this: &'a SgSet<T, N>, other: &'a SgSet<T, N>) -> Self {
        let mut sym_diff = ArrayVec::default();
        let mut len = 0;

        for (idx, val) in this.iter().enumerate() {
            if !other.contains(val) {
                sym_diff.push((Idx::checked_from(idx), true));
                len += 1;
            }
        }

        for (idx, val) in other.iter().enumerate() {
            if !this.contains(val) {
                sym_diff.push((Idx::checked_from(idx), false));
                len += 1;
            }
        }

        // Ascending order
        sym_diff.sort_unstable_by_key(|(idx, in_this): &(Idx, bool)| match in_this {
            true => this.iter().nth(idx.usize()),
            false => other.iter().nth(idx.usize()),
        });

        SymmetricDifference {
            inner: sym_diff.into_iter(),
            set_this: this,
            set_other: other,
            total_cnt: len,
            spent_cnt: 0,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> Iterator for SymmetricDifference<'a, T, N> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        match self.inner.next() {
            Some((idx, in_this)) => match in_this {
                true => match self.set_this.iter().nth(idx.usize()) {
                    Some(item) => {
                        self.spent_cnt += 1;
                        Some(item)
                    }
                    None => None,
                },
                false => match self.set_other.iter().nth(idx.usize()) {
                    Some(item) => {
                        self.spent_cnt += 1;
                        Some(item)
                    }
                    None => None,
                },
            },
            None => None,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> ExactSizeIterator for SymmetricDifference<'a, T, N> {
    fn len(&self) -> usize {
        debug_assert!(self.spent_cnt <= self.total_cnt);
        self.total_cnt - self.spent_cnt
    }
}

// Union Iterator ------------------------------------------------------------------------------------------------------

// TODO: these need more trait implementations for full compatibility
// TODO: make this a lazy iterator like `std::collections::btree_set::Union`

/// An iterator producing elements in the union of [`SgSet`][crate::set::SgSet]s.
///
/// This `struct` is created by the [`union`][crate::set::SgSet::difference] method on [`SgSet`][crate::set::SgSet].
/// See its documentation for more.
pub struct Union<'a, T: Ord + Default, const N: usize> {
    pub(crate) inner: ArrayVecIterator<[(Idx, bool); PLACEHOLDER_2N]>,
    set_this: &'a SgSet<T, N>,
    set_other: &'a SgSet<T, N>,
    total_cnt: usize,
    spent_cnt: usize,
}

impl<'a, T: Ord + Default, const N: usize> Union<'a, T, N> {
    /// Construct `Union` iterator.
    /// Values in `this` or `other`, without duplicates.
    pub(crate) fn new(this: &'a SgSet<T, N>, other: &'a SgSet<T, N>) -> Self {
        let mut uni = ArrayVec::default();
        let mut len = 0;

        for (idx, _) in this.iter().enumerate() {
            uni.push((Idx::checked_from(idx), true));
            len += 1;
        }

        for (idx, val) in other.iter().enumerate() {
            if !this.contains(val) {
                uni.push((Idx::checked_from(idx), false));
                len += 1;
            }
        }

        // Ascending order
        uni.sort_unstable_by_key(|(idx, in_this): &(Idx, bool)| match in_this {
            true => this.iter().nth(idx.usize()),
            false => other.iter().nth(idx.usize()),
        });

        Union {
            inner: uni.into_iter(),
            set_this: this,
            set_other: other,
            total_cnt: len,
            spent_cnt: 0,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> Iterator for Union<'a, T, N> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        match self.inner.next() {
            Some((idx, in_this)) => match in_this {
                true => match self.set_this.iter().nth(idx.usize()) {
                    Some(item) => {
                        self.spent_cnt += 1;
                        Some(item)
                    }
                    None => None,
                },
                false => match self.set_other.iter().nth(idx.usize()) {
                    Some(item) => {
                        self.spent_cnt += 1;
                        Some(item)
                    }
                    None => None,
                },
            },
            None => None,
        }
    }
}

impl<'a, T: Ord + Default, const N: usize> ExactSizeIterator for Union<'a, T, N> {
    fn len(&self) -> usize {
        debug_assert!(self.spent_cnt <= self.total_cnt);
        self.total_cnt - self.spent_cnt
    }
}