Skip to main content

vortex_array/arrays/bool/
patch.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use itertools::Itertools;
5use vortex_error::VortexResult;
6
7use crate::ExecutionCtx;
8use crate::arrays::BoolArray;
9use crate::arrays::PrimitiveArray;
10use crate::match_each_unsigned_integer_ptype;
11use crate::patches::Patches;
12use crate::vtable::ValidityHelper;
13
14impl BoolArray {
15    pub fn patch(self, patches: &Patches, ctx: &mut ExecutionCtx) -> VortexResult<Self> {
16        let len = self.len();
17        let offset = patches.offset();
18        let indices = patches.indices().clone().execute::<PrimitiveArray>(ctx)?;
19        let values = patches.values().clone().execute::<BoolArray>(ctx)?;
20
21        let patched_validity = self.validity().clone().patch(
22            len,
23            offset,
24            &indices.to_array(),
25            values.validity(),
26            ctx,
27        )?;
28
29        let mut own_values = self.into_bit_buffer().into_mut();
30        match_each_unsigned_integer_ptype!(indices.ptype(), |I| {
31            for (idx, value) in indices
32                .as_slice::<I>()
33                .iter()
34                .zip_eq(values.to_bit_buffer().iter())
35            {
36                #[allow(clippy::cast_possible_truncation)]
37                own_values.set_to(*idx as usize - offset, value);
38            }
39        });
40
41        Ok(Self::new(own_values.freeze(), patched_validity))
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use vortex_buffer::BitBuffer;
48
49    use crate::arrays::BoolArray;
50    use crate::assert_arrays_eq;
51
52    #[test]
53    fn patch_sliced_bools() {
54        let arr = BoolArray::from(BitBuffer::new_set(12));
55        let sliced = arr.slice(4..12).unwrap();
56        let expected = BoolArray::from_iter([true; 8]);
57        assert_arrays_eq!(sliced, expected);
58    }
59
60    #[test]
61    fn patch_sliced_bools_offset() {
62        let arr = BoolArray::from(BitBuffer::new_set(15));
63        let sliced = arr.slice(4..15).unwrap();
64        let expected = BoolArray::from_iter([true; 11]);
65        assert_arrays_eq!(sliced, expected);
66    }
67}