1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc_data_structures::fx::FxHashMap;
12 use rustc_data_structures::graph::implementation::{
13 Direction, INCOMING, Graph, NodeIndex, OUTGOING
18 pub struct DepGraphQuery {
19 pub graph: Graph<DepNode, ()>,
20 pub indices: FxHashMap<DepNode, NodeIndex>,
24 pub fn new(nodes: &[DepNode],
25 edges: &[(DepNode, DepNode)])
27 let mut graph = Graph::with_capacity(nodes.len(), edges.len());
28 let mut indices = FxHashMap();
30 indices.insert(node.clone(), graph.add_node(node.clone()));
33 for &(ref source, ref target) in edges {
34 let source = indices[source];
35 let target = indices[target];
36 graph.add_edge(source, target, ());
45 pub fn contains_node(&self, node: &DepNode) -> bool {
46 self.indices.contains_key(&node)
49 pub fn nodes(&self) -> Vec<&DepNode> {
50 self.graph.all_nodes()
56 pub fn edges(&self) -> Vec<(&DepNode,&DepNode)> {
57 self.graph.all_edges()
59 .map(|edge| (edge.source(), edge.target()))
60 .map(|(s, t)| (self.graph.node_data(s),
61 self.graph.node_data(t)))
65 fn reachable_nodes(&self, node: &DepNode, direction: Direction) -> Vec<&DepNode> {
66 if let Some(&index) = self.indices.get(node) {
67 self.graph.depth_traverse(index, direction)
68 .map(|s| self.graph.node_data(s))
75 /// All nodes reachable from `node`. In other words, things that
76 /// will have to be recomputed if `node` changes.
77 pub fn transitive_successors(&self, node: &DepNode) -> Vec<&DepNode> {
78 self.reachable_nodes(node, OUTGOING)
81 /// All nodes that can reach `node`.
82 pub fn transitive_predecessors(&self, node: &DepNode) -> Vec<&DepNode> {
83 self.reachable_nodes(node, INCOMING)
86 /// Just the outgoing edges from `node`.
87 pub fn immediate_successors(&self, node: &DepNode) -> Vec<&DepNode> {
88 if let Some(&index) = self.indices.get(&node) {
89 self.graph.successor_nodes(index)
90 .map(|s| self.graph.node_data(s))