mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Add AncestorIterator by jgraham
This commit is contained in:
parent
bcd7c0b8c6
commit
47c5279e31
1 changed files with 24 additions and 0 deletions
|
@ -42,6 +42,23 @@ impl<Node, Ref: TreeNodeRef<Node>> Iterator<Ref> for ChildIterator<Ref> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct AncestorIterator<Ref> {
|
||||||
|
priv current: Option<Ref>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Node, Ref: TreeNodeRef<Node>> Iterator<Ref> for AncestorIterator<Ref> {
|
||||||
|
fn next(&mut self) -> Option<Ref> {
|
||||||
|
if self.current.is_none() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Do we need two clones here?
|
||||||
|
let x = self.current.get_ref().clone();
|
||||||
|
self.current = x.with_base(|n| TreeNodeRef::<Node>::parent_node(n));
|
||||||
|
Some(x.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: Do this without precomputing a vector of refs.
|
// FIXME: Do this without precomputing a vector of refs.
|
||||||
// Easy for preorder; harder for postorder.
|
// Easy for preorder; harder for postorder.
|
||||||
pub struct TreeIterator<Ref> {
|
pub struct TreeIterator<Ref> {
|
||||||
|
@ -196,6 +213,13 @@ pub trait TreeNodeRef<Node>: Clone {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterates over all ancestors of this node.
|
||||||
|
fn ancestors(&self) -> AncestorIterator<Self> {
|
||||||
|
AncestorIterator {
|
||||||
|
current: self.with_base(|n| get!(n, parent_node)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Iterates over this node and all its descendants, in preorder.
|
/// Iterates over this node and all its descendants, in preorder.
|
||||||
fn traverse_preorder(&self) -> TreeIterator<Self> {
|
fn traverse_preorder(&self) -> TreeIterator<Self> {
|
||||||
self.traverse_preorder_prune(|_| false)
|
self.traverse_preorder_prune(|_| false)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue