surrealdb_core/idx/trees/
dynamicset.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::idx::trees::hnsw::ElementId;
use ahash::{HashSet, HashSetExt};
use std::fmt::Debug;

pub trait DynamicSet: Debug + Send + Sync {
	fn with_capacity(capacity: usize) -> Self;
	fn insert(&mut self, v: ElementId) -> bool;
	fn contains(&self, v: &ElementId) -> bool;
	fn remove(&mut self, v: &ElementId) -> bool;
	fn len(&self) -> usize;
	fn is_empty(&self) -> bool;
	fn iter(&self) -> impl Iterator<Item = &ElementId>;
}

#[derive(Debug)]
pub struct AHashSet(HashSet<ElementId>);

impl DynamicSet for AHashSet {
	#[inline]
	fn with_capacity(capacity: usize) -> Self {
		Self(HashSet::with_capacity(capacity))
	}

	#[inline]
	fn insert(&mut self, v: ElementId) -> bool {
		self.0.insert(v)
	}

	#[inline]
	fn contains(&self, v: &ElementId) -> bool {
		self.0.contains(v)
	}

	#[inline]
	fn remove(&mut self, v: &ElementId) -> bool {
		self.0.remove(v)
	}

	#[inline]
	fn len(&self) -> usize {
		self.0.len()
	}

	#[inline]
	fn is_empty(&self) -> bool {
		self.0.is_empty()
	}

	#[inline]
	fn iter(&self) -> impl Iterator<Item = &ElementId> {
		self.0.iter()
	}
}

#[derive(Debug)]
pub struct ArraySet<const N: usize> {
	array: [ElementId; N],
	size: usize,
}

impl<const N: usize> DynamicSet for ArraySet<N> {
	fn with_capacity(_capacity: usize) -> Self {
		#[cfg(debug_assertions)]
		assert!(_capacity <= N);
		Self {
			array: [0; N],
			size: 0,
		}
	}

	#[inline]
	fn insert(&mut self, v: ElementId) -> bool {
		if !self.contains(&v) {
			self.array[self.size] = v;
			self.size += 1;
			true
		} else {
			false
		}
	}

	#[inline]
	fn contains(&self, v: &ElementId) -> bool {
		self.array[0..self.size].contains(v)
	}

	#[inline]
	fn remove(&mut self, v: &ElementId) -> bool {
		if let Some(p) = self.array[0..self.size].iter().position(|e| e.eq(v)) {
			self.array[p..].rotate_left(1);
			self.size -= 1;
			true
		} else {
			false
		}
	}

	#[inline]
	fn len(&self) -> usize {
		self.size
	}

	#[inline]
	fn is_empty(&self) -> bool {
		self.size == 0
	}

	#[inline]
	fn iter(&self) -> impl Iterator<Item = &ElementId> {
		self.array[0..self.size].iter()
	}
}

#[cfg(test)]
mod tests {
	use crate::idx::trees::dynamicset::{AHashSet, ArraySet, DynamicSet};
	use crate::idx::trees::hnsw::ElementId;
	use ahash::HashSet;

	fn test_dynamic_set<S: DynamicSet>(capacity: ElementId) {
		let mut dyn_set = S::with_capacity(capacity as usize);
		let mut control = HashSet::default();
		// Test insertions
		for sample in 0..capacity {
			assert_eq!(dyn_set.len(), control.len(), "{capacity} - {sample}");
			let v: HashSet<ElementId> = dyn_set.iter().cloned().collect();
			assert_eq!(v, control, "{capacity} - {sample}");
			// We should not have the element yet
			assert!(!dyn_set.contains(&sample), "{capacity} - {sample}");
			// The first insertion returns true
			assert!(dyn_set.insert(sample));
			assert!(dyn_set.contains(&sample), "{capacity} - {sample}");
			// The second insertion returns false
			assert!(!dyn_set.insert(sample));
			assert!(dyn_set.contains(&sample), "{capacity} - {sample}");
			// We update the control structure
			control.insert(sample);
		}
		// Test removals
		for sample in 0..capacity {
			// The first removal returns true
			assert!(dyn_set.remove(&sample));
			assert!(!dyn_set.contains(&sample), "{capacity} - {sample}");
			// The second removal returns false
			assert!(!dyn_set.remove(&sample));
			assert!(!dyn_set.contains(&sample), "{capacity} - {sample}");
			// We update the control structure
			control.remove(&sample);
			// The control structure and the dyn_set should be identical
			assert_eq!(dyn_set.len(), control.len(), "{capacity} - {sample}");
			let v: HashSet<ElementId> = dyn_set.iter().cloned().collect();
			assert_eq!(v, control, "{capacity} - {sample}");
		}
	}

	#[test]
	fn test_dynamic_set_hash() {
		for capacity in 1..50 {
			test_dynamic_set::<AHashSet>(capacity);
		}
	}

	#[test]
	fn test_dynamic_set_array() {
		test_dynamic_set::<ArraySet<1>>(1);
		test_dynamic_set::<ArraySet<2>>(2);
		test_dynamic_set::<ArraySet<4>>(4);
		test_dynamic_set::<ArraySet<10>>(10);
		test_dynamic_set::<ArraySet<20>>(20);
		test_dynamic_set::<ArraySet<30>>(30);
	}
}