1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! O(1) TDG Graph - trueno-graph integration for fast dependency tracking
//!
//! This module provides CSR-backed graph storage for TDG (Test-Driven Grade) analysis,
//! enabling O(1) function dependency lookups and PageRank-based critical function identification.
//!
//! # Architecture
//!
//! **Graph Schema**:
//! - Nodes: Function names
//! - Edges: Function calls (caller → callee)
//! - PageRank: Identifies critical functions (high in-degree = many callers)
//!
//! # Example
//!
//! ```rust,ignore
//! use pmat::tdg::tdg_graph::TdgGraph;
//!
//! let mut graph = TdgGraph::new();
//!
//! // Add functions
//! graph.add_function("main".to_string())?;
//! graph.add_function("helper".to_string())?;
//!
//! // Add edge: main calls helper
//! graph.add_edge("main", "helper")?;
//!
//! // O(1) lookup
//! let exists = graph.has_function("main");
//! assert!(exists);
//!
//! // PageRank identifies critical functions
//! graph.update_criticality()?;
//! let critical = graph.critical_functions();
//! println!("Most critical: {:?}", critical[0]);
//! ```
use anyhow::{Context as _, Result};
use std::collections::HashMap;
use trueno_graph::{pagerank, CsrGraph, NodeId};
/// CSR-backed TDG dependency graph for O(1) function lookups
///
/// Uses trueno-graph for fast access and PageRank-based criticality scoring.
#[derive(Debug, Clone)]
pub struct TdgGraph {
/// CSR graph for function dependencies
/// Nodes: Function names
/// Edges: (caller → callee)
graph: CsrGraph,
/// Node ID mapping (function_name → NodeId)
node_map: HashMap<String, NodeId>,
/// Reverse mapping (NodeId → function_name)
reverse_node_map: HashMap<NodeId, String>,
/// PageRank scores (function_name → criticality score)
/// Higher score = more critical (many incoming edges)
criticality_scores: HashMap<String, f32>,
/// Next node ID counter
next_node_id: u32,
}
impl Default for TdgGraph {
fn default() -> Self {
Self::new()
}
}
// Core operations: new, add_function, add_edge, has_function,
// update_criticality, critical_functions, num_nodes, num_edges
include!("tdg_graph_operations.rs");
// Terminal graph visualization (trueno-viz integration)
include!("tdg_graph_viz.rs");
// Unit tests
include!("tdg_graph_tests.rs");