1 19 20 package soot.toolkits.graph; 21 22 import java.util.*; 23 24 29 public class HashReversibleGraph extends HashMutableDirectedGraph 30 implements ReversibleGraph 31 { 32 protected boolean reversed; 33 34 public HashReversibleGraph(DirectedGraph dg) 35 { 36 this(); 37 38 for(Iterator i = dg.iterator(); i.hasNext();){ 39 Object s = i.next(); 40 addNode(s); 41 } 42 43 for(Iterator i = dg.iterator(); i.hasNext();){ 44 Object s = i.next(); 45 List succs = dg.getSuccsOf(s); 46 for(Iterator succsIt = succs.iterator(); succsIt.hasNext();){ 47 Object t = succsIt.next(); 48 addEdge(s, t); 49 } 50 } 51 52 53 54 heads.clear(); 55 heads.addAll(dg.getHeads()); 56 tails.clear(); 57 tails.addAll(dg.getTails()); 58 } 59 60 public HashReversibleGraph() 61 { 62 super(); 63 reversed = false; 64 } 65 66 public boolean isReversed() 67 { 68 return reversed; 69 } 70 71 public ReversibleGraph reverse() 72 { 73 reversed = !reversed; 74 return this; 75 } 76 77 public void addEdge(Object from, Object to) 78 { 79 if(reversed) 80 super.addEdge(to, from); 81 else 82 super.addEdge(from, to); 83 } 84 85 public void removeEdge(Object from, Object to) 86 { 87 if(reversed) 88 super.removeEdge(to, from); 89 else 90 super.removeEdge(from, to); 91 } 92 93 public boolean containsEdge(Object from, Object to) 94 { 95 return reversed ? super.containsEdge(to, from) : super.containsEdge(from, to); 96 } 97 98 public List getHeads() 99 { 100 return reversed ? super.getTails() : super.getHeads(); 101 } 102 103 public List getTails() 104 { 105 return reversed ? super.getHeads() : super.getTails(); 106 } 107 108 public List getPredsOf(Object s) 109 { 110 return reversed ? super.getSuccsOf(s) : super.getPredsOf(s); 111 } 112 113 public List getSuccsOf(Object s) 114 { 115 return reversed ? super.getPredsOf(s) : super.getSuccsOf(s); 116 } 117 } 118 | Popular Tags |