Search in sources :

Example 1 with TIntIterator

use of gnu.trove.TIntIterator in project intellij-community by JetBrains.

the class RichControlFlow method reducible.

// Tarjan. Testing flow graph reducibility.
// Journal of Computer and System Sciences 9.3 (1974): 355-365.
public boolean reducible() {
    if (dfsTree.back.isEmpty()) {
        return true;
    }
    int size = controlFlow.transitions.length;
    boolean[] loopEnters = dfsTree.loopEnters;
    TIntHashSet[] cycleIncomings = new TIntHashSet[size];
    // really this may be array, since dfs already ensures no duplicates
    TIntArrayList[] nonCycleIncomings = new TIntArrayList[size];
    int[] collapsedTo = new int[size];
    int[] queue = new int[size];
    int top;
    for (int i = 0; i < size; i++) {
        if (loopEnters[i]) {
            cycleIncomings[i] = new TIntHashSet();
        }
        nonCycleIncomings[i] = new TIntArrayList();
        collapsedTo[i] = i;
    }
    // from whom back connections
    for (Edge edge : dfsTree.back) {
        cycleIncomings[edge.to].add(edge.from);
    }
    // from whom ordinary connections
    for (Edge edge : dfsTree.nonBack) {
        nonCycleIncomings[edge.to].add(edge.from);
    }
    for (int w = size - 1; w >= 0; w--) {
        top = 0;
        // NB - it is modified later!
        TIntHashSet p = cycleIncomings[w];
        if (p == null) {
            continue;
        }
        TIntIterator iter = p.iterator();
        while (iter.hasNext()) {
            queue[top++] = iter.next();
        }
        while (top > 0) {
            int x = queue[--top];
            TIntArrayList incoming = nonCycleIncomings[x];
            for (int i = 0; i < incoming.size(); i++) {
                int y1 = collapsedTo[incoming.getQuick(i)];
                if (!dfsTree.isDescendant(y1, w)) {
                    return false;
                }
                if (y1 != w && p.add(y1)) {
                    queue[top++] = y1;
                }
            }
        }
        iter = p.iterator();
        while (iter.hasNext()) {
            collapsedTo[iter.next()] = w;
        }
    }
    return true;
}
Also used : TIntIterator(gnu.trove.TIntIterator) Edge(com.intellij.codeInspection.bytecodeAnalysis.asm.ControlFlowGraph.Edge) TIntHashSet(gnu.trove.TIntHashSet) TIntArrayList(gnu.trove.TIntArrayList)

Example 2 with TIntIterator

use of gnu.trove.TIntIterator in project intellij-community by JetBrains.

the class Util method findParametersToRemove.

// returns parameters that are used solely in specified expression
@NotNull
public static TIntArrayList findParametersToRemove(@NotNull PsiMethod method, @NotNull final PsiExpression expr, @Nullable final PsiExpression[] occurences) {
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    if (parameters.length == 0)
        return new TIntArrayList();
    PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    final PsiMethod[] allMethods = ArrayUtil.append(overridingMethods, method);
    final TIntHashSet suspects = new TIntHashSet();
    expr.accept(new JavaRecursiveElementWalkingVisitor() {

        @Override
        public void visitReferenceExpression(final PsiReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            PsiElement resolved = expression.resolve();
            if (resolved instanceof PsiParameter) {
                int i = ArrayUtil.find(parameters, resolved);
                if (i != -1) {
                    suspects.add(i);
                }
            }
        }
    });
    final TIntIterator iterator = suspects.iterator();
    while (iterator.hasNext()) {
        final int paramNum = iterator.next();
        for (PsiMethod psiMethod : allMethods) {
            PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
            if (paramNum >= psiParameters.length)
                continue;
            PsiParameter parameter = psiParameters[paramNum];
            if (!ReferencesSearch.search(parameter, parameter.getResolveScope(), false).forEach(reference -> {
                PsiElement element = reference.getElement();
                boolean stillCanBeRemoved = false;
                if (element != null) {
                    stillCanBeRemoved = isAncestor(expr, element, false) || PsiUtil.isInsideJavadocComment(getPhysical(element));
                    if (!stillCanBeRemoved && occurences != null) {
                        for (PsiExpression occurence : occurences) {
                            if (isAncestor(occurence, element, false)) {
                                stillCanBeRemoved = true;
                                break;
                            }
                        }
                    }
                }
                if (!stillCanBeRemoved) {
                    iterator.remove();
                    return false;
                }
                return true;
            }))
                break;
        }
    }
    return new TIntArrayList(suspects.toArray());
}
Also used : ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) TIntHashSet(gnu.trove.TIntHashSet) ArrayUtil(com.intellij.util.ArrayUtil) TIntIterator(gnu.trove.TIntIterator) OverridingMethodsSearch(com.intellij.psi.search.searches.OverridingMethodsSearch) ElementToWorkOn(com.intellij.refactoring.introduceField.ElementToWorkOn) UsageInfo(com.intellij.usageView.UsageInfo) GenerateMembersUtil(com.intellij.codeInsight.generation.GenerateMembersUtil) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) List(java.util.List) Processor(com.intellij.util.Processor) PsiUtil(com.intellij.psi.util.PsiUtil) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) TIntArrayList(gnu.trove.TIntArrayList) TIntIterator(gnu.trove.TIntIterator) TIntArrayList(gnu.trove.TIntArrayList) TIntHashSet(gnu.trove.TIntHashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TIntArrayList (gnu.trove.TIntArrayList)2 TIntHashSet (gnu.trove.TIntHashSet)2 TIntIterator (gnu.trove.TIntIterator)2 GenerateMembersUtil (com.intellij.codeInsight.generation.GenerateMembersUtil)1 Edge (com.intellij.codeInspection.bytecodeAnalysis.asm.ControlFlowGraph.Edge)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1 OverridingMethodsSearch (com.intellij.psi.search.searches.OverridingMethodsSearch)1 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 PsiUtil (com.intellij.psi.util.PsiUtil)1 ElementToWorkOn (com.intellij.refactoring.introduceField.ElementToWorkOn)1 UsageInfo (com.intellij.usageView.UsageInfo)1 ArrayUtil (com.intellij.util.ArrayUtil)1 Processor (com.intellij.util.Processor)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1