LANG-839 ArrayUtils removeElements methods use unnecessary HashSet

Replace HashSet with BitSet (less memory; faster)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1395837 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-10-09 01:26:13 +00:00
parent ec242df460
commit 17910d3cc7
2 changed files with 27 additions and 26 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.2" date="TBA" description="Next release">
<action issue="LANG-839" type="update">ArrayUtils removeElements methods use unnecessary HashSet</action>
<action issue="LANG-838" type="update">ArrayUtils removeElements methods clone temporary index arrays unnecessarily</action>
<action issue="LANG-832" type="fix">FastDateParser does not handle unterminated quotes correctly</action>
<action issue="LANG-831" type="fix">FastDateParser does not handle white-space properly</action>

View File

@ -18,8 +18,8 @@
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.apache.commons.lang3.builder.EqualsBuilder;
@ -5007,7 +5007,7 @@ public static <T> T[] removeElements(T[] array, T... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<T, MutableInt> e : occurrences.entrySet()) {
T v = e.getKey();
int found = 0;
@ -5016,7 +5016,7 @@ public static <T> T[] removeElements(T[] array, T... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
@SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input
@ -5098,7 +5098,7 @@ public static byte[] removeElements(byte[] array, byte... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Byte, MutableInt> e : occurrences.entrySet()) {
Byte v = e.getKey();
int found = 0;
@ -5107,7 +5107,7 @@ public static byte[] removeElements(byte[] array, byte... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (byte[]) removeAll((Object)array, extractIndices(toRemove));
@ -5187,7 +5187,7 @@ public static short[] removeElements(short[] array, short... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Short, MutableInt> e : occurrences.entrySet()) {
Short v = e.getKey();
int found = 0;
@ -5196,7 +5196,7 @@ public static short[] removeElements(short[] array, short... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (short[]) removeAll((Object)array, extractIndices(toRemove));
@ -5276,7 +5276,7 @@ public static int[] removeElements(int[] array, int... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Integer, MutableInt> e : occurrences.entrySet()) {
Integer v = e.getKey();
int found = 0;
@ -5285,7 +5285,7 @@ public static int[] removeElements(int[] array, int... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (int[]) removeAll((Object)array, extractIndices(toRemove));
@ -5365,7 +5365,7 @@ public static char[] removeElements(char[] array, char... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Character, MutableInt> e : occurrences.entrySet()) {
Character v = e.getKey();
int found = 0;
@ -5374,7 +5374,7 @@ public static char[] removeElements(char[] array, char... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (char[]) removeAll((Object)array, extractIndices(toRemove));
@ -5454,7 +5454,7 @@ public static long[] removeElements(long[] array, long... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Long, MutableInt> e : occurrences.entrySet()) {
Long v = e.getKey();
int found = 0;
@ -5463,7 +5463,7 @@ public static long[] removeElements(long[] array, long... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (long[]) removeAll((Object)array, extractIndices(toRemove));
@ -5543,7 +5543,7 @@ public static float[] removeElements(float[] array, float... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Float, MutableInt> e : occurrences.entrySet()) {
Float v = e.getKey();
int found = 0;
@ -5552,7 +5552,7 @@ public static float[] removeElements(float[] array, float... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (float[]) removeAll((Object)array, extractIndices(toRemove));
@ -5632,7 +5632,7 @@ public static double[] removeElements(double[] array, double... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Double, MutableInt> e : occurrences.entrySet()) {
Double v = e.getKey();
int found = 0;
@ -5641,7 +5641,7 @@ public static double[] removeElements(double[] array, double... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (double[]) removeAll((Object)array, extractIndices(toRemove));
@ -5717,7 +5717,7 @@ public static boolean[] removeElements(boolean[] array, boolean... values) {
count.increment();
}
}
HashSet<Integer> toRemove = new HashSet<Integer>();
BitSet toRemove = new BitSet();
for (Map.Entry<Boolean, MutableInt> e : occurrences.entrySet()) {
Boolean v = e.getKey();
int found = 0;
@ -5726,7 +5726,7 @@ public static boolean[] removeElements(boolean[] array, boolean... values) {
if (found < 0) {
break;
}
toRemove.add(found++);
toRemove.set(found++);
}
}
return (boolean[]) removeAll((Object)array, extractIndices(toRemove));
@ -5782,16 +5782,16 @@ private static Object removeAll(Object array, int... indices) {
}
/**
* Extract a set of Integer indices into an int[].
* @param coll {@code HashSet} of {@code Integer}
* Extract a set of BitSet indices into an int[].
* @param coll Bitset
* @return int[]
* @since 3.0.1
*/
private static int[] extractIndices(HashSet<Integer> coll) {
int[] result = new int[coll.size()];
private static int[] extractIndices(BitSet coll) {
int[] result = new int[coll.cardinality()];
int i = 0;
for (Integer index : coll) {
result[i++] = index.intValue();
int j=0;
while((j=coll.nextSetBit(j)) != -1) {
result[i++] = j++;
}
return result;
}