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