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 typeT: 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
| Feature | IterState | SparseIterState |
|---|---|---|
| Cache storage | Contiguous Vec<T> | Sparse BTreeMap<usize, T> |
| Access pattern | Prefix (0..=i) cached | Only accessed indices cached |
| Skipped elements | All cached | Gone forever (return NA) |
| Memory for sparse access | O(max_index) | O(num_accessed) |
as_slice() support | Yes (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_INTEGERFields§
§len: usizeVector 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>,
impl<I, T> SparseIterState<I, T>where
I: Iterator<Item = T>,
Sourcepub fn new(iter: I, len: usize) -> Self
pub fn new(iter: I, len: usize) -> Self
Create a new sparse iterator state with an explicit length.
§Arguments
iter: The iterator to wraplen: The expected number of elements
Sourcepub fn get_element(&self, i: usize) -> Option<T>where
T: Copy,
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 accessibleNoneif:- Index is out of bounds
- Element was already skipped (iterator advanced past it)
- Iterator exhausted before reaching the index
Sourcepub fn iterator_position(&self) -> Option<usize>
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.
Sourcepub fn cached_count(&self) -> usize
pub fn cached_count(&self) -> usize
Get the number of cached elements.
Source§impl<I, T> SparseIterState<I, T>where
I: ExactSizeIterator<Item = T>,
impl<I, T> SparseIterState<I, T>where
I: ExactSizeIterator<Item = T>,
Sourcepub fn from_exact_size(iter: I) -> Self
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>
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>where
T: RefUnwindSafe,
I: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> SizeEq<MaybeUninit<T>> for T
impl<T> SizeEq<MaybeUninit<T>> for T
type CastFrom = CastSizedExact
Source§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read moreimpl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> InvariantsEq<ManuallyDrop<T>> for Twhere
T: ?Sized,
impl<T> InvariantsEq<T> for Twhere
T: ?Sized,
impl<T> InvariantsEq<Unalign<T>> for T
impl<T> InvariantsEq<Wrapping<T>> for T
impl<Src, Dst, A, SV, DV, R> MutationCompatible<Src, A, SV, DV, (BecauseRead, R)> for Dst
impl<Src, Dst, A, SV, DV> MutationCompatible<Src, A, SV, DV, BecauseInvariantsEq> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: TransmuteFrom<Src, SV, DV> + InvariantsEq<Src> + ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Cell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ManuallyDrop<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<ReadOnly<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<Src, Dst> TransmuteFrom<Src, Initialized, Initialized> for Dst
impl<Src, Dst, V> TransmuteFrom<Src, V, Uninit> for Dst
impl<Src, Dst> TransmuteFrom<Src, Valid, Initialized> for Dst
impl<T> TransmuteFrom<Unalign<T>, Valid, Valid> for T
impl<T> TransmuteFrom<UnsafeCell<T>, Valid, Valid> for Twhere
T: ?Sized,
impl<T> TransmuteFrom<Wrapping<T>, Valid, Valid> for T
impl<Src, Dst, A, SV, DV, C, R> TransmuteFromPtr<Src, A, SV, DV, C, R> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
C: CastExact<Src, Dst>,
Dst: TransmuteFrom<Src, SV, DV> + TryTransmuteFromPtr<Src, A, SV, DV, C, R> + ?Sized,
Src: ?Sized,
impl<Src, Dst, SV, DV, A, C, R> TryTransmuteFromPtr<Src, A, SV, DV, C, (BecauseMutationCompatible, R)> for Dstwhere
A: Aliasing,
SV: Validity,
DV: Validity,
Src: TransmuteFrom<Dst, DV, SV> + ?Sized,
Dst: MutationCompatible<Src, A, SV, DV, R> + ?Sized,
C: CastExact<Src, Dst>,
Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.