import com.intellij.psi.tree.TokenSet;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;
+import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.CharSequenceSubSequence;
import gnu.trove.TIntArrayList;
import org.jetbrains.annotations.NotNull;
* @author yole
*/
public class IndexPatternSearcher implements QueryExecutor<IndexPatternOccurrence, IndexPatternSearch.SearchParameters> {
- public boolean execute(@NotNull final IndexPatternSearch.SearchParameters queryParameters, @NotNull final Processor<IndexPatternOccurrence> consumer) {
+ public boolean execute(@NotNull final IndexPatternSearch.SearchParameters queryParameters,
+ @NotNull final Processor<IndexPatternOccurrence> consumer) {
final PsiFile file = queryParameters.getFile();
VirtualFile virtualFile = file.getVirtualFile();
if (file instanceof PsiBinaryFile || file instanceof PsiCompiledElement || virtualFile == null) {
}
private static void mergeCommentLists(TIntArrayList commentStarts,
- TIntArrayList commentEnds,
- TIntArrayList commentStartsList,
- TIntArrayList commentEndsList) {
+ TIntArrayList commentEnds,
+ TIntArrayList commentStartsList,
+ TIntArrayList commentEndsList) {
if (commentStarts.size() == 0 && commentEnds.size() == 0) {
commentStarts.add(commentStartsList.toNativeArray());
commentEnds.add(commentEndsList.toNativeArray());
return;
}
- // TODO: this is costly for large N !
- for (int i = 0; i < commentStartsList.size(); i++) {
- boolean found = false;
- for (int j = 0; j < commentStarts.size(); j++) {
- if (commentStarts.get(j) == commentStartsList.get(i) && commentEnds.get(j) == commentEndsList.get(i)) {
- found = true;
- break;
- }
- }
- if (!found) {
- commentStarts.add(commentStartsList.get(i));
- commentEnds.add(commentEndsList.get(i));
- }
- }
+ ContainerUtil.mergeSortedArrays(commentStarts, commentEnds, commentStartsList, commentEndsList);
}
private static void findComments(final Lexer lexer,
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Factory;
import com.intellij.util.*;
+import gnu.trove.TIntArrayList;
import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
return (List<T>)EmptyList.INSTANCE;
}
+ /**
+ * Merge sorted points, which are sorted by x and with equal x by y.
+ * Result is put to x1 y1.
+ */
+ public static void mergeSortedArrays(TIntArrayList x1,
+ TIntArrayList y1,
+ TIntArrayList x2,
+ TIntArrayList y2) {
+ TIntArrayList newX = new TIntArrayList();
+ TIntArrayList newY = new TIntArrayList();
+
+ int i = 0;
+ int j = 0;
+
+ while (i < x1.size() && j < x2.size()) {
+ if (x1.get(i) < x2.get(j) || (x1.get(i) == x2.get(j) && y1.get(i) < y2.get(j))) {
+ newX.add(x1.get(i));
+ newY.add(y1.get(i));
+ i++;
+ }
+ else if (x1.get(i) > x2.get(j) || (x1.get(i) == x2.get(j) && y1.get(i) > y2.get(j))) {
+ newX.add(x2.get(j));
+ newY.add(y2.get(j));
+ j++;
+ }
+ else { //equals
+ newX.add(x1.get(i));
+ newY.add(y1.get(i));
+ i++;
+ j++;
+ }
+ }
+
+ while (i < x1.size()) {
+ newX.add(x1.get(i));
+ newY.add(y1.get(i));
+ i++;
+ }
+
+ while (j < x2.size()) {
+ newX.add(x2.get(j));
+ newY.add(y2.get(j));
+ j++;
+ }
+
+ x1.clear();
+ y1.clear();
+ x1.add(newX.toNativeArray());
+ y1.add(newY.toNativeArray());
+ }
+
private static class EmptyList extends AbstractList<Object> implements RandomAccess, Serializable {
private static final EmptyList INSTANCE = new EmptyList();
package com.intellij.util.containers;
import com.intellij.openapi.util.Condition;
+import gnu.trove.TIntArrayList;
import java.util.*;
assertEquals("abccba", log);
}
+
+ public void testMergeSortedArrays() {
+ TIntArrayList x1 = new TIntArrayList(new int[]{0, 2, 4, 6});
+ TIntArrayList y1 = new TIntArrayList(new int[]{0, 2, 4, 6});
+ TIntArrayList x2 = new TIntArrayList(new int[]{1, 2, 2});
+ TIntArrayList y2 = new TIntArrayList(new int[]{1, 2, 3});
+ ContainerUtil.mergeSortedArrays(x1, y1, x2, y2);
+ assertEquals(new TIntArrayList(new int[]{0, 1, 2, 2, 4, 6}), x1);
+ assertEquals(new TIntArrayList(new int[]{0, 1, 2, 3, 4, 6}), y1);
+ x2 = new TIntArrayList(new int[]{1, 2, 2});
+ y2 = new TIntArrayList(new int[]{1, 2, 3});
+ ContainerUtil.mergeSortedArrays(x1, y1, x2, y2);
+ assertEquals(new TIntArrayList(new int[]{0, 1, 2, 2, 4, 6}), x1);
+ assertEquals(new TIntArrayList(new int[]{0, 1, 2, 3, 4, 6}), y1);
+
+ x2 = new TIntArrayList(new int[]{-1, -1, -2});
+ y2 = new TIntArrayList(new int[]{-1, -2, -3});
+ ContainerUtil.mergeSortedArrays(x1, y1, x2, y2);
+ assertEquals(new TIntArrayList(new int[]{-1, -1, -2, 0, 1, 2, 2, 4, 6}), x1);
+ assertEquals(new TIntArrayList(new int[]{-1, -2, -3, 0, 1, 2, 3, 4, 6}), y1);
+ }
}