1use std::cell::Cell;
19use std::fmt;
20
21pub struct Node<'a, T: 'a> {
23 parent: Cell<Option<&'a Node<'a, T>>>,
24 previous_sibling: Cell<Option<&'a Node<'a, T>>>,
25 next_sibling: Cell<Option<&'a Node<'a, T>>>,
26 first_child: Cell<Option<&'a Node<'a, T>>>,
27 last_child: Cell<Option<&'a Node<'a, T>>>,
28
29 pub data: T,
31}
32
33impl<'a, T: 'a> fmt::Debug for Node<'a, T>
36where
37 T: fmt::Debug,
38{
39 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
40 struct Children<'a, T>(Option<&'a Node<'a, T>>);
41 impl<T: fmt::Debug> fmt::Debug for Children<'_, T> {
42 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
43 f.debug_list()
44 .entries(std::iter::successors(self.0, |child| {
45 child.next_sibling.get()
46 }))
47 .finish()
48 }
49 }
50
51 let mut struct_fmt = f.debug_struct("Node");
52 struct_fmt.field("data", &self.data);
53 struct_fmt.field("children", &Children(self.first_child.get()));
54 struct_fmt.finish()?;
55
56 Ok(())
57 }
58}
59
60impl<'a, T> Node<'a, T> {
61 pub fn new(data: T) -> Node<'a, T> {
66 Node {
67 parent: Cell::new(None),
68 first_child: Cell::new(None),
69 last_child: Cell::new(None),
70 previous_sibling: Cell::new(None),
71 next_sibling: Cell::new(None),
72 data,
73 }
74 }
75
76 pub fn parent(&self) -> Option<&'a Node<'a, T>> {
78 self.parent.get()
79 }
80
81 pub fn first_child(&self) -> Option<&'a Node<'a, T>> {
83 self.first_child.get()
84 }
85
86 pub fn last_child(&self) -> Option<&'a Node<'a, T>> {
88 self.last_child.get()
89 }
90
91 pub fn previous_sibling(&self) -> Option<&'a Node<'a, T>> {
93 self.previous_sibling.get()
94 }
95
96 pub fn next_sibling(&self) -> Option<&'a Node<'a, T>> {
98 self.next_sibling.get()
99 }
100
101 pub fn same_node(&self, other: &Node<'a, T>) -> bool {
103 std::ptr::eq(self, other)
104 }
105
106 pub fn ancestors(&'a self) -> Ancestors<'a, T> {
110 Ancestors(Some(self))
111 }
112
113 pub fn preceding_siblings(&'a self) -> PrecedingSiblings<'a, T> {
117 PrecedingSiblings(Some(self))
118 }
119
120 pub fn following_siblings(&'a self) -> FollowingSiblings<'a, T> {
124 FollowingSiblings(Some(self))
125 }
126
127 pub fn children(&'a self) -> Children<'a, T> {
129 Children(self.first_child.get())
130 }
131
132 pub fn reverse_children(&'a self) -> ReverseChildren<'a, T> {
134 ReverseChildren(self.last_child.get())
135 }
136
137 pub fn descendants(&'a self) -> Descendants<'a, T> {
145 Descendants(self.traverse())
146 }
147
148 pub fn traverse(&'a self) -> Traverse<'a, T> {
155 Traverse {
156 root: self,
157 next: Some(NodeEdge::Start(self)),
158 }
159 }
160
161 pub fn reverse_traverse(&'a self) -> ReverseTraverse<'a, T> {
168 ReverseTraverse {
169 root: self,
170 next: Some(NodeEdge::End(self)),
171 }
172 }
173
174 pub fn detach(&self) {
176 let parent = self.parent.take();
177 let previous_sibling = self.previous_sibling.take();
178 let next_sibling = self.next_sibling.take();
179
180 if let Some(next_sibling) = next_sibling {
181 next_sibling.previous_sibling.set(previous_sibling);
182 } else if let Some(parent) = parent {
183 parent.last_child.set(previous_sibling);
184 }
185
186 if let Some(previous_sibling) = previous_sibling {
187 previous_sibling.next_sibling.set(next_sibling);
188 } else if let Some(parent) = parent {
189 parent.first_child.set(next_sibling);
190 }
191 }
192
193 pub fn append(&'a self, new_child: &'a Node<'a, T>) {
195 new_child.detach();
196 new_child.parent.set(Some(self));
197 if let Some(last_child) = self.last_child.take() {
198 new_child.previous_sibling.set(Some(last_child));
199 debug_assert!(last_child.next_sibling.get().is_none());
200 last_child.next_sibling.set(Some(new_child));
201 } else {
202 debug_assert!(self.first_child.get().is_none());
203 self.first_child.set(Some(new_child));
204 }
205 self.last_child.set(Some(new_child));
206 }
207
208 pub fn prepend(&'a self, new_child: &'a Node<'a, T>) {
210 new_child.detach();
211 new_child.parent.set(Some(self));
212 if let Some(first_child) = self.first_child.take() {
213 debug_assert!(first_child.previous_sibling.get().is_none());
214 first_child.previous_sibling.set(Some(new_child));
215 new_child.next_sibling.set(Some(first_child));
216 } else {
217 debug_assert!(self.first_child.get().is_none());
218 self.last_child.set(Some(new_child));
219 }
220 self.first_child.set(Some(new_child));
221 }
222
223 pub fn insert_after(&'a self, new_sibling: &'a Node<'a, T>) {
225 new_sibling.detach();
226 new_sibling.parent.set(self.parent.get());
227 new_sibling.previous_sibling.set(Some(self));
228 if let Some(next_sibling) = self.next_sibling.take() {
229 debug_assert!(std::ptr::eq(
230 next_sibling.previous_sibling.get().unwrap(),
231 self
232 ));
233 next_sibling.previous_sibling.set(Some(new_sibling));
234 new_sibling.next_sibling.set(Some(next_sibling));
235 } else if let Some(parent) = self.parent.get() {
236 debug_assert!(std::ptr::eq(parent.last_child.get().unwrap(), self));
237 parent.last_child.set(Some(new_sibling));
238 }
239 self.next_sibling.set(Some(new_sibling));
240 }
241
242 pub fn insert_before(&'a self, new_sibling: &'a Node<'a, T>) {
244 new_sibling.detach();
245 new_sibling.parent.set(self.parent.get());
246 new_sibling.next_sibling.set(Some(self));
247 if let Some(previous_sibling) = self.previous_sibling.take() {
248 new_sibling.previous_sibling.set(Some(previous_sibling));
249 debug_assert!(std::ptr::eq(
250 previous_sibling.next_sibling.get().unwrap(),
251 self
252 ));
253 previous_sibling.next_sibling.set(Some(new_sibling));
254 } else if let Some(parent) = self.parent.get() {
255 debug_assert!(std::ptr::eq(parent.first_child.get().unwrap(), self));
256 parent.first_child.set(Some(new_sibling));
257 }
258 self.previous_sibling.set(Some(new_sibling));
259 }
260}
261
262macro_rules! axis_iterator {
263 (#[$attr:meta] $name:ident : $next:ident) => {
264 #[$attr]
265 #[derive(Debug)]
266 pub struct $name<'a, T: 'a>(Option<&'a Node<'a, T>>);
267
268 impl<'a, T> Iterator for $name<'a, T> {
269 type Item = &'a Node<'a, T>;
270
271 fn next(&mut self) -> Option<&'a Node<'a, T>> {
272 match self.0.take() {
273 Some(node) => {
274 self.0 = node.$next.get();
275 Some(node)
276 }
277 None => None,
278 }
279 }
280 }
281 };
282}
283
284axis_iterator! {
285 #[doc = "An iterator of references to the ancestors a given node."]
286 Ancestors: parent
287}
288
289axis_iterator! {
290 #[doc = "An iterator of references to the siblings before a given node."]
291 PrecedingSiblings: previous_sibling
292}
293
294axis_iterator! {
295 #[doc = "An iterator of references to the siblings after a given node."]
296 FollowingSiblings: next_sibling
297}
298
299axis_iterator! {
300 #[doc = "An iterator of references to the children of a given node."]
301 Children: next_sibling
302}
303
304axis_iterator! {
305 #[doc = "An iterator of references to the children of a given node, in reverse order."]
306 ReverseChildren: previous_sibling
307}
308
309#[derive(Debug)]
311pub struct Descendants<'a, T: 'a>(Traverse<'a, T>);
312
313impl<'a, T> Iterator for Descendants<'a, T> {
314 type Item = &'a Node<'a, T>;
315
316 fn next(&mut self) -> Option<&'a Node<'a, T>> {
317 loop {
318 match self.0.next() {
319 Some(NodeEdge::Start(node)) => return Some(node),
320 Some(NodeEdge::End(_)) => {}
321 None => return None,
322 }
323 }
324 }
325}
326
327#[derive(Debug, Clone)]
329pub enum NodeEdge<T> {
330 Start(T),
334
335 End(T),
339}
340
341macro_rules! traverse_iterator {
342 (#[$attr:meta] $name:ident : $first_child:ident, $next_sibling:ident) => {
343 #[$attr]
344 #[derive(Debug)]
345 pub struct $name<'a, T: 'a> {
346 root: &'a Node<'a, T>,
347 next: Option<NodeEdge<&'a Node<'a, T>>>,
348 }
349
350 impl<'a, T> Iterator for $name<'a, T> {
351 type Item = NodeEdge<&'a Node<'a, T>>;
352
353 fn next(&mut self) -> Option<NodeEdge<&'a Node<'a, T>>> {
354 match self.next.take() {
355 Some(item) => {
356 self.next = match item {
357 NodeEdge::Start(node) => match node.$first_child.get() {
358 Some(child) => Some(NodeEdge::Start(child)),
359 None => Some(NodeEdge::End(node)),
360 },
361 NodeEdge::End(node) => {
362 if node.same_node(self.root) {
363 None
364 } else {
365 match node.$next_sibling.get() {
366 Some(sibling) => Some(NodeEdge::Start(sibling)),
367 None => match node.parent.get() {
368 Some(parent) => Some(NodeEdge::End(parent)),
369 None => panic!("tree modified during iteration"),
370 },
371 }
372 }
373 }
374 };
375 Some(item)
376 }
377 None => None,
378 }
379 }
380 }
381 };
382}
383
384traverse_iterator! {
385 #[doc = "An iterator of the start and end edges of a given
386 node and its descendants, in tree order."]
387 Traverse: first_child, next_sibling
388}
389
390traverse_iterator! {
391 #[doc = "An iterator of the start and end edges of a given
392 node and its descendants, in reverse tree order."]
393 ReverseTraverse: last_child, previous_sibling
394}
395
396#[test]
397fn it_works() {
398 struct DropTracker<'a>(&'a Cell<u32>);
399 impl<'a> Drop for DropTracker<'a> {
400 fn drop(&mut self) {
401 self.0.set(self.0.get() + 1);
402 }
403 }
404
405 let drop_counter = Cell::new(0);
406 {
407 let mut new_counter = 0;
408 let arena = typed_arena::Arena::new();
409 let mut new = || {
410 new_counter += 1;
411 arena.alloc(Node::new((new_counter, DropTracker(&drop_counter))))
412 };
413
414 let a = new(); a.append(new()); a.append(new()); a.prepend(new()); let b = new(); b.append(a);
420 a.insert_before(new()); a.insert_before(new()); a.insert_after(new()); a.insert_after(new()); let c = new(); b.append(c);
426
427 assert_eq!(drop_counter.get(), 0);
428 c.previous_sibling.get().unwrap().detach();
429 assert_eq!(drop_counter.get(), 0);
430
431 assert_eq!(
432 b.descendants().map(|node| node.data.0).collect::<Vec<_>>(),
433 [5, 6, 7, 1, 4, 2, 3, 9, 10]
434 );
435 }
436
437 assert_eq!(drop_counter.get(), 10);
438}