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.inspect(|&ptr| {
28            let node = unsafe { &*ptr.ptr() };
29
30            self.current = match ptr == self.root_ptr {
31                false => node.prev().get(),
32                true => None,
33            };
34        })
35    }
36}
37
38impl<V: TreeVariant> Clone for AncestorsIterPtr<V> {
39    fn clone(&self) -> Self {
40        Self {
41            root_ptr: self.root_ptr,
42            current: self.current,
43        }
44    }
45}
46
47unsafe impl<V: TreeVariant> Send for AncestorsIterPtr<V> where V::Item: Send {}
48
49unsafe impl<V: TreeVariant> Sync for AncestorsIterPtr<V> where V::Item: Sync {}