[][src]Struct memoiter::MemoIter

pub struct MemoIter<I, T> where
    I: Iterator<Item = T>, 
{ /* fields omitted */ }

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]);

Implementations

impl<I, T> MemoIter<I, T> where
    I: Iterator<Item = T>, 
[src]

pub fn new(iterator: I) -> Self[src]

Create an empty MemoIter wrapping a given Iterator.

pub fn with_capacity(capacity: usize, iterator: I) -> Self[src]

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_vec(iterator: I, sequence: Vec<T>) -> Self[src]

Create a MemoIter wrapping a given Iterator, using a provided Vector for its storage.

pub fn evaluated(&self) -> usize[src]

Return the number of items evaluated. This value will be one more than the highest index available via MemoIter::recall().

pub fn get(&mut self, idx: usize) -> Option<&T>[src]

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_slice<R>(&mut self, range: R) -> &[T] where
    R: RangeBounds<usize> + SliceIndex<[T], Output = [T]>, 
[src]

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 is_exhausted(&self) -> bool[src]

Return true if the internal Iterator has been exhausted and is done returning new values.

pub fn recall(&mut self, idx: usize) -> Option<&T>[src]

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 consume(self) -> (Vec<T>, I)[src]

Consume self, returning a Tuple containing the internal stored Vec<T> and the original Iterator.

Trait Implementations

impl<I: Debug, T: Debug> Debug for MemoIter<I, T> where
    I: Iterator<Item = T>, 
[src]

impl<I, T> ExactSizeIterator for MemoIter<I, T> where
    I: ExactSizeIterator + Iterator<Item = T>,
    T: Copy
[src]

impl<F, I, T> From<F> for MemoIter<I, T> where
    F: IntoIterator<Item = T, IntoIter = I>,
    I: Iterator<Item = T>, 
[src]

impl<I, T> Iterator for MemoIter<I, T> where
    I: Iterator<Item = T>,
    T: Copy
[src]

type Item = T

The type of the elements being iterated over.

Auto Trait Implementations

impl<I, T> RefUnwindSafe for MemoIter<I, T> where
    I: RefUnwindSafe,
    T: RefUnwindSafe

impl<I, T> Send for MemoIter<I, T> where
    I: Send,
    T: Send

impl<I, T> Sync for MemoIter<I, T> where
    I: Sync,
    T: Sync

impl<I, T> Unpin for MemoIter<I, T> where
    I: Unpin,
    T: Unpin

impl<I, T> UnwindSafe for MemoIter<I, T> where
    I: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.