indextree_ng/
lib.rs

1//! # Arena based tree data structure
2//!
3//! This is a fork of the [`indextree` crate](https://github.com/saschagrunert/indextree) 
4//! which allows to remove nodes. The original version was not capable of removing
5//! nodes as the initial idea was to drop all nodes at the same time if the lifetime 
6//! of the underlying memory arena has ended.
7//! 
8//! The arena tree structure is using a single `Vec`, a `HashMap` and numerical 
9//! identifiers. Every node holds an id which is mapped to an index of the vector
10//! via the `HashMap`. This allows to drop single nodes before the lifetime of the
11//! arena hash ended. 
12//! 
13//! There is no `RefCell` and mutability is handled in a way much more idiomatic to Rust 
14//! through unique (&mut) access to the arena. 
15//!
16//! # Example usage
17//! ```
18//! use indextree_ng::Arena;
19//!
20//! // Create a new arena
21//! let arena = &mut Arena::new();
22//!
23//! // Add some new nodes to the arena
24//! let a = arena.new_node(1);
25//! let b = arena.new_node(2);
26//!
27//! // Append b to a
28//! a.append(b, arena);
29//! assert_eq!(b.ancestors(arena).into_iter().count(), 2);
30//!
31//! //Access a node
32//! assert_eq!(arena[b].data, 2);
33//!
34//! // Remove a node
35//! arena.remove_node(a);
36//! assert_eq!(b.ancestors(arena).into_iter().count(), 1);
37//!
38//! ```
39use std::{mem, fmt};
40use std::error;
41use std::collections::HashMap;
42use std::ops::{Index, IndexMut};
43
44pub use self::IndexTreeError::*;
45
46#[doc = "
47Use this for catching errors that
48can happen when using indextree_ng::Tree.
49"]
50#[derive(Debug, Clone, PartialEq, Eq, Copy)]
51pub enum IndexTreeError {
52    /// Tried to apply a method to the same node like node.append(node, arena)
53    SameNodeErr,
54}
55
56impl fmt::Display for IndexTreeError {
57    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
58        fmt.write_str(error::Error::description(self))
59    }
60}
61
62impl error::Error for IndexTreeError {
63    fn description(&self) -> &str {
64        match *self {
65            SameNodeErr => "Tried to apply a method to the same node like node.append(node, arena)",
66        }
67    }
68}
69
70
71#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
72/// A node identifier within a particular `Arena`
73pub struct NodeId {
74    id: usize,
75}
76
77#[derive(Clone, Debug)]
78/// A node within a particular `Arena`
79pub struct Node<T> {
80    // Keep these private (with read-only accessors) so that we can keep them consistent.
81    // E.g. the parent of a node’s child is that node.
82    parent: Option<NodeId>,
83    previous_sibling: Option<NodeId>,
84    next_sibling: Option<NodeId>,
85    first_child: Option<NodeId>,
86    last_child: Option<NodeId>,
87
88    /// The actual data which will be stored within the tree
89    pub data: T,
90}
91
92impl<T> fmt::Display for Node<T> {
93    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94        write!(f, "Parent: {:?}, ", self.parent)?;
95        write!(f, "Previous sibling: {:?}, ", self.previous_sibling)?;
96        write!(f, "Next sibling: {:?}, ", self.next_sibling)?;
97        write!(f, "First child: {:?}, ", self.first_child)?;
98        write!(f, "Last child: {:?}", self.last_child)
99    }
100}
101
102#[derive(Clone, Debug)]
103/// An `Arena` structure containing certain Nodes
104pub struct Arena<T> {
105    nodes: Vec<Node<T>>,
106    lookup: HashMap<usize, usize>,
107}
108
109impl<T> Arena<T> {
110    /// Create a new empty `Arena`
111    pub fn new() -> Arena<T> {
112        Arena { nodes: Vec::new(),
113                lookup: HashMap::new(), }
114    }
115
116    /// Create a new node from its associated data.
117    pub fn new_node(&mut self, data: T) -> NodeId {
118        let next_index = self.nodes.len();
119        let next_id = self.lookup.keys().fold(0, |acc, &x| std::cmp::max(acc+1, x));
120        self.nodes.push(Node {
121            parent: None,
122            first_child: None,
123            last_child: None,
124            previous_sibling: None,
125            next_sibling: None,
126            data: data,
127        });
128        self.lookup.insert(next_id, next_index);
129        NodeId { id: next_id }
130    }
131
132    /// Removes a node from the arena. Detaches all children before.
133    pub fn remove_node(&mut self, node: NodeId) {
134        let index = *self.lookup.get(&node.id).expect("No node for given NodeId");
135
136        let mut children: Vec<NodeId> = vec![];
137        for child in node.children(self) {
138            children.push(child.clone());
139        }
140        for child in children {
141            child.detach(self);
142        }
143
144        let highest_index = self.nodes.len() - 1;
145        let mut highest_index_id: usize = 0;
146        for (key, value)  in self.lookup.iter() {
147            if *value == highest_index {
148                highest_index_id = *key;
149                break;
150            }
151        }
152
153        let _ = self.lookup.remove(&node.id);
154        let _ = self.nodes.swap_remove(index);
155        self.lookup.insert(highest_index_id, index);
156    }
157    
158    /// Returns the number of nodes in Arena
159    pub fn len(&self) -> usize {
160        self.nodes.len()
161    }
162    
163    /// Returns true if arena has no nodes, false otherwise
164    pub fn is_empty(&self) -> bool {
165        if self.len() == 0 {
166            true
167        }
168        else {
169            false
170        }
171    }
172
173    // Get mutable references to two distinct nodes. Returns IndexTreeError::SameNodeErr if a == b.
174    fn get_pair_mut(&mut self, a: &NodeId, b: &NodeId) -> Result<(&mut Node<T>, &mut Node<T>), IndexTreeError> {
175        if a.id == b.id {
176            return Err(IndexTreeError::SameNodeErr);
177        }
178
179        let error_msg = "No node for NodeId";
180        let a_index = *self.lookup.get(&a.id).expect(error_msg);
181        let b_index = *self.lookup.get(&b.id).expect(error_msg);
182        
183        let (xs, ys) = self.nodes.split_at_mut(std::cmp::max(a_index, b_index));
184        if a_index < b_index {
185            Ok((&mut xs[a_index], &mut ys[0]))
186        } else {
187            Ok((&mut ys[0], &mut xs[b_index]))
188        }
189    }
190}
191
192impl<T> Index<NodeId> for Arena<T> {
193    type Output = Node<T>;
194
195    fn index(&self, node: NodeId) -> &Node<T> {
196        let node_index = *self.lookup.get(&node.id).expect("No node for this NodeId");
197        &self.nodes[node_index]
198    }
199}
200
201impl<T> IndexMut<NodeId> for Arena<T> {
202    fn index_mut(&mut self, node: NodeId) -> &mut Node<T> {
203        let node_index = *self.lookup.get(&node.id).expect("No node for this NodeId");
204        &mut self.nodes[node_index]
205    }
206}
207
208impl<T> Node<T> {
209    /// Return the ID of the parent node, unless this node is the root of the tree.
210    pub fn parent(&self) -> Option<NodeId> {
211        self.parent
212    }
213
214    /// Return the ID of the first child of this node, unless it has no child.
215    pub fn first_child(&self) -> Option<NodeId> {
216        self.first_child
217    }
218
219    /// Return the ID of the last child of this node, unless it has no child.
220    pub fn last_child(&self) -> Option<NodeId> {
221        self.last_child
222    }
223
224    /// Return the ID of the previous sibling of this node, unless it is a first child.
225    pub fn previous_sibling(&self) -> Option<NodeId> {
226        self.previous_sibling
227    }
228
229    /// Return the ID of the previous sibling of this node, unless it is a first child.
230    pub fn next_sibling(&self) -> Option<NodeId> {
231        self.next_sibling
232    }
233}
234
235impl NodeId {
236    /// Return an iterator of references to this node and its ancestors.
237    ///
238    /// Call `.next().unwrap()` once on the iterator to skip the node itself.
239    pub fn ancestors<T>(self, arena: &Arena<T>) -> Ancestors<T> {
240        Ancestors {
241            arena: arena,
242            node: Some(self),
243        }
244    }
245
246    /// Return an iterator of references to this node and the siblings before it.
247    ///
248    /// Call `.next().unwrap()` once on the iterator to skip the node itself.
249    pub fn preceding_siblings<T>(self, arena: &Arena<T>) -> PrecedingSiblings<T> {
250        PrecedingSiblings {
251            arena: arena,
252            node: Some(self),
253        }
254    }
255
256    /// Return an iterator of references to this node and the siblings after it.
257    ///
258    /// Call `.next().unwrap()` once on the iterator to skip the node itself.
259    pub fn following_siblings<T>(self, arena: &Arena<T>) -> FollowingSiblings<T> {
260        FollowingSiblings {
261            arena: arena,
262            node: Some(self),
263        }
264    }
265
266    /// Return an iterator of references to this node’s children.
267    pub fn children<T>(self, arena: &Arena<T>) -> Children<T> {
268        Children {
269            arena: arena,
270            node: arena[self].first_child,
271        }
272    }
273
274    /// Return an iterator of references to this node’s children, in reverse order.
275    pub fn reverse_children<T>(self, arena: &Arena<T>) -> ReverseChildren<T> {
276        ReverseChildren {
277            arena: arena,
278            node: arena[self].last_child,
279        }
280    }
281
282    /// Return an iterator of references to this node and its descendants, in tree order.
283    ///
284    /// Parent nodes appear before the descendants.
285    /// Call `.next().unwrap()` once on the iterator to skip the node itself.
286    pub fn descendants<T>(self, arena: &Arena<T>) -> Descendants<T> {
287        Descendants(self.traverse(arena))
288    }
289
290    /// Return an iterator of references to this node and its descendants, in tree order.
291    pub fn traverse<T>(self, arena: &Arena<T>) -> Traverse<T> {
292        Traverse {
293            arena: arena,
294            root: self,
295            next: Some(NodeEdge::Start(self)),
296        }
297    }
298
299    /// Return an iterator of references to this node and its descendants, in tree order.
300    pub fn reverse_traverse<T>(self, arena: &Arena<T>) -> ReverseTraverse<T> {
301        ReverseTraverse {
302            arena: arena,
303            root: self,
304            next: Some(NodeEdge::End(self)),
305        }
306    }
307
308    /// Detach a node from its parent and siblings. Children are not affected.
309    pub fn detach<T>(self, arena: &mut Arena<T>) {
310        let (parent, previous_sibling, next_sibling) = {
311            let node = &mut arena[self];
312            (node.parent.take(), node.previous_sibling.take(), node.next_sibling.take())
313        };
314
315        if let Some(next_sibling) = next_sibling {
316            arena[next_sibling].previous_sibling = previous_sibling;
317        } else if let Some(parent) = parent {
318            arena[parent].last_child = previous_sibling;
319        }
320
321        if let Some(previous_sibling) = previous_sibling {
322            arena[previous_sibling].next_sibling = next_sibling;
323        } else if let Some(parent) = parent {
324            arena[parent].first_child = next_sibling;
325        }
326    }
327
328    /// Append a new child to this node, after existing children.
329    pub fn append<T>(self, new_child: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
330        new_child.detach(arena);
331        let last_child_opt;
332        {
333            let (self_borrow , new_child_borrow) = arena.get_pair_mut(&self,
334                                                                      &new_child)?;
335            new_child_borrow.parent = Some(self);
336            last_child_opt = mem::replace(&mut self_borrow.last_child, Some(new_child));
337            if let Some(last_child) = last_child_opt {
338                new_child_borrow.previous_sibling = Some(last_child);
339            } else {
340                debug_assert!(self_borrow.first_child.is_none());
341                self_borrow.first_child = Some(new_child);
342            }
343        }
344        if let Some(last_child) = last_child_opt {
345            debug_assert!(arena[last_child].next_sibling.is_none());
346            arena[last_child].next_sibling = Some(new_child);
347        }
348
349        Ok(())
350    }
351
352    /// Prepend a new child to this node, before existing children.
353    pub fn prepend<T>(self, new_child: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
354        new_child.detach(arena);
355        let first_child_opt;
356        {
357            let (self_borrow, new_child_borrow) = arena.get_pair_mut(&self,
358                                                                     &new_child)?;
359        
360            new_child_borrow.parent = Some(self);
361            first_child_opt = mem::replace(&mut self_borrow.first_child, Some(new_child));
362            if let Some(first_child) = first_child_opt {
363                new_child_borrow.next_sibling = Some(first_child);
364            } else {
365                self_borrow.last_child = Some(new_child);
366                debug_assert!(&self_borrow.first_child.is_none());
367            }
368        }
369        if let Some(first_child) = first_child_opt {
370            debug_assert!(arena[first_child].previous_sibling.is_none());
371            arena[first_child].previous_sibling = Some(new_child);
372        }
373
374        Ok(())
375    }
376
377    /// Insert a new sibling after this node.
378    pub fn insert_after<T>(self, new_sibling: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
379        new_sibling.detach(arena);
380        let next_sibling_opt;
381        let parent_opt;
382        {
383            let (self_borrow, new_sibling_borrow) = arena.get_pair_mut(&self,
384                                                                      &new_sibling)?;
385        
386            parent_opt = self_borrow.parent;
387            new_sibling_borrow.parent = parent_opt;
388            new_sibling_borrow.previous_sibling = Some(self);
389            next_sibling_opt = mem::replace(&mut self_borrow.next_sibling, Some(new_sibling));
390            if let Some(next_sibling) = next_sibling_opt {
391                new_sibling_borrow.next_sibling = Some(next_sibling);
392            }
393        }
394        if let Some(next_sibling) = next_sibling_opt {
395            debug_assert!(arena[next_sibling].previous_sibling.unwrap() == self);
396            arena[next_sibling].previous_sibling = Some(new_sibling);
397        } else if let Some(parent) = parent_opt {
398            debug_assert!(arena[parent].last_child.unwrap() == self);
399            arena[parent].last_child = Some(new_sibling);
400        }
401
402        Ok(())
403    }
404
405    /// Insert a new sibling before this node.
406    pub fn insert_before<T>(self, new_sibling: NodeId, arena: &mut Arena<T>) -> Result<(), IndexTreeError> {
407        new_sibling.detach(arena);
408        let previous_sibling_opt;
409        let parent_opt;
410        {
411            let (self_borrow, new_sibling_borrow) = arena.get_pair_mut(&self,
412                                                                      &new_sibling)?;
413            parent_opt = self_borrow.parent;
414            new_sibling_borrow.parent = parent_opt;
415            new_sibling_borrow.next_sibling = Some(self);
416            previous_sibling_opt = mem::replace(&mut self_borrow.previous_sibling, Some(new_sibling));
417            if let Some(previous_sibling) = previous_sibling_opt {
418                new_sibling_borrow.previous_sibling = Some(previous_sibling);
419            }
420        }
421        if let Some(previous_sibling) = previous_sibling_opt {
422            debug_assert!(arena[previous_sibling].next_sibling.unwrap() == self);
423            arena[previous_sibling].next_sibling = Some(new_sibling);
424        } else if let Some(parent) = parent_opt {
425            debug_assert!(arena[parent].first_child.unwrap() == self);
426            arena[parent].first_child = Some(new_sibling);
427        }
428
429        Ok(())
430    }
431}
432
433macro_rules! impl_node_iterator {
434    ($name: ident, $next: expr) => {
435        impl<'a, T> Iterator for $name<'a, T> {
436            type Item = NodeId;
437
438            fn next(&mut self) -> Option<NodeId> {
439                match self.node.take() {
440                    Some(node) => {
441                        self.node = $next(&self.arena[node]);
442                        Some(node)
443                    }
444                    None => None
445                }
446            }
447        }
448    }
449}
450
451/// An iterator of references to the ancestors a given node.
452pub struct Ancestors<'a, T: 'a> {
453    arena: &'a Arena<T>,
454    node: Option<NodeId>,
455}
456impl_node_iterator!(Ancestors, |node: &Node<T>| node.parent);
457
458/// An iterator of references to the siblings before a given node.
459pub struct PrecedingSiblings<'a, T: 'a> {
460    arena: &'a Arena<T>,
461    node: Option<NodeId>,
462}
463impl_node_iterator!(PrecedingSiblings, |node: &Node<T>| node.previous_sibling);
464
465/// An iterator of references to the siblings after a given node.
466pub struct FollowingSiblings<'a, T: 'a> {
467    arena: &'a Arena<T>,
468    node: Option<NodeId>,
469}
470impl_node_iterator!(FollowingSiblings, |node: &Node<T>| node.next_sibling);
471
472/// An iterator of references to the children of a given node.
473pub struct Children<'a, T: 'a> {
474    arena: &'a Arena<T>,
475    node: Option<NodeId>,
476}
477impl_node_iterator!(Children, |node: &Node<T>| node.next_sibling);
478
479/// An iterator of references to the children of a given node, in reverse order.
480pub struct ReverseChildren<'a, T: 'a> {
481    arena: &'a Arena<T>,
482    node: Option<NodeId>,
483}
484impl_node_iterator!(ReverseChildren, |node: &Node<T>| node.previous_sibling);
485
486/// An iterator of references to a given node and its descendants, in tree order.
487pub struct Descendants<'a, T: 'a>(Traverse<'a, T>);
488
489impl<'a, T> Iterator for Descendants<'a, T> {
490    type Item = NodeId;
491
492    fn next(&mut self) -> Option<NodeId> {
493        loop {
494            match self.0.next() {
495                Some(NodeEdge::Start(node)) => return Some(node),
496                Some(NodeEdge::End(_)) => {}
497                None => return None,
498            }
499        }
500    }
501}
502
503#[derive(Debug, Clone)]
504/// Indicator if the node is at a start or endpoint of the tree
505pub enum NodeEdge<T> {
506    /// Indicates that start of a node that has children. Yielded by `Traverse::next` before the
507    /// node’s descendants. In HTML or XML, this corresponds to an opening tag like `<div>`
508    Start(T),
509
510    /// Indicates that end of a node that has children. Yielded by `Traverse::next` after the
511    /// node’s descendants. In HTML or XML, this corresponds to a closing tag like `</div>`
512    End(T),
513}
514
515
516/// An iterator of references to a given node and its descendants, in tree order.
517pub struct Traverse<'a, T: 'a> {
518    arena: &'a Arena<T>,
519    root: NodeId,
520    next: Option<NodeEdge<NodeId>>,
521}
522
523impl<'a, T> Iterator for Traverse<'a, T> {
524    type Item = NodeEdge<NodeId>;
525
526    fn next(&mut self) -> Option<NodeEdge<NodeId>> {
527        match self.next.take() {
528            Some(item) => {
529                self.next = match item {
530                    NodeEdge::Start(node) => {
531                        match self.arena[node].first_child {
532                            Some(first_child) => Some(NodeEdge::Start(first_child)),
533                            None => Some(NodeEdge::End(node)),
534                        }
535                    }
536                    NodeEdge::End(node) => {
537                        if node == self.root {
538                            None
539                        } else {
540                            match self.arena[node].next_sibling {
541                                Some(next_sibling) => Some(NodeEdge::Start(next_sibling)),
542                                None => {
543                                    match self.arena[node].parent {
544                                        Some(parent) => Some(NodeEdge::End(parent)),
545
546                                        // `node.parent()` here can only be `None`
547                                        // if the tree has been modified during iteration,
548                                        // but silently stoping iteration
549                                        // seems a more sensible behavior than panicking.
550                                        None => None,
551                                    }
552                                }
553                            }
554                        }
555                    }
556                };
557                Some(item)
558            }
559            None => None,
560        }
561    }
562}
563
564/// An iterator of references to a given node and its descendants, in reverse tree order.
565pub struct ReverseTraverse<'a, T: 'a> {
566    arena: &'a Arena<T>,
567    root: NodeId,
568    next: Option<NodeEdge<NodeId>>,
569}
570
571impl<'a, T> Iterator for ReverseTraverse<'a, T> {
572    type Item = NodeEdge<NodeId>;
573
574    fn next(&mut self) -> Option<NodeEdge<NodeId>> {
575        match self.next.take() {
576            Some(item) => {
577                self.next = match item {
578                    NodeEdge::End(node) => {
579                        match self.arena[node].last_child {
580                            Some(last_child) => Some(NodeEdge::End(last_child)),
581                            None => Some(NodeEdge::Start(node)),
582                        }
583                    }
584                    NodeEdge::Start(node) => {
585                        if node == self.root {
586                            None
587                        } else {
588                            match self.arena[node].previous_sibling {
589                                Some(previous_sibling) => Some(NodeEdge::End(previous_sibling)),
590                                None => {
591                                    match self.arena[node].parent {
592                                        Some(parent) => Some(NodeEdge::Start(parent)),
593
594                                        // `node.parent()` here can only be `None`
595                                        // if the tree has been modified during iteration,
596                                        // but silently stoping iteration
597                                        // seems a more sensible behavior than panicking.
598                                        None => None,
599                                    }
600                                }
601                            }
602                        }
603                    }
604                };
605                Some(item)
606            }
607            None => None,
608        }
609    }
610}