diff --git a/src/graph/dag.rs b/src/graph/dag.rs index 5a70142..df634bc 100644 --- a/src/graph/dag.rs +++ b/src/graph/dag.rs @@ -13,8 +13,8 @@ where DirectedAcyclicGraph(AdjacencyGraph::new()) } - fn to_adjecency_graph(&self) -> AdjacencyGraph { - self.0.clone() + fn to_adjecency_graph(&self) -> &AdjacencyGraph { + &self.0 } fn nodes(&self) -> BTreeSet { diff --git a/src/graph/directed.rs b/src/graph/directed.rs index 93bb4e8..fb0dac5 100644 --- a/src/graph/directed.rs +++ b/src/graph/directed.rs @@ -21,8 +21,8 @@ where } } - fn to_adjecency_graph(&self) -> AdjacencyGraph { - self.clone() + fn to_adjecency_graph(&self) -> &AdjacencyGraph { + &self } fn nodes(&self) -> BTreeSet { @@ -103,9 +103,7 @@ where undirected.add_edge(to.clone(), from.clone()); } - UndirectedGraph { - directed: undirected, - } + UndirectedGraph(undirected) } pub fn has_edge(&self, from: &V, to: &V) -> bool { diff --git a/src/graph/mod.rs b/src/graph/mod.rs index 15821a8..18ce52d 100644 --- a/src/graph/mod.rs +++ b/src/graph/mod.rs @@ -24,7 +24,7 @@ where graph } - fn to_adjecency_graph(&self) -> AdjacencyGraph; + fn to_adjecency_graph(&self) -> &AdjacencyGraph; fn nodes(&self) -> BTreeSet; fn adjacencies(&self) -> BTreeMap>; @@ -65,16 +65,13 @@ where adjacencies: BTreeMap>, } -#[derive(Debug)] -pub struct UndirectedGraph +#[derive(Debug, Clone)] +pub struct UndirectedGraph(AdjacencyGraph) where - V: Clone, -{ - pub directed: AdjacencyGraph, -} + V: Clone; -#[derive(Debug)] -pub struct DirectedAcyclicGraph(pub AdjacencyGraph) +#[derive(Debug, Clone)] +pub struct DirectedAcyclicGraph(AdjacencyGraph) where V: Clone; diff --git a/src/graph/undirected.rs b/src/graph/undirected.rs index fda9a4e..e0f198b 100644 --- a/src/graph/undirected.rs +++ b/src/graph/undirected.rs @@ -15,47 +15,45 @@ where where Self: Sized, { - UndirectedGraph { - directed: AdjacencyGraph::new(), - } + UndirectedGraph(AdjacencyGraph::new()) } - fn to_adjecency_graph(&self) -> AdjacencyGraph { - self.directed.clone() + fn to_adjecency_graph(&self) -> &AdjacencyGraph { + &self.0 } fn nodes(&self) -> BTreeSet { - self.directed.nodes() + self.0.nodes() } fn adjacencies(&self) -> BTreeMap> { - self.directed.adjacencies() + self.0.adjacencies() } fn neighbors(&self, from: &V) -> BTreeSet { - self.directed.neighbors(from) + self.0.neighbors(from) } fn edges(&self) -> BTreeSet<(V, V)> { - self.directed.edges() + self.0.edges() } fn add_node(&mut self, node: V) { - self.directed.add_node(node); + self.0.add_node(node); } fn add_edge(&mut self, from: V, to: V) { - self.directed.add_edge(from.clone(), to.clone()); - self.directed.add_edge(to, from); + self.0.add_edge(from.clone(), to.clone()); + self.0.add_edge(to, from); } fn remove_node(&mut self, node: &V) { - self.directed.remove_node(node); + self.0.remove_node(node); } fn remove_edge(&mut self, from: &V, to: &V) { - self.directed.remove_edge(from, to); - self.directed.remove_edge(to, from); + self.0.remove_edge(from, to); + self.0.remove_edge(to, from); } } @@ -64,22 +62,22 @@ where V: Ord + Eq + Clone + Debug, { pub fn add_edge(&mut self, from: V, to: V) { - self.directed.add_edge(from.clone(), to.clone()); - self.directed.add_edge(to.clone(), from.clone()); + self.0.add_edge(from.clone(), to.clone()); + self.0.add_edge(to.clone(), from.clone()); } pub fn remove_edge(&mut self, from: &V, to: &V) { - self.directed.remove_edge(from, to); - self.directed.remove_edge(to, from); + self.0.remove_edge(from, to); + self.0.remove_edge(to, from); } pub fn connected_components(&self) -> Vec> { let mut visited = BTreeSet::new(); let mut result = Vec::new(); - let pb = ProgressBar::new(self.directed.nodes.len() as u64); + let pb = ProgressBar::new(self.0.nodes.len() as u64); - for node in self.directed.nodes.iter() { + for node in self.0.nodes.iter() { if visited.contains(node) { continue; } @@ -114,7 +112,7 @@ where pub fn compact_chains(&mut self) { let mut visited = BTreeSet::new(); - let nodes = self.directed.nodes.clone(); + let nodes = self.0.nodes.clone(); let pb = ProgressBar::new(nodes.len() as u64); @@ -180,7 +178,7 @@ where println!("Compacted {} nodes", compacted_count); - self.directed.gc(); + self.0.gc(); pb.finish(); } diff --git a/src/main.rs b/src/main.rs index b3f4a80..460f880 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,9 +115,9 @@ fn main() -> std::io::Result<()> { let dag = graph.dag(); - compute_edge_types(&dag.0); + compute_edge_types(&dag); - let ccs = compute_ccs(&dag.0); + let ccs = compute_ccs(&dag); println!("Picking largest connected component..."); // pick the largest connected component @@ -351,12 +351,15 @@ fn compute_orientation_histogram(graph: &impl Graph<(String, Orientation)>) { println!(); } -fn compute_ccs(graph: &AdjacencyGraph) -> Vec> +fn compute_ccs(graph: &impl Graph) -> Vec> where V: Ord + Eq + Clone + Debug, { println!("Computing connected components..."); - let ccs = graph.undirected().connected_components(); + let ccs = graph + .to_adjecency_graph() + .undirected() + .connected_components(); println!("Computing sizes histogram..."); let hist: BTreeMap<_, _> = ccs