[][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.take();
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 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.

pub fn take(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.