reifydb_value/value/container/
any.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::{Result, util::bitvec::BitVec, value::Value};
13
14pub struct AnyContainer {
15 data: Vec<Value>,
16}
17
18impl Clone for AnyContainer {
19 fn clone(&self) -> Self {
20 Self {
21 data: self.data.clone(),
22 }
23 }
24}
25
26impl Debug for AnyContainer {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 f.debug_struct("AnyContainer").field("data", &self.data).finish()
29 }
30}
31
32impl PartialEq for AnyContainer {
33 fn eq(&self, other: &Self) -> bool {
34 self.data == other.data
35 }
36}
37
38impl Serialize for AnyContainer {
39 fn serialize<Ser: Serializer>(&self, serializer: Ser) -> StdResult<Ser::Ok, Ser::Error> {
40 #[derive(Serialize)]
41 struct Helper<'a> {
42 data: &'a Vec<Value>,
43 }
44 Helper {
45 data: &self.data,
46 }
47 .serialize(serializer)
48 }
49}
50
51impl<'de> Deserialize<'de> for AnyContainer {
52 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
53 #[derive(Deserialize)]
54 struct Helper {
55 data: Vec<Value>,
56 }
57 let h = Helper::deserialize(deserializer)?;
58 Ok(AnyContainer {
59 data: h.data,
60 })
61 }
62}
63
64impl Deref for AnyContainer {
65 type Target = [Value];
66
67 fn deref(&self) -> &Self::Target {
68 self.data.as_slice()
69 }
70}
71
72impl AnyContainer {
73 pub fn new(data: Vec<Value>) -> Self {
74 Self {
75 data,
76 }
77 }
78
79 pub fn with_capacity(capacity: usize) -> Self {
80 Self {
81 data: Vec::with_capacity(capacity),
82 }
83 }
84
85 pub fn from_vec(data: Vec<Value>) -> Self {
86 Self {
87 data,
88 }
89 }
90}
91
92impl AnyContainer {
93 pub fn from_parts(data: Vec<Value>) -> Self {
94 Self {
95 data,
96 }
97 }
98
99 pub fn len(&self) -> usize {
100 self.data.len()
101 }
102
103 pub fn capacity(&self) -> usize {
104 self.data.capacity()
105 }
106
107 pub fn heap_size(&self) -> usize {
108 self.capacity() * size_of::<Value>()
109 }
110
111 pub fn is_empty(&self) -> bool {
112 self.data.is_empty()
113 }
114
115 pub fn clear(&mut self) {
116 self.data.clear();
117 }
118
119 pub fn push(&mut self, value: Value) {
120 self.data.push(value);
121 }
122
123 pub fn push_default(&mut self) {
124 self.data.push(Value::none());
125 }
126
127 pub fn get(&self, index: usize) -> Option<&Value> {
128 self.data.get(index)
129 }
130
131 pub fn is_defined(&self, idx: usize) -> bool {
132 idx < self.len()
133 }
134
135 pub fn is_fully_defined(&self) -> bool {
136 true
137 }
138
139 pub fn data(&self) -> &Vec<Value> {
140 &self.data
141 }
142
143 pub fn data_mut(&mut self) -> &mut Vec<Value> {
144 &mut self.data
145 }
146
147 pub fn as_string(&self, index: usize) -> String {
148 if index < self.len() {
149 format!("{}", self.data[index])
150 } else {
151 "none".to_string()
152 }
153 }
154
155 pub fn get_value(&self, index: usize) -> Value {
156 if index < self.len() {
157 Value::Any(Box::new(self.data[index].clone()))
158 } else {
159 Value::none()
160 }
161 }
162
163 pub fn none_count(&self) -> usize {
164 0
165 }
166
167 pub fn take(&self, num: usize) -> Self {
168 Self {
169 data: self.data[..num.min(self.data.len())].to_vec(),
170 }
171 }
172
173 pub fn slice(&self, start: usize, end: usize) -> Self {
174 let count = (end - start).min(self.len().saturating_sub(start));
175 let mut new_data = Vec::with_capacity(count);
176 for i in start..(start + count) {
177 new_data.push(self.data[i].clone());
178 }
179 Self {
180 data: new_data,
181 }
182 }
183
184 pub fn filter(&mut self, mask: &BitVec) {
185 let mut new_data = Vec::with_capacity(mask.count_ones());
186
187 for (i, keep) in mask.iter().enumerate() {
188 if keep && i < self.len() {
189 new_data.push(self.data[i].clone());
190 }
191 }
192
193 self.data = new_data;
194 }
195
196 pub fn reorder(&mut self, indices: &[usize]) {
197 let mut new_data = Vec::with_capacity(indices.len());
198
199 for &idx in indices {
200 if idx < self.len() {
201 new_data.push(self.data[idx].clone());
202 } else {
203 new_data.push(Value::none());
204 }
205 }
206
207 self.data = new_data;
208 }
209
210 pub fn extend(&mut self, other: &Self) -> Result<()> {
211 self.data.extend(other.data.iter().cloned());
212 Ok(())
213 }
214}