2019-09-26 05:38:33 +00:00
|
|
|
use rustc_index::vec::Idx;
|
2016-06-09 22:49:07 +00:00
|
|
|
|
|
|
|
pub mod dominators;
|
2018-07-01 21:06:00 +00:00
|
|
|
pub mod implementation;
|
2016-06-09 22:49:07 +00:00
|
|
|
pub mod iterate;
|
|
|
|
mod reference;
|
2018-07-02 14:40:03 +00:00
|
|
|
pub mod scc;
|
2019-06-11 17:40:24 +00:00
|
|
|
pub mod vec_graph;
|
2016-06-09 22:49:07 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-08-01 20:57:23 +00:00
|
|
|
mod tests;
|
2016-06-09 22:49:07 +00:00
|
|
|
|
2018-07-01 20:54:01 +00:00
|
|
|
pub trait DirectedGraph {
|
2016-06-09 22:49:07 +00:00
|
|
|
type Node: Idx;
|
2018-07-01 20:54:01 +00:00
|
|
|
}
|
2016-06-09 22:49:07 +00:00
|
|
|
|
2018-07-01 20:54:01 +00:00
|
|
|
pub trait WithNumNodes: DirectedGraph {
|
2016-06-09 22:49:07 +00:00
|
|
|
fn num_nodes(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
2019-06-11 17:40:24 +00:00
|
|
|
pub trait WithNumEdges: DirectedGraph {
|
|
|
|
fn num_edges(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
2018-07-01 20:54:01 +00:00
|
|
|
pub trait WithSuccessors: DirectedGraph
|
|
|
|
where
|
|
|
|
Self: for<'graph> GraphSuccessors<'graph, Item = <Self as DirectedGraph>::Node>,
|
|
|
|
{
|
2019-12-22 22:42:04 +00:00
|
|
|
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter;
|
2019-06-11 20:29:27 +00:00
|
|
|
|
|
|
|
fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self>
|
|
|
|
where
|
|
|
|
Self: WithNumNodes,
|
|
|
|
{
|
2020-11-23 14:26:10 +00:00
|
|
|
iterate::DepthFirstSearch::new(self).with_start_node(from)
|
2019-06-11 20:29:27 +00:00
|
|
|
}
|
2018-07-01 20:54:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 19:17:27 +00:00
|
|
|
#[allow(unused_lifetimes)]
|
2018-07-01 20:54:01 +00:00
|
|
|
pub trait GraphSuccessors<'graph> {
|
2016-06-09 22:49:07 +00:00
|
|
|
type Item;
|
2016-10-19 18:55:19 +00:00
|
|
|
type Iter: Iterator<Item = Self::Item>;
|
2016-06-09 22:49:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 20:54:01 +00:00
|
|
|
pub trait WithPredecessors: DirectedGraph
|
|
|
|
where
|
|
|
|
Self: for<'graph> GraphPredecessors<'graph, Item = <Self as DirectedGraph>::Node>,
|
|
|
|
{
|
2019-12-22 22:42:04 +00:00
|
|
|
fn predecessors(&self, node: Self::Node) -> <Self as GraphPredecessors<'_>>::Iter;
|
2018-07-01 20:54:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 19:17:27 +00:00
|
|
|
#[allow(unused_lifetimes)]
|
2018-07-01 20:54:01 +00:00
|
|
|
pub trait GraphPredecessors<'graph> {
|
2016-06-09 22:49:07 +00:00
|
|
|
type Item;
|
2016-10-19 18:55:19 +00:00
|
|
|
type Iter: Iterator<Item = Self::Item>;
|
|
|
|
}
|
2018-07-01 20:54:01 +00:00
|
|
|
|
|
|
|
pub trait WithStartNode: DirectedGraph {
|
|
|
|
fn start_node(&self) -> Self::Node;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ControlFlowGraph:
|
2021-08-09 13:52:04 +00:00
|
|
|
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
|
2018-07-01 20:54:01 +00:00
|
|
|
{
|
|
|
|
// convenient trait
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
impl<T> ControlFlowGraph for T where
|
2021-08-09 13:52:04 +00:00
|
|
|
T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
|
2018-07-01 20:54:01 +00:00
|
|
|
{
|
|
|
|
}
|
2019-09-18 21:35:56 +00: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
|
|
|
|
G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors + WithNumNodes,
|
|
|
|
{
|
|
|
|
iterate::TriColorDepthFirstSearch::new(graph)
|
|
|
|
.run_from_start(&mut iterate::CycleDetector)
|
|
|
|
.is_some()
|
|
|
|
}
|