Skip to main content

SparseIterState

Struct SparseIterState 

Source
pub struct SparseIterState<I, T> {
    len: usize,
    iter: RefCell<Option<(I, usize)>>,
    cache: RefCell<BTreeMap<usize, T>>,
}
Expand description

Core state for sparse iterator-backed ALTREP vectors.

Unlike super::IterState, this variant uses Iterator::nth() to skip elements efficiently, only caching the elements that are actually accessed.

§Type Parameters

  • I: The iterator type
  • T: The element type produced by the iterator

§Design

  • Sparse: Only accessed elements are cached (uses BTreeMap)
  • Skipping: Uses nth() to skip directly to requested indices
  • Trade-off: Skipped elements are gone forever (iterator is consumed)
  • Best for: Large iterators where only a small subset of elements are accessed

§Comparison with IterState

FeatureIterStateSparseIterState
Cache storageContiguous Vec<T>Sparse BTreeMap<usize, T>
Access patternPrefix (0..=i) cachedOnly accessed indices cached
Skipped elementsAll cachedGone forever (return NA)
Memory for sparse accessO(max_index)O(num_accessed)
as_slice() supportYes (after full materialization)No (sparse)

§Example

use miniextendr_api::altrep_data::SparseIterIntData;

// Create from an infinite-ish iterator
let data = SparseIterIntData::from_iter((0..).map(|x| x * 2), 1_000_000);

// Access only element 999_999 - skips directly there
let last = data.elt(999_999);  // Only this element is generated

// Element 0 was skipped and is now inaccessible
let first = data.elt(0);  // Returns NA_INTEGER

Fields§

§len: usize

Vector length

§iter: RefCell<Option<(I, usize)>>

Iterator state: (iterator, next index the iterator will produce)

§cache: RefCell<BTreeMap<usize, T>>

Sparse cache of accessed elements

Implementations§

Source§

impl<I, T> SparseIterState<I, T>
where I: Iterator<Item = T>,

Source

pub fn new(iter: I, len: usize) -> Self

Create a new sparse iterator state with an explicit length.

§Arguments
  • iter: The iterator to wrap
  • len: The expected number of elements
Source

pub fn get_element(&self, i: usize) -> Option<T>
where T: Copy,

Get an element, skipping intermediate elements if needed.

Uses Iterator::nth() to skip efficiently. Skipped elements are consumed from the iterator and cannot be retrieved later.

§Returns
  • Some(T) if element exists and is accessible
  • None if:
    • Index is out of bounds
    • Element was already skipped (iterator advanced past it)
    • Iterator exhausted before reaching the index
Source

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

Get the current iterator position (next index to be produced).

Returns None if the iterator has been exhausted.

Source

pub fn is_cached(&self, i: usize) -> bool

Check if an element has been cached.

Source

pub fn cached_count(&self) -> usize

Get the number of cached elements.

Source

pub fn len(&self) -> usize

Get the current length.

Source

pub fn is_empty(&self) -> bool

Check if the vector is empty.

Source§

impl<I, T> SparseIterState<I, T>
where I: ExactSizeIterator<Item = T>,

Source

pub fn from_exact_size(iter: I) -> Self

Create a new sparse iterator state from an ExactSizeIterator.

Auto Trait Implementations§

§

impl<I, T> !Freeze for SparseIterState<I, T>

§

impl<I, T> !RefUnwindSafe for SparseIterState<I, T>

§

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

§

impl<I, T> !Sync for SparseIterState<I, T>

§

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

§

impl<I, T> UnsafeUnpin for SparseIterState<I, T>
where I: UnsafeUnpin,

§

impl<I, T> UnwindSafe for SparseIterState<I, T>

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> 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> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.