Skip to content

Commit 40dddfd

Browse files
committed
clean up unused, comments, line len
1 parent 3d5e127 commit 40dddfd

File tree

2 files changed

+61
-143
lines changed
  • src/librustc_data_structures/graph_algorithms

2 files changed

+61
-143
lines changed

src/librustc_data_structures/graph_algorithms/dominators/mod.rs

+59-43
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,19 @@ use std::fmt;
2323
#[cfg(test)]
2424
mod test;
2525

26-
pub fn dominators<G: Graph>(graph: &G)
27-
-> Dominators<G>
28-
{
26+
pub fn dominators<G: Graph>(graph: &G) -> Dominators<G> {
2927
let start_node = graph.start_node();
3028
let rpo = reverse_post_order(graph, start_node);
3129
dominators_given_rpo(graph, &rpo)
3230
}
3331

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> {
3833
let start_node = graph.start_node();
3934
assert_eq!(rpo[0], start_node);
4035

4136
// 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());
4339
for (index, node) in rpo.iter().rev().cloned().enumerate() {
4440
post_order_rank[node] = index;
4541
}
@@ -55,12 +51,13 @@ pub fn dominators_given_rpo<G: Graph>(graph: &G,
5551
for &node in &rpo[1..] {
5652
let mut new_idom = None;
5753
for pred in graph.predecessors(node) {
58-
if immediate_dominators[pred].is_some() { // (*)
54+
if immediate_dominators[pred].is_some() {
55+
// (*)
5956
// (*) dominators for `pred` have been calculated
6057
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));
6461
}
6562
}
6663

@@ -81,24 +78,19 @@ fn intersect_opt<G: Graph>(post_order_rank: &IndexVec<G::Node, usize>,
8178
immediate_dominators: &IndexVec<G::Node, Option<G::Node>>,
8279
node1: Option<G::Node>,
8380
node2: Option<G::Node>)
84-
-> Option<G::Node>
85-
{
81+
-> Option<G::Node> {
8682
match (node1, node2) {
8783
(None, None) => None,
8884
(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)),
9386
}
9487
}
9588

9689
fn intersect<G: Graph>(post_order_rank: &IndexVec<G::Node, usize>,
9790
immediate_dominators: &IndexVec<G::Node, Option<G::Node>>,
9891
mut node1: G::Node,
9992
mut node2: G::Node)
100-
-> G::Node
101-
{
93+
-> G::Node {
10294
while node1 != node2 {
10395
while post_order_rank[node1] < post_order_rank[node2] {
10496
node1 = immediate_dominators[node1].unwrap();
@@ -129,7 +121,10 @@ impl<G: Graph> Dominators<G> {
129121

130122
pub fn dominators(&self, node: G::Node) -> Iter<G> {
131123
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+
}
133128
}
134129

135130
pub fn is_dominated_by(&self, node: G::Node, dom: G::Node) -> bool {
@@ -138,19 +133,24 @@ impl<G: Graph> Dominators<G> {
138133
}
139134

140135
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)
144146
}
145147

146148
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>
148150
{
149151
let mut iter = iter.into_iter();
150152
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)))
154154
}
155155

156156
pub fn all_immediate_dominators(&self) -> &IndexVec<G::Node, Option<G::Node>> {
@@ -165,7 +165,9 @@ impl<G: Graph> Dominators<G> {
165165
for (index, immed_dom) in self.immediate_dominators.iter().enumerate() {
166166
let node = G::Node::new(index);
167167
match *immed_dom {
168-
None => { /* node not reachable */ }
168+
None => {
169+
// node not reachable
170+
}
169171
Some(immed_dom) => {
170172
if node == immed_dom {
171173
root = Some(node);
@@ -175,13 +177,16 @@ impl<G: Graph> Dominators<G> {
175177
}
176178
}
177179
}
178-
DominatorTree { root: root.unwrap(), children: children }
180+
DominatorTree {
181+
root: root.unwrap(),
182+
children: children,
183+
}
179184
}
180185
}
181186

182187
pub struct Iter<'dom, G: Graph + 'dom> {
183188
dominators: &'dom Dominators<G>,
184-
node: Option<G::Node>
189+
node: Option<G::Node>,
185190
}
186191

187192
impl<'dom, G: Graph> Iterator for Iter<'dom, G> {
@@ -217,13 +222,16 @@ impl<G: Graph> DominatorTree<G> {
217222
}
218223

219224
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+
}
221229
}
222230
}
223231

224232
pub struct IterChildrenOf<'iter, G: Graph + 'iter> {
225233
tree: &'iter DominatorTree<G>,
226-
stack: Vec<G::Node>
234+
stack: Vec<G::Node>,
227235
}
228236

229237
impl<'iter, G: Graph> Iterator for IterChildrenOf<'iter, G> {
@@ -241,7 +249,11 @@ impl<'iter, G: Graph> Iterator for IterChildrenOf<'iter, G> {
241249

242250
impl<G: Graph> fmt::Debug for DominatorTree<G> {
243251
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)
245257
}
246258
}
247259

@@ -252,15 +264,19 @@ struct DominatorTreeNode<'tree, G: Graph + 'tree> {
252264

253265
impl<'tree, G: Graph> fmt::Debug for DominatorTreeNode<'tree, G> {
254266
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();
260277
fmt.debug_tuple("")
261-
.field(&self.node)
262-
.field(&subtrees)
263-
.finish()
278+
.field(&self.node)
279+
.field(&subtrees)
280+
.finish()
264281
}
265282
}
266-

src/librustc_data_structures/graph_algorithms/mod.rs

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

11-
use super::indexed_vec::{Idx, IndexVec};
12-
use core::marker::{PhantomData};
11+
use super::indexed_vec::Idx;
1312
pub use std::slice::Iter;
14-
use std::ops::{Index, IndexMut};
15-
use std::clone::Clone;
16-
use std;
1713

18-
//pub mod bit_set;
1914
pub mod dominators;
2015
pub mod iterate;
2116
pub mod reachable;
2217
mod reference;
23-
//pub mod node_vec;
2418
pub mod transpose;
2519

2620
#[cfg(test)]
@@ -29,23 +23,15 @@ mod test;
2923
pub trait Graph
3024
where Self: for<'graph> GraphPredecessors<'graph, Item=<Self as Graph>::Node>,
3125
Self: for<'graph> GraphSuccessors<'graph, Item=<Self as Graph>::Node>
32-
//where Self: Sized
3326
{
3427
type Node: Idx;
3528

3629
fn num_nodes(&self) -> usize;
3730
fn start_node(&self) -> Self::Node;
3831
fn predecessors<'graph>(&'graph self, node: Self::Node)
3932
-> <Self as GraphPredecessors<'graph>>::Iter;
40-
// why is returning an iterator so complicated?
41-
//-> NodeVec<Self, Self::Node>;
4233
fn successors<'graph>(&'graph self, node: Self::Node)
4334
-> <Self as GraphSuccessors<'graph>>::Iter;
44-
//-> NodeVec<Self, Self::Node>;
45-
//-> 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-
//}
4935
}
5036

5137
pub trait GraphPredecessors<'graph> {
@@ -56,88 +42,4 @@ pub trait GraphPredecessors<'graph> {
5642
pub trait GraphSuccessors<'graph> {
5743
type Item;
5844
type Iter: Iterator<Item=Self::Item>;
59-
}
60-
61-
//pub trait NodeIndex: Copy + Debug + Eq + Ord + Hash + Into<usize> + From<usize> {
62-
// fn as_usize(self) -> usize {
63-
// self.into()
64-
// }
65-
//}
66-
67-
//#[derive(Clone, Debug)]
68-
//pub struct NodeVec<G: Graph, T> {
69-
// pub vec: Vec<T>,
70-
// graph: PhantomData<G>,
71-
//}
72-
//
73-
//impl<G: Graph, T: Clone> NodeVec<G, T> {
74-
// pub fn from_elem(graph: &G, default: &T) -> Self {
75-
// NodeVec::from_fn(graph, |_| default.clone())
76-
// }
77-
//
78-
// pub fn from_elem_with_len(num_nodes: usize, default: &T) -> Self {
79-
// NodeVec::from_fn_with_len(num_nodes, |_| default.clone())
80-
// }
81-
//}
82-
//
83-
//impl<G: Graph, T: Default> NodeVec<G, T> {
84-
// pub fn from_default(graph: &G) -> Self {
85-
// NodeVec::from_fn(graph, |_| T::default())
86-
// }
87-
//
88-
// pub fn from_default_with_len(num_nodes: usize) -> Self {
89-
// NodeVec::from_fn_with_len(num_nodes, |_| T::default())
90-
// }
91-
//}
92-
//
93-
//impl<G: Graph, T> NodeVec<G, T> {
94-
// pub fn from_vec(v: Vec<T>) -> Self
95-
// where T: Clone
96-
// {
97-
//
98-
// NodeVec {
99-
// vec: v.clone(),
100-
// graph: PhantomData,
101-
// }
102-
// }
103-
//
104-
// pub fn from_fn<F>(graph: &G, f: F) -> Self
105-
// where F: FnMut(G::Node) -> T
106-
// {
107-
// Self::from_fn_with_len(graph.num_nodes(), f)
108-
// }
109-
//
110-
// pub fn from_fn_with_len<F>(num_nodes: usize, f: F) -> Self
111-
// where F: FnMut(G::Node) -> T
112-
// {
113-
// NodeVec {
114-
// vec: (0..num_nodes).map(G::Node::new).map(f).collect(),
115-
// graph: PhantomData,
116-
// }
117-
// }
118-
//
119-
// pub fn iter(&self) -> Iter<T> {
120-
// self.vec.iter()
121-
// }
122-
//
123-
// pub fn len(&self) -> usize {
124-
// self.vec.len()
125-
// }
126-
//}
127-
//
128-
//impl<G: Graph, T> Index<G::Node> for NodeVec<G, T> {
129-
// type Output = T;
130-
//
131-
// fn index(&self, index: G::Node) -> &T {
132-
// let index: usize = index.index();
133-
// &self.vec[index]
134-
// }
135-
//}
136-
//
137-
//impl<G: Graph, T> IndexMut<G::Node> for NodeVec<G, T> {
138-
// fn index_mut(&mut self, index: G::Node) -> &mut T {
139-
// let index: usize = index.index();
140-
// &mut self.vec[index]
141-
// }
142-
//}
143-
45+
}

0 commit comments

Comments
 (0)