Path: blob/main/crates/polars-arrow/src/array/iterator.rs
6939 views
use crate::bitmap::Bitmap;1use crate::bitmap::iterator::TrueIdxIter;2use crate::trusted_len::TrustedLen;34mod private {5pub trait Sealed {}67impl<'a, T: super::ArrayAccessor<'a> + ?Sized> Sealed for T {}8}910/// Sealed trait representing access to a value of an array.11/// # Safety12/// Implementers of this trait guarantee that13/// `value_unchecked` is safe when called up to `len`14pub unsafe trait ArrayAccessor<'a>: private::Sealed {15type Item: 'a;16/// # Safety17/// The index must be in-bounds in the array.18unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item;19fn len(&self) -> usize;20}2122/// Iterator of values of an [`ArrayAccessor`].23#[derive(Debug, Clone)]24pub struct ArrayValuesIter<'a, A: ArrayAccessor<'a>> {25array: &'a A,26index: usize,27end: usize,28}2930impl<'a, A: ArrayAccessor<'a>> ArrayValuesIter<'a, A> {31/// Creates a new [`ArrayValuesIter`]32#[inline]33pub fn new(array: &'a A) -> Self {34Self {35array,36index: 0,37end: array.len(),38}39}40}4142impl<'a, A: ArrayAccessor<'a>> Iterator for ArrayValuesIter<'a, A> {43type Item = A::Item;4445#[inline]46fn next(&mut self) -> Option<Self::Item> {47if self.index == self.end {48return None;49}50let old = self.index;51self.index += 1;52Some(unsafe { self.array.value_unchecked(old) })53}5455#[inline]56fn size_hint(&self) -> (usize, Option<usize>) {57(self.end - self.index, Some(self.end - self.index))58}5960#[inline]61fn nth(&mut self, n: usize) -> Option<Self::Item> {62let new_index = self.index + n;63if new_index > self.end {64self.index = self.end;65None66} else {67self.index = new_index;68self.next()69}70}71}7273impl<'a, A: ArrayAccessor<'a>> DoubleEndedIterator for ArrayValuesIter<'a, A> {74#[inline]75fn next_back(&mut self) -> Option<Self::Item> {76if self.index == self.end {77None78} else {79self.end -= 1;80Some(unsafe { self.array.value_unchecked(self.end) })81}82}83}8485unsafe impl<'a, A: ArrayAccessor<'a>> TrustedLen for ArrayValuesIter<'a, A> {}86impl<'a, A: ArrayAccessor<'a>> ExactSizeIterator for ArrayValuesIter<'a, A> {}8788pub struct NonNullValuesIter<'a, A: ?Sized> {89accessor: &'a A,90idxs: TrueIdxIter<'a>,91}9293impl<'a, A: ArrayAccessor<'a> + ?Sized> NonNullValuesIter<'a, A> {94pub fn new(accessor: &'a A, validity: Option<&'a Bitmap>) -> Self {95Self {96idxs: TrueIdxIter::new(accessor.len(), validity),97accessor,98}99}100}101102impl<'a, A: ArrayAccessor<'a> + ?Sized> Iterator for NonNullValuesIter<'a, A> {103type Item = A::Item;104105#[inline]106fn next(&mut self) -> Option<Self::Item> {107if let Some(i) = self.idxs.next() {108return Some(unsafe { self.accessor.value_unchecked(i) });109}110None111}112113fn size_hint(&self) -> (usize, Option<usize>) {114self.idxs.size_hint()115}116}117118unsafe impl<'a, A: ArrayAccessor<'a> + ?Sized> TrustedLen for NonNullValuesIter<'a, A> {}119120impl<A: ?Sized> Clone for NonNullValuesIter<'_, A> {121fn clone(&self) -> Self {122Self {123accessor: self.accessor,124idxs: self.idxs.clone(),125}126}127}128129130