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