orx_tree/iter/
custom_walk.rs

1use crate::{
2    MemoryPolicy, Node, TreeVariant, aliases::Col, node_ref::NodeRefCore,
3    pinned_storage::PinnedStorage,
4};
5use orx_selfref_col::NodePtr;
6
7/// An iterator which can traverse the tree arbitrarily in any direction where the walk direction
8/// is determined by a custom `next_node` closure with signature `Fn(Node) -> Option(Node)`.
9pub struct CustomWalkIterPtr<'a, V, M, P, F>
10where
11    V: TreeVariant + 'a,
12    M: MemoryPolicy,
13    P: PinnedStorage,
14    F: Fn(Node<'a, V, M, P>) -> Option<Node<'a, V, M, P>>,
15{
16    col: &'a Col<V, M, P>,
17    current: Option<NodePtr<V>>,
18    next_node: F,
19}
20
21impl<'a, V, M, P, F> CustomWalkIterPtr<'a, V, M, P, F>
22where
23    V: TreeVariant + 'a,
24    M: MemoryPolicy,
25    P: PinnedStorage,
26    F: Fn(Node<'a, V, M, P>) -> Option<Node<'a, V, M, P>>,
27{
28    pub(crate) fn new(col: &'a Col<V, M, P>, current: Option<NodePtr<V>>, next_node: F) -> Self {
29        Self {
30            col,
31            current,
32            next_node,
33        }
34    }
35}
36
37impl<'a, V, M, P, F> Iterator for CustomWalkIterPtr<'a, V, M, P, F>
38where
39    V: TreeVariant + 'a,
40    M: MemoryPolicy,
41    P: PinnedStorage,
42    F: Fn(Node<'a, V, M, P>) -> Option<Node<'a, V, M, P>>,
43{
44    type Item = NodePtr<V>;
45
46    fn next(&mut self) -> Option<Self::Item> {
47        self.current.clone().inspect(|current| {
48            let node = Node::new(self.col, current.clone());
49            self.current = (self.next_node)(node).map(|x| x.node_ptr().clone());
50        })
51    }
52}