Path: blob/main/crates/polars-arrow/src/array/dictionary/iterator.rs
6939 views
use super::{DictionaryArray, DictionaryKey};1use crate::bitmap::utils::{BitmapIter, ZipValidity};2use crate::scalar::Scalar;3use crate::trusted_len::TrustedLen;45/// Iterator of values of an `ListArray`.6pub struct DictionaryValuesIter<'a, K: DictionaryKey> {7array: &'a DictionaryArray<K>,8index: usize,9end: usize,10}1112impl<'a, K: DictionaryKey> DictionaryValuesIter<'a, K> {13#[inline]14pub fn new(array: &'a DictionaryArray<K>) -> Self {15Self {16array,17index: 0,18end: array.len(),19}20}21}2223impl<K: DictionaryKey> Iterator for DictionaryValuesIter<'_, K> {24type Item = Box<dyn Scalar>;2526#[inline]27fn next(&mut self) -> Option<Self::Item> {28if self.index == self.end {29return None;30}31let old = self.index;32self.index += 1;33Some(self.array.value(old))34}3536#[inline]37fn size_hint(&self) -> (usize, Option<usize>) {38(self.end - self.index, Some(self.end - self.index))39}40}4142unsafe impl<K: DictionaryKey> TrustedLen for DictionaryValuesIter<'_, K> {}4344impl<K: DictionaryKey> DoubleEndedIterator for DictionaryValuesIter<'_, K> {45#[inline]46fn next_back(&mut self) -> Option<Self::Item> {47if self.index == self.end {48None49} else {50self.end -= 1;51Some(self.array.value(self.end))52}53}54}5556type ValuesIter<'a, K> = DictionaryValuesIter<'a, K>;57type ZipIter<'a, K> = ZipValidity<Box<dyn Scalar>, ValuesIter<'a, K>, BitmapIter<'a>>;5859impl<'a, K: DictionaryKey> IntoIterator for &'a DictionaryArray<K> {60type Item = Option<Box<dyn Scalar>>;61type IntoIter = ZipIter<'a, K>;6263fn into_iter(self) -> Self::IntoIter {64self.iter()65}66}676869