From 398da593a53161c1ef9ca7dabbc5e9edf67deac6 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 14 Apr 2024 15:15:03 +0000 Subject: Merge `WithNumNodes` into DirectedGraph --- .../rustc_data_structures/src/graph/iterate/mod.rs | 24 +++++++++++----------- compiler/rustc_data_structures/src/graph/mod.rs | 13 ++++-------- .../rustc_data_structures/src/graph/reference.rs | 2 -- .../rustc_data_structures/src/graph/scc/mod.rs | 14 ++++++------- compiler/rustc_data_structures/src/graph/tests.rs | 10 ++++----- .../src/graph/vec_graph/mod.rs | 4 +--- 6 files changed, 27 insertions(+), 40 deletions(-) (limited to 'compiler/rustc_data_structures/src/graph') diff --git a/compiler/rustc_data_structures/src/graph/iterate/mod.rs b/compiler/rustc_data_structures/src/graph/iterate/mod.rs index 9eb4b5278c0..c46fdb4e4f7 100644 --- a/compiler/rustc_data_structures/src/graph/iterate/mod.rs +++ b/compiler/rustc_data_structures/src/graph/iterate/mod.rs @@ -1,4 +1,4 @@ -use super::{DirectedGraph, WithNumNodes, WithStartNode, WithSuccessors}; +use super::{DirectedGraph, WithStartNode, WithSuccessors}; use rustc_index::bit_set::BitSet; use rustc_index::{IndexSlice, IndexVec}; use std::ops::ControlFlow; @@ -6,14 +6,14 @@ use std::ops::ControlFlow; #[cfg(test)] mod tests; -pub fn post_order_from( +pub fn post_order_from( graph: &G, start_node: G::Node, ) -> Vec { post_order_from_to(graph, start_node, None) } -pub fn post_order_from_to( +pub fn post_order_from_to( graph: &G, start_node: G::Node, end_node: Option, @@ -27,7 +27,7 @@ pub fn post_order_from_to( result } -fn post_order_walk( +fn post_order_walk( graph: &G, node: G::Node, result: &mut Vec, @@ -60,7 +60,7 @@ fn post_order_walk( } } -pub fn reverse_post_order( +pub fn reverse_post_order( graph: &G, start_node: G::Node, ) -> Vec { @@ -72,7 +72,7 @@ pub fn reverse_post_order( /// A "depth-first search" iterator for a directed graph. pub struct DepthFirstSearch<'graph, G> where - G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors, + G: ?Sized + DirectedGraph + WithSuccessors, { graph: &'graph G, stack: Vec, @@ -81,7 +81,7 @@ where impl<'graph, G> DepthFirstSearch<'graph, G> where - G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors, + G: ?Sized + DirectedGraph + WithSuccessors, { pub fn new(graph: &'graph G) -> Self { Self { graph, stack: vec![], visited: BitSet::new_empty(graph.num_nodes()) } @@ -127,7 +127,7 @@ where impl std::fmt::Debug for DepthFirstSearch<'_, G> where - G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors, + G: ?Sized + DirectedGraph + WithSuccessors, { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut f = fmt.debug_set(); @@ -140,7 +140,7 @@ where impl Iterator for DepthFirstSearch<'_, G> where - G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors, + G: ?Sized + DirectedGraph + WithSuccessors, { type Item = G::Node; @@ -201,7 +201,7 @@ struct Event { /// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms pub struct TriColorDepthFirstSearch<'graph, G> where - G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors, + G: ?Sized + DirectedGraph + WithSuccessors, { graph: &'graph G, stack: Vec>, @@ -211,7 +211,7 @@ where impl<'graph, G> TriColorDepthFirstSearch<'graph, G> where - G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors, + G: ?Sized + DirectedGraph + WithSuccessors, { pub fn new(graph: &'graph G) -> Self { TriColorDepthFirstSearch { @@ -278,7 +278,7 @@ where impl TriColorDepthFirstSearch<'_, G> where - G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors + WithStartNode, + G: ?Sized + DirectedGraph + WithSuccessors + WithStartNode, { /// Performs a depth-first search, starting from `G::start_node()`. /// diff --git a/compiler/rustc_data_structures/src/graph/mod.rs b/compiler/rustc_data_structures/src/graph/mod.rs index e06ab2fe36b..853b8f1775e 100644 --- a/compiler/rustc_data_structures/src/graph/mod.rs +++ b/compiler/rustc_data_structures/src/graph/mod.rs @@ -12,9 +12,7 @@ mod tests; pub trait DirectedGraph { type Node: Idx; -} -pub trait WithNumNodes: DirectedGraph { fn num_nodes(&self) -> usize; } @@ -28,10 +26,7 @@ where { fn successors(&self, node: Self::Node) -> >::Iter; - fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self> - where - Self: WithNumNodes, - { + fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self> { iterate::DepthFirstSearch::new(self).with_start_node(from) } } @@ -60,20 +55,20 @@ pub trait WithStartNode: DirectedGraph { } pub trait ControlFlowGraph: - DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes + DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors { // convenient trait } impl ControlFlowGraph for T where - T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes + T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors { } /// Returns `true` if the graph has a cycle that is reachable from the start node. pub fn is_cyclic(graph: &G) -> bool where - G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors + WithNumNodes, + G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors, { iterate::TriColorDepthFirstSearch::new(graph) .run_from_start(&mut iterate::CycleDetector) diff --git a/compiler/rustc_data_structures/src/graph/reference.rs b/compiler/rustc_data_structures/src/graph/reference.rs index c259fe56c15..d15513c95db 100644 --- a/compiler/rustc_data_structures/src/graph/reference.rs +++ b/compiler/rustc_data_structures/src/graph/reference.rs @@ -2,9 +2,7 @@ use super::*; impl<'graph, G: DirectedGraph> DirectedGraph for &'graph G { type Node = G::Node; -} -impl<'graph, G: WithNumNodes> WithNumNodes for &'graph G { fn num_nodes(&self) -> usize { (**self).num_nodes() } diff --git a/compiler/rustc_data_structures/src/graph/scc/mod.rs b/compiler/rustc_data_structures/src/graph/scc/mod.rs index b54d75f7ed7..a9967c2ecbb 100644 --- a/compiler/rustc_data_structures/src/graph/scc/mod.rs +++ b/compiler/rustc_data_structures/src/graph/scc/mod.rs @@ -7,7 +7,7 @@ use crate::fx::FxHashSet; use crate::graph::vec_graph::VecGraph; -use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithNumNodes, WithSuccessors}; +use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithSuccessors}; use rustc_index::{Idx, IndexSlice, IndexVec}; use std::ops::Range; @@ -39,7 +39,7 @@ pub struct SccData { } impl Sccs { - pub fn new(graph: &(impl DirectedGraph + WithNumNodes + WithSuccessors)) -> Self { + pub fn new(graph: &(impl DirectedGraph + WithSuccessors)) -> Self { SccsConstruction::construct(graph) } @@ -89,17 +89,15 @@ impl Sccs { } } -impl DirectedGraph for Sccs { +impl DirectedGraph for Sccs { type Node = S; -} -impl WithNumNodes for Sccs { fn num_nodes(&self) -> usize { self.num_sccs() } } -impl WithNumEdges for Sccs { +impl WithNumEdges for Sccs { fn num_edges(&self) -> usize { self.scc_data.all_successors.len() } @@ -158,7 +156,7 @@ impl SccData { } } -struct SccsConstruction<'c, G: DirectedGraph + WithNumNodes + WithSuccessors, S: Idx> { +struct SccsConstruction<'c, G: DirectedGraph + WithSuccessors, S: Idx> { graph: &'c G, /// The state of each node; used during walk to record the stack @@ -218,7 +216,7 @@ enum WalkReturn { impl<'c, G, S> SccsConstruction<'c, G, S> where - G: DirectedGraph + WithNumNodes + WithSuccessors, + G: DirectedGraph + WithSuccessors, S: Idx, { /// Identifies SCCs in the graph `G` and computes the resulting diff --git a/compiler/rustc_data_structures/src/graph/tests.rs b/compiler/rustc_data_structures/src/graph/tests.rs index 7f4ef906b36..df58b965ccf 100644 --- a/compiler/rustc_data_structures/src/graph/tests.rs +++ b/compiler/rustc_data_structures/src/graph/tests.rs @@ -36,6 +36,10 @@ impl TestGraph { impl DirectedGraph for TestGraph { type Node = usize; + + fn num_nodes(&self) -> usize { + self.num_nodes + } } impl WithStartNode for TestGraph { @@ -44,12 +48,6 @@ impl WithStartNode for TestGraph { } } -impl WithNumNodes for TestGraph { - fn num_nodes(&self) -> usize { - self.num_nodes - } -} - impl WithPredecessors for TestGraph { fn predecessors(&self, node: usize) -> >::Iter { self.predecessors[&node].iter().cloned() diff --git a/compiler/rustc_data_structures/src/graph/vec_graph/mod.rs b/compiler/rustc_data_structures/src/graph/vec_graph/mod.rs index 00f6266ce1d..79efe3fb8e0 100644 --- a/compiler/rustc_data_structures/src/graph/vec_graph/mod.rs +++ b/compiler/rustc_data_structures/src/graph/vec_graph/mod.rs @@ -1,4 +1,4 @@ -use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithNumNodes, WithSuccessors}; +use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithSuccessors}; use rustc_index::{Idx, IndexVec}; #[cfg(test)] @@ -80,9 +80,7 @@ impl VecGraph { impl DirectedGraph for VecGraph { type Node = N; -} -impl WithNumNodes for VecGraph { fn num_nodes(&self) -> usize { self.node_starts.len() - 1 } -- cgit 1.4.1-3-g733a5