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.
|
|
|
|
|
|
|
|
use rustc_data_structures::fnv::FnvHashMap;
|
|
|
|
use rustc_data_structures::graph::{Graph, NodeIndex};
|
2016-03-28 17:37:34 -04:00
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::hash::Hash;
|
2016-01-05 13:02:57 -05:00
|
|
|
|
|
|
|
use super::DepNode;
|
|
|
|
|
2016-03-28 17:37:34 -04:00
|
|
|
pub struct DepGraphQuery<D: Clone + Debug + Hash + Eq> {
|
|
|
|
pub graph: Graph<DepNode<D>, ()>,
|
|
|
|
pub indices: FnvHashMap<DepNode<D>, NodeIndex>,
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
2016-03-28 17:37:34 -04:00
|
|
|
impl<D: Clone + Debug + Hash + Eq> DepGraphQuery<D> {
|
|
|
|
pub fn new(nodes: &[DepNode<D>],
|
|
|
|
edges: &[(DepNode<D>, DepNode<D>)])
|
|
|
|
-> DepGraphQuery<D> {
|
2016-01-05 13:02:57 -05:00
|
|
|
let mut graph = Graph::new();
|
|
|
|
let mut indices = FnvHashMap();
|
|
|
|
for node in nodes {
|
|
|
|
indices.insert(node.clone(), graph.next_node_index());
|
|
|
|
graph.add_node(node.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
for &(ref source, ref target) in edges {
|
|
|
|
let source = indices[source];
|
|
|
|
let target = indices[target];
|
|
|
|
graph.add_edge(source, target, ());
|
|
|
|
}
|
|
|
|
|
|
|
|
DepGraphQuery {
|
|
|
|
graph: graph,
|
|
|
|
indices: indices
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 17:37:34 -04:00
|
|
|
pub fn contains_node(&self, node: &DepNode<D>) -> bool {
|
|
|
|
self.indices.contains_key(&node)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nodes(&self) -> Vec<DepNode<D>> {
|
2016-01-05 13:02:57 -05:00
|
|
|
self.graph.all_nodes()
|
|
|
|
.iter()
|
|
|
|
.map(|n| n.data.clone())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2016-03-28 17:37:34 -04:00
|
|
|
pub fn edges(&self) -> Vec<(DepNode<D>,DepNode<D>)> {
|
2016-01-05 13:02:57 -05:00
|
|
|
self.graph.all_edges()
|
|
|
|
.iter()
|
|
|
|
.map(|edge| (edge.source(), edge.target()))
|
2016-03-28 17:37:34 -04:00
|
|
|
.map(|(s, t)| (self.graph.node_data(s).clone(),
|
|
|
|
self.graph.node_data(t).clone()))
|
2016-01-05 13:02:57 -05:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// All nodes reachable from `node`. In other words, things that
|
|
|
|
/// will have to be recomputed if `node` changes.
|
2016-03-28 17:37:34 -04:00
|
|
|
pub fn transitive_dependents(&self, node: DepNode<D>) -> Vec<DepNode<D>> {
|
2016-01-05 13:02:57 -05:00
|
|
|
if let Some(&index) = self.indices.get(&node) {
|
|
|
|
self.graph.depth_traverse(index)
|
2016-03-28 17:37:34 -04:00
|
|
|
.map(|s| self.graph.node_data(s).clone())
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Just the outgoing edges from `node`.
|
|
|
|
pub fn immediate_dependents(&self, node: DepNode<D>) -> Vec<DepNode<D>> {
|
|
|
|
if let Some(&index) = self.indices.get(&node) {
|
|
|
|
self.graph.successor_nodes(index)
|
|
|
|
.map(|s| self.graph.node_data(s).clone())
|
2016-01-05 13:02:57 -05:00
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|