LANG-1178: ArrayUtils.removeAll(Object array, int... indices) should do the clone, not its callers (closes #116)

This commit is contained in:
Henri Yandell 2015-11-19 21:58:47 +00:00 committed by pascalschumacher
parent 28f7862ab7
commit 5eae0a6464
1 changed files with 20 additions and 17 deletions

View File

@ -6536,7 +6536,7 @@ private static Object remove(final Object array, final int index) {
*/
@SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input
public static <T> T[] removeAll(final T[] array, final int... indices) {
return (T[]) removeAll((Object) array, clone(indices));
return (T[]) removeAll((Object) array, indices);
}
/**
@ -6628,7 +6628,7 @@ public static <T> T[] removeElements(final T[] array, final T... values) {
* @since 3.0.1
*/
public static byte[] removeAll(final byte[] array, final int... indices) {
return (byte[]) removeAll((Object) array, clone(indices));
return (byte[]) removeAll((Object) array, indices);
}
/**
@ -6717,7 +6717,7 @@ public static byte[] removeElements(final byte[] array, final byte... values) {
* @since 3.0.1
*/
public static short[] removeAll(final short[] array, final int... indices) {
return (short[]) removeAll((Object) array, clone(indices));
return (short[]) removeAll((Object) array, indices);
}
/**
@ -6806,7 +6806,7 @@ public static short[] removeElements(final short[] array, final short... values)
* @since 3.0.1
*/
public static int[] removeAll(final int[] array, final int... indices) {
return (int[]) removeAll((Object) array, clone(indices));
return (int[]) removeAll((Object) array, indices);
}
/**
@ -6895,7 +6895,7 @@ public static int[] removeElements(final int[] array, final int... values) {
* @since 3.0.1
*/
public static char[] removeAll(final char[] array, final int... indices) {
return (char[]) removeAll((Object) array, clone(indices));
return (char[]) removeAll((Object) array, indices);
}
/**
@ -6984,7 +6984,7 @@ public static char[] removeElements(final char[] array, final char... values) {
* @since 3.0.1
*/
public static long[] removeAll(final long[] array, final int... indices) {
return (long[]) removeAll((Object) array, clone(indices));
return (long[]) removeAll((Object) array, indices);
}
/**
@ -7073,7 +7073,7 @@ public static long[] removeElements(final long[] array, final long... values) {
* @since 3.0.1
*/
public static float[] removeAll(final float[] array, final int... indices) {
return (float[]) removeAll((Object) array, clone(indices));
return (float[]) removeAll((Object) array, indices);
}
/**
@ -7162,7 +7162,7 @@ public static float[] removeElements(final float[] array, final float... values)
* @since 3.0.1
*/
public static double[] removeAll(final double[] array, final int... indices) {
return (double[]) removeAll((Object) array, clone(indices));
return (double[]) removeAll((Object) array, indices);
}
/**
@ -7247,7 +7247,7 @@ public static double[] removeElements(final double[] array, final double... valu
* @since 3.0.1
*/
public static boolean[] removeAll(final boolean[] array, final int... indices) {
return (boolean[]) removeAll((Object) array, clone(indices));
return (boolean[]) removeAll((Object) array, indices);
}
/**
@ -7309,7 +7309,7 @@ public static boolean[] removeElements(final boolean[] array, final boolean... v
/**
* Removes multiple array elements specified by index.
* @param array source
* @param indices to remove, WILL BE SORTED--so only clones of user-owned arrays!
* @param indices to remove
* @return new array of same type minus elements specified by unique values of {@code indices}
* @since 3.0.1
*/
@ -7317,14 +7317,15 @@ public static boolean[] removeElements(final boolean[] array, final boolean... v
static Object removeAll(final Object array, final int... indices) {
final int length = getLength(array);
int diff = 0; // number of distinct indexes, i.e. number of entries that will be removed
int[] clonedIndices = clone(indices);
Arrays.sort(clonedIndices);
if (isNotEmpty(indices)) {
Arrays.sort(indices);
int i = indices.length;
// identify length of result array
if (isNotEmpty(clonedIndices)) {
int i = clonedIndices.length;
int prevIndex = length;
while (--i >= 0) {
final int index = indices[i];
final int index = clonedIndices[i];
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
}
@ -7335,12 +7336,14 @@ static Object removeAll(final Object array, final int... indices) {
prevIndex = index;
}
}
// create result array
final Object result = Array.newInstance(array.getClass().getComponentType(), length - diff);
if (diff < length) {
int end = length; // index just after last copy
int dest = length - diff; // number of entries so far not copied
for (int i = indices.length - 1; i >= 0; i--) {
final int index = indices[i];
for (int i = clonedIndices.length - 1; i >= 0; i--) {
final int index = clonedIndices[i];
if (end - index > 1) { // same as (cp > 0)
final int cp = end - index - 1;
dest -= cp;