Skip to content

Commit 0932749

Browse files
committed
convert remaing NodeVecs to IndexVecs
1 parent 5af6275 commit 0932749

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

src/librustc_data_structures/graph_algorithms/dominators/mod.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
1717
use super::Graph;
1818
use super::iterate::reverse_post_order;
19-
use super::node_vec::NodeVec;
20-
use super::super::indexed_vec::Idx;
19+
use super::super::indexed_vec::{IndexVec, Idx};
2120

2221
use std::fmt;
2322

@@ -40,13 +39,13 @@ pub fn dominators_given_rpo<G: Graph>(graph: &G,
4039
assert_eq!(rpo[0], start_node);
4140

4241
// compute the post order index (rank) for each node
43-
let mut post_order_rank: NodeVec<G, usize> = NodeVec::from_default(graph);
42+
let mut post_order_rank: IndexVec<G::Node, usize> = IndexVec::from_elem_n(usize::default(), graph.num_nodes());
4443
for (index, node) in rpo.iter().rev().cloned().enumerate() {
4544
post_order_rank[node] = index;
4645
}
4746

48-
let mut immediate_dominators: NodeVec<G, Option<G::Node>> =
49-
NodeVec::from_default(graph);
47+
let mut immediate_dominators: IndexVec<G::Node, Option<G::Node>> =
48+
IndexVec::from_elem_n(Option::default(), graph.num_nodes());
5049
immediate_dominators[start_node] = Some(start_node);
5150

5251
let mut changed = true;
@@ -58,7 +57,7 @@ pub fn dominators_given_rpo<G: Graph>(graph: &G,
5857
for pred in graph.predecessors(node) {
5958
if immediate_dominators[pred].is_some() { // (*)
6059
// (*) dominators for `pred` have been calculated
61-
new_idom = intersect_opt(&post_order_rank,
60+
new_idom = intersect_opt::<G>(&post_order_rank,
6261
&immediate_dominators,
6362
new_idom,
6463
Some(pred));
@@ -78,24 +77,24 @@ pub fn dominators_given_rpo<G: Graph>(graph: &G,
7877
}
7978
}
8079

81-
fn intersect_opt<G: Graph>(post_order_rank: &NodeVec<G, usize>,
82-
immediate_dominators: &NodeVec<G, Option<G::Node>>,
80+
fn intersect_opt<G: Graph>(post_order_rank: &IndexVec<G::Node, usize>,
81+
immediate_dominators: &IndexVec<G::Node, Option<G::Node>>,
8382
node1: Option<G::Node>,
8483
node2: Option<G::Node>)
8584
-> Option<G::Node>
8685
{
8786
match (node1, node2) {
8887
(None, None) => None,
8988
(Some(n), None) | (None, Some(n)) => Some(n),
90-
(Some(n1), Some(n2)) => Some(intersect(post_order_rank,
89+
(Some(n1), Some(n2)) => Some(intersect::<G>(post_order_rank,
9190
immediate_dominators,
9291
n1,
9392
n2)),
9493
}
9594
}
9695

97-
fn intersect<G: Graph>(post_order_rank: &NodeVec<G, usize>,
98-
immediate_dominators: &NodeVec<G, Option<G::Node>>,
96+
fn intersect<G: Graph>(post_order_rank: &IndexVec<G::Node, usize>,
97+
immediate_dominators: &IndexVec<G::Node, Option<G::Node>>,
9998
mut node1: G::Node,
10099
mut node2: G::Node)
101100
-> G::Node
@@ -114,8 +113,8 @@ fn intersect<G: Graph>(post_order_rank: &NodeVec<G, usize>,
114113

115114
#[derive(Clone, Debug)]
116115
pub struct Dominators<G: Graph> {
117-
post_order_rank: NodeVec<G, usize>,
118-
immediate_dominators: NodeVec<G, Option<G::Node>>,
116+
post_order_rank: IndexVec<G::Node, usize>,
117+
immediate_dominators: IndexVec<G::Node, Option<G::Node>>,
119118
}
120119

121120
impl<G: Graph> Dominators<G> {
@@ -141,7 +140,7 @@ impl<G: Graph> Dominators<G> {
141140
pub fn mutual_dominator_node(&self, node1: G::Node, node2: G::Node) -> G::Node {
142141
assert!(self.is_reachable(node1), "node {:?} is not reachable", node1);
143142
assert!(self.is_reachable(node2), "node {:?} is not reachable", node2);
144-
intersect(&self.post_order_rank, &self.immediate_dominators, node1, node2)
143+
intersect::<G>(&self.post_order_rank, &self.immediate_dominators, node1, node2)
145144
}
146145

147146
pub fn mutual_dominator<I>(&self, iter: I) -> Option<G::Node>
@@ -154,13 +153,14 @@ impl<G: Graph> Dominators<G> {
154153
})
155154
}
156155

157-
pub fn all_immediate_dominators(&self) -> &NodeVec<G, Option<G::Node>> {
156+
pub fn all_immediate_dominators(&self) -> &IndexVec<G::Node, Option<G::Node>> {
158157
&self.immediate_dominators
159158
}
160159

161160
pub fn dominator_tree(&self) -> DominatorTree<G> {
162-
let mut children: NodeVec<G, Vec<G::Node>> =
163-
NodeVec::from_default_with_len(self.immediate_dominators.len());
161+
let elem: Vec<G::Node> = Vec::new();
162+
let mut children: IndexVec<G::Node, Vec<G::Node>> =
163+
IndexVec::from_elem_n(elem, self.immediate_dominators.len());
164164
let mut root = None;
165165
for (index, immed_dom) in self.immediate_dominators.iter().enumerate() {
166166
let node = G::Node::new(index);
@@ -204,7 +204,7 @@ impl<'dom, G: Graph> Iterator for Iter<'dom, G> {
204204

205205
pub struct DominatorTree<G: Graph> {
206206
root: G::Node,
207-
children: NodeVec<G, Vec<G::Node>>,
207+
children: IndexVec<G::Node, Vec<G::Node>>,
208208
}
209209

210210
impl<G: Graph> DominatorTree<G> {

src/librustc_data_structures/graph_algorithms/iterate/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use super::Graph;
12-
use super::node_vec::NodeVec;
12+
use super::super::indexed_vec::IndexVec;
1313

1414
#[cfg(test)]
1515
mod test;
@@ -22,7 +22,7 @@ pub fn post_order_from_to<G: Graph>(graph: &G,
2222
start_node: G::Node,
2323
end_node: Option<G::Node>)
2424
-> Vec<G::Node> {
25-
let mut visited: NodeVec<G, bool> = NodeVec::from_default(graph);
25+
let mut visited: IndexVec<G::Node, bool> = IndexVec::from_elem_n(false, graph.num_nodes());
2626
let mut result: Vec<G::Node> = Vec::with_capacity(graph.num_nodes());
2727
if let Some(end_node) = end_node {
2828
visited[end_node] = true;
@@ -34,7 +34,7 @@ pub fn post_order_from_to<G: Graph>(graph: &G,
3434
fn post_order_walk<G: Graph>(graph: &G,
3535
node: G::Node,
3636
result: &mut Vec<G::Node>,
37-
visited: &mut NodeVec<G, bool>) {
37+
visited: &mut IndexVec<G::Node, bool>) {
3838
if visited[node] {
3939
return;
4040
}
@@ -50,7 +50,7 @@ fn post_order_walk<G: Graph>(graph: &G,
5050
pub fn pre_order_walk<G: Graph>(graph: &G,
5151
node: G::Node,
5252
result: &mut Vec<G::Node>,
53-
visited: &mut NodeVec<G, bool>) {
53+
visited: &mut IndexVec<G::Node, bool>) {
5454
if visited[node] {
5555
return;
5656
}

src/librustc_data_structures/graph_algorithms/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use super::indexed_vec::Idx;
11+
use super::indexed_vec::{Idx, IndexVec};
1212
use core::marker::{PhantomData};
1313
pub use std::slice::Iter;
1414
use std::ops::{Index, IndexMut};
@@ -20,7 +20,7 @@ pub mod dominators;
2020
pub mod iterate;
2121
pub mod reachable;
2222
mod reference;
23-
pub mod node_vec;
23+
//pub mod node_vec;
2424
pub mod transpose;
2525

2626
#[cfg(test)]
@@ -43,6 +43,9 @@ pub trait Graph
4343
-> <Self as GraphSuccessors<'graph>>::Iter;
4444
//-> NodeVec<Self, Self::Node>;
4545
//-> std::slice::Iter<'graph, Self::Node>;
46+
//fn from_default(&self) -> IndexVec<Self::Node, Self::Node> {
47+
// (0..self.num_nodes()).map(|| Self::Node::default()).collect()
48+
//}
4649
}
4750

4851
pub trait GraphPredecessors<'graph> {

src/librustc_data_structures/indexed_vec.rs

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ impl<I: Idx, T> IndexVec<I, T> {
7777
IndexVec { raw: vec![elem; universe.len()], _marker: PhantomData }
7878
}
7979

80+
#[inline]
81+
pub fn from_elem_n(elem: T, n: usize) -> Self
82+
where T: Clone
83+
{
84+
IndexVec { raw: vec![elem; n], _marker: PhantomData }
85+
}
86+
8087
#[inline]
8188
pub fn push(&mut self, d: T) -> I {
8289
let idx = I::new(self.len());

0 commit comments

Comments
 (0)