orx_tree/common_traits/
debug.rs

1use crate::{
2    Node, NodeMut, NodeRef, Traversal, Traverser, Tree, TreeVariant, memory::MemoryPolicy,
3    pinned_storage::PinnedStorage, traversal::traverser_core::TraverserCore,
4};
5use core::fmt::Debug;
6
7impl<V, M, P> Debug for Tree<V, M, P>
8where
9    V: TreeVariant,
10    M: MemoryPolicy,
11    P: PinnedStorage,
12    V::Item: Debug,
13{
14    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
15        fn safe_avg(a: usize, b: usize) -> f64 {
16            match b {
17                0 => 0.0,
18                _ => (100 * a / b) as f64 / 100.0,
19            }
20        }
21
22        let mut depth_first_sequence = alloc::vec::Vec::new();
23        let mut t = Traversal.dfs().over_nodes().with_depth();
24        let root = self.get_root();
25        let mut num_leaves = 0usize;
26        let mut max_depth = 0usize;
27        let mut total_num_children = 0usize;
28        let mut total_depth = 0usize;
29
30        if let Some(root) = root.as_ref() {
31            for (depth, node) in t.iter(root) {
32                depth_first_sequence.push((depth, node.data()));
33
34                total_num_children += node.num_children();
35                total_depth += depth;
36
37                if depth > max_depth {
38                    max_depth = depth;
39                }
40
41                if node.is_leaf() {
42                    num_leaves += 1;
43                }
44            }
45        }
46
47        let avg_degree = safe_avg(total_num_children, self.len());
48        let avg_non_leaf_degree = safe_avg(total_num_children, self.len() - num_leaves);
49        let avg_depth = safe_avg(total_depth, self.len());
50
51        f.debug_struct("Tree")
52            .field("len", &self.len())
53            .field("max_depth", &max_depth)
54            .field("num_leaves", &num_leaves)
55            .field("avg_depth", &avg_depth)
56            .field("avg_degree", &avg_degree)
57            .field("avg_non_leaf_degree", &avg_non_leaf_degree)
58            .field(
59                "depth_value_pairs_in_depth_first_sequence",
60                &depth_first_sequence,
61            )
62            .finish()
63    }
64}
65
66// nodes
67
68impl<V, M, P> Debug for Node<'_, V, M, P>
69where
70    V: TreeVariant,
71    M: MemoryPolicy,
72    P: PinnedStorage,
73    V::Item: Debug,
74{
75    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76        fmt_node(f, self)
77    }
78}
79
80impl<V, M, P> Debug for NodeMut<'_, V, M, P>
81where
82    V: TreeVariant,
83    M: MemoryPolicy,
84    P: PinnedStorage,
85    V::Item: Debug,
86{
87    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
88        fmt_node(f, self)
89    }
90}
91
92fn fmt_node<'a, V, M, P>(
93    f: &mut core::fmt::Formatter<'_>,
94    node: &impl NodeRef<'a, V, M, P>,
95) -> core::fmt::Result
96where
97    V: TreeVariant + 'a,
98    M: MemoryPolicy,
99    P: PinnedStorage,
100    V::Item: Debug,
101{
102    f.debug_struct("Node")
103        .field("is_root", &node.is_root())
104        .field("is_leaf", &node.is_leaf())
105        .field("sibling_idx", &node.sibling_idx())
106        .field("num_children", &node.num_children())
107        .field("depth", &node.depth())
108        .field("height", &node.height())
109        .field("data", node.data())
110        .finish()
111}