Skip to content

Commit

Permalink
src/matcher.rs: revised Iterator for Matches
Browse files Browse the repository at this point in the history
  • Loading branch information
niklak committed Oct 26, 2024
1 parent c8625f9 commit ec296cb
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,19 @@ impl<'a, 'b> Iterator for Matches<'a, NodeRef<'b, NodeData>> {
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.nodes.is_empty() {
if self.roots.is_empty() {
return None;
}

let root = self.roots.remove(0);

let root = self.roots.pop()?;
match self.match_scope {
MatchScope::IncludeNode => self.nodes.insert(0, root),
MatchScope::IncludeNode => {
self.nodes.push(root);
}
MatchScope::ChildrenOnly => {
for child in root.children().into_iter().rev() {
self.nodes.insert(0, child);
}
self.nodes.extend(root.children().into_iter().rev());
}
}
}

while !self.nodes.is_empty() {
let node = self.nodes.remove(0);

for node in node.children().into_iter().rev() {
self.nodes.insert(0, node);
}
while let Some(node) = self.nodes.pop() {
self.nodes.extend(node.children().into_iter().rev());

if self.set.contains(&node.id) {
continue;
Expand All @@ -134,6 +125,10 @@ impl<'a, 'b> Iterator for Matches<'a, NodeRef<'b, NodeData>> {
return Some(node);
}
}

if self.roots.is_empty() {
return None;
}
}
}
}
Expand Down

0 comments on commit ec296cb

Please sign in to comment.