1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::collections::HashMap;
16 use super::{ControlFlowGraph, GraphPredecessors, GraphSuccessors};
18 pub struct TestGraph {
21 successors: HashMap<usize, Vec<usize>>,
22 predecessors: HashMap<usize, Vec<usize>>,
26 pub fn new(start_node: usize, edges: &[(usize, usize)]) -> Self {
27 let mut graph = TestGraph {
28 num_nodes: start_node + 1,
30 successors: HashMap::new(),
31 predecessors: HashMap::new(),
33 for &(source, target) in edges {
34 graph.num_nodes = max(graph.num_nodes, source + 1);
35 graph.num_nodes = max(graph.num_nodes, target + 1);
36 graph.successors.entry(source).or_insert(vec![]).push(target);
37 graph.predecessors.entry(target).or_insert(vec![]).push(source);
39 for node in 0..graph.num_nodes {
40 graph.successors.entry(node).or_insert(vec![]);
41 graph.predecessors.entry(node).or_insert(vec![]);
47 impl ControlFlowGraph for TestGraph {
50 fn start_node(&self) -> usize {
54 fn num_nodes(&self) -> usize {
58 fn predecessors<'graph>(&'graph self,
60 -> <Self as GraphPredecessors<'graph>>::Iter {
61 self.predecessors[&node].iter().cloned()
64 fn successors<'graph>(&'graph self, node: usize) -> <Self as GraphSuccessors<'graph>>::Iter {
65 self.successors[&node].iter().cloned()
69 impl<'graph> GraphPredecessors<'graph> for TestGraph {
71 type Iter = iter::Cloned<slice::Iter<'graph, usize>>;
74 impl<'graph> GraphSuccessors<'graph> for TestGraph {
76 type Iter = iter::Cloned<slice::Iter<'graph, usize>>;