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;
}
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));
}
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));
}
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;
}
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;
}
Aggregations