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
//! Implements a Memoized Iterator, which pairs a normal `Iterator<Item=T>` with
//!     an owned `Vec<T>` to store the items it returns. Alternatively, it may
//!     be considered as a wrapped `Vec<T>` that is lazily populated with items
//!     from the Iterator.
//!
//! This is useful for infinite Iterators where each value depends on the last,
//!     such as the factorial function: Calculating the factorial of 1000 is
//!     quite expensive, but it also includes, as byproducts, the factorials of
//!     999, 998, and so on. If these are stored, they can be retrieved later,
//!     without needing to be recalculated for their own sake.

use std::{
    collections::Bound,
    ops::RangeBounds,
    slice::SliceIndex,
};


/// A Memoized Iterator. Wraps an Iterator, associating it with a Vector to
///     store its returns. Past returns can then be retrieved by index.
///
/// # Examples
///
/// The following example shows a `MemoIter` being used to cache the results of
///     calculating the Fibonacci Sequence.
///
/// ```
/// use memoiter::MemoIter;
/// use std::iter::successors;
///
/// let mut fibonacci: MemoIter<_, u32> = successors(
///     Some((0, 1)),
///     |&(a, b)| Some((b, b + a)),
/// ).map(|p| p.0).into();
///
/// assert_eq!(fibonacci.get(0), Some(&0));
/// assert_eq!(fibonacci.get(1), Some(&1));
/// assert_eq!(fibonacci.get(4), Some(&3));
/// assert_eq!(fibonacci.get(9), Some(&34));
///
/// //  This value was calculated as a byproduct of calculating 4, and is simply
/// //      retrieved here.
/// assert_eq!(fibonacci.get(3), Some(&2));
///
/// let (seq, _) = fibonacci.consume();
/// assert_eq!(seq, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]);
/// ```
#[derive(Debug)]
pub struct MemoIter<I, T> where
    I: Iterator<Item=T>,
{
    exhausted: bool,
    iterator: I,
    sequence: Vec<T>,
}


impl<I, T> MemoIter<I, T> where
    I: Iterator<Item=T>,
{
    /// Create an empty `MemoIter` wrapping a given Iterator.
    pub fn new(iterator: I) -> Self {
        Self {
            exhausted: false,
            iterator,
            sequence: Vec::new(),
        }
    }

    /// Create an empty `MemoIter`, but with a specified capacity, wrapping a
    ///     given Iterator. This only affects the initial capacity, and does
    ///     **not** restrict the size of the internal vector.
    pub fn with_capacity(capacity: usize, iterator: I) -> Self {
        Self {
            exhausted: false,
            iterator,
            sequence: Vec::with_capacity(capacity),
        }
    }

    /// Create a `MemoIter` wrapping a given Iterator, using a provided Vector
    ///     for its storage.
    pub fn with_vec(iterator: I, sequence: Vec<T>) -> Self {
        Self {
            exhausted: false,
            iterator,
            sequence,
        }
    }

    /// Return the number of items evaluated. This value will be one more than
    ///     the highest index available via `MemoIter::recall()`.
    #[inline]
    pub fn evaluated(&self) -> usize {
        self.sequence.len()
    }

    fn expand_to_contain(&mut self, idx: usize) {
        if !self.exhausted {
            let len: usize = self.sequence.len();

            if idx >= len {
                self.sequence.reserve(idx - len + 1);

                for _i in len..=idx {
                    #[cfg(test)] println!("+ {}", _i);

                    match self.iterator.next() {
                        Some(next) => self.sequence.push(next),
                        None => {
                            self.exhausted = true;
                            self.sequence.shrink_to_fit();
                            return;
                        }
                    }
                }
            }
        }
    }

    /// Retrieve, by its index, a value returned by the Iterator. If the value
    ///     at the index given has not yet been evaluated, it will be. Returns
    ///     `None` if the internal Iterator terminates before reaching the given
    ///     index.
    pub fn get(&mut self, idx: usize) -> Option<&T> {
        #[cfg(test)] println!("get({}):", idx);
        self.expand_to_contain(idx);
        self.sequence.get(idx)
    }

    /// Retrieve a slice of values returned by the Iterator. If the values in
    ///     the range in question have not yet been evaluated, they will be.
    ///
    /// Retrieving a slice whose end bound has not yet been evaluated will cause
    ///     all values up to that point to be evaluated. Because an Iterator may
    ///     be infinite, an *unbounded* slice will, out of caution, **not** do
    ///     any evaluations, instead being limited to the existing indices in
    ///     the stored sequence. A convenient side effect of this is that a full
    ///     range -- that is, `memiter.get_slice(..)` -- will return a full
    ///     slice of the *stored* sequence, doing no new evaluations.
    ///
    /// However, because the final index may not be knowable, this method also
    ///     includes a check to ensure that it will not panic if given a range
    ///     with indices outside the final sequence, instead returning an empty
    ///     slice.
    pub fn get_slice<R>(&mut self, range: R) -> &[T] where
        R: RangeBounds<usize> + SliceIndex<[T], Output=[T]>,
    {
        let first: usize = match range.start_bound() {
            Bound::Unbounded => 0,
            Bound::Included(&i) => i,
            Bound::Excluded(&i) => i + 1,
        };

        match range.end_bound() {
            Bound::Unbounded => {
                let end: usize = self.sequence.len();

                &self.sequence[first.min(end)..end]
            }
            Bound::Included(&i) => {
                self.expand_to_contain(i);
                let last: usize = self.sequence.len().saturating_sub(1).min(i);

                if first <= last {
                    &self.sequence[first..=last]
                } else {
                    //  NOTE: Edge case. Prevents `&[0, 1, 2, 3][10..=20]` from
                    //      being evaluated as `&[3]`.
                    &[]
                }
            }
            Bound::Excluded(&i) => {
                self.expand_to_contain(i.saturating_sub(1));
                let end: usize = self.sequence.len().min(i);

                &self.sequence[first.min(end)..end]
            }
        }
    }

    /// Return `true` if the internal Iterator has been exhausted and is done
    ///     returning new values.
    #[inline]
    pub fn is_exhausted(&self) -> bool {
        self.exhausted
    }

    /// Retrieve, by its index, a value returned by the Iterator. If the value
    ///     at the index given has not yet been evaluated, it will **NOT** be
    ///     evaluated now, and this method will return `None`.
    pub fn recall(&mut self, idx: usize) -> Option<&T> {
        #[cfg(test)] println!("recall({})", idx);
        self.sequence.get(idx)
    }

    /// Consume self, returning a Tuple containing the internal stored `Vec<T>`
    ///     and the original Iterator.
    pub fn consume(self) -> (Vec<T>, I) {
        let Self { sequence, iterator, .. } = self;
        (sequence, iterator)
    }
}


impl<I, T> ExactSizeIterator for MemoIter<I, T> where
    I: ExactSizeIterator + Iterator<Item=T>,
    T: Copy,
{
    #[inline]
    fn len(&self) -> usize {
        self.sequence.len() + self.iterator.len()
    }

    // #[cfg(exact_size_is_empty)]
    // fn is_empty(&self) -> bool {
    //     self.iterator.is_empty()
    // }
}


impl<F, I, T> From<F> for MemoIter<I, T> where
    F: IntoIterator<Item=T, IntoIter=I>,
    I: Iterator<Item=T>,
{
    fn from(into: F) -> Self {
        Self::new(into.into_iter())
    }
}


impl<I, T> Iterator for MemoIter<I, T> where
    I: Iterator<Item=T>,
    T: Copy,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.exhausted {
            match self.iterator.next() {
                Some(next) => {
                    self.sequence.push(next);
                    Some(next)
                }
                None => {
                    self.exhausted = true;
                    self.sequence.shrink_to_fit();
                    None
                }
            }
        } else { None }
    }
}


#[cfg(test)]
mod tests {
    use std::iter::successors;
    use super::*;

    #[test]
    fn test_factorial() {
        //  Instantiate a `MemoIter` that calculates factorials.
        let mut factorial: MemoIter<_, u32> = successors(
            Some((0, 1)),
            |&(idx0, acc)| {
                let idx1: u32 = idx0 + 1;
                Some((idx1, idx1 * acc))
            },
        ).map(|p| p.1).into();

        //  Ensure that it starts empty.
        assert_eq!(factorial.sequence, [], "MemoIter does not start empty.");
        assert_eq!(factorial.recall(3), None);

        //  Ensure that its specific values are calculated correctly.
        assert_eq!(factorial.get(0), Some(&1)); // 0!
        assert_eq!(factorial.get(1), Some(&1)); // 1!
        assert_eq!(factorial.get(4), Some(&24)); // 4!
        assert_eq!(factorial.get(6), Some(&720)); // 6!
        assert_eq!(factorial.get(4), Some(&24)); // 4!
        assert_eq!(factorial.get(2), Some(&2)); // 2!
        assert_eq!(factorial.get(0), Some(&1)); // 0!

        assert_eq!(factorial.recall(3), Some(&6));
        println!("{:?}", &factorial);

        assert_eq!(factorial.get_slice(..), [1, 1, 2, 6, 24, 120, 720]);
        assert_eq!(factorial.get_slice(0..4), [1, 1, 2, 6]);
        assert_eq!(factorial.get_slice(..=7), [1, 1, 2, 6, 24, 120, 720, 5040]);
        assert_eq!(factorial.get_slice(5..8), [120, 720, 5040]);

        //  Ensure that it maintains its returns, in order.
        let (seq, _) = factorial.consume();
        assert_eq!(
            seq, [1, 1, 2, 6, 24, 120, 720, 5040],
            "MemoIter does not correctly store its past values.",
        );
    }

    #[test]
    fn test_fibonacci() {
        let mut fibonacci: MemoIter<_, u32> = successors(
            Some((0, 1)),
            |&(a, b)| Some((b, b + a)),
        ).map(|p| p.0).into();

        assert_eq!(fibonacci.get(0), Some(&0));
        assert_eq!(fibonacci.get(1), Some(&1));
        assert_eq!(fibonacci.get(4), Some(&3));
        assert_eq!(fibonacci.get(9), Some(&34));

        println!("{:?}", &fibonacci);

        let (seq, _) = fibonacci.consume();
        assert_eq!(seq, [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]);
    }

    #[test]
    fn test_len() {
        let mut five = MemoIter::new(0..5);

        assert!(!five.is_exhausted());
        assert_eq!(five.evaluated(), 0);
        assert_eq!(five.len(), 5);
        assert_eq!(five.get(3), Some(&3));

        assert!(!five.is_exhausted());
        assert_eq!(five.evaluated(), 4);
        assert_eq!(five.len(), 5);
        assert_eq!(five.get(7), None);

        assert!(five.is_exhausted());
        assert_eq!(five.evaluated(), 5);
        assert_eq!(five.len(), 5);
    }

    #[test]
    fn test_prevec() {
        let mut five = MemoIter::with_vec(1..5, vec![0]);

        assert!(!five.is_exhausted());
        assert_eq!(five.len(), 5);
        assert_eq!(five.evaluated(), 1);
        assert_eq!(five.recall(0), Some(&0));
        assert_eq!(five.recall(1), None);
        assert_eq!(five.get_slice(..), [0]);

        assert_eq!(five.get(10), None);

        assert!(five.is_exhausted());
        assert_eq!(five.len(), 5);
        assert_eq!(five.evaluated(), 5);
        assert_eq!(five.recall(0), Some(&0));
        assert_eq!(five.recall(1), Some(&1));
        assert_eq!(five.get_slice(..), [0, 1, 2, 3, 4]);
    }

    #[test]
    fn test_slice() {
        let mut five = MemoIter::new(0..5);

        assert!(!five.is_exhausted());
        assert_eq!(five.evaluated(), 0);

        assert_eq!(five.get_slice(..), []);
        assert_eq!(five.get_slice(..0), []);
        assert_eq!(five.get_slice(..=0), [0]);
        assert_eq!(five.get_slice(0..1), [0]);
        assert_eq!(five.get_slice(0..), [0]);
        assert_eq!(five.get_slice(..), [0]);

        assert!(!five.is_exhausted());
        assert_eq!(five.evaluated(), 1);

        assert_eq!(five.get_slice(10..20), []);
        assert_eq!(five.get_slice(4..=20), [4]);
        assert_eq!(five.get_slice(10..=20), []);
        assert_eq!(five.get_slice(..20), [0, 1, 2, 3, 4]);
        assert_eq!(five.get_slice(..=9), [0, 1, 2, 3, 4]);
        assert_eq!(five.get_slice(10..), []);
        assert_eq!(five.get_slice(..), [0, 1, 2, 3, 4]);

        assert_eq!(five.get_slice(..=usize::MAX), [0, 1, 2, 3, 4]);
        assert_eq!(five.get_slice(50..40), []);

        assert!(five.is_exhausted());
        assert_eq!(five.evaluated(), 5);
        assert_eq!(five.len(), 5);
    }
}