From ec296cb8c38a5826000b3e1c12ea9b5f6c8c31e2 Mon Sep 17 00:00:00 2001 From: Mykola Humanov Date: Sat, 26 Oct 2024 12:33:11 +0300 Subject: [PATCH] src/matcher.rs: revised Iterator for Matches --- src/matcher.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/matcher.rs b/src/matcher.rs index 4c2137c..1b81e8c 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -99,28 +99,19 @@ impl<'a, 'b> Iterator for Matches<'a, NodeRef<'b, NodeData>> { fn next(&mut self) -> Option { 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; @@ -134,6 +125,10 @@ impl<'a, 'b> Iterator for Matches<'a, NodeRef<'b, NodeData>> { return Some(node); } } + + if self.roots.is_empty() { + return None; + } } } }