Implement an iter_until.

This commit is contained in:
Tom Alexander 2022-12-15 23:52:52 -05:00
parent 0d2dfe3a14
commit 0504c48b66
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 45 additions and 0 deletions

View File

@ -84,6 +84,13 @@ impl<T: Debug> List<T> {
position: &self.head,
}
}
pub fn iter_until<'a>(&'a self, other: &'a List<T>) -> impl Iterator<Item = &Rc<Node<T>>> {
NodeIterUntil {
position: &self.head,
stop: &other.head,
}
}
}
pub struct NodeIter<'a, T> {
@ -106,3 +113,34 @@ impl<'a, T> Iterator for NodeIter<'a, T> {
Some(return_value)
}
}
pub struct NodeIterUntil<'a, T> {
position: &'a Option<Rc<Node<T>>>,
stop: &'a Option<Rc<Node<T>>>,
}
impl<'a, T> Iterator for NodeIterUntil<'a, T> {
type Item = &'a Rc<Node<T>>;
fn next(&mut self) -> Option<Self::Item> {
match (self.position, self.stop) {
(_, None) => {}
(None, _) => {}
(Some(this_rc), Some(stop_rc)) => {
if Rc::ptr_eq(this_rc, stop_rc) {
return None;
}
}
};
let (return_value, next_position) = match &self.position {
None => return None,
Some(rc_node) => {
let next_position = &rc_node.parent;
let return_value = rc_node;
(return_value, next_position)
}
};
self.position = next_position;
Some(return_value)
}
}

View File

@ -43,6 +43,13 @@ impl<'r, 's> ContextTree<'r, 's> {
self.tree.iter()
}
pub fn iter_until<'x: 'r>(
&'r self,
other: &'x ContextTree<'x, 's>,
) -> impl Iterator<Item = &Rc<Node<ContextElement<'r, 's>>>> {
self.tree.iter_until(&other.tree)
}
pub fn check_exit_matcher(
&'r self,
i: &'s str,