mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-21 17:52:12 +03:00
Rollup merge of #37285 - srinivasreddy:cfg, r=nikomatsakis
run rustfmt on control_flow_graph folder
This commit is contained in:
@@ -57,9 +57,9 @@ pub fn dominators_given_rpo<G: ControlFlowGraph>(graph: &G,
|
||||
// (*)
|
||||
// (*) dominators for `pred` have been calculated
|
||||
new_idom = intersect_opt(&post_order_rank,
|
||||
&immediate_dominators,
|
||||
new_idom,
|
||||
Some(pred));
|
||||
&immediate_dominators,
|
||||
new_idom,
|
||||
Some(pred));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,10 +77,10 @@ pub fn dominators_given_rpo<G: ControlFlowGraph>(graph: &G,
|
||||
}
|
||||
|
||||
fn intersect_opt<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
|
||||
immediate_dominators: &IndexVec<Node, Option<Node>>,
|
||||
node1: Option<Node>,
|
||||
node2: Option<Node>)
|
||||
-> Option<Node> {
|
||||
immediate_dominators: &IndexVec<Node, Option<Node>>,
|
||||
node1: Option<Node>,
|
||||
node2: Option<Node>)
|
||||
-> Option<Node> {
|
||||
match (node1, node2) {
|
||||
(None, None) => None,
|
||||
(Some(n), None) | (None, Some(n)) => Some(n),
|
||||
@@ -89,10 +89,10 @@ fn intersect_opt<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
|
||||
}
|
||||
|
||||
fn intersect<Node: Idx>(post_order_rank: &IndexVec<Node, usize>,
|
||||
immediate_dominators: &IndexVec<Node, Option<Node>>,
|
||||
mut node1: Node,
|
||||
mut node2: Node)
|
||||
-> Node {
|
||||
immediate_dominators: &IndexVec<Node, Option<Node>>,
|
||||
mut node1: Node,
|
||||
mut node2: Node)
|
||||
-> Node {
|
||||
while node1 != node2 {
|
||||
while post_order_rank[node1] < post_order_rank[node2] {
|
||||
node1 = immediate_dominators[node1].unwrap();
|
||||
@@ -142,9 +142,9 @@ pub fn mutual_dominator_node(&self, node1: Node, node2: Node) -> Node {
|
||||
"node {:?} is not reachable",
|
||||
node2);
|
||||
intersect::<Node>(&self.post_order_rank,
|
||||
&self.immediate_dominators,
|
||||
node1,
|
||||
node2)
|
||||
&self.immediate_dominators,
|
||||
node1,
|
||||
node2)
|
||||
}
|
||||
|
||||
pub fn mutual_dominator<I>(&self, iter: I) -> Option<Node>
|
||||
|
||||
@@ -14,12 +14,7 @@
|
||||
|
||||
#[test]
|
||||
fn diamond() {
|
||||
let graph = TestGraph::new(0, &[
|
||||
(0, 1),
|
||||
(0, 2),
|
||||
(1, 3),
|
||||
(2, 3),
|
||||
]);
|
||||
let graph = TestGraph::new(0, &[(0, 1), (0, 2), (1, 3), (2, 3)]);
|
||||
|
||||
let dominators = dominators(&graph);
|
||||
let immediate_dominators = dominators.all_immediate_dominators();
|
||||
@@ -32,17 +27,9 @@ fn diamond() {
|
||||
#[test]
|
||||
fn paper() {
|
||||
// example from the paper:
|
||||
let graph = TestGraph::new(6, &[
|
||||
(6, 5),
|
||||
(6, 4),
|
||||
(5, 1),
|
||||
(4, 2),
|
||||
(4, 3),
|
||||
(1, 2),
|
||||
(2, 3),
|
||||
(3, 2),
|
||||
(2, 1),
|
||||
]);
|
||||
let graph = TestGraph::new(6,
|
||||
&[(6, 5), (6, 4), (5, 1), (4, 2), (4, 3), (1, 2), (2, 3), (3, 2),
|
||||
(2, 1)]);
|
||||
|
||||
let dominators = dominators(&graph);
|
||||
let immediate_dominators = dominators.all_immediate_dominators();
|
||||
@@ -54,4 +41,3 @@ fn paper() {
|
||||
assert_eq!(immediate_dominators[5], Some(6));
|
||||
assert_eq!(immediate_dominators[6], Some(6));
|
||||
}
|
||||
|
||||
|
||||
@@ -15,12 +15,7 @@
|
||||
|
||||
#[test]
|
||||
fn diamond_post_order() {
|
||||
let graph = TestGraph::new(0, &[
|
||||
(0, 1),
|
||||
(0, 2),
|
||||
(1, 3),
|
||||
(2, 3),
|
||||
]);
|
||||
let graph = TestGraph::new(0, &[(0, 1), (0, 2), (1, 3), (2, 3)]);
|
||||
|
||||
let result = post_order_from(&graph, 0);
|
||||
assert_eq!(result, vec![3, 1, 2, 0]);
|
||||
@@ -33,16 +28,8 @@ fn rev_post_order_inner_loop() {
|
||||
// ^ ^ v |
|
||||
// | 6 <- 4 |
|
||||
// +-----------------+
|
||||
let graph = TestGraph::new(0, &[
|
||||
(0, 1),
|
||||
(1, 2),
|
||||
(2, 3),
|
||||
(3, 5),
|
||||
(3, 1),
|
||||
(2, 4),
|
||||
(4, 6),
|
||||
(6, 2),
|
||||
]);
|
||||
let graph = TestGraph::new(0,
|
||||
&[(0, 1), (1, 2), (2, 3), (3, 5), (3, 1), (2, 4), (4, 6), (6, 2)]);
|
||||
|
||||
let rev_graph = TransposedGraph::new(&graph);
|
||||
|
||||
@@ -52,4 +39,3 @@ fn rev_post_order_inner_loop() {
|
||||
let result = post_order_from_to(&rev_graph, 3, Some(1));
|
||||
assert_eq!(result, vec![4, 6, 2, 3]);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,10 +36,10 @@ fn successors<'graph>(&'graph self, node: Self::Node)
|
||||
|
||||
pub trait GraphPredecessors<'graph> {
|
||||
type Item;
|
||||
type Iter: Iterator<Item=Self::Item>;
|
||||
type Iter: Iterator<Item = Self::Item>;
|
||||
}
|
||||
|
||||
pub trait GraphSuccessors<'graph> {
|
||||
type Item;
|
||||
type Iter: Iterator<Item=Self::Item>;
|
||||
}
|
||||
type Iter: Iterator<Item = Self::Item>;
|
||||
}
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
pub fn reachable<G: ControlFlowGraph>(graph: &G)
|
||||
-> Reachability<G::Node> {
|
||||
pub fn reachable<G: ControlFlowGraph>(graph: &G) -> Reachability<G::Node> {
|
||||
let reverse_post_order = reverse_post_order(graph, graph.start_node());
|
||||
reachable_given_rpo(graph, &reverse_post_order)
|
||||
}
|
||||
@@ -53,12 +52,10 @@ pub struct Reachability<Node: Idx> {
|
||||
impl<Node: Idx> Reachability<Node> {
|
||||
fn new<G: ControlFlowGraph>(graph: &G) -> Self {
|
||||
let num_nodes = graph.num_nodes();
|
||||
Reachability {
|
||||
bits: IndexVec::from_elem_n(BitVector::new(num_nodes), num_nodes),
|
||||
}
|
||||
Reachability { bits: IndexVec::from_elem_n(BitVector::new(num_nodes), num_nodes) }
|
||||
}
|
||||
|
||||
pub fn can_reach(&self, source: Node, target: Node)-> bool {
|
||||
pub fn can_reach(&self, source: Node, target: Node) -> bool {
|
||||
let bit: usize = target.index();
|
||||
self.bits[source].contains(bit)
|
||||
}
|
||||
|
||||
@@ -17,15 +17,7 @@ fn test1() {
|
||||
// 0 -> 1 -> 2 -> 3
|
||||
// ^ v
|
||||
// 6 <- 4 -> 5
|
||||
let graph = TestGraph::new(0, &[
|
||||
(0, 1),
|
||||
(1, 2),
|
||||
(2, 3),
|
||||
(2, 4),
|
||||
(4, 5),
|
||||
(4, 6),
|
||||
(6, 1),
|
||||
]);
|
||||
let graph = TestGraph::new(0, &[(0, 1), (1, 2), (2, 3), (2, 4), (4, 5), (4, 6), (6, 1)]);
|
||||
let reachable = reachable(&graph);
|
||||
assert!((0..6).all(|i| reachable.can_reach(0, i)));
|
||||
assert!((1..6).all(|i| reachable.can_reach(1, i)));
|
||||
@@ -43,15 +35,9 @@ fn test2() {
|
||||
// 30 -> 31 -> 32 -> 33
|
||||
// ^ v
|
||||
// 36 <- 34 -> 35
|
||||
let graph = TestGraph::new(30, &[
|
||||
(30, 31),
|
||||
(31, 32),
|
||||
(32, 33),
|
||||
(32, 34),
|
||||
(34, 35),
|
||||
(34, 36),
|
||||
(36, 31),
|
||||
]);
|
||||
let graph = TestGraph::new(30,
|
||||
&[(30, 31), (31, 32), (32, 33), (32, 34), (34, 35), (34, 36),
|
||||
(36, 31)]);
|
||||
let reachable = reachable(&graph);
|
||||
assert!((30..36).all(|i| reachable.can_reach(30, i)));
|
||||
assert!((31..36).all(|i| reachable.can_reach(31, i)));
|
||||
|
||||
@@ -21,13 +21,13 @@ fn start_node(&self) -> Self::Node {
|
||||
(**self).start_node()
|
||||
}
|
||||
|
||||
fn predecessors<'iter>(&'iter self, node: Self::Node)
|
||||
-> <Self as GraphPredecessors<'iter>>::Iter {
|
||||
fn predecessors<'iter>(&'iter self,
|
||||
node: Self::Node)
|
||||
-> <Self as GraphPredecessors<'iter>>::Iter {
|
||||
(**self).predecessors(node)
|
||||
}
|
||||
|
||||
fn successors<'iter>(&'iter self, node: Self::Node)
|
||||
-> <Self as GraphSuccessors<'iter>>::Iter {
|
||||
fn successors<'iter>(&'iter self, node: Self::Node) -> <Self as GraphSuccessors<'iter>>::Iter {
|
||||
(**self).successors(node)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn new(start_node: usize, edges: &[(usize, usize)]) -> Self {
|
||||
num_nodes: start_node + 1,
|
||||
start_node: start_node,
|
||||
successors: HashMap::new(),
|
||||
predecessors: HashMap::new()
|
||||
predecessors: HashMap::new(),
|
||||
};
|
||||
for &(source, target) in edges {
|
||||
graph.num_nodes = max(graph.num_nodes, source + 1);
|
||||
@@ -55,13 +55,13 @@ fn num_nodes(&self) -> usize {
|
||||
self.num_nodes
|
||||
}
|
||||
|
||||
fn predecessors<'graph>(&'graph self, node: usize)
|
||||
fn predecessors<'graph>(&'graph self,
|
||||
node: usize)
|
||||
-> <Self as GraphPredecessors<'graph>>::Iter {
|
||||
self.predecessors[&node].iter().cloned()
|
||||
self.predecessors[&node].iter().cloned()
|
||||
}
|
||||
|
||||
fn successors<'graph>(&'graph self, node: usize)
|
||||
-> <Self as GraphSuccessors<'graph>>::Iter {
|
||||
fn successors<'graph>(&'graph self, node: usize) -> <Self as GraphSuccessors<'graph>>::Iter {
|
||||
self.successors[&node].iter().cloned()
|
||||
}
|
||||
}
|
||||
@@ -75,4 +75,3 @@ impl<'graph> GraphSuccessors<'graph> for TestGraph {
|
||||
type Item = usize;
|
||||
type Iter = iter::Cloned<slice::Iter<'graph, usize>>;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,10 @@ pub fn new(base_graph: G) -> Self {
|
||||
}
|
||||
|
||||
pub fn with_start(base_graph: G, start_node: G::Node) -> Self {
|
||||
TransposedGraph { base_graph: base_graph, start_node: start_node }
|
||||
TransposedGraph {
|
||||
base_graph: base_graph,
|
||||
start_node: start_node,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,12 +40,14 @@ fn start_node(&self) -> Self::Node {
|
||||
self.start_node
|
||||
}
|
||||
|
||||
fn predecessors<'graph>(&'graph self, node: Self::Node)
|
||||
fn predecessors<'graph>(&'graph self,
|
||||
node: Self::Node)
|
||||
-> <Self as GraphPredecessors<'graph>>::Iter {
|
||||
self.base_graph.successors(node)
|
||||
}
|
||||
|
||||
fn successors<'graph>(&'graph self, node: Self::Node)
|
||||
fn successors<'graph>(&'graph self,
|
||||
node: Self::Node)
|
||||
-> <Self as GraphSuccessors<'graph>>::Iter {
|
||||
self.base_graph.predecessors(node)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user