@@ -23,23 +23,19 @@ use std::fmt;
23
23
#[ cfg( test) ]
24
24
mod test;
25
25
26
- pub fn dominators < G : Graph > ( graph : & G )
27
- -> Dominators < G >
28
- {
26
+ pub fn dominators < G : Graph > ( graph : & G ) -> Dominators < G > {
29
27
let start_node = graph. start_node ( ) ;
30
28
let rpo = reverse_post_order ( graph, start_node) ;
31
29
dominators_given_rpo ( graph, & rpo)
32
30
}
33
31
34
- pub fn dominators_given_rpo < G : Graph > ( graph : & G ,
35
- rpo : & [ G :: Node ] )
36
- -> Dominators < G >
37
- {
32
+ pub fn dominators_given_rpo < G : Graph > ( graph : & G , rpo : & [ G :: Node ] ) -> Dominators < G > {
38
33
let start_node = graph. start_node ( ) ;
39
34
assert_eq ! ( rpo[ 0 ] , start_node) ;
40
35
41
36
// compute the post order index (rank) for each node
42
- let mut post_order_rank: IndexVec < G :: Node , usize > = IndexVec :: from_elem_n ( usize:: default ( ) , graph. num_nodes ( ) ) ;
37
+ let mut post_order_rank: IndexVec < G :: Node , usize > = IndexVec :: from_elem_n ( usize:: default ( ) ,
38
+ graph. num_nodes ( ) ) ;
43
39
for ( index, node) in rpo. iter ( ) . rev ( ) . cloned ( ) . enumerate ( ) {
44
40
post_order_rank[ node] = index;
45
41
}
@@ -55,12 +51,13 @@ pub fn dominators_given_rpo<G: Graph>(graph: &G,
55
51
for & node in & rpo[ 1 ..] {
56
52
let mut new_idom = None ;
57
53
for pred in graph. predecessors ( node) {
58
- if immediate_dominators[ pred] . is_some ( ) { // (*)
54
+ if immediate_dominators[ pred] . is_some ( ) {
55
+ // (*)
59
56
// (*) dominators for `pred` have been calculated
60
57
new_idom = intersect_opt :: < G > ( & post_order_rank,
61
- & immediate_dominators,
62
- new_idom,
63
- Some ( pred) ) ;
58
+ & immediate_dominators,
59
+ new_idom,
60
+ Some ( pred) ) ;
64
61
}
65
62
}
66
63
@@ -81,24 +78,19 @@ fn intersect_opt<G: Graph>(post_order_rank: &IndexVec<G::Node, usize>,
81
78
immediate_dominators : & IndexVec < G :: Node , Option < G :: Node > > ,
82
79
node1 : Option < G :: Node > ,
83
80
node2 : Option < G :: Node > )
84
- -> Option < G :: Node >
85
- {
81
+ -> Option < G :: Node > {
86
82
match ( node1, node2) {
87
83
( None , None ) => None ,
88
84
( Some ( n) , None ) | ( None , Some ( n) ) => Some ( n) ,
89
- ( Some ( n1) , Some ( n2) ) => Some ( intersect :: < G > ( post_order_rank,
90
- immediate_dominators,
91
- n1,
92
- n2) ) ,
85
+ ( Some ( n1) , Some ( n2) ) => Some ( intersect :: < G > ( post_order_rank, immediate_dominators, n1, n2) ) ,
93
86
}
94
87
}
95
88
96
89
fn intersect < G : Graph > ( post_order_rank : & IndexVec < G :: Node , usize > ,
97
90
immediate_dominators : & IndexVec < G :: Node , Option < G :: Node > > ,
98
91
mut node1 : G :: Node ,
99
92
mut node2 : G :: Node )
100
- -> G :: Node
101
- {
93
+ -> G :: Node {
102
94
while node1 != node2 {
103
95
while post_order_rank[ node1] < post_order_rank[ node2] {
104
96
node1 = immediate_dominators[ node1] . unwrap ( ) ;
@@ -129,7 +121,10 @@ impl<G: Graph> Dominators<G> {
129
121
130
122
pub fn dominators ( & self , node : G :: Node ) -> Iter < G > {
131
123
assert ! ( self . is_reachable( node) , "node {:?} is not reachable" , node) ;
132
- Iter { dominators : self , node : Some ( node) }
124
+ Iter {
125
+ dominators : self ,
126
+ node : Some ( node) ,
127
+ }
133
128
}
134
129
135
130
pub fn is_dominated_by ( & self , node : G :: Node , dom : G :: Node ) -> bool {
@@ -138,19 +133,24 @@ impl<G: Graph> Dominators<G> {
138
133
}
139
134
140
135
pub fn mutual_dominator_node ( & self , node1 : G :: Node , node2 : G :: Node ) -> G :: Node {
141
- assert ! ( self . is_reachable( node1) , "node {:?} is not reachable" , node1) ;
142
- assert ! ( self . is_reachable( node2) , "node {:?} is not reachable" , node2) ;
143
- intersect :: < G > ( & self . post_order_rank , & self . immediate_dominators , node1, node2)
136
+ assert ! ( self . is_reachable( node1) ,
137
+ "node {:?} is not reachable" ,
138
+ node1) ;
139
+ assert ! ( self . is_reachable( node2) ,
140
+ "node {:?} is not reachable" ,
141
+ node2) ;
142
+ intersect :: < G > ( & self . post_order_rank ,
143
+ & self . immediate_dominators ,
144
+ node1,
145
+ node2)
144
146
}
145
147
146
148
pub fn mutual_dominator < I > ( & self , iter : I ) -> Option < G :: Node >
147
- where I : IntoIterator < Item = G :: Node >
149
+ where I : IntoIterator < Item = G :: Node >
148
150
{
149
151
let mut iter = iter. into_iter ( ) ;
150
152
iter. next ( )
151
- . map ( |dom| {
152
- iter. fold ( dom, |dom, node| self . mutual_dominator_node ( dom, node) )
153
- } )
153
+ . map ( |dom| iter. fold ( dom, |dom, node| self . mutual_dominator_node ( dom, node) ) )
154
154
}
155
155
156
156
pub fn all_immediate_dominators ( & self ) -> & IndexVec < G :: Node , Option < G :: Node > > {
@@ -165,7 +165,9 @@ impl<G: Graph> Dominators<G> {
165
165
for ( index, immed_dom) in self . immediate_dominators . iter ( ) . enumerate ( ) {
166
166
let node = G :: Node :: new ( index) ;
167
167
match * immed_dom {
168
- None => { /* node not reachable */ }
168
+ None => {
169
+ // node not reachable
170
+ }
169
171
Some ( immed_dom) => {
170
172
if node == immed_dom {
171
173
root = Some ( node) ;
@@ -175,13 +177,16 @@ impl<G: Graph> Dominators<G> {
175
177
}
176
178
}
177
179
}
178
- DominatorTree { root : root. unwrap ( ) , children : children }
180
+ DominatorTree {
181
+ root : root. unwrap ( ) ,
182
+ children : children,
183
+ }
179
184
}
180
185
}
181
186
182
187
pub struct Iter < ' dom , G : Graph + ' dom > {
183
188
dominators : & ' dom Dominators < G > ,
184
- node : Option < G :: Node >
189
+ node : Option < G :: Node > ,
185
190
}
186
191
187
192
impl < ' dom , G : Graph > Iterator for Iter < ' dom , G > {
@@ -217,13 +222,16 @@ impl<G: Graph> DominatorTree<G> {
217
222
}
218
223
219
224
pub fn iter_children_of ( & self , node : G :: Node ) -> IterChildrenOf < G > {
220
- IterChildrenOf { tree : self , stack : vec ! [ node] }
225
+ IterChildrenOf {
226
+ tree : self ,
227
+ stack : vec ! [ node] ,
228
+ }
221
229
}
222
230
}
223
231
224
232
pub struct IterChildrenOf < ' iter , G : Graph + ' iter > {
225
233
tree : & ' iter DominatorTree < G > ,
226
- stack : Vec < G :: Node >
234
+ stack : Vec < G :: Node > ,
227
235
}
228
236
229
237
impl < ' iter , G : Graph > Iterator for IterChildrenOf < ' iter , G > {
@@ -241,7 +249,11 @@ impl<'iter, G: Graph> Iterator for IterChildrenOf<'iter, G> {
241
249
242
250
impl < G : Graph > fmt:: Debug for DominatorTree < G > {
243
251
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
244
- fmt:: Debug :: fmt ( & DominatorTreeNode { tree : self , node : self . root } , fmt)
252
+ fmt:: Debug :: fmt ( & DominatorTreeNode {
253
+ tree : self ,
254
+ node : self . root ,
255
+ } ,
256
+ fmt)
245
257
}
246
258
}
247
259
@@ -252,15 +264,19 @@ struct DominatorTreeNode<'tree, G: Graph + 'tree> {
252
264
253
265
impl < ' tree , G : Graph > fmt:: Debug for DominatorTreeNode < ' tree , G > {
254
266
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
255
- let subtrees: Vec < _ > =
256
- self . tree . children ( self . node )
257
- . iter ( )
258
- . map ( |& child| DominatorTreeNode { tree : self . tree , node : child } )
259
- . collect ( ) ;
267
+ let subtrees: Vec < _ > = self . tree
268
+ . children ( self . node )
269
+ . iter ( )
270
+ . map ( |& child| {
271
+ DominatorTreeNode {
272
+ tree : self . tree ,
273
+ node : child,
274
+ }
275
+ } )
276
+ . collect ( ) ;
260
277
fmt. debug_tuple ( "" )
261
- . field ( & self . node )
262
- . field ( & subtrees)
263
- . finish ( )
278
+ . field ( & self . node )
279
+ . field ( & subtrees)
280
+ . finish ( )
264
281
}
265
282
}
266
-
0 commit comments