Path: blob/main/crates/polars-arrow/src/array/map/iterator.rs
6939 views
use super::MapArray;1use crate::array::Array;2use crate::bitmap::utils::{BitmapIter, ZipValidity};3use crate::trusted_len::TrustedLen;45/// Iterator of values of an [`ListArray`].6#[derive(Clone, Debug)]7pub struct MapValuesIter<'a> {8array: &'a MapArray,9index: usize,10end: usize,11}1213impl<'a> MapValuesIter<'a> {14#[inline]15pub fn new(array: &'a MapArray) -> Self {16Self {17array,18index: 0,19end: array.len(),20}21}22}2324impl Iterator for MapValuesIter<'_> {25type Item = Box<dyn Array>;2627#[inline]28fn next(&mut self) -> Option<Self::Item> {29if self.index == self.end {30return None;31}32let old = self.index;33self.index += 1;34// SAFETY:35// self.end is maximized by the length of the array36Some(unsafe { self.array.value_unchecked(old) })37}3839#[inline]40fn size_hint(&self) -> (usize, Option<usize>) {41(self.end - self.index, Some(self.end - self.index))42}43}4445unsafe impl TrustedLen for MapValuesIter<'_> {}4647impl DoubleEndedIterator for MapValuesIter<'_> {48#[inline]49fn next_back(&mut self) -> Option<Self::Item> {50if self.index == self.end {51None52} else {53self.end -= 1;54// SAFETY:55// self.end is maximized by the length of the array56Some(unsafe { self.array.value_unchecked(self.end) })57}58}59}6061impl<'a> IntoIterator for &'a MapArray {62type Item = Option<Box<dyn Array>>;63type IntoIter = ZipValidity<Box<dyn Array>, MapValuesIter<'a>, BitmapIter<'a>>;6465fn into_iter(self) -> Self::IntoIter {66self.iter()67}68}6970impl<'a> MapArray {71/// Returns an iterator of `Option<Box<dyn Array>>`72pub fn iter(&'a self) -> ZipValidity<Box<dyn Array>, MapValuesIter<'a>, BitmapIter<'a>> {73ZipValidity::new_with_validity(MapValuesIter::new(self), self.validity())74}7576/// Returns an iterator of `Box<dyn Array>`77pub fn values_iter(&'a self) -> MapValuesIter<'a> {78MapValuesIter::new(self)79}80}818283