vortex_array/arrays/varbin/
accessor.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::iter;
5
6use vortex_dtype::match_each_integer_ptype;
7
8use crate::ToCanonical;
9use crate::accessor::ArrayAccessor;
10use crate::arrays::varbin::VarBinArray;
11use crate::validity::Validity;
12use crate::vtable::ValidityHelper;
13
14impl ArrayAccessor<[u8]> for VarBinArray {
15    fn with_iterator<F, R>(&self, f: F) -> R
16    where
17        F: for<'a> FnOnce(&mut dyn Iterator<Item = Option<&'a [u8]>>) -> R,
18    {
19        let offsets = self.offsets().to_primitive();
20        let validity = self.validity();
21
22        let bytes = self.bytes();
23        let bytes = bytes.as_slice();
24
25        match_each_integer_ptype!(offsets.ptype(), |T| {
26            let offsets = offsets.as_slice::<T>();
27
28            #[allow(clippy::cast_possible_truncation)]
29            match validity {
30                Validity::NonNullable | Validity::AllValid => {
31                    let mut iter = offsets
32                        .windows(2)
33                        .map(|w| Some(&bytes[w[0] as usize..w[1] as usize]));
34                    f(&mut iter)
35                }
36                Validity::AllInvalid => f(&mut iter::repeat_n(None, self.len())),
37                Validity::Array(v) => {
38                    let validity = v.to_bool();
39                    let mut iter = offsets
40                        .windows(2)
41                        .zip(validity.bit_buffer())
42                        .map(|(w, valid)| valid.then(|| &bytes[w[0] as usize..w[1] as usize]));
43                    f(&mut iter)
44                }
45            }
46        })
47    }
48}
49
50impl ArrayAccessor<[u8]> for &VarBinArray {
51    fn with_iterator<F, R>(&self, f: F) -> R
52    where
53        F: for<'a> FnOnce(&mut dyn Iterator<Item = Option<&'a [u8]>>) -> R,
54    {
55        <VarBinArray as ArrayAccessor<[u8]>>::with_iterator(*self, f)
56    }
57}