From 4dec7e3937c7c604b1b4c1e8cb8ff439716c6fba Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Fri, 25 Feb 2011 13:10:21 +0300 Subject: [PATCH] Added test for TODOs in Django. Removed n^2 scan for merging comments. --- .../psi/impl/search/IndexPatternSearcher.java | 25 +++------ .../util/containers/ContainerUtil.java | 52 +++++++++++++++++++ .../util/containers/ContainerUtilTest.java | 22 ++++++++ 3 files changed, 81 insertions(+), 18 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/impl/search/IndexPatternSearcher.java b/platform/lang-impl/src/com/intellij/psi/impl/search/IndexPatternSearcher.java index df9be16654eb..41b29cf49bab 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/search/IndexPatternSearcher.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/search/IndexPatternSearcher.java @@ -39,6 +39,7 @@ import com.intellij.psi.tree.IElementType; 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; @@ -51,7 +52,8 @@ import java.util.regex.Pattern; * @author yole */ public class IndexPatternSearcher implements QueryExecutor { - public boolean execute(@NotNull final IndexPatternSearch.SearchParameters queryParameters, @NotNull final Processor consumer) { + public boolean execute(@NotNull final IndexPatternSearch.SearchParameters queryParameters, + @NotNull final Processor consumer) { final PsiFile file = queryParameters.getFile(); VirtualFile virtualFile = file.getVirtualFile(); if (file instanceof PsiBinaryFile || file instanceof PsiCompiledElement || virtualFile == null) { @@ -161,29 +163,16 @@ public class IndexPatternSearcher implements QueryExecutor)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 implements RandomAccess, Serializable { private static final EmptyList INSTANCE = new EmptyList(); diff --git a/platform/util/testSrc/com/intellij/util/containers/ContainerUtilTest.java b/platform/util/testSrc/com/intellij/util/containers/ContainerUtilTest.java index c7e8d7f68faa..34e0c723fba5 100644 --- a/platform/util/testSrc/com/intellij/util/containers/ContainerUtilTest.java +++ b/platform/util/testSrc/com/intellij/util/containers/ContainerUtilTest.java @@ -17,6 +17,7 @@ package com.intellij.util.containers; import com.intellij.openapi.util.Condition; +import gnu.trove.TIntArrayList; import java.util.*; @@ -90,4 +91,25 @@ public class ContainerUtilTest extends junit.framework.TestCase { 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); + } }