pub struct IteratorCache<I: Iterator> { /* private fields */ }
Expand description

Remembers values produced by an iterator.

After wrapping an iterator with an IteratorCache, you can retrieve a reference to the $n$th element of an iterator, and then retrieve a reference to the $m$th element in constant time for any $m \leq n$ (not counting the time it took to first get the $n$th element).

Implementations§

source§

impl<I: Iterator> IteratorCache<I>

source

pub const fn new(xs: I) -> IteratorCache<I>

Creates a new IteratorCache.

This function does not allocate any memory or advance the iterator.

§Worst-case complexity

Constant time and additional memory.

§Examples
use malachite_base::iterators::iterator_cache::IteratorCache;

let xs = IteratorCache::new([1, 2, 3].iter());
source

pub fn get(&mut self, index: usize) -> Option<&I::Item>

Retrieves the $n$th element of an iterator. Indexing starts at 0.

If the index is higher than any other previously-requested index, the iterator is advanced to that index, or until it runs out. If the iterator has previously been advanced past the index, the requested element is returned from the cache in constant time. If the iterator is too short to have an element at the index, None is returned.

If you know that the element is present, and want to only take an immutable reference to self, consider using assert_get instead.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is 1 if get has previously been called with an index at least this large, or index otherwise.

§Examples
use malachite_base::iterators::iterator_cache::IteratorCache;

let mut xs = IteratorCache::new([1, 2, 3].iter().cloned());
assert_eq!(xs.get(1), Some(&2));
assert_eq!(xs.get(0), Some(&1));
assert_eq!(xs.get(3), None);
assert_eq!(xs.get(2), Some(&3));
source

pub fn assert_get(&self, index: usize) -> &I::Item

Retrieves the $n$th element of an iterator, while asserting that the iterator has already reached that element. Indexing starts at 0.

If the iterator has not advanced that far, or if it has fewer than $n + 1$ elements, this function panics.

The purpose of this function is to allow the caller to get an element immutably, assuming that the caller knows that the element is present. The get function has to take a mutable reference.

§Worst-case complexity

Constant time and additional memory.

§Examples
use malachite_base::iterators::iterator_cache::IteratorCache;

let mut xs = IteratorCache::new([1, 2, 3].iter().cloned());
// Force the iterator to iterate to completion
xs.get(3);
assert_eq!(xs.assert_get(1), &2);
assert_eq!(xs.assert_get(0), &1);
assert_eq!(xs.assert_get(2), &3);
source

pub fn known_len(&self) -> Option<usize>

Returns the total number of elements in the iterator, if the iterator has been completely consumed.

If the iterator has not been completely consumed yet, returns None.

§Worst-case complexity

Constant time and additional memory.

§Examples
use malachite_base::iterators::iterator_cache::IteratorCache;

let mut xs = IteratorCache::new([1, 2, 3].iter().cloned());
assert_eq!(xs.known_len(), None);
assert_eq!(xs.get(1), Some(&2));
assert_eq!(xs.known_len(), None);
assert_eq!(xs.get(0), Some(&1));
assert_eq!(xs.get(3), None);
assert_eq!(xs.known_len(), Some(3));
assert_eq!(xs.get(2), Some(&3));

Trait Implementations§

source§

impl<I: Clone + Iterator> Clone for IteratorCache<I>
where I::Item: Clone,

source§

fn clone(&self) -> IteratorCache<I>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<I: Debug + Iterator> Debug for IteratorCache<I>
where I::Item: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<I> RefUnwindSafe for IteratorCache<I>

§

impl<I> Send for IteratorCache<I>
where I: Send, <I as Iterator>::Item: Send,

§

impl<I> Sync for IteratorCache<I>
where I: Sync, <I as Iterator>::Item: Sync,

§

impl<I> Unpin for IteratorCache<I>
where I: Unpin, <I as Iterator>::Item: Unpin,

§

impl<I> UnwindSafe for IteratorCache<I>
where I: UnwindSafe, <I as Iterator>::Item: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

source§

fn exact_from(value: T) -> U

source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

source§

fn exact_into(self) -> U

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

source§

impl<T> ToDebugString for T
where T: Debug,

source§

fn to_debug_string(&self) -> String

Returns the String produced by Ts Debug implementation.

§Examples
use malachite_base::strings::ToDebugString;

assert_eq!([1, 2, 3].to_debug_string(), "[1, 2, 3]");
assert_eq!(
    [vec![2, 3], vec![], vec![4]].to_debug_string(),
    "[[2, 3], [], [4]]"
);
assert_eq!(Some(5).to_debug_string(), "Some(5)");
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

source§

fn wrapping_into(self) -> U