Skip to main content

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.to_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    use crate::compute::is_constant;
33
34    #[rstest]
35    #[case(vec![true], true)]
36    #[case(vec![false; 65], true)]
37    #[case({
38        let mut v = vec![true; 64];
39        v.push(false);
40        v
41    }, false)]
42    fn test_is_constant(#[case] input: Vec<bool>, #[case] expected: bool) {
43        let array = BoolArray::from_iter(input);
44        assert_eq!(is_constant(array.as_ref()).unwrap(), Some(expected));
45    }
46}