orx_tree/iter/
ancestors.rs

1use crate::TreeVariant;
2use orx_selfref_col::NodePtr;
3
4/// Ancestors iterators which starts from a node and yields nodes bottom-up until the root
5/// of the tree is reached.
6pub struct AncestorsIterPtr<V>
7where
8    V: TreeVariant,
9{
10    root_ptr: NodePtr<V>,
11    current: Option<NodePtr<V>>,
12}
13
14impl<V: TreeVariant> AncestorsIterPtr<V> {
15    pub(crate) fn new(root_ptr: NodePtr<V>, descendant_ptr: NodePtr<V>) -> Self {
16        Self {
17            root_ptr,
18            current: Some(descendant_ptr),
19        }
20    }
21}
22
23impl<V: TreeVariant> Iterator for AncestorsIterPtr<V> {
24    type Item = NodePtr<V>;
25
26    fn next(&mut self) -> Option<Self::Item> {
27        self.current.clone().map(|ptr| {
28            let node = unsafe { &*ptr.ptr() };
29
30            self.current = match ptr == self.root_ptr {
31                false => node.prev().get().cloned(),
32                true => None,
33            };
34
35            ptr
36        })
37    }
38}
39
40impl<V: TreeVariant> Clone for AncestorsIterPtr<V> {
41    fn clone(&self) -> Self {
42        Self {
43            root_ptr: self.root_ptr.clone(),
44            current: self.current.clone(),
45        }
46    }
47}
48
49unsafe impl<V: TreeVariant> Send for AncestorsIterPtr<V> where V::Item: Send {}
50
51unsafe impl<V: TreeVariant> Sync for AncestorsIterPtr<V> where V::Item: Sync {}