Skip to main content

vortex_array/arrays/primitive/array/
accessor.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::iter;
5
6use vortex_error::VortexExpect;
7
8use crate::ToCanonical;
9use crate::accessor::ArrayAccessor;
10use crate::arrays::PrimitiveArray;
11use crate::dtype::NativePType;
12use crate::validity::Validity;
13
14impl<T: NativePType> ArrayAccessor<T> for PrimitiveArray {
15    fn with_iterator<F, R>(&self, f: F) -> R
16    where
17        F: for<'a> FnOnce(&mut dyn Iterator<Item = Option<&'a T>>) -> R,
18    {
19        match self
20            .validity()
21            .vortex_expect("primitive validity should be derivable")
22        {
23            Validity::NonNullable | Validity::AllValid => {
24                let mut iter = self.as_slice::<T>().iter().map(Some);
25                f(&mut iter)
26            }
27            Validity::AllInvalid => f(&mut iter::repeat_n(None, self.len())),
28            Validity::Array(v) => {
29                let validity = v.to_bool().into_bit_buffer();
30                let mut iter = self
31                    .as_slice::<T>()
32                    .iter()
33                    .zip(validity.iter())
34                    .map(|(value, valid)| valid.then_some(value));
35                f(&mut iter)
36            }
37        }
38    }
39}