vortex_vector/bool/
scalar.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use crate::Scalar;
5use crate::ScalarOps;
6use crate::VectorMut;
7use crate::VectorMutOps;
8use crate::bool::BoolVectorMut;
9
10/// A scalar value for boolean types.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct BoolScalar(Option<bool>);
13
14impl BoolScalar {
15    /// Creates a new bool scalar with the given value.
16    pub fn new(value: Option<bool>) -> Self {
17        Self(value)
18    }
19
20    /// Returns the value of the bool scalar, or `None` if the scalar is null.
21    pub fn value(&self) -> Option<bool> {
22        self.0
23    }
24
25    /// Creates a zero (false) bool scalar.
26    pub fn zero() -> Self {
27        Self::new(Some(false))
28    }
29
30    /// Creates a null bool scalar.
31    pub fn null() -> Self {
32        Self::new(None)
33    }
34}
35
36impl ScalarOps for BoolScalar {
37    fn is_valid(&self) -> bool {
38        self.0.is_some()
39    }
40
41    fn mask_validity(&mut self, mask: bool) {
42        if !mask {
43            self.0 = None;
44        }
45    }
46
47    fn repeat(&self, n: usize) -> VectorMut {
48        let mut vec = BoolVectorMut::with_capacity(n);
49        match self.0 {
50            None => vec.append_nulls(n),
51            Some(value) => vec.append_values(value, n),
52        }
53        vec.into()
54    }
55}
56
57impl From<BoolScalar> for Scalar {
58    fn from(val: BoolScalar) -> Self {
59        Scalar::Bool(val)
60    }
61}
62
63impl From<bool> for Scalar {
64    fn from(value: bool) -> Self {
65        BoolScalar::new(Some(value)).into()
66    }
67}