capability-skeleton 0.1.0

A Rust library for managing and building complex hierarchical tree structures such as skill trees. Supports serialization, error handling, and deep tree metrics.
Documentation
// ---------------- [ File: capability-skeleton/src/tree_weighted_branching_measurer.rs ]
crate::ix!();

impl TreeWeightedBranchingMeasurer for Skeleton {
    #[instrument(level = "trace", skip(self))]
    fn measure_tree_weighted_branching(&self) -> TreeWeightedBranchingStats {
        let node_count = self.nodes().len();
        if node_count == 0 {
            return TreeWeightedBranchingStatsBuilder::default()
                .build()
                .unwrap_or_default();
        }

        let mut total_children = 0usize;
        let mut min_child_count = u8::MAX;
        let mut max_child_count = u8::MIN;

        for node in self.nodes() {
            let child_count = node.child_ids().len() as u8;
            total_children += child_count as usize;
            if child_count < min_child_count {
                min_child_count = child_count;
            }
            if child_count > max_child_count {
                max_child_count = child_count;
            }
        }

        let average_children = total_children as f32 / node_count as f32;
        TreeWeightedBranchingStatsBuilder::default()
            .total_nodes_in_tree(node_count)
            .total_child_count_in_tree(total_children)
            .min_child_count_in_tree(min_child_count)
            .max_child_count_in_tree(max_child_count)
            .average_child_count_in_tree(average_children)
            .build()
            .unwrap_or_default()
    }
}

#[cfg(test)]
mod skeleton_weighted_branching_measurer_assessment {
    use super::*;

    #[traced_test]
    fn check_weighted_branching_empty() {
        let skel = SkeletonBuilder::default().build().unwrap();
        let stats = skel.measure_tree_weighted_branching();
        assert_eq!(*stats.total_nodes_in_tree(), 0);
        assert_eq!(*stats.total_child_count_in_tree(), 0);
    }

    #[traced_test]
    fn check_weighted_branching_mixed() {
        // 5 nodes => child counts => [2,2,0,0,0]
        let n0 = SkeletonNodeBuilder::default()
            .id(0u16)
            .child_ids(vec![1u16,2u16])
            .name("root")
            .original_key("root")
            .build(NodeKind::Dispatch)
            .unwrap();
        let n1 = SkeletonNodeBuilder::default()
            .id(1u16)
            .child_ids(vec![3u16,4u16])
            .name("childA")
            .original_key("childA")
            .build(NodeKind::Dispatch)
            .unwrap();
        let n2 = SkeletonNodeBuilder::default()
            .id(2u16)
            .name("childB")
            .original_key("childB")
            .build(NodeKind::LeafHolder)
            .unwrap();
        let n3 = SkeletonNodeBuilder::default()
            .id(3u16)
            .name("childC")
            .original_key("childC")
            .build(NodeKind::LeafHolder)
            .unwrap();
        let n4 = SkeletonNodeBuilder::default()
            .id(4u16)
            .name("childD")
            .original_key("childD")
            .build(NodeKind::LeafHolder)
            .unwrap();

        let skel = SkeletonBuilder::default()
            .nodes(vec![n0, n1, n2, n3, n4])
            .root_id(Some(0u16))
            .build()
            .unwrap();

        let stats = skel.measure_tree_weighted_branching();
        assert_eq!(*stats.total_nodes_in_tree(), 5);
        assert_eq!(*stats.total_child_count_in_tree(), 4); // 2 + 2 + 0 + 0 + 0
        assert_eq!(*stats.min_child_count_in_tree(), 0);
        assert_eq!(*stats.max_child_count_in_tree(), 2);
    }
}