nb_tree/tree/iter/
absolute.rs1use crate::prelude::Path;
2
3use super::{depth::Traversal, TraversalNode};
4
5pub struct Absolute<I, B> {
6 iter: I,
7 path: Path<B>,
8}
9
10impl<I, B> Absolute<I, B>
11where
12 I: Iterator,
13{
14 pub fn new(iter: I) -> Absolute<I, B> {
15 Absolute {
16 iter,
17 path: Path::new(),
18 }
19 }
20}
21
22impl<N, I, B> Iterator for Absolute<I, B>
23where
24 I: Iterator<Item = Traversal<N, B>>,
25 B: Clone,
26{
27 type Item = TraversalNode<N, B>;
29
30 fn next(&mut self) -> Option<Self::Item> {
31 if let Some(item) = self.iter.next() {
32 Some(match item {
33 Traversal::Start(data) => TraversalNode {
34 path: Path::new(),
35 data,
36 },
37 Traversal::Step { up, branch, data } => {
38 (0..up).for_each(|_| {
39 self.path.pop_last();
40 });
41 self.path.push_last(branch.clone());
42 TraversalNode {
43 path: self.path.clone(),
44 data,
45 }
46 }
47 })
48 } else {
49 None
50 }
51 }
52}