Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/primitive/iterator.rs
6939 views
1
use super::{MutablePrimitiveArray, PrimitiveArray};
2
use crate::array::{ArrayAccessor, MutableArray};
3
use crate::bitmap::IntoIter as BitmapIntoIter;
4
use crate::bitmap::utils::{BitmapIter, ZipValidity};
5
use crate::buffer::IntoIter;
6
use crate::types::NativeType;
7
8
unsafe impl<'a, T: NativeType> ArrayAccessor<'a> for [T] {
9
type Item = T;
10
11
#[inline]
12
unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item {
13
*self.get_unchecked(index)
14
}
15
16
#[inline]
17
fn len(&self) -> usize {
18
(*self).len()
19
}
20
}
21
22
impl<T: NativeType> IntoIterator for PrimitiveArray<T> {
23
type Item = Option<T>;
24
type IntoIter = ZipValidity<T, IntoIter<T>, BitmapIntoIter>;
25
26
#[inline]
27
fn into_iter(self) -> Self::IntoIter {
28
let (_, values, validity) = self.into_inner();
29
let values = values.into_iter();
30
let validity =
31
validity.and_then(|validity| (validity.unset_bits() > 0).then(|| validity.into_iter()));
32
ZipValidity::new(values, validity)
33
}
34
}
35
36
impl<'a, T: NativeType> IntoIterator for &'a PrimitiveArray<T> {
37
type Item = Option<&'a T>;
38
type IntoIter = ZipValidity<&'a T, std::slice::Iter<'a, T>, BitmapIter<'a>>;
39
40
#[inline]
41
fn into_iter(self) -> Self::IntoIter {
42
self.iter()
43
}
44
}
45
46
impl<'a, T: NativeType> MutablePrimitiveArray<T> {
47
/// Returns an iterator over `Option<T>`
48
#[inline]
49
pub fn iter(&'a self) -> ZipValidity<&'a T, std::slice::Iter<'a, T>, BitmapIter<'a>> {
50
ZipValidity::new(
51
self.values().iter(),
52
self.validity().as_ref().map(|x| x.iter()),
53
)
54
}
55
56
/// Returns an iterator of `T`
57
#[inline]
58
pub fn values_iter(&'a self) -> std::slice::Iter<'a, T> {
59
self.values().iter()
60
}
61
}
62
63