Formatting adjustments, courtesy of Dave Hoover.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2325 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2008-06-10 20:40:13 +00:00
parent 7202cbd84e
commit 9e41658569
+55 -84
View File
@@ -35,8 +35,7 @@ public class ArrayUtil
* Looks for an element that tests true for Object equality with the supplied value and * Looks for an element that tests true for Object equality with the supplied value and
* returns its index in the array. * returns its index in the array.
* *
* @return the index of the first matching value if one was found, -1 * @return the index of the first matching value if one was found, -1 otherwise.
* otherwise.
*/ */
public static <T extends Object> int indexOf (T[] values, T value) public static <T extends Object> int indexOf (T[] values, T value)
{ {
@@ -50,11 +49,9 @@ public class ArrayUtil
} }
/** /**
* Looks for an element that is equal to the supplied value and * Looks for an element that is equal to the supplied value and returns its index in the array.
* returns its index in the array.
* *
* @return the index of the first matching value if one was found, -1 * @return the index of the first matching value if one was found, -1 otherwise.
* otherwise.
*/ */
public static int indexOf (byte[] values, byte value) public static int indexOf (byte[] values, byte value)
{ {
@@ -68,11 +65,9 @@ public class ArrayUtil
} }
/** /**
* Looks for an element that is equal to the supplied value and * Looks for an element that is equal to the supplied value and returns its index in the array.
* returns its index in the array.
* *
* @return the index of the first matching value if one was found, -1 * @return the index of the first matching value if one was found, -1 otherwise.
* otherwise.
*/ */
public static int indexOf (float[] values, float value) public static int indexOf (float[] values, float value)
{ {
@@ -159,8 +154,7 @@ public class ArrayUtil
* Shuffles the elements in the given array into a random sequence. * Shuffles the elements in the given array into a random sequence.
* *
* @param values the array to shuffle. * @param values the array to shuffle.
* @param rnd the source from which random values for shuffling the * @param rnd the source from which random values for shuffling the array are obtained.
* array are obtained.
*/ */
public static void shuffle (byte[] values, Random rnd) public static void shuffle (byte[] values, Random rnd)
{ {
@@ -168,8 +162,7 @@ public class ArrayUtil
} }
/** /**
* Shuffles a subset of elements within the specified array into a * Shuffles a subset of elements within the specified array into a random sequence.
* random sequence.
* *
* @param values the array containing elements to shuffle. * @param values the array containing elements to shuffle.
* @param offset the index at which to start shuffling elements. * @param offset the index at which to start shuffling elements.
@@ -181,14 +174,12 @@ public class ArrayUtil
} }
/** /**
* Shuffles a subset of elements within the specified array into a * Shuffles a subset of elements within the specified array into a random sequence.
* random sequence.
* *
* @param values the array containing elements to shuffle. * @param values the array containing elements to shuffle.
* @param offset the index at which to start shuffling elements. * @param offset the index at which to start shuffling elements.
* @param length the number of elements to shuffle. * @param length the number of elements to shuffle.
* @param rnd the source from which random values for shuffling the * @param rnd the source from which random values for shuffling the array are obtained.
* array are obtained.
*/ */
public static void shuffle ( public static void shuffle (
byte[] values, int offset, int length, Random rnd) byte[] values, int offset, int length, Random rnd)
@@ -218,8 +209,7 @@ public class ArrayUtil
* Shuffles the elements in the given array into a random sequence. * Shuffles the elements in the given array into a random sequence.
* *
* @param values the array to shuffle. * @param values the array to shuffle.
* @param rnd the source from which random values for shuffling the * @param rnd the source from which random values for shuffling the array are obtained.
* array are obtained.
*/ */
public static void shuffle (int[] values, Random rnd) public static void shuffle (int[] values, Random rnd)
{ {
@@ -227,8 +217,7 @@ public class ArrayUtil
} }
/** /**
* Shuffles a subset of elements within the specified array into a * Shuffles a subset of elements within the specified array into a random sequence.
* random sequence.
* *
* @param values the array containing elements to shuffle. * @param values the array containing elements to shuffle.
* @param offset the index at which to start shuffling elements. * @param offset the index at which to start shuffling elements.
@@ -240,17 +229,14 @@ public class ArrayUtil
} }
/** /**
* Shuffles a subset of elements within the specified array into a * Shuffles a subset of elements within the specified array into a random sequence.
* random sequence.
* *
* @param values the array containing elements to shuffle. * @param values the array containing elements to shuffle.
* @param offset the index at which to start shuffling elements. * @param offset the index at which to start shuffling elements.
* @param length the number of elements to shuffle. * @param length the number of elements to shuffle.
* @param rnd the source from which random values for shuffling the * @param rnd the source from which random values for shuffling the array are obtained.
* array are obtained.
*/ */
public static void shuffle ( public static void shuffle (int[] values, int offset, int length, Random rnd)
int[] values, int offset, int length, Random rnd)
{ {
// starting from the end of the specified region, repeatedly swap // starting from the end of the specified region, repeatedly swap
// the element in question with a random element previous to it // the element in question with a random element previous to it
@@ -277,8 +263,7 @@ public class ArrayUtil
* Shuffles the elements in the given array into a random sequence. * Shuffles the elements in the given array into a random sequence.
* *
* @param values the array to shuffle. * @param values the array to shuffle.
* @param rnd the source from which random values for shuffling the * @param rnd the source from which random values for shuffling the array are obtained.
* array are obtained.
*/ */
public static void shuffle (Object[] values, Random rnd) public static void shuffle (Object[] values, Random rnd)
{ {
@@ -286,8 +271,7 @@ public class ArrayUtil
} }
/** /**
* Shuffles a subset of elements within the specified array into a * Shuffles a subset of elements within the specified array into a random sequence.
* random sequence.
* *
* @param values the array containing elements to shuffle. * @param values the array containing elements to shuffle.
* @param offset the index at which to start shuffling elements. * @param offset the index at which to start shuffling elements.
@@ -299,17 +283,14 @@ public class ArrayUtil
} }
/** /**
* Shuffles a subset of elements within the specified array into a * Shuffles a subset of elements within the specified array into a random sequence.
* random sequence.
* *
* @param values the array containing elements to shuffle. * @param values the array containing elements to shuffle.
* @param offset the index at which to start shuffling elements. * @param offset the index at which to start shuffling elements.
* @param length the number of elements to shuffle. * @param length the number of elements to shuffle.
* @param rnd the source from which random values for shuffling the * @param rnd the source from which random values for shuffling the array are obtained.
* array are obtained.
*/ */
public static void shuffle ( public static void shuffle (Object[] values, int offset, int length, Random rnd)
Object[] values, int offset, int length, Random rnd)
{ {
// starting from the end of the specified region, repeatedly swap // starting from the end of the specified region, repeatedly swap
// the element in question with a random element previous to it // the element in question with a random element previous to it
@@ -329,8 +310,7 @@ public class ArrayUtil
* (and actually be comparable to one another). * (and actually be comparable to one another).
* *
* @param array the array of {@link Comparable}s to be searched. * @param array the array of {@link Comparable}s to be searched.
* @param offset the index of the first element in the array to be * @param offset the index of the first element in the array to be considered.
* considered.
* @param length the number of elements including and following the * @param length the number of elements including and following the
* element at <code>offset</code> to consider when searching. * element at <code>offset</code> to consider when searching.
* @param key the object to be located. * @param key the object to be located.
@@ -342,20 +322,20 @@ public class ArrayUtil
public static <T extends Comparable<? super T>> int binarySearch ( public static <T extends Comparable<? super T>> int binarySearch (
T[] array, int offset, int length, T key) T[] array, int offset, int length, T key)
{ {
int low = offset, high = offset+length-1; int low = offset, high = offset+length-1;
while (low <= high) { while (low <= high) {
int mid = (low + high) >>> 1; int mid = (low + high) >>> 1;
T midVal = array[mid]; T midVal = array[mid];
int cmp = midVal.compareTo(key); int cmp = midVal.compareTo(key);
if (cmp < 0) { if (cmp < 0) {
low = mid + 1; low = mid + 1;
} else if (cmp > 0) { } else if (cmp > 0) {
high = mid - 1; high = mid - 1;
} else { } else {
return mid; // key found return mid; // key found
} }
} }
return -(low + 1); // key not found. return -(low + 1); // key not found.
} }
/** /**
@@ -364,8 +344,7 @@ public class ArrayUtil
* {@link Comparator} for this to operate correctly. * {@link Comparator} for this to operate correctly.
* *
* @param array the array of objects to be searched. * @param array the array of objects to be searched.
* @param offset the index of the first element in the array to be * @param offset the index of the first element in the array to be* considered.
* considered.
* @param length the number of elements including and following the * @param length the number of elements including and following the
* element at <code>offset</code> to consider when searching. * element at <code>offset</code> to consider when searching.
* @param key the object to be located. * @param key the object to be located.
@@ -378,20 +357,20 @@ public class ArrayUtil
public static <T> int binarySearch ( public static <T> int binarySearch (
T[] array, int offset, int length, T key, Comparator<T> comp) T[] array, int offset, int length, T key, Comparator<T> comp)
{ {
int low = offset, high = offset+length-1; int low = offset, high = offset+length-1;
while (low <= high) { while (low <= high) {
int mid = (low + high) >>> 1; int mid = (low + high) >>> 1;
T midVal = array[mid]; T midVal = array[mid];
int cmp = comp.compare(midVal, key); int cmp = comp.compare(midVal, key);
if (cmp < 0) { if (cmp < 0) {
low = mid + 1; low = mid + 1;
} else if (cmp > 0) { } else if (cmp > 0) {
high = mid - 1; high = mid - 1;
} else { } else {
return mid; // key found return mid; // key found
} }
} }
return -(low + 1); // key not found. return -(low + 1); // key not found.
} }
/** /**
@@ -596,8 +575,7 @@ public class ArrayUtil
* <code>offset + length</code> must be a valid index within the * <code>offset + length</code> must be a valid index within the
* <code>values</code> array. * <code>values</code> array.
*/ */
public static <T extends Object> T[] splice ( public static <T extends Object> T[] splice (T[] values, int offset, int length)
T[] values, int offset, int length)
{ {
// make sure we've something to work with // make sure we've something to work with
if (values == null) { if (values == null) {
@@ -619,8 +597,7 @@ public class ArrayUtil
// create a new array and populate it with the spliced-in values // create a new array and populate it with the spliced-in values
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T[] nvalues = (T[])Array.newInstance( T[] nvalues = (T[])Array.newInstance(values.getClass().getComponentType(), size - length);
values.getClass().getComponentType(), size - length);
System.arraycopy(values, 0, nvalues, 0, offset); System.arraycopy(values, 0, nvalues, 0, offset);
System.arraycopy(values, tstart, nvalues, offset, size - tstart); System.arraycopy(values, tstart, nvalues, offset, size - tstart);
return nvalues; return nvalues;
@@ -695,8 +672,7 @@ public class ArrayUtil
} }
nvalues[index] = value; nvalues[index] = value;
if (index < values.length) { if (index < values.length) {
System.arraycopy( System.arraycopy(values, index, nvalues, index+1, values.length-index);
values, index, nvalues, index+1, values.length-index);
} }
return nvalues; return nvalues;
} }
@@ -713,8 +689,7 @@ public class ArrayUtil
} }
nvalues[index] = value; nvalues[index] = value;
if (index < values.length) { if (index < values.length) {
System.arraycopy( System.arraycopy(values, index, nvalues, index+1, values.length-index);
values, index, nvalues, index+1, values.length-index);
} }
return nvalues; return nvalues;
} }
@@ -749,8 +724,7 @@ public class ArrayUtil
} }
nvalues[index] = value; nvalues[index] = value;
if (index < values.length) { if (index < values.length) {
System.arraycopy( System.arraycopy(values, index, nvalues, index+1, values.length-index);
values, index, nvalues, index+1, values.length-index);
} }
return nvalues; return nvalues;
} }
@@ -763,15 +737,13 @@ public class ArrayUtil
public static <T extends Object> T[] insert (T[] values, T value, int index) public static <T extends Object> T[] insert (T[] values, T value, int index)
{ {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T[] nvalues = (T[])Array.newInstance( T[] nvalues = (T[])Array.newInstance(values.getClass().getComponentType(), values.length+1);
values.getClass().getComponentType(), values.length+1);
if (index > 0) { if (index > 0) {
System.arraycopy(values, 0, nvalues, 0, index); System.arraycopy(values, 0, nvalues, 0, index);
} }
nvalues[index] = value; nvalues[index] = value;
if (index < values.length) { if (index < values.length) {
System.arraycopy( System.arraycopy(values, index, nvalues, index+1, values.length-index);
values, index, nvalues, index+1, values.length-index);
} }
return nvalues; return nvalues;
} }
@@ -859,8 +831,7 @@ public class ArrayUtil
{ {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T[] dest = (store.length >= values.length) ? store : T[] dest = (store.length >= values.length) ? store :
(T[])Array.newInstance(store.getClass().getComponentType(), (T[])Array.newInstance(store.getClass().getComponentType(), values.length);
values.length);
System.arraycopy(values, 0, dest, 0, values.length); System.arraycopy(values, 0, dest, 0, values.length);
return dest; return dest;
} }