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