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
// Copyright (c) 2024 <Wei Li>.
//
// This source code is licensed under the GNU license found in the
// LICENSE file in the root directory of this source tree.

use core::ops::{Index, IndexMut};

type NodeId = usize;

/// The tree's node type.
pub struct Node<T> {
    pub(crate) parent: Option<NodeId>,
    /// Node id of this node's next sibling node
    pub(crate) next_sibling: Option<NodeId>,
    /// Node id of this node's first child node
    pub(crate) first_child: Option<NodeId>,
    /// Node id of this node's last child node
    pub(crate) last_child: Option<NodeId>,
    /// Associated tree data.
    pub(crate) data: T,
}

impl<T> Node<T> {
    fn new(data: T) -> Self {
        Node {
            parent: None,
            next_sibling: None,
            first_child: None,
            last_child: None,
            data: data,
        }
    }

    /// Returns a reference to the node data.
    pub fn get(&self) -> &T {
        &self.data
    }

    /// Returns a mutable reference to the node data.
    pub fn get_mut(&mut self) -> &mut T {
        &mut self.data
    }
}

/// A tree structure implemented using a single Vec and numerical identifiers (indices
/// in the vector) instead of reference counted pointers like.
pub struct IndexTree<T> {
    nodes: Vec<Node<T>>,
}

impl<T> IndexTree<T> {
    /// Creates a new empty Tree
    pub fn new_root(data: T) -> IndexTree<T> {
        let root = Node::new(data);
        IndexTree { nodes: vec![root] }
    }

    /// Counts the number of nodes
    #[inline]
    pub fn count(&self) -> usize {
        self.nodes.len()
    }

    /// Returns a reference to the node with the given id if in the tree.
    #[inline]
    pub fn get(&self, id: NodeId) -> Option<&Node<T>> {
        self.nodes.get(id)
    }

    /// Returns a mutable reference to the node with the given id if in the tree.
    #[inline]
    pub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node<T>> {
        self.nodes.get_mut(id)
    }

    /// Appends a new child to the node with parent_id, after existing children.
    pub fn add_child(&mut self, parent_id: NodeId, data: T) -> NodeId {
        let child_id = self.new_node(data);
        let parent = self.nodes.get_mut(parent_id).unwrap();
        match parent.last_child {
            None => {
                parent.first_child = Some(child_id);
                parent.last_child = Some(child_id);
            }
            Some(id) => {
                parent.last_child = Some(child_id);
                let last_child = self.nodes.get_mut(id).unwrap();
                last_child.next_sibling = Some(child_id);
            }
        }
        self.nodes.get_mut(child_id).unwrap().parent = Some(parent_id);
        child_id
    }

    /// Returns an iterator of IDs of a given node’s children.
    pub fn children(&self, id: NodeId) -> Children<'_, T> {
        Children::new(self, id)
    }

    /// Finds the child specified by the predicate.
    pub fn find_child<F>(&self, parent_id: NodeId, f: F) -> Option<NodeId>
    where
        F: Fn(&T) -> bool,
    {
        for child in self.children(parent_id) {
            if f(&self[child].data) {
                return Some(child);
            }
        }
        None
    }

    /// An iterator of the IDs of a given node and its descendants, as a pre-order
    /// depth-first search where children are visited in insertion order.
    pub fn descendants(&self, id: NodeId) -> Descendants<'_, T> {
        Descendants::new(self, id)
    }

    fn new_node(&mut self, data: T) -> NodeId {
        let index = self.nodes.len();
        let node = Node::new(data);
        self.nodes.push(node);
        index
    }
}

impl<T> Index<NodeId> for IndexTree<T> {
    type Output = Node<T>;

    fn index(&self, id: NodeId) -> &Node<T> {
        &self.nodes[id]
    }
}

impl<T> IndexMut<NodeId> for IndexTree<T> {
    fn index_mut(&mut self, id: NodeId) -> &mut Node<T> {
        &mut self.nodes[id]
    }
}

macro_rules! impl_node_iterator {
    ($name:ident, $next:expr) => {
        impl<'a, T> Iterator for $name<'a, T> {
            type Item = NodeId;

            fn next(&mut self) -> Option<NodeId> {
                let node = self.node.take()?;
                self.node = $next(&self.tree[node]);
                Some(node)
            }
        }
    };
}

/// An iterator of the IDs of the children of a given node, in insertion order.
pub struct Children<'a, T> {
    tree: &'a IndexTree<T>,
    node: Option<NodeId>,
}

impl<'a, T> Children<'a, T> {
    pub fn new(tree: &'a IndexTree<T>, current: NodeId) -> Self {
        Self {
            tree,
            node: tree[current].first_child,
        }
    }
}

impl_node_iterator!(Children, |node: &Node<T>| node.next_sibling);

/// An iterator of the IDs of a given node and its descendants, as a pre-order depth-first search where children are visited in insertion order.
///
/// i.e. node -> first child -> second child
pub struct Descendants<'a, T>(Traverse<'a, T>, NodeId);

impl<'a, T> Descendants<'a, T> {
    pub(crate) fn new(tree: &'a IndexTree<T>, root: NodeId) -> Self {
        Self(Traverse::new(tree, root), root)
    }
}

impl<'a, T> Iterator for Descendants<'a, T> {
    type Item = NodeId;

    fn next(&mut self) -> Option<NodeId> {
        self.0.find_map(|edge| match edge {
            NodeEdge::Start(node) if node == self.1 => None,
            NodeEdge::Start(node) => Some(node),
            NodeEdge::End(_) => None,
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// Indicator if the node is at a start or endpoint of the tree
pub enum NodeEdge {
    /// Indicates that start of a node that has children.
    ///
    /// Yielded by `Traverse::next()` before the node’s descendants.
    Start(NodeId),

    /// Indicates that end of a node that has children.
    ///
    /// Yielded by `Traverse::next()` after the node’s descendants.
    End(NodeId),
}

#[derive(Clone)]
/// An iterator of the "sides" of a node visited during a depth-first pre-order traversal,
/// where node sides are visited start to end and children are visited in insertion order.
///
/// i.e. node.start -> first child -> second child -> node.end
pub struct Traverse<'a, T> {
    tree: &'a IndexTree<T>,
    root: NodeId,
    next: Option<NodeEdge>,
}

impl<'a, T> Traverse<'a, T> {
    pub(crate) fn new(tree: &'a IndexTree<T>, root: NodeId) -> Self {
        Self {
            tree,
            root,
            next: Some(NodeEdge::Start(root)),
        }
    }

    /// Calculates the next node.
    fn next_of_next(&mut self, next: NodeEdge) -> Option<NodeEdge> {
        match next {
            NodeEdge::Start(node) => match self.tree[node].first_child {
                Some(first_child) => Some(NodeEdge::Start(first_child)),
                None => Some(NodeEdge::End(node)),
            },
            NodeEdge::End(node) => {
                if node == self.root {
                    return None;
                }
                let node = &self.tree[node];
                match node.next_sibling {
                    Some(next_sibling) => Some(NodeEdge::Start(next_sibling)),
                    None => node.parent.map(NodeEdge::End),
                }
            }
        }
    }
}

impl<'a, T> Iterator for Traverse<'a, T> {
    type Item = NodeEdge;

    fn next(&mut self) -> Option<NodeEdge> {
        let next = self.next.take()?;
        self.next = self.next_of_next(next);
        Some(next)
    }
}

#[test]
fn index_tree_tests() {
    let mut tree = IndexTree::<u32>::new_root(0);
    tree.add_child(0, 1);
    tree.add_child(0, 2);

    let node = &tree[1];
    assert_eq!(node.data, 1);
    tree.add_child(1, 12);
    tree.add_child(1, 14);
    tree.add_child(1, 15);
    tree.add_child(2, 21);
    tree.add_child(2, 25);
    tree.add_child(2, 27);

    assert_eq!(tree.count(), 9);
    assert_eq!(tree[4].next_sibling, Some(5));
    let node = tree.find_child(1, |&data| data == 14).unwrap();
    assert_eq!(node, 4);
    tree.add_child(node, 143);
    tree.add_child(node, 147);
    let mut children = tree.children(node);
    assert_eq!(tree[children.next().unwrap()].data, 143);
    assert_eq!(tree[children.next().unwrap()].data, 147);
    assert_eq!(children.next(), None);
    let mut descendants = tree.descendants(0);
    // assert_eq!(tree[descendants.next().unwrap()].data, 0);
    assert_eq!(tree[descendants.next().unwrap()].data, 1);
    assert_eq!(tree[descendants.next().unwrap()].data, 12);
    assert_eq!(tree[descendants.next().unwrap()].data, 14);
    assert_eq!(tree[descendants.next().unwrap()].data, 143);
    assert_eq!(tree[descendants.next().unwrap()].data, 147);
    assert_eq!(tree[descendants.next().unwrap()].data, 15);
    assert_eq!(tree[descendants.next().unwrap()].data, 2);
    assert_eq!(tree[descendants.next().unwrap()].data, 21);
    assert_eq!(tree[descendants.next().unwrap()].data, 25);
    assert_eq!(tree[descendants.next().unwrap()].data, 27);
    assert_eq!(descendants.next(), None);
}