1use std::ops::BitAnd;
5use std::ops::BitOr;
6use std::ops::Not;
7
8use vortex_error::vortex_panic;
9
10use crate::AllOr;
11use crate::Mask;
12
13impl BitAnd for &Mask {
14 type Output = Mask;
15
16 fn bitand(self, rhs: Self) -> Self::Output {
17 if self.len() != rhs.len() {
18 vortex_panic!("Masks must have the same length");
19 }
20
21 match (self.bit_buffer(), rhs.bit_buffer()) {
22 (AllOr::All, _) => rhs.clone(),
23 (_, AllOr::All) => self.clone(),
24 (AllOr::None, _) => Mask::new_false(self.len()),
25 (_, AllOr::None) => Mask::new_false(self.len()),
26 (AllOr::Some(lhs), AllOr::Some(rhs)) => Mask::from_buffer(lhs & rhs),
27 }
28 }
29}
30
31impl BitAnd<&Mask> for Mask {
32 type Output = Mask;
33
34 fn bitand(self, rhs: &Mask) -> Self::Output {
36 if self.len() != rhs.len() {
37 vortex_panic!("Masks must have the same length");
38 }
39
40 match (self.bit_buffer(), rhs.bit_buffer()) {
41 (AllOr::All, _) => rhs.clone(),
42 (AllOr::None, _) | (_, AllOr::None) => Mask::new_false(self.len()),
43 (_, AllOr::All) => self,
44 (AllOr::Some(_), AllOr::Some(rhs_buf)) => {
45 Mask::from_buffer(self.into_bit_buffer() & rhs_buf)
46 }
47 }
48 }
49}
50
51impl BitOr for &Mask {
52 type Output = Mask;
53
54 fn bitor(self, rhs: Self) -> Self::Output {
55 if self.len() != rhs.len() {
56 vortex_panic!("Masks must have the same length");
57 }
58
59 match (self.bit_buffer(), rhs.bit_buffer()) {
60 (AllOr::All, _) => Mask::new_true(self.len()),
61 (_, AllOr::All) => Mask::new_true(self.len()),
62 (AllOr::None, _) => rhs.clone(),
63 (_, AllOr::None) => self.clone(),
64 (AllOr::Some(lhs), AllOr::Some(rhs)) => Mask::from_buffer(lhs | rhs),
65 }
66 }
67}
68
69impl BitOr<&Mask> for Mask {
70 type Output = Mask;
71
72 fn bitor(self, rhs: &Mask) -> Self::Output {
74 if self.len() != rhs.len() {
75 vortex_panic!("Masks must have the same length");
76 }
77
78 match (self.bit_buffer(), rhs.bit_buffer()) {
79 (AllOr::All, _) | (_, AllOr::All) => Mask::new_true(self.len()),
80 (AllOr::None, _) => rhs.clone(),
81 (_, AllOr::None) => self,
82 (AllOr::Some(_), AllOr::Some(rhs_buf)) => {
83 Mask::from_buffer(self.into_bit_buffer() | rhs_buf)
84 }
85 }
86 }
87}
88
89impl Mask {
90 pub fn bitand_not(self, rhs: &Mask) -> Mask {
92 if self.len() != rhs.len() {
93 vortex_panic!("Masks must have the same length");
94 }
95 match (self.bit_buffer(), rhs.bit_buffer()) {
96 (AllOr::None, _) | (_, AllOr::All) => Mask::new_false(self.len()),
97 (_, AllOr::None) => self,
98 (AllOr::All, _) => !rhs,
99 (AllOr::Some(_), AllOr::Some(rhs_buf)) => {
100 Mask::from_buffer(self.into_bit_buffer().into_bitand_not(rhs_buf))
101 }
102 }
103 }
104}
105
106impl Not for Mask {
107 type Output = Mask;
108
109 fn not(self) -> Self::Output {
110 !(&self)
111 }
112}
113
114impl Not for &Mask {
115 type Output = Mask;
116
117 fn not(self) -> Self::Output {
118 match self.bit_buffer() {
119 AllOr::All => Mask::new_false(self.len()),
120 AllOr::None => Mask::new_true(self.len()),
121 AllOr::Some(buffer) => Mask::from_buffer(!buffer),
122 }
123 }
124}