reifydb_value/value/container/
bool.rs1use std::{
5 fmt::{self, Debug},
6 ops::Deref,
7 result::Result as StdResult,
8};
9
10use serde::{Deserialize, Deserializer, Serialize, Serializer};
11
12use crate::{
13 Result,
14 util::bitvec::BitVec,
15 value::{Value, value_type::ValueType},
16};
17
18pub struct BoolContainer {
19 data: BitVec,
20}
21
22impl Clone for BoolContainer {
23 fn clone(&self) -> Self {
24 Self {
25 data: self.data.clone(),
26 }
27 }
28}
29
30impl Debug for BoolContainer {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 f.debug_struct("BoolContainer").field("data", &self.data).finish()
33 }
34}
35
36impl PartialEq for BoolContainer {
37 fn eq(&self, other: &Self) -> bool {
38 self.data == other.data
39 }
40}
41
42impl Serialize for BoolContainer {
43 fn serialize<Ser: Serializer>(&self, serializer: Ser) -> StdResult<Ser::Ok, Ser::Error> {
44 #[derive(Serialize)]
45 struct Helper<'a> {
46 data: &'a BitVec,
47 }
48 Helper {
49 data: &self.data,
50 }
51 .serialize(serializer)
52 }
53}
54
55impl<'de> Deserialize<'de> for BoolContainer {
56 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
57 #[derive(Deserialize)]
58 struct Helper {
59 data: BitVec,
60 }
61 let h = Helper::deserialize(deserializer)?;
62 Ok(BoolContainer {
63 data: h.data,
64 })
65 }
66}
67
68impl Deref for BoolContainer {
69 type Target = BitVec;
70
71 fn deref(&self) -> &Self::Target {
72 &self.data
73 }
74}
75
76impl BoolContainer {
77 pub fn new(data: Vec<bool>) -> Self {
78 Self {
79 data: BitVec::from_slice(&data),
80 }
81 }
82
83 pub fn with_capacity(capacity: usize) -> Self {
84 Self {
85 data: BitVec::with_capacity(capacity),
86 }
87 }
88
89 pub fn from_vec(data: Vec<bool>) -> Self {
90 Self {
91 data: BitVec::from_slice(&data),
92 }
93 }
94}
95
96impl BoolContainer {
97 pub fn from_parts(data: BitVec) -> Self {
98 Self {
99 data,
100 }
101 }
102
103 pub fn len(&self) -> usize {
104 self.data.len()
105 }
106
107 pub fn capacity(&self) -> usize {
108 self.data.capacity()
109 }
110
111 pub fn heap_size(&self) -> usize {
112 self.capacity().div_ceil(8)
113 }
114
115 pub fn is_empty(&self) -> bool {
116 self.data.len() == 0
117 }
118
119 pub fn clear(&mut self) {
120 self.data.clear();
121 }
122
123 pub fn push(&mut self, value: bool) {
124 self.data.push(value);
125 }
126
127 pub fn push_default(&mut self) {
128 self.data.push(false);
129 }
130
131 pub fn get(&self, index: usize) -> Option<bool> {
132 if index < self.len() {
133 Some(self.data.get(index))
134 } else {
135 None
136 }
137 }
138
139 pub fn is_defined(&self, idx: usize) -> bool {
140 idx < self.len()
141 }
142
143 pub fn is_fully_defined(&self) -> bool {
144 true
145 }
146
147 pub fn data(&self) -> &BitVec {
148 &self.data
149 }
150
151 pub fn data_mut(&mut self) -> &mut BitVec {
152 &mut self.data
153 }
154
155 pub fn as_string(&self, index: usize) -> String {
156 if index < self.len() {
157 self.data.get(index).to_string()
158 } else {
159 "none".to_string()
160 }
161 }
162
163 pub fn get_value(&self, index: usize) -> Value {
164 if index < self.len() {
165 Value::Boolean(self.data.get(index))
166 } else {
167 Value::none_of(ValueType::Boolean)
168 }
169 }
170
171 pub fn extend(&mut self, other: &Self) -> Result<()> {
172 self.data.extend(&other.data);
173 Ok(())
174 }
175
176 pub fn iter(&self) -> impl Iterator<Item = Option<bool>> + '_ {
177 self.data.iter().map(Some)
178 }
179
180 pub fn slice(&self, start: usize, end: usize) -> Self {
181 let count = (end - start).min(self.len().saturating_sub(start));
182 let mut new_data = BitVec::with_capacity(count);
183 for i in start..(start + count) {
184 new_data.push(self.data.get(i));
185 }
186 Self {
187 data: new_data,
188 }
189 }
190
191 pub fn filter(&mut self, mask: &BitVec) {
192 let mut new_data = BitVec::with_capacity(mask.count_ones());
193
194 for (i, keep) in mask.iter().enumerate() {
195 if keep && i < self.len() {
196 new_data.push(self.data.get(i));
197 }
198 }
199
200 self.data = new_data;
201 }
202
203 pub fn reorder(&mut self, indices: &[usize]) {
204 let mut new_data = BitVec::with_capacity(indices.len());
205
206 for &idx in indices {
207 if idx < self.len() {
208 new_data.push(self.data.get(idx));
209 } else {
210 new_data.push(false);
211 }
212 }
213
214 self.data = new_data;
215 }
216
217 pub fn take(&self, num: usize) -> Self {
218 Self {
219 data: self.data.take(num),
220 }
221 }
222}
223
224impl IntoIterator for BoolContainer {
225 type Item = Option<bool>;
226 type IntoIter = std::iter::Map<std::vec::IntoIter<bool>, fn(bool) -> Option<bool>>;
227
228 fn into_iter(self) -> Self::IntoIter {
229 let data: Vec<bool> = self.data.iter().collect();
230 data.into_iter().map(Some as fn(bool) -> Option<bool>)
231 }
232}
233
234impl Default for BoolContainer {
235 fn default() -> Self {
236 Self::with_capacity(0)
237 }
238}
239
240#[cfg(test)]
241pub mod tests {
242 use super::*;
243 use crate::util::bitvec::BitVec;
244
245 #[test]
246 fn test_new() {
247 let data = vec![true, false, true];
248 let container = BoolContainer::new(data.clone());
249
250 assert_eq!(container.len(), 3);
251 assert_eq!(container.get(0), Some(true));
252 assert_eq!(container.get(1), Some(false));
253 assert_eq!(container.get(2), Some(true));
254 }
255
256 #[test]
257 fn test_from_vec() {
258 let data = vec![true, false, true];
259 let container = BoolContainer::from_vec(data);
260
261 assert_eq!(container.len(), 3);
262 assert_eq!(container.get(0), Some(true));
263 assert_eq!(container.get(1), Some(false));
264 assert_eq!(container.get(2), Some(true));
265
266 for i in 0..3 {
267 assert!(container.is_defined(i));
268 }
269 }
270
271 #[test]
272 fn test_with_capacity() {
273 let container = BoolContainer::with_capacity(10);
274 assert_eq!(container.len(), 0);
275 assert!(container.is_empty());
276 assert!(container.capacity() >= 10);
277 }
278
279 #[test]
280 fn test_push() {
281 let mut container = BoolContainer::with_capacity(3);
282
283 container.push(true);
284 container.push(false);
285 container.push_default();
286
287 assert_eq!(container.len(), 3);
288 assert_eq!(container.get(0), Some(true));
289 assert_eq!(container.get(1), Some(false));
290 assert_eq!(container.get(2), Some(false)); assert!(container.is_defined(0));
293 assert!(container.is_defined(1));
294 assert!(container.is_defined(2));
295 }
296
297 #[test]
298 fn test_extend() {
299 let mut container1 = BoolContainer::from_vec(vec![true, false]);
300 let container2 = BoolContainer::from_vec(vec![false, true]);
301
302 container1.extend(&container2).unwrap();
303
304 assert_eq!(container1.len(), 4);
305 assert_eq!(container1.get(0), Some(true));
306 assert_eq!(container1.get(1), Some(false));
307 assert_eq!(container1.get(2), Some(false));
308 assert_eq!(container1.get(3), Some(true));
309 }
310
311 #[test]
312 fn test_iter() {
313 let data = vec![true, false, true];
314 let container = BoolContainer::new(data);
315
316 let collected: Vec<Option<bool>> = container.iter().collect();
317 assert_eq!(collected, vec![Some(true), Some(false), Some(true)]);
318 }
319
320 #[test]
321 fn test_slice() {
322 let container = BoolContainer::from_vec(vec![true, false, true, false]);
323 let sliced = container.slice(1, 3);
324
325 assert_eq!(sliced.len(), 2);
326 assert_eq!(sliced.get(0), Some(false));
327 assert_eq!(sliced.get(1), Some(true));
328 }
329
330 #[test]
331 fn test_filter() {
332 let mut container = BoolContainer::from_vec(vec![true, false, true, false]);
333 let mask = BitVec::from_slice(&[true, false, true, false]);
334
335 container.filter(&mask);
336
337 assert_eq!(container.len(), 2);
338 assert_eq!(container.get(0), Some(true));
339 assert_eq!(container.get(1), Some(true));
340 }
341
342 #[test]
343 fn test_reorder() {
344 let mut container = BoolContainer::from_vec(vec![true, false, true]);
345 let indices = [2, 0, 1];
346
347 container.reorder(&indices);
348
349 assert_eq!(container.len(), 3);
350 assert_eq!(container.get(0), Some(true)); assert_eq!(container.get(1), Some(true)); assert_eq!(container.get(2), Some(false)); }
354
355 #[test]
356 fn test_reorder_with_out_of_bounds() {
357 let mut container = BoolContainer::from_vec(vec![true, false]);
358 let indices = [1, 5, 0]; container.reorder(&indices);
361
362 assert_eq!(container.len(), 3);
363 assert_eq!(container.get(0), Some(false)); assert_eq!(container.get(1), Some(false)); assert_eq!(container.get(2), Some(true)); }
367
368 #[test]
369 fn testault() {
370 let container = BoolContainer::default();
371 assert_eq!(container.len(), 0);
372 assert!(container.is_empty());
373 }
374}