Skip to main content

tree_iterators_rs/dfs_postorder_iterators/
mod.rs

1pub mod borrow;
2pub mod mut_borrow;
3pub mod owned;
4
5macro_rules! dfs_postorder_next {
6    ($get_value_and_children: ident) => {
7        fn next(&mut self) -> Option<Self::Item> {
8            loop {
9                if let Some(root) = self.root.take() {
10                    let (value, children) = root.$get_value_and_children();
11                    self.traversal_stack.push(children.into_iter());
12                    self.item_stack.push(value);
13                    continue;
14                }
15
16                loop {
17                    if let Some(last) = self.traversal_stack.last_mut() {
18                        if let Some(next) = last.next() {
19                            let (value, children) = next.$get_value_and_children();
20                            self.item_stack.push(value);
21                            self.traversal_stack.push(children.into_iter());
22                            continue;
23                        }
24
25                        self.traversal_stack.pop();
26                    }
27
28                    return self.item_stack.pop();
29                }
30            }
31        }
32    };
33}
34
35macro_rules! get_mut_context {
36    () => {
37        fn get_mut(&mut self) -> Option<&mut Self::Item> {
38            if self.current_context.ancestors.is_empty() {
39                None
40            } else {
41                Some(&mut self.current_context)
42            }
43        }
44    };
45}
46
47macro_rules! postorder_ancestors_streaming_iterator_impl {
48    ($get_value_and_children: ident) => {
49        fn advance(&mut self) {
50            if let Some(next) = self.root.take() {
51                let (value, children) = next.$get_value_and_children();
52                self.traversal_stack.push(children.into_iter());
53                self.item_stack.push(value);
54            } else {
55                self.item_stack.pop();
56            }
57
58            while let Some(top) = self.traversal_stack.last_mut() {
59                if let Some(node) = top.next() {
60                    let (value, children) = node.$get_value_and_children();
61
62                    self.traversal_stack.push(children.into_iter());
63                    self.item_stack.push(value);
64                    continue;
65                }
66
67                self.traversal_stack.pop();
68                break;
69            }
70        }
71
72        fn get(&self) -> Option<&Self::Item> {
73            if self.item_stack.len() > 0 {
74                Some(&self.item_stack.as_slice())
75            } else {
76                None
77            }
78        }
79    };
80}
81
82macro_rules! get_mut_ancestors {
83    () => {
84        fn get_mut(&mut self) -> Option<&mut Self::Item> {
85            if self.item_stack.len() == 0 {
86                None
87            } else {
88                Some(&mut self.item_stack[..])
89            }
90        }
91    };
92}
93
94pub(crate) use dfs_postorder_next;
95pub(crate) use get_mut_ancestors;
96pub(crate) use get_mut_context;
97pub(crate) use postorder_ancestors_streaming_iterator_impl;