Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/union/iterator.rs
6939 views
1
use super::UnionArray;
2
use crate::scalar::Scalar;
3
use crate::trusted_len::TrustedLen;
4
5
#[derive(Debug, Clone)]
6
pub struct UnionIter<'a> {
7
array: &'a UnionArray,
8
current: usize,
9
}
10
11
impl<'a> UnionIter<'a> {
12
#[inline]
13
pub fn new(array: &'a UnionArray) -> Self {
14
Self { array, current: 0 }
15
}
16
}
17
18
impl Iterator for UnionIter<'_> {
19
type Item = Box<dyn Scalar>;
20
21
#[inline]
22
fn next(&mut self) -> Option<Self::Item> {
23
if self.current == self.array.len() {
24
None
25
} else {
26
let old = self.current;
27
self.current += 1;
28
Some(unsafe { self.array.value_unchecked(old) })
29
}
30
}
31
32
#[inline]
33
fn size_hint(&self) -> (usize, Option<usize>) {
34
let len = self.array.len() - self.current;
35
(len, Some(len))
36
}
37
}
38
39
impl<'a> IntoIterator for &'a UnionArray {
40
type Item = Box<dyn Scalar>;
41
type IntoIter = UnionIter<'a>;
42
43
#[inline]
44
fn into_iter(self) -> Self::IntoIter {
45
self.iter()
46
}
47
}
48
49
impl<'a> UnionArray {
50
/// constructs a new iterator
51
#[inline]
52
pub fn iter(&'a self) -> UnionIter<'a> {
53
UnionIter::new(self)
54
}
55
}
56
57
impl std::iter::ExactSizeIterator for UnionIter<'_> {}
58
59
unsafe impl TrustedLen for UnionIter<'_> {}
60
61