vortex_array/arrays/bool/compute/
is_constant.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::arrays::BoolArray;
7use crate::arrays::BoolVTable;
8use crate::compute::IsConstantKernel;
9use crate::compute::IsConstantKernelAdapter;
10use crate::compute::IsConstantOpts;
11use crate::register_kernel;
12
13impl IsConstantKernel for BoolVTable {
14    fn is_constant(&self, array: &BoolArray, opts: &IsConstantOpts) -> VortexResult<Option<bool>> {
15        // If the array is small, then it is a constant time operation.
16        if opts.is_negligible_cost() && array.len() > 64 {
17            return Ok(None);
18        }
19
20        let true_count = array.bit_buffer().true_count();
21        Ok(Some(true_count == array.len() || true_count == 0))
22    }
23}
24
25register_kernel!(IsConstantKernelAdapter(BoolVTable).lift());
26
27#[cfg(test)]
28mod tests {
29    use rstest::rstest;
30
31    use super::*;
32
33    #[rstest]
34    #[case(vec![true], true)]
35    #[case(vec![false; 65], true)]
36    #[case({
37        let mut v = vec![true; 64];
38        v.push(false);
39        v
40    }, false)]
41    fn test_is_constant(#[case] input: Vec<bool>, #[case] expected: bool) {
42        let array = BoolArray::from_iter(input);
43        assert_eq!(array.is_constant(), expected);
44    }
45}