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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Arena structure and node storage.
//!
//! The [`Arena`] is the central owner of all tree nodes. Nodes are stored
//! contiguously in a single `Vec` and referenced by [`NodeId`](crate::NodeId).
//! Removed nodes are recycled through an internal free list.
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(not(feature = "std"))]
use core::{
mem,
num::NonZeroUsize,
ops::{Index, IndexMut},
slice,
};
#[cfg(feature = "par_iter")]
use rayon::prelude::*;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use std::{
mem,
num::NonZeroUsize,
ops::{Index, IndexMut},
slice,
};
use crate::{Node, NodeId, node::NodeData};
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "deser", derive(Deserialize, Serialize))]
/// An `Arena` structure containing certain [`Node`]s.
pub struct Arena<T> {
nodes: Vec<Node<T>>,
first_free_slot: Option<usize>,
last_free_slot: Option<usize>,
}
impl<T> Arena<T> {
/// Creates a new empty `Arena`.
#[must_use]
pub fn new() -> Arena<T> {
Self::default()
}
/// Creates a new empty `Arena` with enough capacity to store `n` nodes.
#[must_use]
pub fn with_capacity(n: usize) -> Self {
Self {
nodes: Vec::with_capacity(n),
first_free_slot: None,
last_free_slot: None,
}
}
/// Returns the number of nodes the arena can hold without reallocating.
pub fn capacity(&self) -> usize {
self.nodes.capacity()
}
/// Reserves capacity for `additional` more nodes to be inserted.
///
/// The arena may reserve more space to avoid frequent reallocations.
///
/// # Panics
///
/// Panics if the new capacity exceeds isize::MAX bytes.
pub fn reserve(&mut self, additional: usize) {
self.nodes.reserve(additional);
}
/// Retrieves the `NodeId` corresponding to a `Node` in the `Arena`.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// let node = arena.get(foo).unwrap();
///
/// let node_id = arena.get_node_id(node).unwrap();
/// assert_eq!(*arena[node_id].get(), "foo");
/// ```
pub fn get_node_id(&self, node: &Node<T>) -> Option<NodeId> {
let nodes_range = self.nodes.as_ptr_range();
let p = node as *const Node<T>;
if !nodes_range.contains(&p) {
return None;
}
let node_index = (p as usize - nodes_range.start as usize) / mem::size_of::<Node<T>>();
let node_id = NonZeroUsize::new(node_index.wrapping_add(1))?;
Some(NodeId::from_non_zero_usize(
node_id,
self.nodes[node_index].stamp,
))
}
/// Retrieves the `NodeId` corresponding to the `Node` at `index` in the `Arena`, if it exists.
///
/// Note: We use 1 based indexing, so the first element is at `1` and not `0`.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// # use std::num::NonZeroUsize;
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// let node = arena.get(foo).unwrap();
/// let index: NonZeroUsize = foo.into();
///
/// let new_foo = arena.get_node_id_at(index).unwrap();
/// assert_eq!(foo, new_foo);
///
/// foo.remove(&mut arena);
/// let new_foo = arena.get_node_id_at(index);
/// assert!(new_foo.is_none(), "must be none if the node at the index doesn't exist");
/// ```
pub fn get_node_id_at(&self, index: NonZeroUsize) -> Option<NodeId> {
let index0 = index.get() - 1; // we use 1 based indexing.
self.nodes
.get(index0)
.filter(|n| !n.is_removed())
.map(|node| NodeId::from_non_zero_usize(index, node.stamp))
}
/// Creates a new node from its associated data.
///
/// # Panics
///
/// Panics if the arena already has `usize::max_value()` nodes.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
///
/// assert_eq!(*arena[foo].get(), "foo");
/// ```
pub fn new_node(&mut self, data: T) -> NodeId {
let (index, stamp) = if let Some(index) = self.pop_front_free_node() {
let node = &mut self.nodes[index];
node.reuse(data);
(index, node.stamp)
} else {
let index = self.nodes.len();
let node = Node::new(data);
let stamp = node.stamp;
self.nodes.push(node);
(index, stamp)
};
let next_index1 =
NonZeroUsize::new(index.wrapping_add(1)).expect("Too many nodes in the arena");
NodeId::from_non_zero_usize(next_index1, stamp)
}
/// Returns the number of nodes in the arena, including removed nodes.
///
/// Removed nodes are still counted because they remain in the
/// internal storage. Use [`iter()`] with [`Node::is_removed()`]
/// to count only live nodes.
///
/// [`iter()`]: Arena::iter
/// [`Node::is_removed()`]: crate::Node::is_removed
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// let _bar = arena.new_node("bar");
/// assert_eq!(arena.count(), 2);
///
/// foo.remove(&mut arena);
/// // The removed node is still counted.
/// assert_eq!(arena.count(), 2);
/// ```
pub fn count(&self) -> usize {
self.nodes.len()
}
/// Returns `true` if arena has no nodes, `false` otherwise.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// assert!(arena.is_empty());
///
/// let foo = arena.new_node("foo");
/// assert!(!arena.is_empty());
///
/// foo.remove(&mut arena);
/// assert!(!arena.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.count() == 0
}
/// Returns a reference to the node with the given id if in the arena.
///
/// Returns `None` if not available.
///
/// # Examples
///
/// ```
/// # use indextree::{Arena, NodeId};
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));
/// ```
///
/// Note that this does not check whether the given node ID is created by
/// the arena.
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// let bar = arena.new_node("bar");
/// assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));
///
/// let mut another_arena = Arena::new();
/// let _ = another_arena.new_node("Another arena");
/// assert_eq!(another_arena.get(foo).map(|node| *node.get()), Some("Another arena"));
/// assert!(another_arena.get(bar).is_none());
/// ```
pub fn get(&self, id: NodeId) -> Option<&Node<T>> {
self.nodes.get(id.index0())
}
/// Returns a mutable reference to the node with the given id if in the
/// arena.
///
/// Returns `None` if not available.
///
/// # Examples
///
/// ```
/// # use indextree::{Arena, NodeId};
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// assert_eq!(arena.get(foo).map(|node| *node.get()), Some("foo"));
///
/// *arena.get_mut(foo).expect("The `foo` node exists").get_mut() = "FOO!";
/// assert_eq!(arena.get(foo).map(|node| *node.get()), Some("FOO!"));
/// ```
pub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node<T>> {
self.nodes.get_mut(id.index0())
}
/// Returns an iterator of all nodes in the arena in storage-order.
///
/// Note that this iterator returns also removed elements, which can be
/// tested with the [`is_removed()`] method on the node.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// let _foo = arena.new_node("foo");
/// let _bar = arena.new_node("bar");
///
/// let mut iter = arena.iter();
/// assert_eq!(iter.next().map(|node| *node.get()), Some("foo"));
/// assert_eq!(iter.next().map(|node| *node.get()), Some("bar"));
/// assert_eq!(iter.next().map(|node| *node.get()), None);
/// ```
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// let _foo = arena.new_node("foo");
/// let bar = arena.new_node("bar");
/// bar.remove(&mut arena);
///
/// let mut iter = arena.iter();
/// assert_eq!(iter.next().map(|node| (*node.get(), node.is_removed())), Some(("foo", false)));
/// assert_eq!(iter.next().map_or(false, |node| node.is_removed()), true);
/// assert_eq!(iter.next().map(|node| (*node.get(), node.is_removed())), None);
/// ```
///
/// [`is_removed()`]: Node::is_removed
pub fn iter(&self) -> slice::Iter<'_, Node<T>> {
self.nodes.iter()
}
/// Returns an iterator of [`NodeId`]s of all non-removed nodes in
/// the arena in storage-order.
///
/// Unlike [`iter()`], this skips removed nodes and yields `NodeId`s
/// instead of `&Node<T>`.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// let mut arena = Arena::new();
/// let foo = arena.new_node("foo");
/// let bar = arena.new_node("bar");
/// let baz = arena.new_node("baz");
/// bar.remove(&mut arena);
///
/// let ids: Vec<_> = arena.iter_node_ids().collect();
/// assert_eq!(ids, vec![foo, baz]);
/// ```
///
/// [`iter()`]: Arena::iter
pub fn iter_node_ids(&self) -> impl Iterator<Item = NodeId> + '_ {
self.nodes.iter().enumerate().filter_map(|(i, node)| {
if node.is_removed() {
return None;
}
let index1 = NonZeroUsize::new(i.wrapping_add(1))?;
Some(NodeId::from_non_zero_usize(index1, node.stamp))
})
}
/// Returns a mutable iterator of all nodes in the arena in storage-order.
///
/// Note that this iterator returns also removed elements, which can be
/// tested with the [`is_removed()`] method on the node.
///
/// # Example
///
/// ```
/// # use indextree::Arena;
/// let arena: &mut Arena<i64> = &mut Arena::new();
/// let a = arena.new_node(1);
/// let b = arena.new_node(2);
/// assert!(a.checked_append(b, arena).is_ok());
///
/// for node in arena.iter_mut() {
/// let data = node.get_mut();
/// *data = data.wrapping_add(4);
/// }
///
/// let node_refs = arena.iter().map(|i| i.get().clone()).collect::<Vec<_>>();
/// assert_eq!(node_refs, vec![5, 6]);
/// ```
/// [`is_removed()`]: Node::is_removed
pub fn iter_mut(&mut self) -> slice::IterMut<'_, Node<T>> {
self.nodes.iter_mut()
}
/// Clears all the nodes in the arena, but retains its allocated capacity.
///
/// Note that this does not marks all nodes as removed, but completely
/// removes them from the arena storage, thus invalidating all the node ids
/// that were previously created.
///
/// Any attempt to call the [`is_removed()`] method on the node id will
/// result in panic behavior.
///
/// [`is_removed()`]: NodeId::is_removed
pub fn clear(&mut self) {
self.nodes.clear();
self.first_free_slot = None;
self.last_free_slot = None;
}
/// Returns a slice of the inner nodes collection.
///
/// The slice contains all nodes in storage order, including removed
/// nodes. Use [`Node::is_removed()`] to filter them out.
///
/// [`Node::is_removed()`]: crate::Node::is_removed
pub fn as_slice(&self) -> &[Node<T>] {
self.nodes.as_slice()
}
pub(crate) fn free_node(&mut self, id: NodeId) {
let node = &mut self[id];
node.data = NodeData::NextFree(None);
node.stamp.as_removed();
let stamp = node.stamp;
if stamp.reuseable() {
if let Some(index) = self.last_free_slot {
let new_last = id.index0();
self.nodes[index].data = NodeData::NextFree(Some(new_last));
self.last_free_slot = Some(new_last);
} else {
debug_assert!(self.first_free_slot.is_none());
debug_assert!(self.last_free_slot.is_none());
self.first_free_slot = Some(id.index0());
self.last_free_slot = Some(id.index0());
}
}
}
fn pop_front_free_node(&mut self) -> Option<usize> {
let first = self.first_free_slot.take();
if let Some(index) = first {
if let NodeData::NextFree(next_free) = self.nodes[index].data {
self.first_free_slot = next_free;
} else {
unreachable!("A data node considered as a freed node");
}
if self.first_free_slot.is_none() {
self.last_free_slot = None;
}
}
first
}
}
#[cfg(feature = "par_iter")]
impl<T: Sync> Arena<T> {
/// Returns a parallel iterator over the whole arena.
///
/// Requires the `par_iter` feature. Uses [rayon](https://docs.rs/rayon)
/// for data parallelism across all nodes in storage order.
///
/// Note that this iterator returns also removed elements, which can be
/// tested with the [`is_removed()`] method on the node.
///
/// # Examples
///
/// ```
/// # use indextree::Arena;
/// # use rayon::prelude::*;
/// let mut arena = Arena::new();
/// let root = arena.new_node(1);
/// root.append_value(2, &mut arena);
/// root.append_value(3, &mut arena);
///
/// let sum: i64 = arena.par_iter().map(|node| *node.get()).sum();
/// assert_eq!(sum, 6);
/// ```
///
/// [`is_removed()`]: Node::is_removed
pub fn par_iter(&self) -> rayon::slice::Iter<'_, Node<T>> {
self.nodes.par_iter()
}
}
impl<T> Default for Arena<T> {
fn default() -> Self {
Self {
nodes: Vec::new(),
first_free_slot: None,
last_free_slot: None,
}
}
}
impl<T> Index<NodeId> for Arena<T> {
type Output = Node<T>;
fn index(&self, node: NodeId) -> &Node<T> {
&self.nodes[node.index0()]
}
}
impl<T> IndexMut<NodeId> for Arena<T> {
fn index_mut(&mut self, node: NodeId) -> &mut Node<T> {
&mut self.nodes[node.index0()]
}
}
#[test]
fn reuse_node() {
let mut arena = Arena::new();
let n1_id = arena.new_node("1");
let n2_id = arena.new_node("2");
let n3_id = arena.new_node("3");
n1_id.remove(&mut arena);
n2_id.remove(&mut arena);
n3_id.remove(&mut arena);
let n1_id = arena.new_node("1");
let n2_id = arena.new_node("2");
let n3_id = arena.new_node("3");
assert_eq!(n1_id.index0(), 0);
assert_eq!(n2_id.index0(), 1);
assert_eq!(n3_id.index0(), 2);
assert_eq!(arena.nodes.len(), 3);
}
#[test]
fn conserve_capacity() {
let mut arena = Arena::with_capacity(5);
let cap = arena.capacity();
assert!(cap >= 5);
for i in 0..cap {
arena.new_node(i);
}
arena.clear();
assert!(arena.is_empty());
let n1_id = arena.new_node(1);
let n2_id = arena.new_node(2);
let n3_id = arena.new_node(3);
assert_eq!(n1_id.index0(), 0);
assert_eq!(n2_id.index0(), 1);
assert_eq!(n3_id.index0(), 2);
assert_eq!(arena.count(), 3);
assert_eq!(arena.capacity(), cap);
}
#[test]
fn stamp_no_cycle() {
// Regression test for issue #95: stamps should never cycle back to
// a previously used value after many reuse rounds.
let mut arena = Arena::new();
for _ in 0..=i16::MAX as u32 + 1 {
let id = arena.new_node(42);
assert!(!id.is_removed(&arena));
id.remove(&mut arena);
assert!(id.is_removed(&arena));
let new_id = arena.new_node(42);
assert!(!new_id.is_removed(&arena));
assert!(id.is_removed(&arena));
new_id.remove(&mut arena);
}
}