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
8390 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::types::NativeType;
6
7
unsafe impl<'a, T: NativeType> ArrayAccessor<'a> for [T] {
8
type Item = T;
9
10
#[inline]
11
unsafe fn value_unchecked(&'a self, index: usize) -> Self::Item {
12
*self.get_unchecked(index)
13
}
14
15
#[inline]
16
fn len(&self) -> usize {
17
(*self).len()
18
}
19
}
20
21
impl<T: NativeType> IntoIterator for PrimitiveArray<T> {
22
type Item = Option<T>;
23
type IntoIter = ZipValidity<T, polars_buffer::buffer::IntoIter<T>, BitmapIntoIter>;
24
25
#[inline]
26
fn into_iter(self) -> Self::IntoIter {
27
let (_, values, validity) = self.into_inner();
28
let values = values.into_iter();
29
let validity =
30
validity.and_then(|validity| (validity.unset_bits() > 0).then(|| validity.into_iter()));
31
ZipValidity::new(values, validity)
32
}
33
}
34
35
impl<'a, T: NativeType> IntoIterator for &'a PrimitiveArray<T> {
36
type Item = Option<&'a T>;
37
type IntoIter = ZipValidity<&'a T, std::slice::Iter<'a, T>, BitmapIter<'a>>;
38
39
#[inline]
40
fn into_iter(self) -> Self::IntoIter {
41
self.iter()
42
}
43
}
44
45
impl<'a, T: NativeType> MutablePrimitiveArray<T> {
46
/// Returns an iterator over `Option<T>`
47
#[inline]
48
pub fn iter(&'a self) -> ZipValidity<&'a T, std::slice::Iter<'a, T>, BitmapIter<'a>> {
49
ZipValidity::new(
50
self.values().iter(),
51
self.validity().as_ref().map(|x| x.iter()),
52
)
53
}
54
55
/// Returns an iterator of `T`
56
#[inline]
57
pub fn values_iter(&'a self) -> std::slice::Iter<'a, T> {
58
self.values().iter()
59
}
60
}
61
62