pub enum NodeEdge {
Start(NodeId),
End(NodeId),
}
Expand description
Indicator if the node is at a start or endpoint of the tree
Variants§
Start(NodeId)
Indicates that start of a node that has children.
Yielded by Traverse::next()
before the node’s descendants. In HTML or
XML, this corresponds to an opening tag like <div>
.
End(NodeId)
Indicates that end of a node that has children.
Yielded by Traverse::next()
after the node’s descendants. In HTML or
XML, this corresponds to a closing tag like </div>
Implementations§
source§impl NodeEdge
impl NodeEdge
sourcepub fn next_traverse<T>(self, arena: &Arena<T>) -> Option<Self>
pub fn next_traverse<T>(self, arena: &Arena<T>) -> Option<Self>
Returns the next NodeEdge
to be returned by forward depth-first traversal.
§Examples
// arena
// `-- 1
// |-- 1_1
// | `-- 1_1_1
// |-- 1_2
// `-- 1_3
let steps = std::iter::successors(
Some(NodeEdge::Start(n1)),
|current| current.next_traverse(&arena)
)
.collect::<Vec<_>>();
let traversed_by_iter = n1.traverse(&arena).collect::<Vec<_>>();
assert_eq!(
steps,
traversed_by_iter,
"repeated `.next_traverse()`s emit same events as `NodeId::traverse()` iterator"
);
NodeEdge
itself does not borrow an arena, so you can modify the nodes
being traversed.
// arena: Arena<String>
// `-- 1
// |-- 1_1
// | `-- 1_1_1
// |-- 1_2
// `-- 1_3
assert_eq!(*arena[n1].get(), "1");
assert_eq!(*arena[n1_1_1].get(), "1_1_1");
assert_eq!(*arena[n1_3].get(), "1_3");
let mut next = Some(NodeEdge::Start(n1));
let mut count = 0;
while let Some(current) = next {
next = current.next_traverse(&arena);
let current = match current {
NodeEdge::Start(id) => id,
NodeEdge::End(_) => continue,
};
arena[current].get_mut().push_str(&format!(" (count={})", count));
count += 1;
}
assert_eq!(*arena[n1].get(), "1 (count=0)");
assert_eq!(*arena[n1_1_1].get(), "1_1_1 (count=2)");
assert_eq!(*arena[n1_3].get(), "1_3 (count=4)");
sourcepub fn prev_traverse<T>(self, arena: &Arena<T>) -> Option<Self>
pub fn prev_traverse<T>(self, arena: &Arena<T>) -> Option<Self>
Returns the previous NodeEdge
to be returned by forward depth-first traversal.
§Examples
// arena
// `-- 1
// |-- 1_1
// | `-- 1_1_1
// |-- 1_2
// `-- 1_3
let steps = std::iter::successors(
Some(NodeEdge::End(n1)),
|current| current.prev_traverse(&arena)
)
.collect::<Vec<_>>();
let traversed_by_iter = n1.reverse_traverse(&arena).collect::<Vec<_>>();
assert_eq!(
steps,
traversed_by_iter,
"repeated `.prev_traverse()`s emit same events as \
`NodeId::reverse_traverse()` iterator"
);
NodeEdge
itself does not borrow an arena, so you can modify the nodes
being traversed.
use indextree::{Arena, NodeEdge};
// arena: Arena<String>
// `-- 1
// |-- 1_1
// | `-- 1_1_1
// |-- 1_2
// `-- 1_3
assert_eq!(*arena[n1_3].get(), "1_3");
assert_eq!(*arena[n1_1_1].get(), "1_1_1");
assert_eq!(*arena[n1].get(), "1");
let mut next = Some(NodeEdge::End(n1_3));
let mut count = 0;
while let Some(current) = next {
next = current.prev_traverse(&arena);
let current = match current {
NodeEdge::Start(id) => id,
NodeEdge::End(_) => continue,
};
arena[current].get_mut().push_str(&format!(" (count={})", count));
count += 1;
}
assert_eq!(*arena[n1_3].get(), "1_3 (count=0)");
assert_eq!(*arena[n1_1_1].get(), "1_1_1 (count=2)");
assert_eq!(*arena[n1].get(), "1 (count=4)");
Trait Implementations§
source§impl PartialEq for NodeEdge
impl PartialEq for NodeEdge
impl Copy for NodeEdge
impl Eq for NodeEdge
impl StructuralPartialEq for NodeEdge
Auto Trait Implementations§
impl Freeze for NodeEdge
impl RefUnwindSafe for NodeEdge
impl Send for NodeEdge
impl Sync for NodeEdge
impl Unpin for NodeEdge
impl UnwindSafe for NodeEdge
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more