From 5b223744b49d3ceac9608959d2db82bbadb47897 Mon Sep 17 00:00:00 2001 From: pascalschumacher Date: Thu, 19 May 2016 21:52:51 +0200 Subject: [PATCH] LANG-1176: Improve ArrayUtils removeElements time complexity to O(n) (closes #144) based on patch submitted by Jeffery Yuan --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/ArrayUtils.java | 144 +++++++++--------- 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 4dd3cbf06..f33413136 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Improve ArrayUtils removeElements time complexity to O(n) getLevenshteinDistance with a threshold: optimize implementation if the strings lengths differ more than the threshold Add SystemUtils.IS_OS_WINDOWS_10 property DiffBuilder: Add null check on fieldName when appending Object or Object[] diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 6fe185df4..0d6a02acc 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -6581,15 +6581,15 @@ public static T[] removeElements(final T[] array, final T... values) { } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final T v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v, found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final T key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } @SuppressWarnings("unchecked") // removeAll() always creates an array of the same type as its input @@ -6673,15 +6673,15 @@ public static byte[] removeElements(final byte[] array, final byte... values) { } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Byte v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.byteValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final byte key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (byte[]) removeAll(array, toRemove); @@ -6762,15 +6762,15 @@ public static short[] removeElements(final short[] array, final short... values) } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Short v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.shortValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final short key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (short[]) removeAll(array, toRemove); @@ -6851,15 +6851,15 @@ public static int[] removeElements(final int[] array, final int... values) { } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Integer v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.intValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final int key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (int[]) removeAll(array, toRemove); @@ -6940,15 +6940,15 @@ public static char[] removeElements(final char[] array, final char... values) { } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Character v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.charValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final char key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (char[]) removeAll(array, toRemove); @@ -7029,15 +7029,15 @@ public static long[] removeElements(final long[] array, final long... values) { } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Long v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.longValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final long key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (long[]) removeAll(array, toRemove); @@ -7118,15 +7118,15 @@ public static float[] removeElements(final float[] array, final float... values) } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Float v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.floatValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final float key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (float[]) removeAll(array, toRemove); @@ -7207,15 +7207,15 @@ public static double[] removeElements(final double[] array, final double... valu } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Double v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.doubleValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final double key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (double[]) removeAll(array, toRemove); @@ -7292,15 +7292,15 @@ public static boolean[] removeElements(final boolean[] array, final boolean... v } } final BitSet toRemove = new BitSet(); - for (final Map.Entry e : occurrences.entrySet()) { - final Boolean v = e.getKey(); - int found = 0; - for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { - found = indexOf(array, v.booleanValue(), found); - if (found < 0) { - break; + for (int i = 0; i < array.length; i++) { + final boolean key = array[i]; + final MutableInt count = occurrences.get(key); + if (count != null) { + count.decrement(); + if (count.intValue() == 0) { + occurrences.remove(key); } - toRemove.set(found++); + toRemove.set(i); } } return (boolean[]) removeAll(array, toRemove);