2016-01-05 13:02:57 -05:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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::{
|
2018-07-01 17:06:00 -04:00
|
|
|
Direction, INCOMING, Graph, NodeIndex, OUTGOING
|
|
|
|
};
|
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 {
|
|
|
|
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());
|
2016-11-08 14:02:55 +11:00
|
|
|
let mut indices = FxHashMap();
|
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, ());
|
|
|
|
}
|
|
|
|
|
|
|
|
DepGraphQuery {
|
2017-07-03 11:19:51 -07:00
|
|
|
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> {
|
2016-01-05 13:02:57 -05:00
|
|
|
self.graph.all_nodes()
|
|
|
|
.iter()
|
2016-05-26 06:11:16 -04:00
|
|
|
.map(|n| &n.data)
|
2016-01-05 13:02:57 -05:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2017-06-02 17:36:30 +02:00
|
|
|
pub fn edges(&self) -> Vec<(&DepNode,&DepNode)> {
|
2016-01-05 13:02:57 -05:00
|
|
|
self.graph.all_edges()
|
|
|
|
.iter()
|
|
|
|
.map(|edge| (edge.source(), edge.target()))
|
2016-05-26 06:11:16 -04:00
|
|
|
.map(|(s, t)| (self.graph.node_data(s),
|
|
|
|
self.graph.node_data(t)))
|
2016-01-05 13:02:57 -05:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-04-06 17:28:59 -04:00
|
|
|
self.graph.depth_traverse(index, direction)
|
2016-05-26 06:11:16 -04:00
|
|
|
.map(|s| self.graph.node_data(s))
|
2016-03-28 17:37:34 -04:00
|
|
|
.collect()
|
|
|
|
} 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) {
|
|
|
|
self.graph.successor_nodes(index)
|
2016-05-26 06:11:16 -04:00
|
|
|
.map(|s| self.graph.node_data(s))
|
2016-01-05 13:02:57 -05:00
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|