Skip to content

Commit 36045c2

Browse files
committed
LinkedGraph: Use IndexVec instead of Vec in LinkedGraph nodes
1 parent 5d04477 commit 36045c2

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

  • compiler/rustc_data_structures/src/graph/linked_graph

compiler/rustc_data_structures/src/graph/linked_graph/mod.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use std::fmt::Debug;
2424

2525
use rustc_index::bit_set::DenseBitSet;
26+
use rustc_index::{Idx, IndexSlice, IndexVec};
2627
use tracing::debug;
2728

2829
#[cfg(test)]
@@ -45,7 +46,7 @@ mod tests;
4546
/// and does not implement those traits, so it has its own implementations of a
4647
/// few basic graph algorithms.
4748
pub struct LinkedGraph<N, E> {
48-
nodes: Vec<Node<N>>,
49+
nodes: IndexVec<NodeIndex, Node<N>>,
4950
edges: Vec<Edge<E>>,
5051
}
5152

@@ -62,7 +63,7 @@ pub struct Edge<E> {
6263
pub data: E,
6364
}
6465

65-
#[derive(Copy, Clone, PartialEq, Debug)]
66+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
6667
pub struct NodeIndex(pub usize);
6768

6869
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -87,19 +88,29 @@ impl NodeIndex {
8788
}
8889
}
8990

91+
impl Idx for NodeIndex {
92+
fn new(idx: usize) -> NodeIndex {
93+
NodeIndex(idx)
94+
}
95+
96+
fn index(self) -> usize {
97+
self.0
98+
}
99+
}
100+
90101
impl<N: Debug, E: Debug> LinkedGraph<N, E> {
91102
pub fn new() -> Self {
92-
Self { nodes: Vec::new(), edges: Vec::new() }
103+
Self { nodes: IndexVec::new(), edges: Vec::new() }
93104
}
94105

95106
pub fn with_capacity(nodes: usize, edges: usize) -> Self {
96-
Self { nodes: Vec::with_capacity(nodes), edges: Vec::with_capacity(edges) }
107+
Self { nodes: IndexVec::with_capacity(nodes), edges: Vec::with_capacity(edges) }
97108
}
98109

99110
// # Simple accessors
100111

101112
#[inline]
102-
pub fn all_nodes(&self) -> &[Node<N>] {
113+
pub fn all_nodes(&self) -> &IndexSlice<NodeIndex, Node<N>> {
103114
&self.nodes
104115
}
105116

@@ -131,15 +142,15 @@ impl<N: Debug, E: Debug> LinkedGraph<N, E> {
131142
}
132143

133144
pub fn mut_node_data(&mut self, idx: NodeIndex) -> &mut N {
134-
&mut self.nodes[idx.0].data
145+
&mut self.nodes[idx].data
135146
}
136147

137148
pub fn node_data(&self, idx: NodeIndex) -> &N {
138-
&self.nodes[idx.0].data
149+
&self.nodes[idx].data
139150
}
140151

141152
pub fn node(&self, idx: NodeIndex) -> &Node<N> {
142-
&self.nodes[idx.0]
153+
&self.nodes[idx]
143154
}
144155

145156
// # Edge construction and queries
@@ -154,16 +165,16 @@ impl<N: Debug, E: Debug> LinkedGraph<N, E> {
154165
let idx = self.next_edge_index();
155166

156167
// read current first of the list of edges from each node
157-
let source_first = self.nodes[source.0].first_edge[OUTGOING.repr];
158-
let target_first = self.nodes[target.0].first_edge[INCOMING.repr];
168+
let source_first = self.nodes[source].first_edge[OUTGOING.repr];
169+
let target_first = self.nodes[target].first_edge[INCOMING.repr];
159170

160171
// create the new edge, with the previous firsts from each node
161172
// as the next pointers
162173
self.edges.push(Edge { next_edge: [source_first, target_first], source, target, data });
163174

164175
// adjust the firsts for each node target be the next object.
165-
self.nodes[source.0].first_edge[OUTGOING.repr] = idx;
166-
self.nodes[target.0].first_edge[INCOMING.repr] = idx;
176+
self.nodes[source].first_edge[OUTGOING.repr] = idx;
177+
self.nodes[target].first_edge[INCOMING.repr] = idx;
167178

168179
idx
169180
}

0 commit comments

Comments
 (0)