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