reifydb_type/value/container/
any.rs1use std::ops::Deref;
5
6use serde::{Deserialize, Serialize};
7
8use crate::{BitVec, CowVec, Value};
9
10#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
11pub struct AnyContainer {
12 data: CowVec<Box<Value>>,
13 bitvec: BitVec,
14}
15
16impl AnyContainer {
17 pub fn new(data: Vec<Box<Value>>, bitvec: BitVec) -> Self {
18 debug_assert_eq!(data.len(), bitvec.len());
19 Self {
20 data: CowVec::new(data),
21 bitvec,
22 }
23 }
24
25 pub fn with_capacity(capacity: usize) -> Self {
26 Self {
27 data: CowVec::with_capacity(capacity),
28 bitvec: BitVec::with_capacity(capacity),
29 }
30 }
31
32 pub fn from_vec(data: Vec<Box<Value>>) -> Self {
33 let len = data.len();
34 Self {
35 data: CowVec::new(data),
36 bitvec: BitVec::repeat(len, true),
37 }
38 }
39
40 pub fn len(&self) -> usize {
41 debug_assert_eq!(self.data.len(), self.bitvec.len());
42 self.data.len()
43 }
44
45 pub fn capacity(&self) -> usize {
46 debug_assert!(self.data.capacity() >= self.bitvec.capacity());
47 self.data.capacity().min(self.bitvec.capacity())
48 }
49
50 pub fn is_empty(&self) -> bool {
51 self.data.is_empty()
52 }
53
54 pub fn push(&mut self, value: Box<Value>) {
55 self.data.push(value);
56 self.bitvec.push(true);
57 }
58
59 pub fn push_undefined(&mut self) {
60 self.data.push(Box::new(Value::Undefined));
61 self.bitvec.push(false);
62 }
63
64 pub fn get(&self, index: usize) -> Option<&Box<Value>> {
65 if index < self.len() && self.is_defined(index) {
66 self.data.get(index)
67 } else {
68 None
69 }
70 }
71
72 pub fn bitvec(&self) -> &BitVec {
73 &self.bitvec
74 }
75
76 pub fn bitvec_mut(&mut self) -> &mut BitVec {
77 &mut self.bitvec
78 }
79
80 pub fn is_defined(&self, idx: usize) -> bool {
81 idx < self.len() && self.bitvec.get(idx)
82 }
83
84 pub fn is_fully_defined(&self) -> bool {
85 self.bitvec.count_ones() == self.len()
86 }
87
88 pub fn data(&self) -> &CowVec<Box<Value>> {
89 &self.data
90 }
91
92 pub fn data_mut(&mut self) -> &mut CowVec<Box<Value>> {
93 &mut self.data
94 }
95
96 pub fn as_string(&self, index: usize) -> String {
97 if index < self.len() && self.is_defined(index) {
98 format!("{}", self.data[index])
99 } else {
100 "undefined".to_string()
101 }
102 }
103
104 pub fn get_value(&self, index: usize) -> Value {
105 if index < self.len() && self.is_defined(index) {
106 Value::Any(self.data[index].clone())
107 } else {
108 Value::Undefined
109 }
110 }
111
112 pub fn undefined_count(&self) -> usize {
113 self.bitvec().count_zeros()
114 }
115
116 pub fn take(&self, num: usize) -> Self {
117 Self {
118 data: self.data.take(num),
119 bitvec: self.bitvec.take(num),
120 }
121 }
122
123 pub fn filter(&mut self, mask: &BitVec) {
124 let mut new_data = Vec::with_capacity(mask.count_ones());
125 let mut new_bitvec = BitVec::with_capacity(mask.count_ones());
126
127 for (i, keep) in mask.iter().enumerate() {
128 if keep && i < self.len() {
129 new_data.push(self.data[i].clone());
130 new_bitvec.push(self.bitvec.get(i));
131 }
132 }
133
134 self.data = CowVec::new(new_data);
135 self.bitvec = new_bitvec;
136 }
137
138 pub fn reorder(&mut self, indices: &[usize]) {
139 let mut new_data = Vec::with_capacity(indices.len());
140 let mut new_bitvec = BitVec::with_capacity(indices.len());
141
142 for &idx in indices {
143 if idx < self.len() {
144 new_data.push(self.data[idx].clone());
145 new_bitvec.push(self.bitvec.get(idx));
146 } else {
147 new_data.push(Box::new(Value::Undefined));
148 new_bitvec.push(false);
149 }
150 }
151
152 self.data = CowVec::new(new_data);
153 self.bitvec = new_bitvec;
154 }
155
156 pub fn extend(&mut self, other: &Self) -> crate::Result<()> {
157 self.data.extend(other.data.iter().cloned());
158 self.bitvec.extend(&other.bitvec);
159 Ok(())
160 }
161
162 pub fn extend_from_undefined(&mut self, len: usize) {
163 self.data.extend(std::iter::repeat(Box::new(Value::Undefined)).take(len));
164 self.bitvec.extend(&BitVec::repeat(len, false));
165 }
166}
167
168impl Deref for AnyContainer {
169 type Target = [Box<Value>];
170
171 fn deref(&self) -> &Self::Target {
172 &self.data
173 }
174}