orx_tree/subtrees/
subtree.rs

1use crate::TreeVariant;
2
3pub(crate) mod sealed {
4
5    use crate::{
6        DepthFirstSequence, MemoryPolicy, NodeIdx, NodeMut, NodeMutOrientation, Tree, TreeVariant,
7        pinned_storage::PinnedStorage,
8    };
9    use orx_selfref_col::NodePtr;
10
11    pub trait SubTreeCore<Vs: TreeVariant>: Sized {
12        fn root_ptr(&self) -> NodePtr<Vs>;
13
14        fn root_parent_ptr(&self) -> Option<NodePtr<Vs>>;
15
16        fn root_sibling_idx(&self) -> usize;
17
18        fn create_subtree(&mut self) -> impl IntoIterator<Item = (usize, Vs::Item)>;
19
20        // provided methods
21
22        fn append_to_node_as_child<V, M, P, MO>(
23            mut self,
24            parent: &mut NodeMut<V, M, P, MO>,
25            child_position: usize,
26        ) -> NodeIdx<V>
27        where
28            V: TreeVariant<Item = Vs::Item>,
29            M: MemoryPolicy,
30            P: PinnedStorage,
31            MO: NodeMutOrientation,
32        {
33            let subtree = self.create_subtree();
34            parent.append_subtree_as_child(subtree, child_position)
35        }
36
37        fn into_new_tree<V2, M2, P2>(mut self) -> Tree<V2, M2, P2>
38        where
39            V2: TreeVariant<Item = Vs::Item>,
40            M2: MemoryPolicy,
41            P2: PinnedStorage,
42            P2::PinnedVec<V2>: Default,
43        {
44            let subtree = self.create_subtree();
45            let dfs = DepthFirstSequence::from(subtree);
46            Tree::try_from(dfs).expect("subtree is a valid depth first sequence")
47        }
48    }
49}
50
51/// A subtree is a subset of a tree, also having a single root and satisfying structural tree properties.
52///
53/// SubTree implementations are used to efficiently and conveniently move parts of the tree between different trees.
54pub trait SubTree<Vs>: sealed::SubTreeCore<Vs>
55where
56    Vs: TreeVariant,
57{
58}
59
60impl<Vs, S> SubTree<Vs> for S
61where
62    Vs: TreeVariant,
63    S: sealed::SubTreeCore<Vs>,
64{
65}