reifydb_core/value/container/
undefined.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use reifydb_type::Value;
5use serde::{Deserialize, Serialize};
6
7use crate::BitVec;
8
9#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
10pub struct UndefinedContainer {
11	len: usize,
12}
13
14impl UndefinedContainer {
15	pub fn new(len: usize) -> Self {
16		Self {
17			len,
18		}
19	}
20
21	pub fn with_capacity(_capacity: usize) -> Self {
22		Self {
23			len: 0,
24		}
25	}
26
27	pub fn len(&self) -> usize {
28		self.len
29	}
30
31	pub fn capacity(&self) -> usize {
32		self.len
33	}
34
35	pub fn is_empty(&self) -> bool {
36		self.len == 0
37	}
38
39	pub fn clear(&mut self) {
40		self.len = 0;
41	}
42
43	pub fn is_defined(&self, _idx: usize) -> bool {
44		false
45	}
46
47	pub fn push_undefined(&mut self) {
48		self.len += 1;
49	}
50
51	pub fn as_string(&self, _index: usize) -> String {
52		"Undefined".to_string()
53	}
54
55	pub fn get_value(&self, _index: usize) -> Value {
56		Value::Undefined
57	}
58
59	pub fn extend(&mut self, other: &Self) -> crate::Result<()> {
60		self.len += other.len;
61		Ok(())
62	}
63
64	pub fn extend_from_undefined(&mut self, len: usize) {
65		self.len += len;
66	}
67
68	pub fn slice(&self, start: usize, end: usize) -> Self {
69		Self {
70			len: (end - start).min(self.len.saturating_sub(start)),
71		}
72	}
73
74	pub fn filter(&mut self, mask: &BitVec) {
75		let mut new_len = 0;
76		for (i, keep) in mask.iter().enumerate() {
77			if keep && i < self.len {
78				new_len += 1;
79			}
80		}
81		self.len = new_len;
82	}
83
84	pub fn reorder(&mut self, indices: &[usize]) {
85		self.len = indices.len();
86	}
87
88	pub fn take(&self, num: usize) -> Self {
89		Self {
90			len: num.min(self.len),
91		}
92	}
93}
94
95impl Default for UndefinedContainer {
96	fn default() -> Self {
97		Self {
98			len: 0,
99		}
100	}
101}
102
103#[cfg(test)]
104mod tests {
105	use super::*;
106	use crate::BitVec;
107
108	#[test]
109	fn test_new() {
110		let container = UndefinedContainer::new(5);
111		assert_eq!(container.len(), 5);
112		assert!(!container.is_empty());
113	}
114
115	#[test]
116	fn test_with_capacity() {
117		let container = UndefinedContainer::with_capacity(10);
118		assert_eq!(container.len(), 0);
119		assert!(container.is_empty());
120		assert_eq!(container.capacity(), 0);
121	}
122
123	#[test]
124	fn test_push_undefined() {
125		let mut container = UndefinedContainer::with_capacity(5);
126
127		container.push_undefined();
128		container.push_undefined();
129		container.push_undefined();
130
131		assert_eq!(container.len(), 3);
132		assert!(!container.is_empty());
133	}
134
135	#[test]
136	fn test_extend() {
137		let mut container1 = UndefinedContainer::new(2);
138		let container2 = UndefinedContainer::new(3);
139
140		container1.extend(&container2).unwrap();
141
142		assert_eq!(container1.len(), 5);
143	}
144
145	#[test]
146	fn test_extend_from_undefined() {
147		let mut container = UndefinedContainer::new(1);
148		container.extend_from_undefined(4);
149
150		assert_eq!(container.len(), 5);
151	}
152
153	#[test]
154	fn test_slice() {
155		let container = UndefinedContainer::new(10);
156		let sliced = container.slice(2, 7);
157
158		assert_eq!(sliced.len(), 5);
159	}
160
161	#[test]
162	fn test_slice_out_of_bounds() {
163		let container = UndefinedContainer::new(5);
164		let sliced = container.slice(3, 10);
165
166		assert_eq!(sliced.len(), 2); // min(10-3, 5-3) = 2
167	}
168
169	#[test]
170	fn test_slice_start_beyond_len() {
171		let container = UndefinedContainer::new(3);
172		let sliced = container.slice(5, 8);
173
174		assert_eq!(sliced.len(), 0); // saturating_sub(3, 5) = 0
175	}
176
177	#[test]
178	fn test_filter() {
179		let mut container = UndefinedContainer::new(6);
180		let mask = BitVec::from_slice(&[true, false, true, true, false, true]);
181
182		container.filter(&mask);
183
184		assert_eq!(container.len(), 4); // 4 true data in mask
185	}
186
187	#[test]
188	fn test_filter_with_mask_longer_than_container() {
189		let mut container = UndefinedContainer::new(3);
190		let mask = BitVec::from_slice(&[true, false, true, true, false]);
191
192		container.filter(&mask);
193
194		assert_eq!(container.len(), 2); // only first 3 elements matter
195	}
196
197	#[test]
198	fn test_reorder() {
199		let mut container = UndefinedContainer::new(3);
200		let indices = [2, 0, 1, 5]; // last index is out of bounds
201
202		container.reorder(&indices);
203
204		assert_eq!(container.len(), 4); // length becomes indices.len()
205	}
206
207	#[test]
208	fn test_clear() {
209		let mut container = UndefinedContainer::new(10);
210
211		container.clear();
212
213		assert_eq!(container.len(), 0);
214		assert!(container.is_empty());
215	}
216
217	#[test]
218	fn test_capacity_equals_len() {
219		let container = UndefinedContainer::new(7);
220		assert_eq!(container.capacity(), container.len());
221	}
222
223	#[test]
224	fn test_default() {
225		let container = UndefinedContainer::default();
226		assert_eq!(container.len(), 0);
227		assert!(container.is_empty());
228		assert_eq!(container.capacity(), 0);
229	}
230
231	#[test]
232	fn test_multiple_operations() {
233		let mut container = UndefinedContainer::with_capacity(0);
234
235		// Start empty
236		assert_eq!(container.len(), 0);
237
238		// Add some undefined data
239		container.push_undefined();
240		container.push_undefined();
241		container.extend_from_undefined(3);
242		assert_eq!(container.len(), 5);
243
244		// Filter with mask
245		let mask = BitVec::from_slice(&[true, false, true, false, true]);
246		container.filter(&mask);
247		assert_eq!(container.len(), 3);
248
249		// Reorder
250		container.reorder(&[1, 0, 2]);
251		assert_eq!(container.len(), 3);
252
253		// Clear
254		container.clear();
255		assert_eq!(container.len(), 0);
256		assert!(container.is_empty());
257	}
258}