Skip to main content

reifydb_type/value/container/
row.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2025 ReifyDB
3
4use std::{
5	fmt::{self, Debug},
6	ops::Deref,
7};
8
9use serde::{Deserialize, Deserializer, Serialize, Serializer};
10
11use crate::{
12	storage::{Cow, DataBitVec, DataVec, Storage},
13	util::cowvec::CowVec,
14	value::{Value, row_number::RowNumber, r#type::Type},
15};
16
17pub struct RowNumberContainer<S: Storage = Cow> {
18	data: S::Vec<RowNumber>,
19}
20
21impl<S: Storage> Clone for RowNumberContainer<S> {
22	fn clone(&self) -> Self {
23		Self {
24			data: self.data.clone(),
25		}
26	}
27}
28
29impl<S: Storage> Debug for RowNumberContainer<S>
30where
31	S::Vec<RowNumber>: Debug,
32{
33	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34		f.debug_struct("RowNumberContainer").field("data", &self.data).finish()
35	}
36}
37
38impl<S: Storage> PartialEq for RowNumberContainer<S>
39where
40	S::Vec<RowNumber>: PartialEq,
41{
42	fn eq(&self, other: &Self) -> bool {
43		self.data == other.data
44	}
45}
46
47impl Serialize for RowNumberContainer<Cow> {
48	fn serialize<Ser: Serializer>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error> {
49		#[derive(Serialize)]
50		struct Helper<'a> {
51			data: &'a CowVec<RowNumber>,
52		}
53		Helper {
54			data: &self.data,
55		}
56		.serialize(serializer)
57	}
58}
59
60impl<'de> Deserialize<'de> for RowNumberContainer<Cow> {
61	fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
62		#[derive(Deserialize)]
63		struct Helper {
64			data: CowVec<RowNumber>,
65		}
66		let h = Helper::deserialize(deserializer)?;
67		Ok(RowNumberContainer {
68			data: h.data,
69		})
70	}
71}
72
73impl<S: Storage> Deref for RowNumberContainer<S> {
74	type Target = [RowNumber];
75
76	fn deref(&self) -> &Self::Target {
77		self.data.as_slice()
78	}
79}
80
81impl RowNumberContainer<Cow> {
82	pub fn new(data: Vec<RowNumber>) -> Self {
83		Self {
84			data: CowVec::new(data),
85		}
86	}
87
88	pub fn with_capacity(capacity: usize) -> Self {
89		Self {
90			data: CowVec::with_capacity(capacity),
91		}
92	}
93
94	pub fn from_vec(data: Vec<RowNumber>) -> Self {
95		Self {
96			data: CowVec::new(data),
97		}
98	}
99}
100
101impl<S: Storage> RowNumberContainer<S> {
102	pub fn from_parts(data: S::Vec<RowNumber>) -> Self {
103		Self {
104			data,
105		}
106	}
107
108	pub fn len(&self) -> usize {
109		DataVec::len(&self.data)
110	}
111
112	pub fn capacity(&self) -> usize {
113		DataVec::capacity(&self.data)
114	}
115
116	pub fn is_empty(&self) -> bool {
117		DataVec::is_empty(&self.data)
118	}
119
120	pub fn push(&mut self, value: RowNumber) {
121		DataVec::push(&mut self.data, value);
122	}
123
124	pub fn push_default(&mut self) {
125		DataVec::push(&mut self.data, RowNumber::default());
126	}
127
128	pub fn get(&self, index: usize) -> Option<&RowNumber> {
129		if index < self.len() {
130			DataVec::get(&self.data, index)
131		} else {
132			None
133		}
134	}
135
136	pub fn is_defined(&self, idx: usize) -> bool {
137		idx < self.len()
138	}
139
140	pub fn data(&self) -> &S::Vec<RowNumber> {
141		&self.data
142	}
143
144	pub fn data_mut(&mut self) -> &mut S::Vec<RowNumber> {
145		&mut self.data
146	}
147
148	pub fn as_string(&self, index: usize) -> String {
149		if index < self.len() {
150			self.data[index].to_string()
151		} else {
152			"none".to_string()
153		}
154	}
155
156	pub fn get_value(&self, index: usize) -> Value {
157		if index < self.len() {
158			Value::Uint8(self.data[index].value())
159		} else {
160			Value::none_of(Type::Uint8)
161		}
162	}
163
164	pub fn extend(&mut self, other: &Self) -> crate::Result<()> {
165		DataVec::extend_iter(&mut self.data, other.data.iter().cloned());
166		Ok(())
167	}
168
169	pub fn iter(&self) -> impl Iterator<Item = Option<RowNumber>> + '_ {
170		self.data.iter().map(|&v| Some(v))
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 = DataVec::spawn(&self.data, count);
176		for i in start..(start + count) {
177			DataVec::push(&mut new_data, self.data[i].clone());
178		}
179		Self {
180			data: new_data,
181		}
182	}
183
184	pub fn filter(&mut self, mask: &S::BitVec) {
185		let mut new_data = DataVec::spawn(&self.data, DataBitVec::count_ones(mask));
186
187		for (i, keep) in DataBitVec::iter(mask).enumerate() {
188			if keep && i < self.len() {
189				DataVec::push(&mut new_data, 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 = DataVec::spawn(&self.data, indices.len());
198
199		for &idx in indices {
200			if idx < self.len() {
201				DataVec::push(&mut new_data, self.data[idx].clone());
202			} else {
203				DataVec::push(&mut new_data, RowNumber::default());
204			}
205		}
206
207		self.data = new_data;
208	}
209
210	pub fn take(&self, num: usize) -> Self {
211		Self {
212			data: DataVec::take(&self.data, num),
213		}
214	}
215}
216
217impl Default for RowNumberContainer<Cow> {
218	fn default() -> Self {
219		Self::with_capacity(0)
220	}
221}