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