Search in sources :

Example 11 with TObjectIntHashMap

use of gnu.trove.TObjectIntHashMap in project android by JetBrains.

the class ViewLoader method parseClass.

private static boolean parseClass(@NotNull Class<?> rClass, @NotNull TIntObjectHashMap<Pair<ResourceType, String>> id2res, @NotNull Map<IntArrayWrapper, String> styleableId2Res, @NotNull Map<ResourceType, TObjectIntHashMap<String>> res2id) throws ClassNotFoundException {
    try {
        final Class<?>[] nestedClasses;
        try {
            nestedClasses = rClass.getDeclaredClasses();
        } catch (LinkageError e) {
            final Throwable cause = e.getCause();
            LOG.debug(e);
            if (cause instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) cause;
            }
            throw e;
        }
        for (Class<?> resClass : nestedClasses) {
            final ResourceType resType = ResourceType.getEnum(resClass.getSimpleName());
            if (resType == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("  '%s' is not a valid resource type", anonymizeClassName(resClass.getSimpleName())));
                }
                continue;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  Defining resource type '%s'", anonymizeClassName(resClass.getSimpleName())));
            }
            final TObjectIntHashMap<String> resName2Id = new TObjectIntHashMap<>();
            res2id.put(resType, resName2Id);
            for (Field field : resClass.getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    // May not be final in library projects
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' field is not static, skipping", field.getName()));
                    }
                    continue;
                }
                final Class<?> type = field.getType();
                if (type.isArray() && type.getComponentType() == int.class) {
                    styleableId2Res.put(new IntArrayWrapper((int[]) field.get(null)), field.getName());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int[]", field.getName()));
                    }
                } else if (type == int.class) {
                    final Integer value = (Integer) field.get(null);
                    id2res.put(value, Pair.of(resType, field.getName()));
                    resName2Id.put(field.getName(), value);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("  '%s' defined as int", field.getName()));
                    }
                } else {
                    LOG.error("Unknown field type in R class: " + type);
                }
            }
        }
    } catch (IllegalAccessException e) {
        LOG.info(e);
        return false;
    }
    return true;
}
Also used : ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) PsiClass(com.intellij.psi.PsiClass) IntArrayWrapper(com.android.ide.common.resources.IntArrayWrapper)

Example 12 with TObjectIntHashMap

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

the class ComputeVirtualFileNameStatAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    long start = System.currentTimeMillis();
    suffixes.clear();
    nameCount.clear();
    VirtualFile[] roots = ManagingFS.getInstance().getRoots(LocalFileSystem.getInstance());
    for (VirtualFile root : roots) {
        compute(root);
    }
    final List<Pair<String, Integer>> names = new ArrayList<>(nameCount.size());
    nameCount.forEachEntry(new TObjectIntProcedure<String>() {

        @Override
        public boolean execute(String name, int count) {
            names.add(Pair.create(name, count));
            return true;
        }
    });
    Collections.sort(names, (o1, o2) -> o2.second - o1.second);
    System.out.println("Most frequent names (" + names.size() + " total):");
    int saveByIntern = 0;
    for (Pair<String, Integer> pair : names) {
        int count = pair.second;
        String name = pair.first;
        System.out.println(name + " -> " + count);
        saveByIntern += count * name.length();
        if (count == 1)
            break;
    }
    System.out.println("Total save if names were interned: " + saveByIntern + "; ------------");
    //System.out.println("Prefixes: ("+prefixes.size()+" total)");
    //show(prefixes);
    System.out.println("Suffix counts:(" + suffixes.size() + " total)");
    show(suffixes);
    final TObjectIntHashMap<String> save = new TObjectIntHashMap<>();
    // compute economy
    suffixes.forEachEntry(new TObjectIntProcedure<String>() {

        @Override
        public boolean execute(String s, int count) {
            save.put(s, count * s.length());
            return true;
        }
    });
    System.out.println("Supposed save by stripping suffixes: (" + save.size() + " total)");
    final List<Pair<String, Integer>> saveSorted = show(save);
    final List<String> picked = new ArrayList<>();
    while (picked.size() != 15) {
        Pair<String, Integer> cp = saveSorted.get(0);
        final String candidate = cp.first;
        picked.add(candidate);
        System.out.println("Candidate: '" + candidate + "', save = " + cp.second);
        Collections.sort(picked, (o1, o2) -> {
            // longer first
            return o2.length() - o1.length();
        });
        saveSorted.clear();
        // adjust
        suffixes.forEachEntry(new TObjectIntProcedure<String>() {

            @Override
            public boolean execute(String s, int count) {
                for (int i = picked.size() - 1; i >= 0; i--) {
                    String pick = picked.get(i);
                    if (pick.endsWith(s)) {
                        count -= suffixes.get(pick);
                        break;
                    }
                }
                saveSorted.add(Pair.create(s, s.length() * count));
                return true;
            }
        });
        Collections.sort(saveSorted, (o1, o2) -> o2.second.compareTo(o1.second));
    }
    System.out.println("Picked: " + StringUtil.join(picked, s -> "\"" + s + "\"", ","));
    Collections.sort(picked, (o1, o2) -> {
        // longer first
        return o2.length() - o1.length();
    });
    int saved = 0;
    for (int i = 0; i < picked.size(); i++) {
        String s = picked.get(i);
        int count = suffixes.get(s);
        for (int k = 0; k < i; k++) {
            String prev = picked.get(k);
            if (prev.endsWith(s)) {
                count -= suffixes.get(prev);
                break;
            }
        }
        saved += count * s.length();
    }
    System.out.println("total saved = " + saved);
    System.out.println("Time spent: " + (System.currentTimeMillis() - start));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) Pair(com.intellij.openapi.util.Pair)

Example 13 with TObjectIntHashMap

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

the class SimpleGraphInfo method build.

public static <CommitId> SimpleGraphInfo<CommitId> build(@NotNull LinearGraph linearGraph, @NotNull GraphLayout oldLayout, @NotNull PermanentCommitsInfo<CommitId> permanentCommitsInfo, int permanentGraphSize, @NotNull Set<Integer> branchNodeIds) {
    // todo get first visible row from table somehow
    int firstVisibleRow = VISIBLE_RANGE;
    int start = Math.max(0, firstVisibleRow - VISIBLE_RANGE);
    // no more than 2*1000 commits;
    int end = Math.min(linearGraph.nodesCount(), start + 2 * VISIBLE_RANGE);
    List<GraphCommit<CommitId>> graphCommits = ContainerUtil.newArrayListWithCapacity(end - start);
    List<CommitId> commitsIdMap = ContainerUtil.newArrayListWithCapacity(end - start);
    for (int row = start; row < end; row++) {
        int nodeId = linearGraph.getNodeId(row);
        CommitId commit = permanentCommitsInfo.getCommitId(nodeId);
        List<CommitId> parents = ContainerUtil.newSmartList();
        parents.addAll(ContainerUtil.mapNotNull(asLiteLinearGraph(linearGraph).getNodes(row, LiteLinearGraph.NodeFilter.DOWN), row1 -> {
            if (row1 < start || row1 >= end)
                return null;
            return permanentCommitsInfo.getCommitId(linearGraph.getNodeId(row1));
        }));
        graphCommits.add(GraphCommitImpl.createCommit(commit, parents, permanentCommitsInfo.getTimestamp(nodeId)));
        commitsIdMap.add(commit);
    }
    IntTimestampGetter timestampGetter = PermanentCommitsInfoImpl.createTimestampGetter(graphCommits);
    NotNullFunction<Integer, CommitId> function = createCommitIdMapFunction(commitsIdMap);
    PermanentLinearGraphImpl newLinearGraph = PermanentLinearGraphBuilder.newInstance(graphCommits).build();
    int[] layoutIndexes = new int[end - start];
    List<Integer> headNodeIndexes = ContainerUtil.newArrayList();
    TObjectIntHashMap<CommitId> commitIdToInteger = reverseCommitIdMap(permanentCommitsInfo, permanentGraphSize);
    for (int row = start; row < end; row++) {
        CommitId commitId = commitsIdMap.get(row - start);
        int layoutIndex = oldLayout.getLayoutIndex(commitIdToInteger.get(commitId));
        layoutIndexes[row - start] = layoutIndex;
        if (asLiteLinearGraph(newLinearGraph).getNodes(row - start, LiteLinearGraph.NodeFilter.UP).isEmpty()) {
            headNodeIndexes.add(row - start);
        }
    }
    ContainerUtil.sort(headNodeIndexes, Comparator.comparingInt(o -> layoutIndexes[o]));
    int[] starts = new int[headNodeIndexes.size()];
    for (int i = 0; i < starts.length; i++) {
        starts[i] = layoutIndexes[headNodeIndexes.get(i)];
    }
    GraphLayoutImpl newLayout = new GraphLayoutImpl(layoutIndexes, headNodeIndexes, starts);
    return new SimpleGraphInfo<>(newLinearGraph, newLayout, function, timestampGetter, LinearGraphUtils.convertIdsToNodeIndexes(linearGraph, branchNodeIds));
}
Also used : GraphCommitImpl(com.intellij.vcs.log.graph.GraphCommitImpl) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TimestampGetter(com.intellij.vcs.log.graph.utils.TimestampGetter) CompressedIntList(com.intellij.vcs.log.graph.utils.impl.CompressedIntList) ContainerUtil(com.intellij.util.containers.ContainerUtil) IntList(com.intellij.vcs.log.graph.utils.IntList) LiteLinearGraph(com.intellij.vcs.log.graph.api.LiteLinearGraph) LinearGraph(com.intellij.vcs.log.graph.api.LinearGraph) PermanentLinearGraphImpl(com.intellij.vcs.log.graph.impl.permanent.PermanentLinearGraphImpl) LinearGraphUtils(com.intellij.vcs.log.graph.utils.LinearGraphUtils) GraphCommit(com.intellij.vcs.log.graph.GraphCommit) GraphLayoutImpl(com.intellij.vcs.log.graph.impl.permanent.GraphLayoutImpl) NotNullFunction(com.intellij.util.NotNullFunction) Collection(java.util.Collection) Set(java.util.Set) PermanentCommitsInfo(com.intellij.vcs.log.graph.api.permanent.PermanentCommitsInfo) PermanentCommitsInfoImpl(com.intellij.vcs.log.graph.impl.permanent.PermanentCommitsInfoImpl) GraphLayout(com.intellij.vcs.log.graph.api.GraphLayout) List(java.util.List) PermanentLinearGraphBuilder(com.intellij.vcs.log.graph.impl.permanent.PermanentLinearGraphBuilder) PermanentGraphInfo(com.intellij.vcs.log.graph.api.permanent.PermanentGraphInfo) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) IntTimestampGetter(com.intellij.vcs.log.graph.utils.impl.IntTimestampGetter) LinearGraphUtils.asLiteLinearGraph(com.intellij.vcs.log.graph.utils.LinearGraphUtils.asLiteLinearGraph) PermanentLinearGraphImpl(com.intellij.vcs.log.graph.impl.permanent.PermanentLinearGraphImpl) IntTimestampGetter(com.intellij.vcs.log.graph.utils.impl.IntTimestampGetter) GraphLayoutImpl(com.intellij.vcs.log.graph.impl.permanent.GraphLayoutImpl) GraphCommit(com.intellij.vcs.log.graph.GraphCommit)

Example 14 with TObjectIntHashMap

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

the class JavaArrangementParseInfo method getOverriddenMethods.

@NotNull
public List<JavaArrangementOverriddenMethodsInfo> getOverriddenMethods() {
    List<JavaArrangementOverriddenMethodsInfo> result = new ArrayList<>();
    final TObjectIntHashMap<PsiMethod> weights = new TObjectIntHashMap<>();
    Comparator<Pair<PsiMethod, PsiMethod>> comparator = (o1, o2) -> weights.get(o1.first) - weights.get(o2.first);
    for (Map.Entry<PsiClass, List<Pair<PsiMethod, PsiMethod>>> entry : myOverriddenMethods.entrySet()) {
        JavaArrangementOverriddenMethodsInfo info = new JavaArrangementOverriddenMethodsInfo(entry.getKey().getName());
        weights.clear();
        int i = 0;
        for (PsiMethod method : entry.getKey().getMethods()) {
            weights.put(method, i++);
        }
        ContainerUtil.sort(entry.getValue(), comparator);
        for (Pair<PsiMethod, PsiMethod> pair : entry.getValue()) {
            JavaElementArrangementEntry overridingMethodEntry = myMethodEntriesMap.get(pair.second);
            if (overridingMethodEntry != null) {
                info.addMethodEntry(overridingMethodEntry);
            }
        }
        if (!info.getMethodEntries().isEmpty()) {
            result.add(info);
        }
    }
    return result;
}
Also used : Nullable(org.jetbrains.annotations.Nullable) PsiClass(com.intellij.psi.PsiClass) java.util(java.util) Stack(com.intellij.util.containers.Stack) PsiMethod(com.intellij.psi.PsiMethod) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) Pair(com.intellij.openapi.util.Pair) PsiField(com.intellij.psi.PsiField) ContainerUtilRt(com.intellij.util.containers.ContainerUtilRt) ContainerUtil(com.intellij.util.containers.ContainerUtil) NotNull(org.jetbrains.annotations.NotNull) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with TObjectIntHashMap

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

the class OfflineViewParseUtil method parse.

public static Map<String, Set<OfflineProblemDescriptor>> parse(final String problems) {
    final TObjectIntHashMap<String> fqName2IdxMap = new TObjectIntHashMap<>();
    final Map<String, Set<OfflineProblemDescriptor>> package2Result = new THashMap<>();
    final XppReader reader = new XppReader(new StringReader(problems));
    try {
        while (reader.hasMoreChildren()) {
            //problem
            reader.moveDown();
            final OfflineProblemDescriptor descriptor = new OfflineProblemDescriptor();
            boolean added = false;
            while (reader.hasMoreChildren()) {
                reader.moveDown();
                if (SmartRefElementPointerImpl.ENTRY_POINT.equals(reader.getNodeName())) {
                    descriptor.setType(reader.getAttribute(SmartRefElementPointerImpl.TYPE_ATTR));
                    final String fqName = reader.getAttribute(SmartRefElementPointerImpl.FQNAME_ATTR);
                    descriptor.setFQName(fqName);
                    if (!fqName2IdxMap.containsKey(fqName)) {
                        fqName2IdxMap.put(fqName, 0);
                    }
                    int idx = fqName2IdxMap.get(fqName);
                    descriptor.setProblemIndex(idx);
                    fqName2IdxMap.put(fqName, idx + 1);
                }
                if (DESCRIPTION.equals(reader.getNodeName())) {
                    descriptor.setDescription(reader.getValue());
                }
                if (LINE.equals(reader.getNodeName())) {
                    descriptor.setLine(Integer.parseInt(reader.getValue()));
                }
                if (MODULE.equals(reader.getNodeName())) {
                    descriptor.setModule(reader.getValue());
                }
                if (HINTS.equals(reader.getNodeName())) {
                    while (reader.hasMoreChildren()) {
                        reader.moveDown();
                        List<String> hints = descriptor.getHints();
                        if (hints == null) {
                            hints = new ArrayList<>();
                            descriptor.setHints(hints);
                        }
                        hints.add(reader.getAttribute("value"));
                        reader.moveUp();
                    }
                }
                if (PACKAGE.equals(reader.getNodeName())) {
                    appendDescriptor(package2Result, reader.getValue(), descriptor);
                    added = true;
                }
                while (reader.hasMoreChildren()) {
                    reader.moveDown();
                    if (PACKAGE.equals(reader.getNodeName())) {
                        appendDescriptor(package2Result, reader.getValue(), descriptor);
                        added = true;
                    }
                    reader.moveUp();
                }
                reader.moveUp();
            }
            if (!added)
                appendDescriptor(package2Result, "", descriptor);
            reader.moveUp();
        }
    } finally {
        reader.close();
    }
    return package2Result;
}
Also used : Set(java.util.Set) THashSet(gnu.trove.THashSet) OfflineProblemDescriptor(com.intellij.codeInspection.offline.OfflineProblemDescriptor) THashMap(gnu.trove.THashMap) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) XppReader(com.thoughtworks.xstream.io.xml.XppReader) StringReader(java.io.StringReader)

Aggregations

TObjectIntHashMap (gnu.trove.TObjectIntHashMap)18 TIntObjectHashMap (gnu.trove.TIntObjectHashMap)4 NotNull (org.jetbrains.annotations.NotNull)4 ResourceType (com.android.resources.ResourceType)3 Pair (com.intellij.openapi.util.Pair)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 IntArrayWrapper (com.android.ide.common.resources.IntArrayWrapper)2 PsiClass (com.intellij.psi.PsiClass)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2 Set (java.util.Set)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)1 Pair (com.android.util.Pair)1 QuickFixBundle (com.intellij.codeInsight.daemon.QuickFixBundle)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 OfflineProblemDescriptor (com.intellij.codeInspection.offline.OfflineProblemDescriptor)1 PsiFragment (com.intellij.dupLocator.util.PsiFragment)1