rustc_middle/dep_graph/
retained.rs1use rustc_data_structures::fx::FxHashMap;
2use rustc_data_structures::graph::linked_graph::{Direction, INCOMING, LinkedGraph, NodeIndex};
3
4use super::{DepNode, DepNodeIndex};
5
6pub struct RetainedDepGraph {
13 pub inner: LinkedGraph<DepNode, ()>,
14 pub indices: FxHashMap<DepNode, NodeIndex>,
15}
16
17impl RetainedDepGraph {
18 pub fn new(prev_node_count: usize) -> Self {
19 let node_count = prev_node_count + prev_node_count / 4;
20 let edge_count = 6 * node_count;
21
22 let inner = LinkedGraph::with_capacity(node_count, edge_count);
23 let indices = FxHashMap::default();
24
25 Self { inner, indices }
26 }
27
28 pub fn push(&mut self, index: DepNodeIndex, node: DepNode, edges: &[DepNodeIndex]) {
29 let source = NodeIndex(index.as_usize());
30 self.inner.add_node_with_idx(source, node);
31 self.indices.insert(node, source);
32
33 for &target in edges.iter() {
34 self.inner.add_edge(source, NodeIndex(target.as_usize()), ());
35 }
36 }
37
38 pub fn nodes(&self) -> Vec<&DepNode> {
39 self.inner.all_nodes().iter().map(|n| n.data.as_ref().unwrap()).collect()
40 }
41
42 pub fn edges(&self) -> Vec<(&DepNode, &DepNode)> {
43 self.inner
44 .all_edges()
45 .iter()
46 .map(|edge| (edge.source(), edge.target()))
47 .map(|(s, t)| (self.inner.node_data(s), self.inner.node_data(t)))
48 .collect()
49 }
50
51 fn reachable_nodes(&self, node: &DepNode, direction: Direction) -> Vec<&DepNode> {
52 if let Some(&index) = self.indices.get(node) {
53 self.inner.depth_traverse(index, direction).map(|s| self.inner.node_data(s)).collect()
54 } else {
55 ::alloc::vec::Vec::new()vec![]
56 }
57 }
58
59 pub fn transitive_predecessors(&self, node: &DepNode) -> Vec<&DepNode> {
61 self.reachable_nodes(node, INCOMING)
62 }
63}