orx_tree/dyn/
aliases.rs

1use super::Dyn;
2use crate::{Node, Tree, memory::Auto, pinned_storage::SplitRecursive};
3
4/// A dynamic tree where each of the nodes might have any number of child nodes.
5///
6/// # Examples
7///
8/// ```
9/// use orx_tree::*;
10///
11/// // # A. BUILDING A TREE
12///
13/// //      1
14/// //     ╱ ╲
15/// //    ╱   ╲
16/// //   2     3
17/// //  ╱ ╲   ╱ ╲
18/// // 4   5 6   7
19/// // |     |  ╱ ╲
20/// // 8     9 10  11
21///
22/// let mut tree = DynTree::new(1i32);
23///
24/// let mut root = tree.root_mut();
25/// let [id2, id3] = root.push_children([2, 3]);
26/// let [id4, _] = tree.node_mut(&id2).push_children([4, 5]);
27/// let id8 = tree.node_mut(&id4).push_child(8);
28/// let [id6, id7] = tree.node_mut(&id3).push_children([6, 7]);
29/// let id9 = tree.node_mut(&id6).push_child(9);
30/// tree.node_mut(&id7).push_children([10, 11]);
31///
32/// // traversals
33///
34/// let bfs: Vec<_> = tree.root().walk::<Bfs>().copied().collect();
35/// assert_eq!(bfs, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
36///
37/// let dfs: Vec<_> = tree.node(&id3).walk::<Dfs>().copied().collect();
38/// assert_eq!(dfs, [3, 6, 9, 7, 10, 11]);
39///
40/// let post_order: Vec<_> = tree.node(&id3).walk::<PostOrder>().copied().collect();
41/// assert_eq!(post_order, [9, 6, 10, 11, 7, 3]);
42///
43/// let leaves: Vec<_> = tree.root().leaves::<Dfs>().copied().collect();
44/// assert_eq!(leaves, [8, 5, 9, 10, 11]);
45///
46/// let node3 = tree.node(&id3);
47/// let paths: Vec<Vec<_>> = node3.paths::<Bfs>().map(|p| p.copied().collect()).collect();
48/// assert_eq!(paths, [[9, 6, 3], [10, 7, 3], [11, 7, 3]]);
49/// ```
50///
51/// # Type Aliases and Generic Parameters
52///
53/// Below is the list of pairs of tree & node type aliases from the simplest to the most complex.
54///
55/// Note that the generic parameter `P` can almost always be omitted since the default storage is almost always preferable.
56///
57/// Generic parameter `M` can also be omitted in most cases to use the default auto reclaim policy.
58/// Therefore, we can use the simplest type signatures.
59/// However, in certain situations it is preferable to use the *never* reclaim policy which guarantees that the node indices
60/// will always remain valid.
61///
62/// Please see the relevant documentations of [`NodeIdx`] and [`MemoryPolicy`].
63///
64/// [`NodeIdx`]: crate::NodeIdx
65/// [`MemoryPolicy`]: crate::MemoryPolicy
66///
67/// *Type aliases with default pinned vector storage and default memory reclaim policy:*
68///
69/// ```ignore
70/// DynTree<T>     ==> Tree<Dyn<T>>
71/// DynNode<'a, T> ==> Node<'a, Dyn<T>>
72/// ```
73///
74/// *Type aliases with default pinned vector storage (recommended):*
75///
76/// ```ignore
77/// DynTree<T, M>     ==> Tree<Dyn<T>, M>
78/// DynNode<'a, T, M> ==> Node<'a, Dyn<T>, M>
79/// ```
80///
81/// *The most general type aliases, by explicitly setting a PinnedVec*
82///
83/// ```ignore
84/// DynTree<T, M, P>     ==> Tree<Dyn<T>, M, P>
85/// DynNode<'a, T, M, P> ==> Node<'a, Dyn<T>, M, P>
86/// ```
87pub type DynTree<T, M = Auto, P = SplitRecursive> = Tree<Dyn<T>, M, P>;
88
89/// Node of a [`DynTree`].
90pub type DynNode<'a, T, M = Auto, P = SplitRecursive> = Node<'a, Dyn<T>, M, P>;