2016-11-08 14:02:55 +11:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-10-09 11:22:54 +02:00
|
|
|
use rustc_data_structures::graph::implementation::{Direction, Graph, NodeIndex, INCOMING};
|
2021-03-06 13:55:20 +01:00
|
|
|
use rustc_index::vec::IndexVec;
|
2016-01-05 13:02:57 -05:00
|
|
|
|
2021-03-02 22:38:49 +01:00
|
|
|
use super::{DepKind, DepNode, DepNodeIndex};
|
2016-01-05 13:02:57 -05:00
|
|
|
|
2020-03-18 10:25:22 +01:00
|
|
|
pub struct DepGraphQuery<K> {
|
|
|
|
|
pub graph: Graph<DepNode<K>, ()>,
|
|
|
|
|
pub indices: FxHashMap<DepNode<K>, NodeIndex>,
|
2021-03-06 13:55:20 +01:00
|
|
|
pub dep_index_to_index: IndexVec<DepNodeIndex, Option<NodeIndex>>,
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
|
2020-03-18 10:25:22 +01:00
|
|
|
impl<K: DepKind> DepGraphQuery<K> {
|
2021-03-02 22:38:49 +01:00
|
|
|
pub fn new(prev_node_count: usize) -> DepGraphQuery<K> {
|
|
|
|
|
let node_count = prev_node_count + prev_node_count / 4;
|
|
|
|
|
let edge_count = 6 * node_count;
|
2016-01-05 13:02:57 -05:00
|
|
|
|
2021-03-02 22:38:49 +01:00
|
|
|
let graph = Graph::with_capacity(node_count, edge_count);
|
|
|
|
|
let indices = FxHashMap::default();
|
2021-03-06 13:55:20 +01:00
|
|
|
let dep_index_to_index = IndexVec::new();
|
2016-01-05 13:02:57 -05:00
|
|
|
|
2021-03-06 13:55:20 +01:00
|
|
|
DepGraphQuery { graph, indices, dep_index_to_index }
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 22:38:49 +01:00
|
|
|
pub fn push(&mut self, index: DepNodeIndex, node: DepNode<K>, edges: &[DepNodeIndex]) {
|
|
|
|
|
let source = self.graph.add_node(node);
|
2023-04-16 01:06:55 -07:00
|
|
|
self.dep_index_to_index.insert(index, source);
|
2021-03-02 22:38:49 +01:00
|
|
|
self.indices.insert(node, source);
|
|
|
|
|
|
|
|
|
|
for &target in edges.iter() {
|
2021-03-06 13:55:20 +01:00
|
|
|
let target = self.dep_index_to_index[target];
|
2021-03-18 19:26:08 +01:00
|
|
|
// We may miss the edges that are pushed while the `DepGraphQuery` is being accessed.
|
|
|
|
|
// Skip them to issues.
|
2021-03-06 13:55:20 +01:00
|
|
|
if let Some(target) = target {
|
|
|
|
|
self.graph.add_edge(source, target, ());
|
|
|
|
|
}
|
2021-03-02 22:38:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 10:25:22 +01:00
|
|
|
pub fn nodes(&self) -> Vec<&DepNode<K>> {
|
2016-01-05 13:02:57 -05:00
|
|
|
self.graph.all_nodes().iter().map(|n| &n.data).collect()
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 10:25:22 +01:00
|
|
|
pub fn edges(&self) -> Vec<(&DepNode<K>, &DepNode<K>)> {
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 10:25:22 +01:00
|
|
|
fn reachable_nodes(&self, node: &DepNode<K>, direction: Direction) -> Vec<&DepNode<K>> {
|
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).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 that can reach `node`.
|
2020-03-18 10:25:22 +01:00
|
|
|
pub fn transitive_predecessors(&self, node: &DepNode<K>) -> Vec<&DepNode<K>> {
|
2016-04-06 17:28:59 -04:00
|
|
|
self.reachable_nodes(node, INCOMING)
|
|
|
|
|
}
|
2016-01-05 13:02:57 -05:00
|
|
|
}
|