Fixes and improvements to IntObjectHashMap. Related to [#2659]

- Rewrite with linear probing, no state array, compaction at cleanup
- Optimize keys() and values() to not use reflection
- Optimize hashCode() and equals() for efficient iteration
- Fixed equals() to not return true for equals(null)
- Optimize iterator to not allocate new Entry at each next()
- Added toString()
- Added some new unit tests
This commit is contained in:
Osvaldo Doederlein 2014-07-19 18:16:50 -04:00 committed by Norman Maurer
parent 5f69fb18d0
commit 8fc58a4a91
2 changed files with 348 additions and 250 deletions

View File

@ -21,40 +21,36 @@ import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
/** /**
* A hash map implementation of {@link IntObjectMap} that uses open addressing for keys. To minimize * A hash map implementation of {@link IntObjectMap} that uses open addressing for keys.
* the memory footprint, this class uses open addressing rather than chaining. Collisions are * To minimize the memory footprint, this class uses open addressing rather than chaining.
* resolved using double hashing. * Collisions are resolved using linear probing. Deletions implement compaction, so cost of
* remove can approach O(N) for full maps, which makes a small loadFactor recommended.
* *
* @param <V> The value type stored in the map. * @param <V> The value type stored in the map.
*/ */
public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectMap.Entry<V>> { public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectMap.Entry<V>> {
/** State indicating that a slot is available.*/
private static final byte AVAILABLE = 0;
/** State indicating that a slot is occupied. */
private static final byte OCCUPIED = 1;
/** State indicating that a slot was removed. */
private static final byte REMOVED = 2;
/** Default initial capacity. Used if not specified in the constructor */ /** Default initial capacity. Used if not specified in the constructor */
private static final int DEFAULT_CAPACITY = 11; private static final int DEFAULT_CAPACITY = 11;
/** Default load factor. Used if not specified in the constructor */ /** Default load factor. Used if not specified in the constructor */
private static final float DEFAULT_LOAD_FACTOR = 0.5f; private static final float DEFAULT_LOAD_FACTOR = 0.5f;
/**
* Placeholder for null values, so we can use the actual null to mean available.
* (Better than using a placeholder for available: less references for GC processing.)
*/
private static final Object NULL_VALUE = new Object();
/** The maximum number of elements allowed without allocating more space. */ /** The maximum number of elements allowed without allocating more space. */
private int maxSize; private int maxSize;
/** The load factor for the map. Used to calculate {@link #maxSize}. */ /** The load factor for the map. Used to calculate {@link #maxSize}. */
private final float loadFactor; private final float loadFactor;
private byte[] states;
private int[] keys; private int[] keys;
private V[] values; private V[] values;
private int size; private int size;
private int available;
public IntObjectHashMap() { public IntObjectHashMap() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
@ -68,93 +64,71 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
if (initialCapacity < 1) { if (initialCapacity < 1) {
throw new IllegalArgumentException("initialCapacity must be >= 1"); throw new IllegalArgumentException("initialCapacity must be >= 1");
} }
if (loadFactor <= 0.0f) { if (loadFactor <= 0.0f || loadFactor > 1.0f) {
throw new IllegalArgumentException("loadFactor must be > 0"); // Cannot exceed 1 because we can never store more than capacity elements;
// using a bigger loadFactor would trigger rehashing before the desired load is reached.
throw new IllegalArgumentException("loadFactor must be > 0 and <= 1");
} }
this.loadFactor = loadFactor; this.loadFactor = loadFactor;
// Adjust the initial capacity if necessary. // Adjust the initial capacity if necessary.
initialCapacity = adjustCapacity(initialCapacity); int capacity = adjustCapacity(initialCapacity);
// Allocate the arrays. // Allocate the arrays.
states = new byte[initialCapacity]; keys = new int[capacity];
keys = new int[initialCapacity]; @SuppressWarnings({ "unchecked", })
@SuppressWarnings({ "unchecked", "SuspiciousArrayCast" }) V[] temp = (V[]) new Object[capacity];
V[] temp = (V[]) new Object[initialCapacity];
values = temp; values = temp;
// Initialize the maximum size value. // Initialize the maximum size value.
maxSize = calcMaxSize(initialCapacity); maxSize = calcMaxSize(capacity);
}
// Initialize the available element count private static <T> T toExternal(T value) {
available = initialCapacity - size; return value == NULL_VALUE ? null : value;
}
@SuppressWarnings("unchecked")
private static <T> T toInternal(T value) {
return value == null ? (T) NULL_VALUE : value;
} }
@Override @Override
public V get(int key) { public V get(int key) {
int index = indexOf(key); int index = indexOf(key);
return index < 0 ? null : values[index]; return index == -1 ? null : toExternal(values[index]);
} }
@Override @Override
public V put(int key, V value) { public V put(int key, V value) {
int hash = hash(key); int startIndex = hashIndex(key);
int capacity = capacity(); int index = startIndex;
int index = hash % capacity;
int increment = 1 + hash % (capacity - 2);
final int startIndex = index;
int firstRemovedIndex = -1;
do {
switch (states[index]) {
case AVAILABLE:
// We only stop probing at a AVAILABLE node, since the value may still exist
// beyond
// a REMOVED node.
if (firstRemovedIndex != -1) {
// We encountered a REMOVED node prior. Store the entry there so that
// retrieval
// will be faster.
insertAt(firstRemovedIndex, key, value);
return null;
}
// No REMOVED node, just store the entry here. for (;;) {
insertAt(index, key, value); if (values[index] == null) {
return null; // Found empty slot, use it.
case OCCUPIED: keys[index] = key;
if (keys[index] == key) { values[index] = toInternal(value);
V previousValue = values[index]; growSize();
insertAt(index, key, value); return null;
return previousValue; } else if (keys[index] == key) {
} // Found existing entry with this key, just replace the value.
break; V previousValue = values[index];
case REMOVED: values[index] = toInternal(value);
// Check for first removed index. return toExternal(previousValue);
if (firstRemovedIndex == -1) {
firstRemovedIndex = index;
}
break;
default:
throw new AssertionError("Invalid state: " + states[index]);
} }
// REMOVED or OCCUPIED but wrong key, keep probing ... // Conflict, keep probing ...
index += increment; if ((index = probeNext(index)) == startIndex) {
if (index >= capacity) { // Can only happen if the map was full at MAX_ARRAY_SIZE and couldn't grow.
// Handle wrap-around by decrement rather than mod. throw new IllegalStateException("Unable to insert");
index -= capacity;
} }
} while (index != startIndex);
if (firstRemovedIndex == -1) {
// Should never happen.
throw new AssertionError("Unable to insert");
} }
}
// Never found a AVAILABLE slot, just use the first REMOVED. private int probeNext(int index) {
insertAt(firstRemovedIndex, key, value); return index == values.length - 1 ? 0 : index + 1;
return null;
} }
@Override @Override
@ -162,9 +136,11 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
if (sourceMap instanceof IntObjectHashMap) { if (sourceMap instanceof IntObjectHashMap) {
// Optimization - iterate through the arrays. // Optimization - iterate through the arrays.
IntObjectHashMap<V> source = (IntObjectHashMap<V>) sourceMap; IntObjectHashMap<V> source = (IntObjectHashMap<V>) sourceMap;
int i = -1; for (int i = 0; i < source.values.length; ++i) {
while ((i = source.nextEntryIndex(i + 1)) >= 0) { V sourceValue = source.values[i];
put(source.keys[i], source.values[i]); if (sourceValue != null) {
put(source.keys[i], sourceValue);
}
} }
return; return;
} }
@ -178,13 +154,13 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
@Override @Override
public V remove(int key) { public V remove(int key) {
int index = indexOf(key); int index = indexOf(key);
if (index < 0) { if (index == -1) {
return null; return null;
} }
V prev = values[index]; V prev = values[index];
removeAt(index); removeAt(index);
return prev; return toExternal(prev);
} }
@Override @Override
@ -199,10 +175,9 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
@Override @Override
public void clear() { public void clear() {
Arrays.fill(states, AVAILABLE); Arrays.fill(keys, 0);
Arrays.fill(values, null); Arrays.fill(values, null);
size = 0; size = 0;
available = capacity();
} }
@Override @Override
@ -212,10 +187,10 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
@Override @Override
public boolean containsValue(V value) { public boolean containsValue(V value) {
int i = -1; V v = toInternal(value);
while ((i = nextEntryIndex(i + 1)) >= 0) { for (int i = 0; i < values.length; ++i) {
V next = values[i]; // The map supports null values; this will be matched as NULL_VALUE.equals(NULL_VALUE).
if (value == next || value != null && value.equals(next)) { if (values[i] != null && values[i].equals(v)) {
return true; return true;
} }
} }
@ -235,7 +210,12 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
@Override @Override
public int[] keys() { public int[] keys() {
int[] outKeys = new int[size()]; int[] outKeys = new int[size()];
copyEntries(keys, outKeys); int targetIx = 0;
for (int i = 0; i < values.length; ++i) {
if (values[i] != null) {
outKeys[targetIx++] = keys[i];
}
}
return outKeys; return outKeys;
} }
@ -243,58 +223,63 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
public V[] values(Class<V> clazz) { public V[] values(Class<V> clazz) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
V[] outValues = (V[]) Array.newInstance(clazz, size()); V[] outValues = (V[]) Array.newInstance(clazz, size());
copyEntries(values, outValues); int targetIx = 0;
for (int i = 0; i < values.length; ++i) {
if (values[i] != null) {
outValues[targetIx++] = values[i];
}
}
return outValues; return outValues;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; // Hashcode is based on all non-zero, valid keys. We have to scan the whole keys
int result = 1; // array, which may have different lengths for two maps of same size(), so the
for (Entry<V> entry : entries()) { // capacity cannot be used as input for hashing but the size can.
V value = entry.value(); int hash = size;
int hash = value == null ? 0 : value.hashCode(); for (int i = 0; i < keys.length; ++i) {
result = prime * result + hash; // 0 can be a valid key or unused slot, but won't impact the hashcode in either case.
// This way we can use a cheap loop without conditionals, or hard-to-unroll operations,
// or the devastatingly bad memory locality of visiting value objects.
// Also, it's important to use a hash function that does not depend on the ordering
// of terms, only their values; since the map is an unordered collection and
// entries can end up in different positions in different maps that have the same
// elements, but with different history of puts/removes, due to conflicts.
hash = hash ^ keys[i];
} }
return result; return hash;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj || obj == null || getClass() != obj.getClass()) { if (this == obj) {
return true; return true;
} } else if (!(obj instanceof IntObjectMap)) {
@SuppressWarnings("rawtypes")
IntObjectHashMap other = (IntObjectHashMap) obj;
if (size != other.size) {
return false; return false;
} }
for (Entry<V> entry : entries()) { @SuppressWarnings("rawtypes")
V value = entry.value(); IntObjectMap other = (IntObjectMap) obj;
Object otherValue = other.get(entry.key()); if (size != other.size()) {
if (value == null) { return false;
if (otherValue != null || !other.containsKey(entry.key())) { }
for (int i = 0; i < values.length; ++i) {
V value = values[i];
if (value != null) {
int key = keys[i];
Object otherValue = other.get(key);
if (value == NULL_VALUE) {
if (otherValue != null) {
return false;
}
} else if (!value.equals(otherValue)) {
return false; return false;
} }
} else if (!value.equals(otherValue)) {
return false;
} }
} }
return true; return true;
} }
/**
* Copies the occupied entries from the source to the target array.
*/
private void copyEntries(Object sourceArray, Object targetArray) {
int sourceIx = -1;
int targetIx = 0;
while ((sourceIx = nextEntryIndex(sourceIx + 1)) >= 0) {
Object obj = Array.get(sourceArray, sourceIx);
Array.set(targetArray, targetIx++, obj);
}
}
/** /**
* Locates the index for the given key. This method probes using double hashing. * Locates the index for the given key. This method probes using double hashing.
* *
@ -302,117 +287,94 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
* @return the index where the key was found, or {@code -1} if no entry is found for that key. * @return the index where the key was found, or {@code -1} if no entry is found for that key.
*/ */
private int indexOf(int key) { private int indexOf(int key) {
int hash = hash(key); int startIndex = hashIndex(key);
int capacity = capacity(); int index = startIndex;
int increment = 1 + hash % (capacity - 2);
int index = hash % capacity; for (;;) {
int startIndex = index; if (values[index] == null) {
do { // It's available, so no chance that this value exists anywhere in the map.
switch(states[index]) { return -1;
case AVAILABLE: } else if (key == keys[index]) {
// It's available, so no chance that this value exists anywhere in the map. return index;
return -1;
case OCCUPIED:
if (key == keys[index]) {
// Found it!
return index;
}
break;
default:
break;
} }
// REMOVED or OCCUPIED but wrong key, keep probing ... // Conflict, keep probing ...
index += increment; if ((index = probeNext(index)) == startIndex) {
if (index >= capacity) { return -1;
// Handle wrap-around by decrement rather than mod.
index -= capacity;
}
} while (index != startIndex);
// Got back to the beginning. Not found.
return -1;
}
/**
* Determines the current capacity (i.e. size of the arrays).
*/
private int capacity() {
return keys.length;
}
/**
* Creates a hash value for the given key.
*/
private static int hash(int key) {
// Just make sure the integer is positive.
return key & Integer.MAX_VALUE;
}
/**
* Performs an insert of the key/value at the given index position. If necessary, performs a
* rehash of the map.
*
* @param index the index at which to insert the key/value
* @param key the entry key
* @param value the entry value
*/
private void insertAt(int index, int key, V value) {
byte state = states[index];
if (state != OCCUPIED) {
// Added a new mapping, increment the size.
size++;
if (state == AVAILABLE) {
// Consumed a OCCUPIED slot, decrement the number of available slots.
available--;
} }
} }
}
keys[index] = key; /**
values[index] = value; * Returns the hashed index for the given key.
states[index] = OCCUPIED; */
private int hashIndex(int key) {
return key % keys.length;
}
/**
* Grows the map size after an insertion. If necessary, performs a rehash of the map.
*/
private void growSize() {
size++;
if (size > maxSize) { if (size > maxSize) {
// Need to grow the arrays. // Need to grow the arrays. We take care to detect integer overflow,
rehash(adjustCapacity(capacity() * 2)); // also limit array size to ArrayList.MAX_ARRAY_SIZE.
} else if (available == 0) { rehash(adjustCapacity((int) Math.min(keys.length * 2.0, Integer.MAX_VALUE - 8)));
} else if (size == keys.length) {
// Open addressing requires that we have at least 1 slot available. Need to refresh // Open addressing requires that we have at least 1 slot available. Need to refresh
// the arrays to clear any removed elements. // the arrays to clear any removed elements.
rehash(capacity()); rehash(keys.length);
} }
} }
/** /**
* Adjusts the given capacity value to ensure that it's odd. Even capacities can break probing. * Adjusts the given capacity value to ensure that it's odd. Even capacities can break probing.
* TODO: would be better to ensure it's prime as well.
*/ */
private static int adjustCapacity(int capacity) { private static int adjustCapacity(int capacity) {
return capacity | 1; return capacity | 1;
} }
/** /**
* Marks the entry at the given index position as {@link #REMOVED} and sets the value to * Removes entry at the given index position. Also performs opportunistic, incremental rehashing
* {@code null}. * if necessary to not break conflict chains.
* <p>
* TODO: consider performing re-compaction.
* *
* @param index the index position of the element to remove. * @param index the index position of the element to remove.
*/ */
private void removeAt(int index) { private void removeAt(int index) {
if (states[index] == OCCUPIED) { --size;
size--; // Clearing the key is not strictly necessary (for GC like in a regular collection),
} // but recommended for security. The memory location is still fresh in the cache anyway.
states[index] = REMOVED; keys[index] = 0;
values[index] = null; values[index] = null;
// In the interval from index to the next available entry, the arrays may have entries
// that are displaced from their base position due to prior conflicts. Iterate these
// entries and move them back if possible, optimizing future lookups.
// Knuth Section 6.4 Algorithm R, also used by the JDK's IdentityHashMap.
int nextFree = index;
for (int i = probeNext(index); values[i] != null; i = probeNext(i)) {
int bucket = hashIndex(keys[i]);
if ((i < bucket && (bucket <= nextFree || nextFree <= i))
|| (bucket <= nextFree && nextFree <= i)) {
// Move the displaced entry "back" to the first available position.
keys[nextFree] = keys[i];
values[nextFree] = values[i];
// Put the first entry after the displaced entry
keys[i] = 0;
values[i] = null;
nextFree = i;
}
}
} }
/** /**
* Calculates the maximum size allowed before rehashing. * Calculates the maximum size allowed before rehashing.
*/ */
private int calcMaxSize(int capacity) { private int calcMaxSize(int capacity) {
// Clip the upper bound so that there will always be at least one // Clip the upper bound so that there will always be at least one available slot.
// available slot.
int upperBound = capacity - 1; int upperBound = capacity - 1;
return Math.min(upperBound, (int) (capacity * loadFactor)); return Math.min(upperBound, (int) (capacity * loadFactor));
} }
@ -423,61 +385,62 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
* @param newCapacity the new capacity for the map. * @param newCapacity the new capacity for the map.
*/ */
private void rehash(int newCapacity) { private void rehash(int newCapacity) {
int oldCapacity = capacity();
int[] oldKeys = keys; int[] oldKeys = keys;
V[] oldVals = values; V[] oldVals = values;
byte[] oldStates = states;
// New states array is automatically initialized to AVAILABLE (i.e. 0 == AVAILABLE).
states = new byte[newCapacity];
keys = new int[newCapacity]; keys = new int[newCapacity];
@SuppressWarnings({ "unchecked", "SuspiciousArrayCast" }) @SuppressWarnings({ "unchecked" })
V[] temp = (V[]) new Object[newCapacity]; V[] temp = (V[]) new Object[newCapacity];
values = temp; values = temp;
size = 0;
available = newCapacity;
maxSize = calcMaxSize(newCapacity); maxSize = calcMaxSize(newCapacity);
// Insert the new states. // Insert to the new arrays.
for (int i = 0; i < oldCapacity; ++i) { for (int i = 0; i < oldVals.length; ++i) {
if (oldStates[i] == OCCUPIED) { V oldVal = oldVals[i];
put(oldKeys[i], oldVals[i]); if (oldVal != null) {
} // Inlined put(), but much simpler: we don't need to worry about
} // duplicated keys, growing/rehashing, or failing to insert.
} int oldKey = oldKeys[i];
int startIndex = hashIndex(oldKey);
int index = startIndex;
/** for (;;) {
* Returns the next index of the next entry in the map. if (values[index] == null) {
* keys[index] = oldKey;
* @param index the index at which to begin the search. values[index] = toInternal(oldVal);
* @return the index of the next entry, or {@code -1} if not found. break;
*/ }
private int nextEntryIndex(int index) {
int capacity = capacity(); // Conflict, keep probing. Can wrap around, but never reaches startIndex again.
for (; index < capacity; ++index) { index = probeNext(index);
if (states[index] == OCCUPIED) { }
return index;
} }
} }
return -1;
} }
/** /**
* Iterator for traversing the entries in this map. * Iterator for traversing the entries in this map.
*/ */
private final class IteratorImpl implements Iterator<Entry<V>> { private final class IteratorImpl implements Iterator<Entry<V>>, Entry<V> {
int prevIndex; private int prevIndex = -1;
int nextIndex; private int nextIndex = -1;
private int entryIndex = -1;
IteratorImpl() { private void scanNext() {
prevIndex = -1; for (;;) {
nextIndex = nextEntryIndex(0); if (++nextIndex == values.length || values[nextIndex] != null) {
break;
}
}
} }
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return nextIndex >= 0; if (nextIndex == -1) {
scanNext();
}
return nextIndex < keys.length;
} }
@Override @Override
@ -487,43 +450,54 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
} }
prevIndex = nextIndex; prevIndex = nextIndex;
nextIndex = nextEntryIndex(nextIndex + 1); scanNext();
return new EntryImpl(prevIndex);
// Always return the same Entry object, just change its index each time.
entryIndex = prevIndex;
return this;
} }
@Override @Override
public void remove() { public void remove() {
if (prevIndex < 0) { if (prevIndex < 0) {
throw new IllegalStateException("Next must be called before removing."); throw new IllegalStateException("next must be called before each remove.");
} }
removeAt(prevIndex); removeAt(prevIndex);
prevIndex = -1; prevIndex = -1;
} }
}
/** // Entry implementation. Since this implementation uses a single Entry, we coalesce that
* {@link Entry} implementation that just references the key/value at the given index position. // into the Iterator object (potentially making loop optimization much easier).
*/
private final class EntryImpl implements Entry<V> {
final int index;
EntryImpl(int index) {
this.index = index;
}
@Override @Override
public int key() { public int key() {
return keys[index]; return keys[entryIndex];
} }
@Override @Override
public V value() { public V value() {
return values[index]; return toExternal(values[entryIndex]);
} }
@Override @Override
public void setValue(V value) { public void setValue(V value) {
values[index] = value; values[entryIndex] = toInternal(value);
} }
} }
@Override
public String toString() {
if (size == 0) {
return "{}";
}
StringBuilder sb = new StringBuilder(4 * size);
for (int i = 0; i < values.length; ++i) {
V value = values[i];
if (value != null) {
sb.append(sb.length() == 0 ? "{" : ", ");
sb.append(keys[i]).append('=').append(value == this ? "(this Map)" : value);
}
}
return sb.append('}').toString();
}
} }

View File

@ -14,14 +14,21 @@
*/ */
package io.netty.util.collection; package io.netty.util.collection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Random;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.*;
/** /**
* Tests for {@link IntObjectHashMap}. * Tests for {@link IntObjectHashMap}.
*/ */
@ -279,4 +286,121 @@ public class IntObjectHashMapTest {
} }
assertEquals(expected, found); assertEquals(expected, found);
} }
@Test
public void mapShouldSupportHashingConflicts() {
for (int mod = 0; mod < 10; ++mod) {
for (int sz = 1; sz <= 101; sz += 2) {
IntObjectHashMap<String> map = new IntObjectHashMap<String>(sz);
for (int i = 0; i < 100; ++i) {
map.put(i * mod, "");
}
}
}
}
@Test
public void hashcodeEqualsTest() {
IntObjectHashMap<Integer> map1 = new IntObjectHashMap<Integer>();
IntObjectHashMap<Integer> map2 = new IntObjectHashMap<Integer>();
Random rnd = new Random(0);
while (map1.size() < 100) {
int key = rnd.nextInt(100);
map1.put(key, key);
map2.put(key, key);
}
assertEquals(map1.hashCode(), map2.hashCode());
assertTrue(map1.equals(map2));
// Remove one "middle" element, maps should now be non-equals.
int[] keys = map1.keys();
map2.remove(keys[50]);
assertFalse(map1.equals(map2));
// Put it back; will likely be in a different position, but maps will be equal again.
map2.put(keys[50], map1.keys()[50]);
assertTrue(map1.equals(map2));
assertEquals(map1.hashCode(), map2.hashCode());
// Make map2 have one extra element, will be non-equal.
map2.put(1000, 1000);
assertFalse(map1.equals(map2));
// Rebuild map2 with elements in a different order, again the maps should be equal.
// (These tests with same elements in different order also show that the hashCode
// function does not depend on the internal ordering of entries.)
map2.clear();
Arrays.sort(keys);
for (int key : keys) {
map2.put(key, key);
}
assertEquals(map1.hashCode(), map2.hashCode());
assertTrue(map1.equals(map2));
}
@Test
public void fuzzTest() {
// This test is so extremely internals-dependent that I'm not even trying to
// minimize that. Any internal changes will not fail the test (so it's not flaky per se)
// but will possibly make it less effective (not test interesting scenarios anymore).
// The RNG algorithm is specified and stable, so this will cause the same exact dataset
// to be used in every run and every JVM implementation.
Random rnd = new Random(0);
int baseSize = 1000;
// Empirically-determined size to expand the capacity exactly once, and before
// the step that creates the long conflict chain. We need to test rehash(),
// but also control when rehash happens because it cleans up the REMOVED entries.
// This size is also chosen so after the single rehash, the map will be densely
// populated, getting close to a second rehash but not triggering it.
int startTableSize = 1105;
IntObjectHashMap<Integer> map = new IntObjectHashMap<Integer>(startTableSize);
// Reference map which implementation we trust to be correct, will mirror all operations.
HashMap<Integer, Integer> goodMap = new HashMap<Integer, Integer>();
// Add initial population.
for (int i = 0; i < baseSize / 4; ++i) {
int key = rnd.nextInt(baseSize);
assertEquals(goodMap.put(key, key), map.put(key, key));
// 50% elements are multiple of a divisor of startTableSize => more conflicts.
key = rnd.nextInt(baseSize) * 17;
assertEquals(goodMap.put(key, key), map.put(key, key));
}
// Now do some mixed adds and removes for further fuzzing
// Rehash will happen here, but only once, and the final size will be closer to max.
for (int i = 0; i < baseSize * 1000; ++i) {
int key = rnd.nextInt(baseSize);
if (rnd.nextDouble() >= 0.2) {
assertEquals(goodMap.put(key, key), map.put(key, key));
} else {
assertEquals(goodMap.remove(key), map.remove(key));
}
}
// Final batch of fuzzing, only searches and removes.
int removeSize = map.size() / 2;
while (removeSize > 0) {
int key = rnd.nextInt(baseSize);
boolean found = goodMap.containsKey(key);
assertEquals(found, map.containsKey(key));
assertEquals(goodMap.remove(key), map.remove(key));
if (found) {
--removeSize;
}
}
// Now gotta write some code to compare the final maps, as equals() won't work.
assertEquals(goodMap.size(), map.size());
Integer[] goodKeys = goodMap.keySet().toArray(new Integer[goodMap.size()]);
Arrays.sort(goodKeys);
int [] keys = map.keys();
Arrays.sort(keys);
for (int i = 0; i < goodKeys.length; ++i) {
assertEquals((int) goodKeys[i], keys[i]);
}
// Finally drain the map.
for (int key : map.keys()) {
assertEquals(goodMap.remove(key), map.remove(key));
}
assertTrue(map.isEmpty());
}
} }