Path: blob/main/crates/polars-arrow/src/array/iterator.rs
8353 views
use polars_buffer::Buffer;12use crate::bitmap::Bitmap;3use crate::bitmap::iterator::TrueIdxIter;4use crate::trusted_len::TrustedLen;56mod private {7pub trait Sealed {}89impl<'a, T: super::ArrayAccessor<'a> + ?Sized> Sealed for T {}10}1112/// Sealed trait representing access to a value of an array.13/// # Safety14/// Implementers of this trait guarantee that15/// `value_unchecked` is safe when called up to `len`16pub unsafe trait ArrayAccessor<'a>: private::Sealed {17type Item: 'a;18/// # Safety19/// The index must be in-bounds in the array.20unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item;21fn len(&self) -> usize;22}2324unsafe impl<'a, T: 'a> ArrayAccessor<'a> for Buffer<T> {25type Item = &'a T;2627unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item {28unsafe { self.get_unchecked(index) }29}3031fn len(&self) -> usize {32Buffer::len(self)33}34}3536/// Iterator of values of an [`ArrayAccessor`].37#[derive(Debug, Clone)]38pub struct ArrayValuesIter<'a, A: ArrayAccessor<'a>> {39array: &'a A,40index: usize,41end: usize,42}4344impl<'a, A: ArrayAccessor<'a>> ArrayValuesIter<'a, A> {45/// Creates a new [`ArrayValuesIter`]46#[inline]47pub fn new(array: &'a A) -> Self {48Self {49array,50index: 0,51end: array.len(),52}53}54}5556impl<'a, A: ArrayAccessor<'a>> Iterator for ArrayValuesIter<'a, A> {57type Item = A::Item;5859#[inline]60fn next(&mut self) -> Option<Self::Item> {61if self.index == self.end {62return None;63}64let old = self.index;65self.index += 1;66Some(unsafe { self.array.value_unchecked(old) })67}6869#[inline]70fn size_hint(&self) -> (usize, Option<usize>) {71(self.end - self.index, Some(self.end - self.index))72}7374#[inline]75fn nth(&mut self, n: usize) -> Option<Self::Item> {76let new_index = self.index + n;77if new_index > self.end {78self.index = self.end;79None80} else {81self.index = new_index;82self.next()83}84}85}8687impl<'a, A: ArrayAccessor<'a>> DoubleEndedIterator for ArrayValuesIter<'a, A> {88#[inline]89fn next_back(&mut self) -> Option<Self::Item> {90if self.index == self.end {91None92} else {93self.end -= 1;94Some(unsafe { self.array.value_unchecked(self.end) })95}96}97}9899unsafe impl<'a, A: ArrayAccessor<'a>> TrustedLen for ArrayValuesIter<'a, A> {}100impl<'a, A: ArrayAccessor<'a>> ExactSizeIterator for ArrayValuesIter<'a, A> {}101102pub struct NonNullValuesIter<'a, A: ?Sized> {103accessor: &'a A,104idxs: TrueIdxIter<'a>,105}106107impl<'a, A: ArrayAccessor<'a> + ?Sized> NonNullValuesIter<'a, A> {108pub fn new(accessor: &'a A, validity: Option<&'a Bitmap>) -> Self {109Self {110idxs: TrueIdxIter::new(accessor.len(), validity),111accessor,112}113}114}115116impl<'a, A: ArrayAccessor<'a> + ?Sized> Iterator for NonNullValuesIter<'a, A> {117type Item = A::Item;118119#[inline]120fn next(&mut self) -> Option<Self::Item> {121if let Some(i) = self.idxs.next() {122return Some(unsafe { self.accessor.value_unchecked(i) });123}124None125}126127fn size_hint(&self) -> (usize, Option<usize>) {128self.idxs.size_hint()129}130}131132unsafe impl<'a, A: ArrayAccessor<'a> + ?Sized> TrustedLen for NonNullValuesIter<'a, A> {}133134impl<A: ?Sized> Clone for NonNullValuesIter<'_, A> {135fn clone(&self) -> Self {136Self {137accessor: self.accessor,138idxs: self.idxs.clone(),139}140}141}142143144