This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push:
new 3bcbef015 Count removed slots when resizing OpenIntToDoubleHashMap
(#358)
3bcbef015 is described below
commit 3bcbef0157ee07b4485b8d7d36f8d3ca9d916a22
Author: insaf021 <[email protected]>
AuthorDate: Sun Nov 29 22:34:00 2026 +0530
Count removed slots when resizing OpenIntToDoubleHashMap (#358)
Counts removed slots and rebuilds the table once live plus removed entries
pass the load factor, doubling it only when the live entries need the room.
---
.../legacy/linear/OpenIntToDoubleHashMap.java | 27 +++++++++++++++----
.../math4/legacy/linear/OpenIntToFieldHashMap.java | 27 +++++++++++++++----
.../legacy/linear/OpenIntToDoubleHashMapTest.java | 31 ++++++++++++++++++++++
.../math4/legacy/linear/OpenIntToFieldTest.java | 31 ++++++++++++++++++++++
4 files changed, 106 insertions(+), 10 deletions(-)
diff --git
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMap.java
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMap.java
index 5ab2ae395..b5fa6f2c2 100644
---
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMap.java
+++
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMap.java
@@ -83,6 +83,9 @@ class OpenIntToDoubleHashMap implements Serializable { // Not
in public API.
/** Bit mask for hash values. */
private int mask;
+ /** Number of removed entries still occupying a slot. */
+ private transient int removed;
+
/** Modifications count. */
private transient int count;
@@ -139,6 +142,7 @@ class OpenIntToDoubleHashMap implements Serializable { //
Not in public API.
missingEntries = source.missingEntries;
size = source.size;
mask = source.mask;
+ removed = source.removed;
count = source.count;
}
@@ -389,6 +393,7 @@ class OpenIntToDoubleHashMap implements Serializable { //
Not in public API.
final double previous = values[index];
values[index] = missingEntries;
--size;
+ ++removed;
++count;
return previous;
}
@@ -407,13 +412,15 @@ class OpenIntToDoubleHashMap implements Serializable { //
Not in public API.
index = changeIndexSign(index);
previous = values[index];
newMapping = false;
+ } else if (states[index] == REMOVED) {
+ --removed;
}
keys[index] = key;
states[index] = FULL;
values[index] = value;
if (newMapping) {
++size;
- if (shouldGrowTable()) {
+ if (size + removed > (mask + 1) * LOAD_FACTOR) {
growTable();
}
++count;
@@ -422,7 +429,8 @@ class OpenIntToDoubleHashMap implements Serializable { //
Not in public API.
}
/**
- * Grow the tables.
+ * Grow the tables, or rebuild them at the same length to discard
+ * removed entries if the live entries still fit.
*/
private void growTable() {
@@ -431,7 +439,7 @@ class OpenIntToDoubleHashMap implements Serializable { //
Not in public API.
final double[] oldValues = values;
final byte[] oldStates = states;
- final int newLength = RESIZE_MULTIPLIER * oldLength;
+ final int newLength = shouldGrowTable() ? RESIZE_MULTIPLIER *
oldLength : oldLength;
final int[] newKeys = new int[newLength];
final double[] newValues = new double[newLength];
final byte[] newStates = new byte[newLength];
@@ -450,14 +458,17 @@ class OpenIntToDoubleHashMap implements Serializable { //
Not in public API.
keys = newKeys;
values = newValues;
states = newStates;
+ removed = 0;
}
/**
- * Check if tables should grow due to increased size.
+ * Check if tables should grow when they are rebuilt.
+ * <p>Growing only once live entries use half of the allowed load leaves
+ * room for further insertions after removed entries are discarded.</p>
* @return true if tables should grow
*/
private boolean shouldGrowTable() {
- return size > (mask + 1) * LOAD_FACTOR;
+ return size > (mask + 1) * LOAD_FACTOR / 2;
}
/**
@@ -581,6 +592,12 @@ class OpenIntToDoubleHashMap implements Serializable { //
Not in public API.
private void readObject(final ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
+ removed = 0;
+ for (final byte state : states) {
+ if (state == REMOVED) {
+ ++removed;
+ }
+ }
count = 0;
}
diff --git
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldHashMap.java
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldHashMap.java
index 35404c95e..4aa2b18d8 100644
---
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldHashMap.java
+++
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldHashMap.java
@@ -89,6 +89,9 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
/** Bit mask for hash values. */
private int mask;
+ /** Number of removed entries still occupying a slot. */
+ private transient int removed;
+
/** Modifications count. */
private transient int count;
@@ -151,6 +154,7 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
missingEntries = source.missingEntries;
size = source.size;
mask = source.mask;
+ removed = source.removed;
count = source.count;
}
@@ -401,6 +405,7 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
final T previous = values[index];
values[index] = missingEntries;
--size;
+ ++removed;
++count;
return previous;
}
@@ -419,13 +424,15 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
index = changeIndexSign(index);
previous = values[index];
newMapping = false;
+ } else if (states[index] == REMOVED) {
+ --removed;
}
keys[index] = key;
states[index] = FULL;
values[index] = value;
if (newMapping) {
++size;
- if (shouldGrowTable()) {
+ if (size + removed > (mask + 1) * LOAD_FACTOR) {
growTable();
}
++count;
@@ -434,7 +441,8 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
}
/**
- * Grow the tables.
+ * Grow the tables, or rebuild them at the same length to discard
+ * removed entries if the live entries still fit.
*/
private void growTable() {
@@ -443,7 +451,7 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
final T[] oldValues = values;
final byte[] oldStates = states;
- final int newLength = RESIZE_MULTIPLIER * oldLength;
+ final int newLength = shouldGrowTable() ? RESIZE_MULTIPLIER *
oldLength : oldLength;
final int[] newKeys = new int[newLength];
final T[] newValues = buildArray(newLength);
final byte[] newStates = new byte[newLength];
@@ -462,14 +470,17 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
keys = newKeys;
values = newValues;
states = newStates;
+ removed = 0;
}
/**
- * Check if tables should grow due to increased size.
+ * Check if tables should grow when they are rebuilt.
+ * <p>Growing only once live entries use half of the allowed load leaves
+ * room for further insertions after removed entries are discarded.</p>
* @return true if tables should grow
*/
private boolean shouldGrowTable() {
- return size > (mask + 1) * LOAD_FACTOR;
+ return size > (mask + 1) * LOAD_FACTOR / 2;
}
/**
@@ -593,6 +604,12 @@ class OpenIntToFieldHashMap<T extends FieldElement<T>>
implements Serializable {
private void readObject(final ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
+ removed = 0;
+ for (final byte state : states) {
+ if (state == REMOVED) {
+ ++removed;
+ }
+ }
count = 0;
}
diff --git
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMapTest.java
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMapTest.java
index e5d90ee7a..bf85a0312 100644
---
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMapTest.java
+++
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToDoubleHashMapTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.commons.math4.legacy.linear;
+import java.time.Duration;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
@@ -28,6 +29,7 @@ import org.apache.commons.numbers.core.Precision;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
/**
@@ -327,4 +329,33 @@ public class OpenIntToDoubleHashMapTest {
Assert.assertEquals(1, map.size());
Assert.assertTrue(Precision.equals(value2, map.get(key2), 1));
}
+
+ /**
+ * Removed entries keep their slot, so repeatedly adding and removing
+ * distinct keys must not use up every free slot of the table.
+ */
+ @Test
+ public void testPutAndRemoveManyKeys() {
+ Assertions.assertTimeoutPreemptively(Duration.ofSeconds(30), () -> {
+ OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap();
+ for (int i = 0; i < 10000; ++i) {
+ map.put(i, i);
+ if (i % 100 != 0) {
+ map.remove(i);
+ }
+ }
+ Assert.assertEquals(100, map.size());
+ for (int i = 0; i < 10000; ++i) {
+ if (i % 100 == 0) {
+ Assert.assertEquals(i, map.get(i), 0);
+ } else {
+ Assert.assertFalse(map.containsKey(i));
+ Assert.assertTrue(Double.isNaN(map.get(i)));
+ }
+ }
+ Assert.assertTrue(Double.isNaN(map.remove(-1)));
+ map.put(-1, 1.0);
+ Assert.assertEquals(1.0, map.get(-1), 0);
+ });
+ }
}
diff --git
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldTest.java
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldTest.java
index 9c4b8a2a8..65398f617 100644
---
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldTest.java
+++
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/linear/OpenIntToFieldTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.commons.math4.legacy.linear;
+import java.time.Duration;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
@@ -31,6 +32,7 @@ import org.apache.commons.math4.legacy.core.dfp.DfpField;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
@SuppressWarnings("boxing")
@@ -330,4 +332,33 @@ public class OpenIntToFieldTest {
Assert.assertEquals(1, map.size());
Assert.assertEquals(value2, map.get(key2));
}
+
+ /**
+ * Removed entries keep their slot, so repeatedly adding and removing
+ * distinct keys must not use up every free slot of the table.
+ */
+ @Test
+ public void testPutAndRemoveManyKeys() {
+ Assertions.assertTimeoutPreemptively(Duration.ofSeconds(30), () -> {
+ OpenIntToFieldHashMap<Dfp> map = new
OpenIntToFieldHashMap<>(field);
+ for (int i = 0; i < 10000; ++i) {
+ map.put(i, Dfp25.of(i));
+ if (i % 100 != 0) {
+ map.remove(i);
+ }
+ }
+ Assert.assertEquals(100, map.size());
+ for (int i = 0; i < 10000; ++i) {
+ if (i % 100 == 0) {
+ Assert.assertEquals(Dfp25.of(i), map.get(i));
+ } else {
+ Assert.assertFalse(map.containsKey(i));
+ Assert.assertEquals(field.getZero(), map.get(i));
+ }
+ }
+ Assert.assertEquals(field.getZero(), map.remove(-1));
+ map.put(-1, Dfp25.of(1));
+ Assert.assertEquals(Dfp25.of(1), map.get(-1));
+ });
+ }
}