Skip to main content

reifydb_type/
storage.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::ops::Deref;
5
6use crate::util::{bitvec::BitVec, cowvec::CowVec};
7
8pub trait DataVec<T: Clone>: Deref<Target = [T]> + Clone {
9	fn spawn(&self, capacity: usize) -> Self;
10	fn push(&mut self, value: T);
11	fn clear(&mut self);
12	fn len(&self) -> usize;
13	fn is_empty(&self) -> bool {
14		self.len() == 0
15	}
16	fn as_slice(&self) -> &[T];
17	fn get(&self, idx: usize) -> Option<&T>;
18	fn extend_from_slice(&mut self, other: &[T]);
19	fn extend_iter(&mut self, iter: impl Iterator<Item = T>);
20	fn capacity(&self) -> usize;
21	fn take(&self, n: usize) -> Self {
22		let len = n.min(self.len());
23		let mut new = self.spawn(len);
24		new.extend_from_slice(&self.as_slice()[..len]);
25		new
26	}
27}
28
29pub trait DataBitVec: Clone {
30	fn spawn(&self, capacity: usize) -> Self;
31	fn push(&mut self, bit: bool);
32	fn get(&self, idx: usize) -> bool;
33	fn set(&mut self, idx: usize, value: bool);
34	fn len(&self) -> usize;
35	fn is_empty(&self) -> bool {
36		self.len() == 0
37	}
38	fn clear(&mut self);
39	fn extend_from(&mut self, other: &Self);
40	fn count_ones(&self) -> usize;
41	fn count_zeros(&self) -> usize {
42		self.len() - self.count_ones()
43	}
44	fn iter(&self) -> impl Iterator<Item = bool> + '_;
45	fn capacity(&self) -> usize;
46	fn take(&self, n: usize) -> Self {
47		let len = n.min(self.len());
48		let mut new = self.spawn(len);
49		for i in 0..len {
50			new.push(self.get(i));
51		}
52		new
53	}
54}
55
56pub trait Storage: Clone {
57	type Vec<T: Clone + PartialEq + 'static>: DataVec<T> + PartialEq;
58	type BitVec: DataBitVec + PartialEq;
59}
60
61#[derive(Clone, Debug)]
62pub struct Cow;
63
64impl Storage for Cow {
65	type Vec<T: Clone + PartialEq + 'static> = CowVec<T>;
66	type BitVec = BitVec;
67}