2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::Idx;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
|
|
|
pub mod dominators;
|
2018-07-01 17:06:00 -04:00
|
|
|
pub mod implementation;
|
2016-06-09 15:49:07 -07:00
|
|
|
pub mod iterate;
|
|
|
|
|
mod reference;
|
2018-07-02 10:40:03 -04:00
|
|
|
pub mod scc;
|
2019-06-11 13:40:24 -04:00
|
|
|
pub mod vec_graph;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-08-01 23:57:23 +03:00
|
|
|
mod tests;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
2018-07-01 16:54:01 -04:00
|
|
|
pub trait DirectedGraph {
|
2016-06-09 15:49:07 -07:00
|
|
|
type Node: Idx;
|
|
|
|
|
|
|
|
|
|
fn num_nodes(&self) -> usize;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:51:29 +00:00
|
|
|
pub trait NumEdges: DirectedGraph {
|
2019-06-11 13:40:24 -04:00
|
|
|
fn num_edges(&self) -> usize;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:51:29 +00:00
|
|
|
pub trait StartNode: DirectedGraph {
|
|
|
|
|
fn start_node(&self) -> Self::Node;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:40:26 +00:00
|
|
|
pub trait Successors: DirectedGraph {
|
|
|
|
|
type Successors<'g>: Iterator<Item = Self::Node>
|
|
|
|
|
where
|
|
|
|
|
Self: 'g;
|
|
|
|
|
|
|
|
|
|
fn successors(&self, node: Self::Node) -> Self::Successors<'_>;
|
2019-06-11 16:29:27 -04:00
|
|
|
|
2024-04-14 15:15:03 +00:00
|
|
|
fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self> {
|
2020-11-23 09:26:10 -05:00
|
|
|
iterate::DepthFirstSearch::new(self).with_start_node(from)
|
2019-06-11 16:29:27 -04:00
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:40:26 +00:00
|
|
|
pub trait Predecessors: DirectedGraph {
|
|
|
|
|
type Predecessors<'g>: Iterator<Item = Self::Node>
|
|
|
|
|
where
|
|
|
|
|
Self: 'g;
|
2016-06-09 15:49:07 -07:00
|
|
|
|
2024-04-14 15:40:26 +00:00
|
|
|
fn predecessors(&self, node: Self::Node) -> Self::Predecessors<'_>;
|
2016-10-20 00:25:19 +05:30
|
|
|
}
|
2018-07-01 16:54:01 -04:00
|
|
|
|
2024-04-14 15:51:29 +00:00
|
|
|
pub trait ControlFlowGraph: DirectedGraph + StartNode + Predecessors + Successors {
|
2018-07-01 16:54:01 -04:00
|
|
|
// convenient trait
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 15:51:29 +00:00
|
|
|
impl<T> ControlFlowGraph for T where T: DirectedGraph + StartNode + Predecessors + Successors {}
|
2019-09-18 14:35:56 -07:00
|
|
|
|
|
|
|
|
/// Returns `true` if the graph has a cycle that is reachable from the start node.
|
|
|
|
|
pub fn is_cyclic<G>(graph: &G) -> bool
|
|
|
|
|
where
|
2024-04-14 15:51:29 +00:00
|
|
|
G: ?Sized + DirectedGraph + StartNode + Successors,
|
2019-09-18 14:35:56 -07:00
|
|
|
{
|
|
|
|
|
iterate::TriColorDepthFirstSearch::new(graph)
|
|
|
|
|
.run_from_start(&mut iterate::CycleDetector)
|
|
|
|
|
.is_some()
|
|
|
|
|
}
|