Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -389,6 +393,7 @@ private double doRemove(int index) {
final double previous = values[index];
values[index] = missingEntries;
--size;
++removed;
++count;
return previous;
}
Expand All @@ -407,13 +412,15 @@ public double put(final int key, final double value) {
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;
Expand All @@ -422,7 +429,8 @@ public double put(final int key, final double value) {
}

/**
* 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() {

Expand All @@ -431,7 +439,7 @@ private void growTable() {
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];
Expand All @@ -450,14 +458,17 @@ private void growTable() {
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;
}

/**
Expand Down Expand Up @@ -581,6 +592,12 @@ public void advance()
private void readObject(final ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
removed = 0;
for (final byte state : states) {
if (state == REMOVED) {
++removed;
}
}
count = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -401,6 +405,7 @@ private T doRemove(int index) {
final T previous = values[index];
values[index] = missingEntries;
--size;
++removed;
++count;
return previous;
}
Expand All @@ -419,13 +424,15 @@ public T put(final int key, final T value) {
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;
Expand All @@ -434,7 +441,8 @@ public T put(final int key, final T value) {
}

/**
* 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() {

Expand All @@ -443,7 +451,7 @@ private void growTable() {
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];
Expand All @@ -462,14 +470,17 @@ private void growTable() {
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;
}

/**
Expand Down Expand Up @@ -593,6 +604,12 @@ public void advance()
private void readObject(final ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
removed = 0;
for (final byte state : states) {
if (state == REMOVED) {
++removed;
}
}
count = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;


/**
Expand Down Expand Up @@ -327,4 +329,33 @@ public void testPutKeysWithCollision2() {
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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;


@SuppressWarnings("boxing")
Expand Down Expand Up @@ -330,4 +332,33 @@ public void testPutKeysWithCollision2() {
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));
});
}
}
Loading