/** * Process control flow graph in depth first order */ public static boolean process(final Instruction[] flow, final int start, final Processor<Instruction> processor){ final int length = flow.length; boolean[] visited = new boolean[length]; Arrays.fill(visited, false); final IntStack stack = new IntStack(length); stack.push(start); while (!stack.empty()) { ProgressManager.checkCanceled(); final int num = stack.pop(); final Instruction instruction = flow[num]; if (!processor.process(instruction)){ return false; } for (Instruction succ : instruction.allSucc()) { final int succNum = succ.num(); if (!visited[succNum]) { visited[succNum] = true; stack.push(succNum); } } } return true; }
public void nodeDfsIterator(int startRowIndex, @NotNull NextNode nextNodeFun) { IntStack myStack = new IntStack(); myStack.push(startRowIndex); while (!myStack.empty()) { int nextNode = nextNodeFun.fun(myStack.peek()); if (nextNode != NextNode.NODE_NOT_FOUND) { myStack.push(nextNode); } else { myStack.pop(); } } myStack.clear(); }
public static int[] postOrder(Instruction[] flow) { final int length = flow.length; int[] result = new int[length]; boolean[] visited = new boolean[length]; Arrays.fill(visited, false); final IntStack stack = new IntStack(length); int N = 0; for (int i = 0; i < length; i++) { //graph might not be connected if (!visited[i]) { visited[i] = true; stack.clear(); stack.push(i); while (!stack.empty()) { final int num = stack.pop(); result[N++] = num; for (Instruction succ : flow[num].allSucc()) { final int succNum = succ.num(); if (!visited[succNum]) { visited[succNum] = true; stack.push(succNum); } } } } } LOG.assertTrue(N == length); return result; }
public void nodeDfsIterator(int startRowIndex, @Nonnull NextNode nextNodeFun) { IntStack myStack = new IntStack(); myStack.push(startRowIndex); while (!myStack.empty()) { int nextNode = nextNodeFun.fun(myStack.peek()); if (nextNode != NextNode.NODE_NOT_FOUND) { myStack.push(nextNode); } else { myStack.pop(); } } myStack.clear(); }