accidental linear access times instead of constant

Methods for finding keys in the map would iterate over all keys
when the key did not exist.
Fixed by introducing a new sentinel value (-1) that is used to
mark slots that were previously occupied.
This commit is contained in:
2021-04-03 09:57:10 +02:00
parent f43cc2eda2
commit 9de619d815
7 changed files with 574 additions and 110 deletions

View File

@@ -0,0 +1,6 @@
package org.lucares.collections;
@FunctionalInterface
public interface BiLongFunction {
long apply(long key, long value);
}

View File

@@ -1,6 +0,0 @@
package org.lucares.collections;
@FunctionalInterface
public interface LongFunction {
long apply(long value);
}

View File

@@ -1,5 +1,6 @@
package org.lucares.collections;
import java.util.Arrays;
/**
@@ -12,7 +13,13 @@ public class LongLongHashMap {
// There is no equivalent to null for primitive values. Therefore we have to add
// special handling for one long value. Otherwise we couldn't tell if a key is
// in the map or not. We chose 0L, because LongList is initially all 0L.
private static final long NULL_KEY = 0L;
static final long NULL_KEY = 0L;
// Needed when checking for the existence of a key. Without it we would be forced to
// iterate over all keys. This is caused by the fact that we search for the next free
// slot when adding new keys.
// We rely on the fact that the value is -1!
static final long REMOVED_KEY = -1L;
private static final long EMPTY_SLOT = 0L;
@@ -27,14 +34,24 @@ public class LongLongHashMap {
private long[] values;
private int size = 0;
/**
* sentinel for the key {@value #NULL_KEY} ({@link #NULL_KEY}).
* If this field is not null, then the map contains the key {@value #NULL_KEY}
*/
private Long zeroValue = null;
/**
* sentinel for the key {@value #REMOVED_KEY} ({@link #REMOVED_KEY}).
* If this field is not null, then the map contains the key {@value #REMOVED_KEY}
*/
private Long removedKeyValue = null;
/**
* Create a new {@link LongLongHashMap} with the given initial capacity and load
* factor.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @param loadFactor the load factor between 0 and 1
*/
public LongLongHashMap(final int initialCapacity, final double loadFactor) {
@@ -44,8 +61,9 @@ public class LongLongHashMap {
if (initialCapacity > MAX_ARRAY_SIZE) {
throw new IllegalArgumentException("initial capacity must be smaller or equal to " + MAX_ARRAY_SIZE);
}
if (loadFactor <= 0 || Double.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
if (loadFactor <= 0 || Double.isNaN(loadFactor) || loadFactor >= 1.0) {
throw new IllegalArgumentException("Illegal load factor, must be between 0 and 1: " + loadFactor);
}
this.fillFactor = loadFactor;
keys = new long[initialCapacity];
@@ -68,6 +86,49 @@ public class LongLongHashMap {
public int size() {
return size;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
if (zeroValue != null) {
s.append(NULL_KEY);
s.append("=");
s.append(zeroValue);
}
if (removedKeyValue != null) {
if (s.length() > 0) {
s.append(", ");
}
s.append(REMOVED_KEY);
s.append("=");
s.append(removedKeyValue);
}
int values = 0;
for (int i = 0; i < keys.length; i++) {
if (keys[i] != EMPTY_SLOT && keys[i] != REMOVED_KEY) {
if (s.length() > 0) {
s.append(", ");
}
s.append(keys[i]);
s.append("=");
s.append(this.values[i]);
values++;
if (values > 10) {
s.append(", ...");
break;
}
}
}
return s.toString();
}
/**
* The capacity of this map.
@@ -91,6 +152,12 @@ public class LongLongHashMap {
zeroValue = value;
return;
}
if (key == REMOVED_KEY) {
size += removedKeyValue == null ? 1 : 0;
removedKeyValue = value;
return;
}
if ((keys.length * fillFactor) < size) {
growAndRehash();
@@ -122,7 +189,13 @@ public class LongLongHashMap {
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
throw new IllegalStateException("map is full");
// Can happen when all slots where occupied at some time in the past.
// Easy to reproduce by adding and immediately removing all keys from 1 to n.
// All slots will be marked with REMOVED_KEY.
// We fix this by calling rehash(), which will effectively replace all REMOVED_KEY
// with EMPTY_SLOT.
rehash();
return putInternal(key, value);
}
/**
@@ -140,16 +213,24 @@ public class LongLongHashMap {
}
return defaultValue;
}
if (key == REMOVED_KEY) {
if (removedKeyValue != null) {
return removedKeyValue;
}
return defaultValue;
}
final int searchStart = spread(key);
int currentPosition = searchStart;
do {
if (keys[currentPosition] == key) {
return values[currentPosition];
} else if (keys[currentPosition] == EMPTY_SLOT) {
return defaultValue;
}
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
return defaultValue;
return defaultValue;// should never be reached unless the map is full at 100%, which should be impossible with loadFactor < 1.0
}
/**
@@ -163,16 +244,21 @@ public class LongLongHashMap {
if (key == NULL_KEY) {
return zeroValue != null;
}
if (key == REMOVED_KEY) {
return removedKeyValue != null;
}
final int searchStart = spread(key);
int currentPosition = searchStart;
do {
if (keys[currentPosition] == key) {
return true;
} else if (keys[currentPosition] == EMPTY_SLOT) {
return false;
}
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
return false;
} while (currentPosition != searchStart);
return false;// should never be reached unless the map is full at 100%, which should be impossible with loadFactor < 1.0
}
/**
@@ -187,17 +273,25 @@ public class LongLongHashMap {
zeroValue = null;
return;
}
if (key == REMOVED_KEY) {
size -= removedKeyValue != null ? 1 : 0;
removedKeyValue = null;
return;
}
final int searchStart = spread(key);
int currentPosition = searchStart;
do {
if (keys[currentPosition] == key) {
keys[currentPosition] = EMPTY_SLOT;
keys[currentPosition] = REMOVED_KEY;
size--;
return;
}else if (keys[currentPosition] == EMPTY_SLOT) {
// key didn't exists
return;
}
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
} while (currentPosition != searchStart); // run around should never happen unless the map is full at 100%
}
/**
@@ -213,13 +307,21 @@ public class LongLongHashMap {
* key
* @param function called to update an existing value
*/
public void compute(final long key, final long initialValueIfAbsent, final LongFunction function) {
public void compute(final long key, final long initialValueIfAbsent, final BiLongFunction function) {
if (key == NULL_KEY) {
if (zeroValue != null) {
zeroValue = function.apply(zeroValue);
zeroValue = function.apply(NULL_KEY,zeroValue);
return;
}
zeroValue = function.apply(initialValueIfAbsent);
zeroValue = function.apply(NULL_KEY,initialValueIfAbsent);
return;
}
if (key == REMOVED_KEY) {
if (removedKeyValue != null) {
removedKeyValue = function.apply(REMOVED_KEY, removedKeyValue);
return;
}
removedKeyValue = function.apply(REMOVED_KEY, initialValueIfAbsent);
return;
}
@@ -227,15 +329,19 @@ public class LongLongHashMap {
int currentPosition = searchStart;
do {
if (keys[currentPosition] == key) {
final long updatedValue = function.apply(values[currentPosition]);
final long updatedValue = function.apply(key, values[currentPosition]);
values[currentPosition] = updatedValue;
return;
}
else if (keys[currentPosition] == EMPTY_SLOT) {
// key not found
break;
}
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
// key not found -> add it
final long newZeroValue = function.apply(initialValueIfAbsent);
final long newZeroValue = function.apply(key, initialValueIfAbsent);
put(key, newZeroValue);
}
@@ -249,11 +355,14 @@ public class LongLongHashMap {
public void forEach(final LongLongConsumer consumer) {
if (zeroValue != null) {
consumer.accept(0, zeroValue);
consumer.accept(NULL_KEY, zeroValue);
}
if (removedKeyValue != null) {
consumer.accept(REMOVED_KEY, removedKeyValue);
}
for (int i = 0; i < keys.length; i++) {
if (keys[i] != EMPTY_SLOT) {
if (keys[i] != EMPTY_SLOT && keys[i] != REMOVED_KEY) {
consumer.accept(keys[i], values[i]);
}
}
@@ -268,26 +377,36 @@ public class LongLongHashMap {
* @param consumer the consumer
*/
public void forEachOrdered(final LongLongConsumer consumer) {
if (zeroValue != null) {
consumer.accept(0, zeroValue);
}
final long[] sortedKeys = Arrays.copyOf(keys, keys.length);
Arrays.parallelSort(sortedKeys);
// handle negative keys
for (int i = 0; i < sortedKeys.length; i++) {
final long key = sortedKeys[i];
if (key != EMPTY_SLOT) {
if (key < REMOVED_KEY) {
consumer.accept(key, get(key, 0)); // the default value of 'get' will not be used, because the key exists
} else if (key == EMPTY_SLOT) {
final int posFirstKey = findPosOfFirstPositiveKey(sortedKeys);
if (posFirstKey < 0) {
return;
}
i = posFirstKey - 1;
} else {
break;
}
}
// handle the special keys
if (removedKeyValue != null) {
consumer.accept(REMOVED_KEY, removedKeyValue);
}
if (zeroValue != null) {
consumer.accept(NULL_KEY, zeroValue);
}
// handle positive keys
final int posFirstKey = findPosOfFirstPositiveKey(sortedKeys);
if (posFirstKey < 0) {
return;
}
for (int i = posFirstKey; i < sortedKeys.length; i++) {
final long key = sortedKeys[i];
consumer.accept(key, get(key, 0)); // the default value of 'get' will not be used, because the key exists
}
}
static int findPosOfFirstPositiveKey(final long[] sortedKeys) {
@@ -318,19 +437,30 @@ public class LongLongHashMap {
return low < sortedKeys.length && sortedKeys[low] > EMPTY_SLOT ? low : -1;
}
/**
* Rehashes all elements of this map.
* <p>
* This is a maintenance operation that should be executed periodically after removing elements.
*/
public void rehash() {
rehash(keys.length);
}
private void growAndRehash() {
final int newSize = Math.min(keys.length * 2, MAX_ARRAY_SIZE);
rehash(newSize);
}
private void rehash(int newSize) {
final long[] oldKeys = keys;
final long[] oldValues = values;
final int newSize = Math.min(keys.length * 2, MAX_ARRAY_SIZE);
keys = new long[newSize];
values = new long[newSize];
for (int i = 0; i < oldKeys.length; i++) {
final long key = oldKeys[i];
if (key != EMPTY_SLOT) {
if (key != EMPTY_SLOT && key != REMOVED_KEY) {
final long value = oldValues[i];
putInternal(key, value);
}

View File

@@ -14,7 +14,12 @@ public class LongObjHashMap<V> {
// There is no equivalent to null for primitive values. Therefore we have to add
// special handling for one long value. Otherwise we couldn't tell if a key is
// in the map or not. We chose 0L, because LongList is initially all 0L.
private static final long NULL_KEY = 0L;
static final long NULL_KEY = 0L;
// Needed when checking for the existence of a key. Without it we would be forced to
// iterate over all keys. This is caused by the fact that we search for the next free
// slot when adding new keys.
static final long REMOVED_KEY = -1L;
private static final long EMPTY_SLOT = 0L;
@@ -30,6 +35,7 @@ public class LongObjHashMap<V> {
private int size = 0;
private V zeroValue = null;
private V removedValue = null;
/**
* Create a new {@link LongLongHashMap} with the given initial capacity and load
@@ -47,7 +53,7 @@ public class LongObjHashMap<V> {
if (initialCapacity > MAX_ARRAY_SIZE) {
throw new IllegalArgumentException("initial capacity must be smaller or equal to " + MAX_ARRAY_SIZE);
}
if (loadFactor <= 0 || Double.isNaN(loadFactor))
if (loadFactor <= 0 || Double.isNaN(loadFactor) || loadFactor >= 1.0)
throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
this.fillFactor = loadFactor;
@@ -94,6 +100,11 @@ public class LongObjHashMap<V> {
zeroValue = value;
return;
}
if (key == REMOVED_KEY) {
size += removedValue == null ? 1 : 0;
removedValue = value;
return;
}
if ((keys.length * fillFactor) < size) {
growAndRehash();
@@ -125,7 +136,13 @@ public class LongObjHashMap<V> {
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
throw new IllegalStateException("map is full");
// Can happen when all slots where occupied at some time in the past.
// Easy to reproduce by adding and immediately removing all keys from 1 to n.
// All slots will be marked with REMOVED_KEY.
// We fix this by calling rehash(), which will effectively replace all REMOVED_KEY
// with EMPTY_SLOT.
rehash();
return putInternal(key, value);
}
/**
@@ -144,6 +161,13 @@ public class LongObjHashMap<V> {
}
return null;
}
if (key == REMOVED_KEY) {
if (removedValue != null) {
return removedValue;
}
return null;
}
final int searchStart = spread(key);
int currentPosition = searchStart;
@@ -167,6 +191,10 @@ public class LongObjHashMap<V> {
if (key == NULL_KEY) {
return zeroValue != null;
}
if (key == REMOVED_KEY) {
return zeroValue != null;
}
final int searchStart = spread(key);
int currentPosition = searchStart;
@@ -176,6 +204,14 @@ public class LongObjHashMap<V> {
}
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
// Can happen when all slots where occupied at some time in the past.
// Easy to reproduce by adding and immediately removing all keys from 1 to n.
// All slots will be marked with REMOVED_KEY.
// We fix this by calling rehash(), which will effectively replace all REMOVED_KEY
// with EMPTY_SLOT.
rehash();
return false;
}
@@ -191,17 +227,36 @@ public class LongObjHashMap<V> {
zeroValue = null;
return;
}
if (key == REMOVED_KEY) {
size -= removedValue != null ? 1 : 0;
removedValue = null;
return;
}
final int searchStart = spread(key);
int currentPosition = searchStart;
do {
if (keys[currentPosition] == key) {
keys[currentPosition] = EMPTY_SLOT;
keys[currentPosition] = REMOVED_KEY;
size--;
return;
}
if (keys[currentPosition] == EMPTY_SLOT) {
// key does not exist
return;
}
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
// Can happen when all slots where occupied at some time in the past.
// Easy to reproduce by adding and immediately removing all keys from 1 to n.
// All slots will be marked with REMOVED_KEY.
// We fix this by calling rehash(), which will effectively replace all REMOVED_KEY
// with EMPTY_SLOT.
rehash();
}
/**
@@ -226,6 +281,15 @@ public class LongObjHashMap<V> {
zeroValue = function.apply(NULL_KEY, initialValueIfAbsent.get());
return;
}
if (key == REMOVED_KEY) {
if (removedValue != null) {
removedValue = function.apply(REMOVED_KEY, removedValue);
return;
}
removedValue = function.apply(REMOVED_KEY, initialValueIfAbsent.get());
return;
}
final int searchStart = spread(key);
int currentPosition = searchStart;
@@ -235,6 +299,10 @@ public class LongObjHashMap<V> {
values[currentPosition] = updatedValue;
return;
}
if (keys[currentPosition] == EMPTY_SLOT) {
// key does not exist
break;
}
currentPosition = (currentPosition + 1) % keys.length;
} while (currentPosition != searchStart);
@@ -253,11 +321,15 @@ public class LongObjHashMap<V> {
public void forEach(final LongObjConsumer<V> consumer) {
if (zeroValue != null) {
consumer.accept(0, zeroValue);
consumer.accept(NULL_KEY, zeroValue);
}
if (removedValue != null) {
consumer.accept(REMOVED_KEY, removedValue);
}
for (int i = 0; i < keys.length; i++) {
if (keys[i] != EMPTY_SLOT) {
if (keys[i] != EMPTY_SLOT && keys[i] != REMOVED_KEY) {
consumer.accept(keys[i], values[i]);
}
}
@@ -272,26 +344,36 @@ public class LongObjHashMap<V> {
* @param consumer the consumer
*/
public void forEachOrdered(final LongObjConsumer<V> consumer) {
if (zeroValue != null) {
consumer.accept(0, zeroValue);
}
final long[] sortedKeys = Arrays.copyOf(keys, keys.length);
Arrays.parallelSort(sortedKeys);
// handle negative keys
for (int i = 0; i < sortedKeys.length; i++) {
final long key = sortedKeys[i];
if (key != EMPTY_SLOT) {
if (key < REMOVED_KEY) {
consumer.accept(key, get(key));
} else if (key == EMPTY_SLOT) {
final int posFirstKey = findPosOfFirstPositiveKey(sortedKeys);
if (posFirstKey < 0) {
return;
}
i = posFirstKey - 1;
} else {
break;
}
}
// handle the special keys
if (removedValue != null) {
consumer.accept(REMOVED_KEY, removedValue);
}
if (zeroValue != null) {
consumer.accept(NULL_KEY, zeroValue);
}
// handle positive keys
final int posFirstKey = findPosOfFirstPositiveKey(sortedKeys);
if (posFirstKey < 0) {
return;
}
for (int i = posFirstKey; i < sortedKeys.length; i++) {
final long key = sortedKeys[i];
consumer.accept(key, get(key));
}
}
static int findPosOfFirstPositiveKey(final long[] sortedKeys) {
@@ -322,20 +404,32 @@ public class LongObjHashMap<V> {
return low < sortedKeys.length && sortedKeys[low] > EMPTY_SLOT ? low : -1;
}
/**
* Rehashes all elements of this map.
* <p>
* This is a maintenance operation that should be executed periodically after removing elements.
*/
public void rehash() {
rehash(keys.length);
}
@SuppressWarnings("unchecked")
private void growAndRehash() {
final int newSize = Math.min(keys.length * 2, MAX_ARRAY_SIZE);
rehash(newSize);
}
@SuppressWarnings("unchecked")
private void rehash(int newSize) {
final long[] oldKeys = keys;
final V[] oldValues = values;
final int newSize = Math.min(keys.length * 2, MAX_ARRAY_SIZE);
keys = new long[newSize];
values = (V[]) new Object[newSize];
for (int i = 0; i < oldKeys.length; i++) {
final long key = oldKeys[i];
if (key != EMPTY_SLOT) {
if (key != EMPTY_SLOT && key != REMOVED_KEY) {
final V value = oldValues[i];
putInternal(key, value);
}