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
//! If part of the iterators outputs are disjoint increasing integer intervals,
//! then the iterators can be zipped together and the iteration can proceeds at
//! the granularity of the common refinements of all the integer intervals.

use crate::{
    interval::{traits::Interval, IntInterval},
    set::traits::Intersect,
};
use num::{Integer, Num, ToPrimitive};
use std::{
    collections::BTreeSet,
    marker::{PhantomData, Sized},
};

pub trait CommonRefinementZip<B, X, P, V>
where
    B: Copy + Num + Ord,
    Self: Iterator<Item = X> + Sized,
    P: Clone + Interval<B> + for<'b> Intersect<&'b P, Option<P>>, {
    fn get_interval_value_extractor(&self) -> Box<dyn Fn(<Self as Iterator>::Item) -> (P, V)>;

    fn common_refinement_zip(
        mut self,
        mut other: Self,
    ) -> CommonRefinementZipped<B, Self, X, P, V> {
        let extractor = self.get_interval_value_extractor();
        let mut intervals = Vec::new();
        let mut values = Vec::new();
        match self.next() {
            None => {
                intervals.push(None);
                values.push(None);
            }
            Some(x) => {
                let (interval, value) = extractor(x);
                intervals.push(Some(interval));
                values.push(Some(value));
            }
        }
        match other.next() {
            None => {
                intervals.push(None);
                values.push(None);
            }
            Some(x) => {
                let (interval, value) = extractor(x);
                intervals.push(Some(interval));
                values.push(Some(value));
            }
        }
        CommonRefinementZipped {
            iters: vec![self, other],
            intervals,
            values,
            extractor,
            phantom: PhantomData,
        }
    }
}

/// # Example
/// ```
/// use math::{
///     interval::{traits::Interval, IntInterval},
///     iter::CommonRefinementZip,
/// };
/// use std::collections::BTreeMap;
///
/// let m1: BTreeMap<IntInterval<usize>, i32> =
///     vec![(IntInterval::new(0, 5), 5), (IntInterval::new(8, 10), 2)]
///         .into_iter()
///         .collect();
///
/// let m2: BTreeMap<IntInterval<usize>, i32> =
///     vec![(IntInterval::new(2, 4), 8), (IntInterval::new(12, 13), 9)]
///         .into_iter()
///         .collect();
///
/// let mut iter = m1.iter().common_refinement_zip(m2.iter());
/// assert_eq!(
///     Some((IntInterval::new(0, 1), vec![Some(5), None])),
///     iter.next()
/// );
/// assert_eq!(
///     Some((IntInterval::new(2, 4), vec![Some(5), Some(8)])),
///     iter.next()
/// );
/// assert_eq!(
///     Some((IntInterval::new(5, 5), vec![Some(5), None])),
///     iter.next()
/// );
/// assert_eq!(
///     Some((IntInterval::new(8, 10), vec![Some(2), None])),
///     iter.next()
/// );
/// assert_eq!(
///     Some((IntInterval::new(12, 13), vec![None, Some(9)])),
///     iter.next()
/// );
/// assert_eq!(None, iter.next());
/// ```
impl<'a, V, B: Integer + Copy + ToPrimitive>
    CommonRefinementZip<B, (&'a IntInterval<B>, &'a V), IntInterval<B>, V>
    for std::collections::btree_map::Iter<'a, IntInterval<B>, V>
where
    B: 'a,
    V: 'a + Clone,
{
    fn get_interval_value_extractor(
        &self,
    ) -> Box<dyn Fn(<Self as Iterator>::Item) -> (IntInterval<B>, V)> {
        Box::new(|item| ((*item.0).clone(), (*item.1).clone()))
    }
}

impl<'a, V, B: Integer + Copy + ToPrimitive>
    CommonRefinementZip<B, (IntInterval<B>, V), IntInterval<B>, V>
    for std::collections::btree_map::IntoIter<IntInterval<B>, V>
where
    B: 'a,
{
    fn get_interval_value_extractor(
        &self,
    ) -> Box<dyn Fn(<Self as Iterator>::Item) -> (IntInterval<B>, V)> {
        Box::new(|item| (item.0, item.1))
    }
}

/// # Iterator Algorithm Description
/// Given a list of iterators, a list of the current minimum interval for each iterator will be
/// maintained together with their associated values. Then, at each pass the smallest minimum common
/// refinement of the current intervals is subtracted from each interval. A list of values will be
/// returned along with the common refinement. Each value will be the value associated with the
/// iterated interval if the common refinement has a non-empty intersection with the corresponding
/// interval, and `None` otherwise.
///
/// If an interval becomes empty after the subtraction, the corresponding iterator will be called
/// to replace the interval with the next interval, together with the associated values.
///
/// # Fields
/// * `iters`: the list of zipped iterators.
/// * `intervals`: the intervals assocaited with each iterator for the current pass.
/// * `values`: the values associated with each iterator for the current pass.
/// * `extractor`: a function that extracts a tuple of (interval, value) from each of the items
///   yielded from the iterators.
pub struct CommonRefinementZipped<B, I, X, P, V>
where
    B: Copy + Num + Ord,
    I: Iterator<Item = X> + Sized,
    P: Clone + Interval<B> + for<'b> Intersect<&'b P, Option<P>>, {
    iters: Vec<I>,
    intervals: Vec<Option<P>>,
    values: Vec<Option<V>>,
    extractor: Box<dyn Fn(X) -> (P, V)>,
    phantom: PhantomData<B>,
}

impl<B, I, X, P, V> Iterator for CommonRefinementZipped<B, I, X, P, V>
where
    B: Copy + Num + Ord,
    I: Iterator<Item = X> + Sized,
    P: Clone + Interval<B> + for<'b> Intersect<&'b P, Option<P>>,
    V: Clone,
{
    type Item = (P, Vec<Option<V>>);

    fn next(&mut self) -> Option<Self::Item> {
        let starts: BTreeSet<B> = self
            .intervals
            .iter()
            .filter_map(|i| i.clone().and_then(|i| Some(i.get_start())))
            .collect();

        let ends: BTreeSet<B> = self
            .intervals
            .iter()
            .filter_map(|i| i.clone().and_then(|i| Some(i.get_end())))
            .collect();

        let mut starts_iter = starts.iter();
        let min_start = match starts_iter.next() {
            // if all intervals are empty, it means that all the iterators have been exhausted
            None => return None,
            Some(&a) => a,
        };
        let second_min_start = starts_iter.next();
        let min_end = *ends.iter().next().unwrap();

        let min_refinement = match second_min_start {
            Some(&second_min_start) => {
                if second_min_start <= min_end {
                    P::from_boundaries(min_start, second_min_start - B::one())
                } else {
                    P::from_boundaries(min_start, min_end)
                }
            }
            None => P::from_boundaries(min_start, min_end),
        };

        let mut refinement_values = Vec::new();
        for ((interval, iter), v) in self
            .intervals
            .iter_mut()
            .zip(self.iters.iter_mut())
            .zip(self.values.iter_mut())
        {
            match interval {
                Some(i) => {
                    if i.has_non_empty_intersection_with(&min_refinement) {
                        refinement_values.push((*v).clone());

                        // subtract the min_refinement from the interval
                        // min_start <= i.get_start() <= min_end <= i.get_end()
                        let remainder =
                            P::from_boundaries(min_refinement.get_end() + B::one(), i.get_end());
                        if remainder.is_empty() {
                            match iter.next() {
                                None => {
                                    *interval = None;
                                    *v = None;
                                }
                                Some(x) => {
                                    let (new_interval, new_val) = (self.extractor)(x);
                                    *interval = Some(new_interval);
                                    *v = Some(new_val);
                                }
                            }
                        } else {
                            *interval = Some(remainder);
                        }
                    } else {
                        refinement_values.push(None);
                    }
                }
                None => {
                    refinement_values.push(None);
                }
            }
        }
        Some((min_refinement, refinement_values))
    }
}

impl<B, I, X, P, V> CommonRefinementZipped<B, I, X, P, V>
where
    B: Copy + Num + Ord,
    I: Iterator<Item = X> + Sized,
    P: Clone + Interval<B> + for<'b> Intersect<&'b P, Option<P>>,
{
    /// ```
    /// use math::{
    ///     interval::{traits::Interval, IntInterval},
    ///     iter::CommonRefinementZip,
    /// };
    /// use std::collections::BTreeMap;
    ///
    /// let m1: BTreeMap<IntInterval<usize>, i32> =
    ///     vec![(IntInterval::new(0, 10), 5), (IntInterval::new(16, 17), 21)]
    ///         .into_iter()
    ///         .collect();
    ///
    /// let m2: BTreeMap<IntInterval<usize>, i32> =
    ///     vec![(IntInterval::new(2, 3), 8), (IntInterval::new(12, 20), 9)]
    ///         .into_iter()
    ///         .collect();
    ///
    /// let m3: BTreeMap<IntInterval<usize>, i32> = vec![
    ///     (IntInterval::new(2, 4), 7),
    ///     (IntInterval::new(9, 10), -1),
    ///     (IntInterval::new(15, 20), 0),
    /// ]
    /// .into_iter()
    /// .collect();
    ///
    /// let mut iter = m1
    ///     .iter()
    ///     .common_refinement_zip(m2.iter())
    ///     .common_refinement_flat_zip(m3.iter());
    ///
    /// assert_eq!(
    ///     Some((IntInterval::new(0, 1), vec![Some(5), None, None])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(2, 3), vec![Some(5), Some(8), Some(7)])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(4, 4), vec![Some(5), None, Some(7)])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(5, 8), vec![Some(5), None, None])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(9, 10), vec![Some(5), None, Some(-1)])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(12, 14), vec![None, Some(9), None])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(15, 15), vec![None, Some(9), Some(0)])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(16, 17), vec![Some(21), Some(9), Some(0)])),
    ///     iter.next()
    /// );
    /// assert_eq!(
    ///     Some((IntInterval::new(18, 20), vec![None, Some(9), Some(0)])),
    ///     iter.next()
    /// );
    /// assert_eq!(None, iter.next());
    /// ```
    pub fn common_refinement_flat_zip(
        mut self,
        mut other: I,
    ) -> CommonRefinementZipped<B, I, X, P, V>
    where
        I: Iterator<Item = X> + Sized, {
        match other.next() {
            None => {
                self.intervals.push(None);
                self.values.push(None);
            }
            Some(x) => {
                let (i, v) = (self.extractor)(x);
                self.intervals.push(Some(i.clone()));
                self.values.push(Some(v));
            }
        }
        self.iters.push(other);
        CommonRefinementZipped {
            iters: self.iters,
            intervals: self.intervals,
            values: self.values,
            extractor: self.extractor,
            phantom: PhantomData,
        }
    }
}