Path: blob/main/crates/polars-arrow/src/array/struct_/iterator.rs
6939 views
use super::StructArray;1use crate::bitmap::utils::{BitmapIter, ZipValidity};2use crate::scalar::{Scalar, new_scalar};3use crate::trusted_len::TrustedLen;45pub struct StructValueIter<'a> {6array: &'a StructArray,7index: usize,8end: usize,9}1011impl<'a> StructValueIter<'a> {12#[inline]13pub fn new(array: &'a StructArray) -> Self {14Self {15array,16index: 0,17end: array.len(),18}19}20}2122impl Iterator for StructValueIter<'_> {23type Item = Vec<Box<dyn Scalar>>;2425#[inline]26fn next(&mut self) -> Option<Self::Item> {27if self.index == self.end {28return None;29}30let old = self.index;31self.index += 1;3233// SAFETY:34// self.end is maximized by the length of the array35Some(36self.array37.values()38.iter()39.map(|v| new_scalar(v.as_ref(), old))40.collect(),41)42}4344#[inline]45fn size_hint(&self) -> (usize, Option<usize>) {46(self.end - self.index, Some(self.end - self.index))47}48}4950unsafe impl TrustedLen for StructValueIter<'_> {}5152impl DoubleEndedIterator for StructValueIter<'_> {53#[inline]54fn next_back(&mut self) -> Option<Self::Item> {55if self.index == self.end {56None57} else {58self.end -= 1;5960// SAFETY:61// self.end is maximized by the length of the array62Some(63self.array64.values()65.iter()66.map(|v| new_scalar(v.as_ref(), self.end))67.collect(),68)69}70}71}7273type ValuesIter<'a> = StructValueIter<'a>;74type ZipIter<'a> = ZipValidity<Vec<Box<dyn Scalar>>, ValuesIter<'a>, BitmapIter<'a>>;7576impl<'a> IntoIterator for &'a StructArray {77type Item = Option<Vec<Box<dyn Scalar>>>;78type IntoIter = ZipIter<'a>;7980fn into_iter(self) -> Self::IntoIter {81self.iter()82}83}8485impl<'a> StructArray {86/// Returns an iterator of `Option<Box<dyn Array>>`87pub fn iter(&'a self) -> ZipIter<'a> {88ZipValidity::new_with_validity(StructValueIter::new(self), self.validity())89}9091/// Returns an iterator of `Box<dyn Array>`92pub fn values_iter(&'a self) -> ValuesIter<'a> {93StructValueIter::new(self)94}95}969798