moved methods not general enough close to their usages

This commit is contained in:
Alexey Kudravtsev
2017-06-07 12:40:22 +03:00
parent 816434dc54
commit 99436f01e9
4 changed files with 73 additions and 76 deletions
@@ -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<IndexPatternOccurrenc
return;
}
ContainerUtil.mergeSortedArrays(commentStarts, commentEnds, commentStartsList, commentEndsList);
mergeSortedArrays(commentStarts, commentEnds, commentStartsList, commentEndsList);
}
/**
* Merge sorted points, which are sorted by x and with equal x by y.
* Result is put to x1 y1.
*/
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());
}
private static void findComments(final Lexer lexer,
@@ -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);
}
}
@@ -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<Object> my = new LockFreeCopyOnWriteArrayList<>();
@@ -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