You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2010/12/12 17:23:51 UTC
svn commit: r1044844 - in /lucene/dev/branches/branch_3x: ./ .hgignore
lucene/ lucene/src/java/org/apache/lucene/util/ArrayUtil.java solr/
Author: uschindler
Date: Sun Dec 12 16:23:51 2010
New Revision: 1044844
URL: http://svn.apache.org/viewvc?rev=1044844&view=rev
Log:
Backport ArrayUtil.grow(T[]) and other useful additions
Modified:
lucene/dev/branches/branch_3x/ (props changed)
lucene/dev/branches/branch_3x/.hgignore
lucene/dev/branches/branch_3x/lucene/ (props changed)
lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/ArrayUtil.java
lucene/dev/branches/branch_3x/solr/ (props changed)
Modified: lucene/dev/branches/branch_3x/.hgignore
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/.hgignore?rev=1044844&r1=1044843&r2=1044844&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/.hgignore (original)
+++ lucene/dev/branches/branch_3x/.hgignore Sun Dec 12 16:23:51 2010
@@ -1,2 +1,4 @@
syntax: glob
*/build/*
+*.class
+
Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/ArrayUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/ArrayUtil.java?rev=1044844&r1=1044843&r2=1044844&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/ArrayUtil.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/util/ArrayUtil.java Sun Dec 12 16:23:51 2010
@@ -19,6 +19,7 @@ package org.apache.lucene.util;
import java.util.Collection;
import java.util.Comparator;
+import java.lang.reflect.Array;
/**
* Methods for manipulating arrays.
@@ -339,7 +340,7 @@ public final class ArrayUtil {
}
/**
- * Returns hash of chars in range start (inclusive) to
+ * Returns hash of bytes in range start (inclusive) to
* end (inclusive)
*/
public static int hashCode(byte[] array, int start, int end) {
@@ -348,6 +349,98 @@ public final class ArrayUtil {
code = code * 31 + array[i];
return code;
}
+
+
+ // Since Arrays.equals doesn't implement offsets for equals
+ /**
+ * See if two array slices are the same.
+ *
+ * @param left The left array to compare
+ * @param offsetLeft The offset into the array. Must be positive
+ * @param right The right array to compare
+ * @param offsetRight the offset into the right array. Must be positive
+ * @param length The length of the section of the array to compare
+ * @return true if the two arrays, starting at their respective offsets, are equal
+ *
+ * @see java.util.Arrays#equals(char[], char[])
+ */
+ public static boolean equals(char[] left, int offsetLeft, char[] right, int offsetRight, int length) {
+ if ((offsetLeft + length <= left.length) && (offsetRight + length <= right.length)) {
+ for (int i = 0; i < length; i++) {
+ if (left[offsetLeft + i] != right[offsetRight + i]) {
+ return false;
+ }
+
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public static <T> T[] grow(T[] array, int minSize) {
+ if (array.length < minSize) {
+ @SuppressWarnings("unchecked") final T[] newArray =
+ (T[]) Array.newInstance(array.getClass().getComponentType(), oversize(minSize, RamUsageEstimator.NUM_BYTES_OBJECT_REF));
+ System.arraycopy(array, 0, newArray, 0, array.length);
+ return newArray;
+ } else
+ return array;
+ }
+
+ public static <T> T[] grow(T[] array) {
+ return grow(array, 1 + array.length);
+ }
+
+ public static <T> T[] shrink(T[] array, int targetSize) {
+ final int newSize = getShrinkSize(array.length, targetSize, RamUsageEstimator.NUM_BYTES_OBJECT_REF);
+ if (newSize != array.length) {
+ @SuppressWarnings("unchecked") final T[] newArray =
+ (T[]) Array.newInstance(array.getClass().getComponentType(), newSize);
+ System.arraycopy(array, 0, newArray, 0, newSize);
+ return newArray;
+ } else
+ return array;
+ }
+
+ // Since Arrays.equals doesn't implement offsets for equals
+ /**
+ * See if two array slices are the same.
+ *
+ * @param left The left array to compare
+ * @param offsetLeft The offset into the array. Must be positive
+ * @param right The right array to compare
+ * @param offsetRight the offset into the right array. Must be positive
+ * @param length The length of the section of the array to compare
+ * @return true if the two arrays, starting at their respective offsets, are equal
+ *
+ * @see java.util.Arrays#equals(char[], char[])
+ */
+ public static boolean equals(int[] left, int offsetLeft, int[] right, int offsetRight, int length) {
+ if ((offsetLeft + length <= left.length) && (offsetRight + length <= right.length)) {
+ for (int i = 0; i < length; i++) {
+ if (left[offsetLeft + i] != right[offsetRight + i]) {
+ return false;
+ }
+
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public static int[] toIntArray(Collection<Integer> ints) {
+
+ final int[] result = new int[ints.size()];
+ int upto = 0;
+ for(int v : ints) {
+ result[upto++] = v;
+ }
+
+ // paranoia:
+ assert upto == result.length;
+
+ return result;
+ }
/** SorterTemplate with custom {@link Comparator} */
private static <T> SorterTemplate getSorter(final T[] a, final Comparator<? super T> comp) {