1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Improved an inefficient algorithm in

LocationMap.getSequenceNumberForFileOffset.
This commit is contained in:
Sergey Prigogin 2014-04-16 20:39:54 -07:00
parent 48fc8d0809
commit 63cb636c7a

View file

@ -13,8 +13,10 @@ package org.eclipse.cdt.internal.core.parser.scanner;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.IASTComment; import org.eclipse.cdt.core.dom.ast.IASTComment;
@ -65,6 +67,8 @@ public class LocationMap implements ILocationResolver {
// Stuff computed on demand // Stuff computed on demand
private IdentityHashMap<IBinding, IASTPreprocessorMacroDefinition> fMacroDefinitionMap; private IdentityHashMap<IBinding, IASTPreprocessorMacroDefinition> fMacroDefinitionMap;
private List<ISkippedIndexedFilesListener> fSkippedFilesListeners= new ArrayList<>(); private List<ISkippedIndexedFilesListener> fSkippedFilesListeners= new ArrayList<>();
// Keyed by file location.
private Map<String, LocationCtxFile> fFileContexts;
public LocationMap(LexerOptions lexOptions) { public LocationMap(LexerOptions lexOptions) {
fLexerOptions= lexOptions; fLexerOptions= lexOptions;
@ -667,23 +671,51 @@ public class LocationMap implements ILocationResolver {
@Override @Override
public int getSequenceNumberForFileOffset(String filePath, int fileOffset) { public int getSequenceNumberForFileOffset(String filePath, int fileOffset) {
LocationCtx ctx= fRootContext; LocationCtxFile ctx= fRootContext;
if (filePath != null) { if (filePath != null) {
ArrayDeque<LocationCtx> contexts= new ArrayDeque<>(); if (fFileContexts == null) {
while (ctx != null) { // Build a map of file contexts keyed by file locations to speed up subsequent calls.
if (ctx instanceof LocationCtxFile) { fFileContexts = new HashMap<>();
if (filePath.equals(ctx.getFilePath())) { fFileContexts.put(fRootContext.getFilePath(), fRootContext);
break; ArrayDeque<LocationCtxContainer> queue= new ArrayDeque<>();
for (LocationCtxContainer c = fRootContext; c != null; c = queue.pollFirst()) {
for (LocationCtx child : c.getChildren()) {
if (child instanceof LocationCtxFile) {
LocationCtxFile childFileContext = (LocationCtxFile) child;
String path = childFileContext.getFilePath();
if (!fFileContexts.containsKey(path)) {
fFileContexts.put(path, childFileContext);
queue.add(childFileContext);
}
} else if (child instanceof LocationCtxContainer) {
queue.add((LocationCtxContainer) child);
}
} }
} }
contexts.addAll(ctx.getChildren());
ctx= contexts.pollFirst();
} }
ctx = fFileContexts.get(filePath);
} }
if (ctx != null) { if (ctx == null) {
return ctx.getSequenceNumberForOffset(fileOffset, true); return -1;
} }
return -1; return ctx.getSequenceNumberForOffset(fileOffset, true);
// LocationCtx ctx= fRootContext;
// if (filePath != null) {
// ArrayDeque<LocationCtx> contexts= new ArrayDeque<>();
// while (ctx != null) {
// if (ctx instanceof LocationCtxFile) {
// if (filePath.equals(ctx.getFilePath())) {
// break;
// }
// }
// contexts.addAll(ctx.getChildren());
// ctx= contexts.pollFirst();
// }
// }
// if (ctx != null) {
// return ctx.getSequenceNumberForOffset(fileOffset, true);
// }
// return -1;
} }
@Override @Override