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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::fmt::Debug;
use std::marker;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::ptr::NonNull;
pub use index::*;
pub use rle::SplitableSpan;
pub use root::DeleteResult;
mod unsafe_cursor;
mod root;
mod leaf;
mod internal;
mod mutations;
mod index;
mod safe_cursor;
pub mod testrange;
#[cfg(debug_assertions)]
pub const DEFAULT_IE: usize = 8;
#[cfg(not(debug_assertions))]
pub const DEFAULT_IE: usize = 10;
#[cfg(debug_assertions)]
pub const DEFAULT_LE: usize = 4;
#[cfg(not(debug_assertions))]
pub const DEFAULT_LE: usize = 32;
pub trait ContentTraits: SplitableSpan + Copy + Debug + Default {}
impl<T: SplitableSpan + Copy + Debug + Default> ContentTraits for T {}
#[derive(Debug)]
pub struct ContentTreeRaw<E: ContentTraits, I: TreeIndex<E>, const INT_ENTRIES: usize, const LEAF_ENTRIES: usize> {
count: I::IndexValue,
root: Node<E, I, INT_ENTRIES, LEAF_ENTRIES>,
_pin: marker::PhantomPinned,
}
pub type ContentTreeWithIndex<E, I> = ContentTreeRaw<E, I, DEFAULT_IE, DEFAULT_LE>;
pub type ContentTree<E> = ContentTreeRaw<E, RawPositionIndex, DEFAULT_IE, DEFAULT_LE>;
#[derive(Debug)]
pub(crate) struct NodeInternal<E: ContentTraits, I: TreeIndex<E>, const INT_ENTRIES: usize, const LEAF_ENTRIES: usize> {
parent: ParentPtr<E, I, INT_ENTRIES, LEAF_ENTRIES>,
index: [I::IndexValue; INT_ENTRIES],
children: [Option<Node<E, I, INT_ENTRIES, LEAF_ENTRIES>>; INT_ENTRIES],
_pin: PhantomPinned,
}
#[derive(Debug)]
pub struct NodeLeaf<E: ContentTraits, I: TreeIndex<E>, const INT_ENTRIES: usize, const LEAF_ENTRIES: usize> {
parent: ParentPtr<E, I, INT_ENTRIES, LEAF_ENTRIES>,
num_entries: u8,
data: [E; LEAF_ENTRIES],
_pin: PhantomPinned,
next: Option<NonNull<Self>>,
}
#[derive(Debug)]
pub(crate) enum Node<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> {
Internal(Pin<Box<NodeInternal<E, I, IE, LE>>>),
Leaf(Pin<Box<NodeLeaf<E, I, IE, LE>>>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum NodePtr<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> {
Internal(NonNull<NodeInternal<E, I, IE, LE>>),
Leaf(NonNull<NodeLeaf<E, I, IE, LE>>),
}
#[derive(Copy, Clone, Debug, Eq)]
pub(crate) enum ParentPtr<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> {
Root(NonNull<ContentTreeRaw<E, I, IE, LE>>),
Internal(NonNull<NodeInternal<E, I, IE, LE>>)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnsafeCursor<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> {
node: NonNull<NodeLeaf<E, I, IE, LE>>,
idx: usize,
pub offset: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Cursor<'a, E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> {
inner: UnsafeCursor<E, I, IE, LE>,
marker: marker::PhantomData<&'a ContentTreeRaw<E, I, IE, LE>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct MutCursor<'a, E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> {
pub inner: UnsafeCursor<E, I, IE, LE>,
marker: marker::PhantomData<&'a mut ContentTreeRaw<E, I, IE, LE>>,
}
impl<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> PartialEq for ParentPtr<E, I, IE, LE> {
fn eq(&self, other: &Self) -> bool {
use ParentPtr::*;
match (self, other) {
(Root(a), Root(b)) => a == b,
(Internal(a), Internal(b)) => a == b,
_ => false
}
}
}
unsafe fn ref_to_nonnull<T>(val: &T) -> NonNull<T> {
NonNull::new_unchecked(val as *const _ as *mut _)
}
pub fn null_notify<E, Node>(_e: E, _node: Node) {}
impl<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> Node<E, I, IE, LE> {
fn set_parent(&mut self, parent: ParentPtr<E, I, IE, LE>) {
unsafe {
match self {
Node::Leaf(l) => l.as_mut().get_unchecked_mut().parent = parent,
Node::Internal(i) => i.as_mut().get_unchecked_mut().parent = parent,
}
}
}
pub fn is_leaf(&self) -> bool {
match self {
Node::Leaf(_) => true,
Node::Internal(_) => false,
}
}
fn unwrap_leaf_mut(&mut self) -> Pin<&mut NodeLeaf<E, I, IE, LE>> {
match self {
Node::Leaf(l) => l.as_mut(),
Node::Internal(_) => panic!("Expected leaf - found internal node"),
}
}
fn unwrap_internal_mut(&mut self) -> Pin<&mut NodeInternal<E, I, IE, LE>> {
match self {
Node::Internal(n) => n.as_mut(),
Node::Leaf(_) => panic!("Expected internal node"),
}
}
unsafe fn as_ptr(&self) -> NodePtr<E, I, IE, LE> {
match self {
Node::Internal(n) => {
NodePtr::Internal(ref_to_nonnull(n.as_ref().get_ref()))
},
Node::Leaf(n) => {
NodePtr::Leaf(ref_to_nonnull(n.as_ref().get_ref()))
},
}
}
fn ptr_eq(&self, ptr: NodePtr<E, I, IE, LE>) -> bool {
match (self, ptr) {
(Node::Internal(n), NodePtr::Internal(ptr)) => {
std::ptr::eq(n.as_ref().get_ref(), ptr.as_ptr())
},
(Node::Leaf(n), NodePtr::Leaf(ptr)) => {
std::ptr::eq(n.as_ref().get_ref(), ptr.as_ptr())
},
_ => panic!("Pointer type does not match")
}
}
}
impl<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> NodePtr<E, I, IE, LE> {
fn unwrap_leaf(self) -> NonNull<NodeLeaf<E, I, IE, LE>> {
match self {
NodePtr::Leaf(l) => l,
NodePtr::Internal(_) => panic!("Expected leaf - found internal node"),
}
}
unsafe fn get_parent(&self) -> ParentPtr<E, I, IE, LE> {
match self {
NodePtr::Internal(n) => { n.as_ref().parent }
NodePtr::Leaf(n) => { n.as_ref().parent }
}
}
}
impl<E: ContentTraits, I: TreeIndex<E>, const IE: usize, const LE: usize> ParentPtr<E, I, IE, LE> {
fn unwrap_internal(self) -> NonNull<NodeInternal<E, I, IE, LE>> {
match self {
ParentPtr::Root(_) => { panic!("Expected internal node"); }
ParentPtr::Internal(ptr) => { ptr }
}
}
fn is_root(&self) -> bool {
match self {
ParentPtr::Root(_) => { true }
ParentPtr::Internal(_) => { false }
}
}
}
#[cfg(test)]
mod test {
use std::mem::size_of;
use super::*;
use crate::testrange::TestRange;
#[test]
fn option_node_size_is_transparent() {
let node_size = size_of::<Node<TestRange, RawPositionIndex, DEFAULT_IE, DEFAULT_LE>>();
let opt_node_size = size_of::<Option<Node<TestRange, RawPositionIndex, DEFAULT_IE, DEFAULT_LE>>>();
assert_eq!(node_size, opt_node_size);
}
}