From 99436f01e930c40f94a9dcde512e07efa141b1f1 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 5 Jun 2017 17:30:29 +0300 Subject: [PATCH] moved methods not general enough close to their usages --- .../psi/impl/search/IndexPatternSearcher.java | 54 ++++++++++++++++++- .../impl/search/LowLevelSearchUtilTest.java | 21 ++++++++ .../util/containers/ContainerUtilTest.java | 23 -------- .../util/containers/ContainerUtil.java | 51 ------------------ 4 files changed, 73 insertions(+), 76 deletions(-) diff --git a/platform/editor-ui-ex/src/com/intellij/psi/impl/search/IndexPatternSearcher.java b/platform/editor-ui-ex/src/com/intellij/psi/impl/search/IndexPatternSearcher.java index adc24b1a1c95..0ac1764a0095 100644 --- a/platform/editor-ui-ex/src/com/intellij/psi/impl/search/IndexPatternSearcher.java +++ b/platform/editor-ui-ex/src/com/intellij/psi/impl/search/IndexPatternSearcher.java @@ -39,7 +39,6 @@ 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; @@ -169,7 +168,58 @@ public class IndexPatternSearcher implements QueryExecutor 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 void findComments(final Lexer lexer, diff --git a/platform/platform-tests/testSrc/com/intellij/psi/impl/search/LowLevelSearchUtilTest.java b/platform/platform-tests/testSrc/com/intellij/psi/impl/search/LowLevelSearchUtilTest.java index c99fe769a89b..35885af6854a 100644 --- a/platform/platform-tests/testSrc/com/intellij/psi/impl/search/LowLevelSearchUtilTest.java +++ b/platform/platform-tests/testSrc/com/intellij/psi/impl/search/LowLevelSearchUtilTest.java @@ -74,4 +74,25 @@ public class LowLevelSearchUtilTest extends TestCase { } }).assertTiming(); } + + 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}); + IndexPatternSearcher.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}); + IndexPatternSearcher.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}); + IndexPatternSearcher.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); + } } diff --git a/platform/platform-tests/testSrc/com/intellij/util/containers/ContainerUtilTest.java b/platform/platform-tests/testSrc/com/intellij/util/containers/ContainerUtilTest.java index 79b79c3a499f..e5b3233213de 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/containers/ContainerUtilTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/containers/ContainerUtilTest.java @@ -18,7 +18,6 @@ package com.intellij.util.containers; import com.intellij.openapi.util.Condition; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.util.ArrayUtil; -import gnu.trove.TIntArrayList; import org.junit.Test; import java.util.*; @@ -108,28 +107,6 @@ public class ContainerUtilTest { assertEquals("abc" + "cba", log); } - @Test - 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); - } - @Test public void testLockFreeSingleThreadPerformance() { final List my = new LockFreeCopyOnWriteArrayList<>(); diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index 088285dc5756..8eaea1201e15 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -2187,57 +2187,6 @@ public class ContainerUtil extends ContainerUtilRt { } } - /** - * Merge sorted points, which are sorted by x and with equal x by y. - * Result is put to x1 y1. - */ - public static void mergeSortedArrays(@NotNull TIntArrayList x1, - @NotNull TIntArrayList y1, - @NotNull TIntArrayList x2, - @NotNull 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()); - } - /** * @return read-only set consisting of the only element o