2016-11-08 14:02:55 +11:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-07-02 06:14:49 -04:00
|
|
|
use rustc_data_structures::graph::implementation::{
|
2019-12-22 17:42:04 -05:00
|
|
|
Direction, Graph, NodeIndex, INCOMING, OUTGOING,
|
2018-07-01 17:06:00 -04:00
|
|
|
};
|
2016-01-05 13:02:57 -05:00
|
|
|
|
|
|
|
use super::DepNode;
|
|
|
|
|
2017-06-02 17:36:30 +02:00
|
|
|
pub struct DepGraphQuery {
|
|
|
|
pub graph: Graph<DepNode, ()>,
|
|
|
|
pub indices: FxHashMap<DepNode, NodeIndex>,
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-02 17:36:30 +02:00
|
|
|
impl DepGraphQuery {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn new(nodes: &[DepNode], edges: &[(DepNode, DepNode)]) -> DepGraphQuery {
|
2017-09-14 21:28:55 -07:00
|
|
|
let mut graph = Graph::with_capacity(nodes.len(), edges.len());
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut indices = FxHashMap::default();
|
2016-01-05 13:02:57 -05:00
|
|
|
for node in nodes {
|
2017-09-14 21:28:55 -07:00
|
|
|
indices.insert(node.clone(), graph.add_node(node.clone()));
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for &(ref source, ref target) in edges {
|
|
|
|
let source = indices[source];
|
|
|
|
let target = indices[target];
|
|
|
|
graph.add_edge(source, target, ());
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
DepGraphQuery { graph, indices }
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-02 17:36:30 +02:00
|
|
|
pub fn contains_node(&self, node: &DepNode) -> bool {
|
2016-03-28 17:37:34 -04:00
|
|
|
self.indices.contains_key(&node)
|
|
|
|
}
|
|
|
|
|
2017-06-02 17:36:30 +02:00
|
|
|
pub fn nodes(&self) -> Vec<&DepNode> {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.graph.all_nodes().iter().map(|n| &n.data).collect()
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn edges(&self) -> Vec<(&DepNode, &DepNode)> {
|
|
|
|
self.graph
|
|
|
|
.all_edges()
|
|
|
|
.iter()
|
|
|
|
.map(|edge| (edge.source(), edge.target()))
|
|
|
|
.map(|(s, t)| (self.graph.node_data(s), self.graph.node_data(t)))
|
|
|
|
.collect()
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-02 17:36:30 +02:00
|
|
|
fn reachable_nodes(&self, node: &DepNode, direction: Direction) -> Vec<&DepNode> {
|
2016-05-26 06:11:16 -04:00
|
|
|
if let Some(&index) = self.indices.get(node) {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.graph.depth_traverse(index, direction).map(|s| self.graph.node_data(s)).collect()
|
2016-03-28 17:37:34 -04:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 17:28:59 -04:00
|
|
|
/// All nodes reachable from `node`. In other words, things that
|
|
|
|
/// will have to be recomputed if `node` changes.
|
2017-06-02 17:36:30 +02:00
|
|
|
pub fn transitive_successors(&self, node: &DepNode) -> Vec<&DepNode> {
|
2016-04-06 17:28:59 -04:00
|
|
|
self.reachable_nodes(node, OUTGOING)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// All nodes that can reach `node`.
|
2017-06-02 17:36:30 +02:00
|
|
|
pub fn transitive_predecessors(&self, node: &DepNode) -> Vec<&DepNode> {
|
2016-04-06 17:28:59 -04:00
|
|
|
self.reachable_nodes(node, INCOMING)
|
|
|
|
}
|
|
|
|
|
2016-03-28 17:37:34 -04:00
|
|
|
/// Just the outgoing edges from `node`.
|
2017-06-02 17:36:30 +02:00
|
|
|
pub fn immediate_successors(&self, node: &DepNode) -> Vec<&DepNode> {
|
2016-03-28 17:37:34 -04:00
|
|
|
if let Some(&index) = self.indices.get(&node) {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.graph.successor_nodes(index).map(|s| self.graph.node_data(s)).collect()
|
2016-01-05 13:02:57 -05:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|