1 19 20 25 26 27 package soot.toolkits.graph; 28 29 import soot.*; 30 import soot.util.*; 31 import java.util.*; 32 33 34 43 44 public class SlowPseudoTopologicalOrderer 45 { 46 public SlowPseudoTopologicalOrderer( Singletons.Global g ) {} 47 public static SlowPseudoTopologicalOrderer v() { return G.v().soot_toolkits_graph_SlowPseudoTopologicalOrderer(); } 48 49 public static final boolean REVERSE = true; 50 public SlowPseudoTopologicalOrderer() {} 51 public SlowPseudoTopologicalOrderer(boolean isReversed) { mIsReversed = isReversed;} 52 53 private Map stmtToColor; 54 private static final int 55 WHITE = 0, 56 GRAY = 1, 57 BLACK = 2; 58 59 private LinkedList order; 60 private boolean mIsReversed = false; 61 private DirectedGraph graph; 62 63 private List reverseOrder; 64 private HashMap succsMap = new HashMap(); 65 66 70 public List newList(DirectedGraph g) 71 { 72 return computeOrder(g); 73 } 74 75 79 public void setReverseOrder(boolean isReversed) 80 { 81 mIsReversed = isReversed; 82 } 83 84 88 public boolean isReverseOrder() 89 { 90 return mIsReversed; 91 } 92 93 98 LinkedList computeOrder(DirectedGraph g) 99 { 100 stmtToColor = new HashMap(); 101 102 order = new LinkedList(); 103 graph = g; 104 105 PseudoTopologicalReverseOrderer orderer = 106 new PseudoTopologicalReverseOrderer(); 107 108 reverseOrder = orderer.newList(g); 109 110 { 112 113 114 Iterator stmtIt = g.iterator(); 115 while(stmtIt.hasNext()) 116 { 117 Object s = stmtIt.next(); 118 119 stmtToColor.put(s, new Integer (WHITE)); 120 } 121 } 122 123 { 125 Iterator stmtIt = g.iterator(); 126 127 while(stmtIt.hasNext()) 128 { 129 Object s = stmtIt.next(); 130 131 if(((Integer ) stmtToColor.get(s)).intValue() == WHITE) 132 visitNode(s); 133 } 134 } 135 136 return order; 137 } 138 139 142 145 146 private void visitNode(Object startStmt) 147 { 148 LinkedList stmtStack = new LinkedList(); 149 LinkedList indexStack = new LinkedList(); 150 151 stmtToColor.put(startStmt, new Integer (GRAY)); 152 153 stmtStack.addLast(startStmt); 154 indexStack.addLast(new Integer (-1)); 155 156 while(!stmtStack.isEmpty()) 157 { 158 int toVisitIndex = ((Integer ) indexStack.removeLast()).intValue(); 159 Object toVisitNode = stmtStack.getLast(); 160 161 toVisitIndex++; 162 163 indexStack.addLast(new Integer (toVisitIndex)); 164 165 if(toVisitIndex >= graph.getSuccsOf(toVisitNode).size()) 166 { 167 if(mIsReversed) 169 order.addLast(toVisitNode); 170 else 171 order.addFirst(toVisitNode); 172 173 stmtToColor.put(toVisitNode, new Integer (BLACK)); 174 175 stmtStack.removeLast(); 177 indexStack.removeLast(); 178 } 179 else 180 { 181 List orderedSuccs = (List)succsMap.get(toVisitNode); 182 if (orderedSuccs == null) 183 { 184 orderedSuccs = new LinkedList(); 185 succsMap.put(toVisitNode, orderedSuccs); 186 187 188 List allsuccs = graph.getSuccsOf(toVisitNode); 189 190 for (int i=0; i<allsuccs.size(); i++) 191 { 192 Object cur = allsuccs.get(i); 193 194 int j=0; 195 196 for (; j<orderedSuccs.size(); j++) 197 { 198 Object comp = orderedSuccs.get(j); 199 200 int idx1 = reverseOrder.indexOf(cur); 201 int idx2 = reverseOrder.indexOf(comp); 202 203 if (idx1 < idx2) 204 break; 205 } 206 207 orderedSuccs.add(j, cur); 208 } 209 } 210 211 Object childNode = orderedSuccs.get(toVisitIndex); 212 213 if(((Integer ) stmtToColor.get(childNode)).intValue() == WHITE) 215 { 216 stmtToColor.put(childNode, new Integer (GRAY)); 217 218 stmtStack.addLast(childNode); 219 indexStack.addLast(new Integer (-1)); 220 } 221 } 222 } 223 } 224 225 private class PseudoTopologicalReverseOrderer 226 { 227 private Map stmtToColor; 228 private static final int 229 WHITE = 0, 230 GRAY = 1, 231 BLACK = 2; 232 233 private LinkedList order; 234 private boolean mIsReversed = false; 235 private DirectedGraph graph; 236 237 241 List newList(DirectedGraph g) 242 { 243 return computeOrder(g); 244 } 245 246 251 LinkedList computeOrder(DirectedGraph g) 252 { 253 stmtToColor = new HashMap(); 254 255 order = new LinkedList(); 256 graph = g; 257 258 { 260 Iterator stmtIt = g.iterator(); 261 while(stmtIt.hasNext()) 262 { 263 Object s = stmtIt.next(); 264 265 stmtToColor.put(s, new Integer (WHITE)); 266 } 267 } 268 269 { 271 Iterator stmtIt = g.iterator(); 272 273 while(stmtIt.hasNext()) 274 { 275 Object s = stmtIt.next(); 276 277 if(((Integer ) stmtToColor.get(s)).intValue() == WHITE) 278 visitNode(s); 279 } 280 } 281 282 return order; 283 } 284 285 private void visitNode(Object startStmt) 286 { 287 LinkedList stmtStack = new LinkedList(); 288 LinkedList indexStack = new LinkedList(); 289 290 stmtToColor.put(startStmt, new Integer (GRAY)); 291 292 stmtStack.addLast(startStmt); 293 indexStack.addLast(new Integer (-1)); 294 295 while(!stmtStack.isEmpty()) 296 { 297 int toVisitIndex = ((Integer ) indexStack.removeLast()).intValue(); 298 Object toVisitNode = stmtStack.getLast(); 299 300 toVisitIndex++; 301 302 indexStack.addLast(new Integer (toVisitIndex)); 303 304 if(toVisitIndex >= graph.getPredsOf(toVisitNode).size()) 305 { 306 if(mIsReversed) 308 order.addLast(toVisitNode); 309 else 310 order.addFirst(toVisitNode); 311 312 stmtToColor.put(toVisitNode, new Integer (BLACK)); 313 314 stmtStack.removeLast(); 316 indexStack.removeLast(); 317 } 318 else 319 { 320 Object childNode = graph.getPredsOf(toVisitNode).get(toVisitIndex); 321 322 if(((Integer ) stmtToColor.get(childNode)).intValue() == WHITE) 324 { 325 stmtToColor.put(childNode, new Integer (GRAY)); 326 327 stmtStack.addLast(childNode); 328 indexStack.addLast(new Integer (-1)); 329 } 330 } 331 } 332 } 333 334 } 335 336 } 337 | Popular Tags |