Search in sources :

Example 1 with IntStack

use of com.intellij.util.containers.IntStack in project intellij-community by JetBrains.

the class ControlFlowUtil method process.

/**
   * 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;
}
Also used : IntStack(com.intellij.util.containers.IntStack)

Example 2 with IntStack

use of com.intellij.util.containers.IntStack in project intellij-community by JetBrains.

the class IndentsPass method buildDescriptors.

private List<IndentGuideDescriptor> buildDescriptors() {
    if (!myEditor.getSettings().isIndentGuidesShown())
        return Collections.emptyList();
    IndentsCalculator calculator = new IndentsCalculator();
    calculator.calculate();
    int[] lineIndents = calculator.lineIndents;
    IntStack lines = new IntStack();
    IntStack indents = new IntStack();
    lines.push(0);
    indents.push(0);
    assert myDocument != null;
    List<IndentGuideDescriptor> descriptors = new ArrayList<>();
    for (int line = 1; line < lineIndents.length; line++) {
        ProgressManager.checkCanceled();
        int curIndent = Math.abs(lineIndents[line]);
        while (!indents.empty() && curIndent <= indents.peek()) {
            ProgressManager.checkCanceled();
            final int level = indents.pop();
            int startLine = lines.pop();
            if (level > 0) {
                for (int i = startLine; i < line; i++) {
                    if (level != Math.abs(lineIndents[i])) {
                        descriptors.add(createDescriptor(level, startLine, line, lineIndents));
                        break;
                    }
                }
            }
        }
        int prevLine = line - 1;
        int prevIndent = Math.abs(lineIndents[prevLine]);
        if (curIndent - prevIndent > 1) {
            lines.push(prevLine);
            indents.push(prevIndent);
        }
    }
    while (!indents.empty()) {
        ProgressManager.checkCanceled();
        final int level = indents.pop();
        int startLine = lines.pop();
        if (level > 0) {
            descriptors.add(createDescriptor(level, startLine, myDocument.getLineCount(), lineIndents));
        }
    }
    return descriptors;
}
Also used : IntStack(com.intellij.util.containers.IntStack)

Example 3 with IntStack

use of com.intellij.util.containers.IntStack in project intellij-community by JetBrains.

the class ControlFlowUtil method iteratePrev.

/**
   * Iterates over write instructions in CFG with reversed order
   */
public static void iteratePrev(final int startInstruction, @NotNull final Instruction[] instructions, @NotNull final Function<Instruction, Operation> closure) {
    final IntStack stack = new IntStack(instructions.length);
    final boolean[] visited = new boolean[instructions.length];
    stack.push(startInstruction);
    while (!stack.empty()) {
        ProgressManager.checkCanceled();
        final int num = stack.pop();
        final Instruction instr = instructions[num];
        final Operation nextOperation = closure.fun(instr);
        // Just ignore previous instructions for current node and move further
        if (nextOperation == Operation.CONTINUE) {
            continue;
        }
        // STOP iteration
        if (nextOperation == Operation.BREAK) {
            break;
        }
        // If we are here, we should process previous nodes in natural way
        assert nextOperation == Operation.NEXT;
        for (Instruction pred : instr.allPred()) {
            final int predNum = pred.num();
            if (!visited[predNum]) {
                visited[predNum] = true;
                stack.push(predNum);
            }
        }
    }
}
Also used : IntStack(com.intellij.util.containers.IntStack)

Example 4 with IntStack

use of com.intellij.util.containers.IntStack in project intellij-community by JetBrains.

the class ControlFlowUtil method postOrder.

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;
}
Also used : IntStack(com.intellij.util.containers.IntStack)

Example 5 with IntStack

use of com.intellij.util.containers.IntStack in project intellij-community by JetBrains.

the class DfsUtil method walk.

public static void walk(int startRowIndex, @NotNull NextNode nextNodeFun) {
    IntStack stack = new IntStack();
    stack.push(startRowIndex);
    while (!stack.empty()) {
        int nextNode = nextNodeFun.fun(stack.peek());
        if (nextNode == NextNode.EXIT)
            return;
        if (nextNode != NextNode.NODE_NOT_FOUND) {
            stack.push(nextNode);
        } else {
            stack.pop();
        }
    }
    stack.clear();
}
Also used : IntStack(com.intellij.util.containers.IntStack)

Aggregations

IntStack (com.intellij.util.containers.IntStack)5