use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct CallGraph {
ids: Vec<String>,
id_to_idx: HashMap<String, u32>,
adj: Vec<Vec<u32>>,
pub total_callee_names: usize,
pub resolved_callee_names: usize,
}
impl CallGraph {
pub fn new() -> Self {
CallGraph {
ids: Vec::new(),
id_to_idx: HashMap::new(),
adj: Vec::new(),
total_callee_names: 0,
resolved_callee_names: 0,
}
}
pub fn intern(&mut self, id: String) -> u32 {
if let Some(&idx) = self.id_to_idx.get(&id) {
return idx;
}
let idx = self.ids.len() as u32;
self.id_to_idx.insert(id.clone(), idx);
self.ids.push(id);
self.adj.push(Vec::new());
idx
}
pub fn node_count(&self) -> usize {
self.ids.len()
}
pub fn edge_count(&self) -> usize {
self.adj.iter().map(|v| v.len()).sum()
}
pub fn contains(&self, id: &str) -> bool {
self.id_to_idx.contains_key(id)
}
pub fn callees_of<'a>(&'a self, id: &str) -> Option<impl Iterator<Item = &'a str>> {
let idx = *self.id_to_idx.get(id)? as usize;
Some(self.adj[idx].iter().map(|&i| self.ids[i as usize].as_str()))
}
pub fn add_adj(&mut self, caller_idx: u32, callee_idx: u32) {
self.adj[caller_idx as usize].push(callee_idx);
}
pub fn all_ids(&self) -> impl Iterator<Item = &str> {
self.ids.iter().map(|s| s.as_str())
}
pub fn add_node(&mut self, function_id: String) {
self.intern(function_id);
}
pub fn add_edge(&mut self, caller: String, callee: String) {
let caller_idx = self.intern(caller);
let callee_idx = self.intern(callee);
self.adj[caller_idx as usize].push(callee_idx);
}
pub fn fan_in(&self, function_id: &str) -> usize {
match self.id_to_idx.get(function_id) {
None => 0,
Some(&target) => self.adj.iter().filter(|c| c.contains(&target)).count(),
}
}
pub fn fan_out(&self, function_id: &str) -> usize {
match self.id_to_idx.get(function_id) {
None => 0,
Some(&idx) => self.adj[idx as usize].len(),
}
}
pub fn build_fan_in_map(&self) -> HashMap<String, usize> {
let n = self.ids.len();
let mut counts = vec![0usize; n];
for callees in &self.adj {
for &callee in callees {
counts[callee as usize] += 1;
}
}
self.ids
.iter()
.enumerate()
.map(|(i, id)| (id.clone(), counts[i]))
.collect()
}
}
impl Default for CallGraph {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_graph() {
let graph = CallGraph::new();
assert_eq!(graph.node_count(), 0);
assert_eq!(graph.edge_count(), 0);
}
#[test]
fn test_fan_in_fan_out() {
let mut graph = CallGraph::new();
graph.add_edge("A".to_string(), "B".to_string());
graph.add_edge("A".to_string(), "C".to_string());
graph.add_edge("B".to_string(), "C".to_string());
assert_eq!(graph.fan_in("A"), 0); assert_eq!(graph.fan_out("A"), 2);
assert_eq!(graph.fan_in("B"), 1); assert_eq!(graph.fan_out("B"), 1);
assert_eq!(graph.fan_in("C"), 2); assert_eq!(graph.fan_out("C"), 0); }
#[test]
fn test_build_fan_in_map() {
let mut graph = CallGraph::new();
graph.add_edge("A".to_string(), "B".to_string());
graph.add_edge("A".to_string(), "C".to_string());
graph.add_edge("B".to_string(), "C".to_string());
let fan_in = graph.build_fan_in_map();
assert_eq!(fan_in.get("A").copied().unwrap_or(0), 0); assert_eq!(fan_in.get("B").copied().unwrap_or(0), 1); assert_eq!(fan_in.get("C").copied().unwrap_or(0), 2); }
}