Add hash map for long to long mappings.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package org.lucares.collections;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface LongFunction {
|
||||
long apply(long value);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.lucares.collections;
|
||||
|
||||
public interface LongLongConsumer {
|
||||
public void accept(long key, long value);
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
package org.lucares.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* A hash map where key and value are primitive longs.
|
||||
*/
|
||||
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;
|
||||
|
||||
private static final long EMPTY_SLOT = 0L;
|
||||
|
||||
/**
|
||||
* The maximum size of an array.
|
||||
*/
|
||||
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
|
||||
|
||||
private final double fillFactor;
|
||||
|
||||
private long[] keys;
|
||||
private long[] values;
|
||||
private int size = 0;
|
||||
|
||||
private Long zeroValue = null;
|
||||
|
||||
/**
|
||||
* Create a new {@link LongLongHashMap} with the given initial capacity and load
|
||||
* factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @param loadFactor the load factor
|
||||
*/
|
||||
public LongLongHashMap(final int initialCapacity, final double loadFactor) {
|
||||
|
||||
if (initialCapacity < 0) {
|
||||
throw new IllegalArgumentException("initial capacity must be non-negative");
|
||||
}
|
||||
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);
|
||||
|
||||
this.fillFactor = loadFactor;
|
||||
keys = new long[initialCapacity];
|
||||
values = new long[initialCapacity];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link LongLongHashMap} with initial capacity 8 and load factor
|
||||
* 0.75.
|
||||
*/
|
||||
public LongLongHashMap() {
|
||||
this(8, 0.75);
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of entries in this map.
|
||||
*
|
||||
* @return the size
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* The capacity of this map.
|
||||
*
|
||||
* @return the capacity
|
||||
*/
|
||||
int getCapacity() {
|
||||
return keys.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given key and value to the map.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
public void put(final long key, final long value) {
|
||||
|
||||
if (key == NULL_KEY) {
|
||||
size += zeroValue == null ? 1 : 0;
|
||||
zeroValue = value;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((keys.length * fillFactor) < size) {
|
||||
growAndRehash();
|
||||
}
|
||||
|
||||
final boolean added = putInternal(key, value);
|
||||
if (added) {
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean putInternal(final long key, final long value) {
|
||||
final int searchStart = spread(key);
|
||||
int currentPosition = searchStart;
|
||||
|
||||
do {
|
||||
// found a free place, insert the value
|
||||
if (keys[currentPosition] == EMPTY_SLOT) {
|
||||
keys[currentPosition] = key;
|
||||
values[currentPosition] = value;
|
||||
return true;
|
||||
}
|
||||
// value exists, update it
|
||||
if (keys[currentPosition] == key) {
|
||||
keys[currentPosition] = key;
|
||||
values[currentPosition] = value;
|
||||
return false;
|
||||
}
|
||||
currentPosition = (currentPosition + 1) % keys.length;
|
||||
} while (currentPosition != searchStart);
|
||||
|
||||
throw new IllegalStateException("map is full");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value for the given key if it exists. This method throws a
|
||||
* {@link NoSuchElementException} if the key does not exist. Use
|
||||
* {@link #containsKey(long)} to check before calling {@link #get(long)}.
|
||||
*
|
||||
* @param key the key
|
||||
* @return the value if it exists
|
||||
* @throws NoSuchElementException if the value does not exist
|
||||
*/
|
||||
public long get(final long key) {
|
||||
|
||||
if (key == NULL_KEY) {
|
||||
if (zeroValue != null) {
|
||||
return zeroValue;
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
final int searchStart = spread(key);
|
||||
int currentPosition = searchStart;
|
||||
do {
|
||||
if (keys[currentPosition] == key) {
|
||||
return values[currentPosition];
|
||||
}
|
||||
currentPosition = (currentPosition + 1) % keys.length;
|
||||
} while (currentPosition != searchStart);
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the map contains the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return true iff the map contains the key
|
||||
*/
|
||||
public boolean containsKey(final long key) {
|
||||
|
||||
if (key == NULL_KEY) {
|
||||
return zeroValue != null;
|
||||
}
|
||||
|
||||
final int searchStart = spread(key);
|
||||
int currentPosition = searchStart;
|
||||
do {
|
||||
if (keys[currentPosition] == key) {
|
||||
return true;
|
||||
}
|
||||
currentPosition = (currentPosition + 1) % keys.length;
|
||||
} while (currentPosition != searchStart);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the given key and its value from the map.
|
||||
*
|
||||
* @param key the key
|
||||
*/
|
||||
public void remove(final long key) {
|
||||
|
||||
if (key == NULL_KEY) {
|
||||
size -= zeroValue != null ? 1 : 0;
|
||||
zeroValue = null;
|
||||
return;
|
||||
}
|
||||
|
||||
final int searchStart = spread(key);
|
||||
int currentPosition = searchStart;
|
||||
do {
|
||||
if (keys[currentPosition] == key) {
|
||||
keys[currentPosition] = EMPTY_SLOT;
|
||||
size--;
|
||||
return;
|
||||
}
|
||||
currentPosition = (currentPosition + 1) % keys.length;
|
||||
} while (currentPosition != searchStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a mapping for the given key and its current value.
|
||||
* <p>
|
||||
* The mapping for given key is updated by calling {@code function} with the old
|
||||
* value. The return value will be set as new value. If the map does not contain
|
||||
* a mapping for the key, then {@code function} is with
|
||||
* {@code initialValueIfAbsent}.
|
||||
*
|
||||
* @param key the key
|
||||
* @param initialValueIfAbsent value used if there is no current mapping for the
|
||||
* key
|
||||
* @param function called to update an existing value
|
||||
*/
|
||||
public void compute(final long key, final long initialValueIfAbsent, final LongFunction function) {
|
||||
if (key == NULL_KEY) {
|
||||
if (zeroValue != null) {
|
||||
zeroValue = function.apply(zeroValue);
|
||||
return;
|
||||
}
|
||||
zeroValue = function.apply(initialValueIfAbsent);
|
||||
return;
|
||||
}
|
||||
|
||||
final int searchStart = spread(key);
|
||||
int currentPosition = searchStart;
|
||||
do {
|
||||
if (keys[currentPosition] == key) {
|
||||
final long updatedValue = function.apply(values[currentPosition]);
|
||||
values[currentPosition] = updatedValue;
|
||||
return;
|
||||
}
|
||||
currentPosition = (currentPosition + 1) % keys.length;
|
||||
} while (currentPosition != searchStart);
|
||||
|
||||
// key not found -> add it
|
||||
final long newZeroValue = function.apply(initialValueIfAbsent);
|
||||
put(key, newZeroValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the {@link LongLongConsumer#accept(long, long)} method for all entries
|
||||
* in this map. The order is based on the hash value and is therefore not
|
||||
* deterministic. Don't rely on the order!
|
||||
*
|
||||
* @param consumer the consumer
|
||||
*/
|
||||
public void forEach(final LongLongConsumer consumer) {
|
||||
|
||||
if (zeroValue != null) {
|
||||
consumer.accept(0, zeroValue);
|
||||
}
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != EMPTY_SLOT) {
|
||||
consumer.accept(keys[i], values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the {@link LongLongConsumer#accept(long, long)} method for all entries
|
||||
* in this map. This method iterates over the keys in ascending order.
|
||||
* <p>
|
||||
* Note: this method is slower than {@link #forEach(LongLongConsumer)}.
|
||||
*
|
||||
* @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);
|
||||
|
||||
for (int i = 0; i < sortedKeys.length; i++) {
|
||||
final long key = sortedKeys[i];
|
||||
if (key != EMPTY_SLOT) {
|
||||
consumer.accept(key, get(key));
|
||||
} else if (key == EMPTY_SLOT) {
|
||||
final int posFirstKey = findPosOfFirstPositiveKey(sortedKeys);
|
||||
i = posFirstKey - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int findPosOfFirstPositiveKey(final long[] sortedKeys) {
|
||||
|
||||
if (sortedKeys.length == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (sortedKeys.length == 1) {
|
||||
return sortedKeys[0] > EMPTY_SLOT ? 0 : -1;
|
||||
}
|
||||
|
||||
int low = 0;
|
||||
int high = sortedKeys.length - 1;
|
||||
int pos = -1;
|
||||
|
||||
while (low <= high) {
|
||||
pos = (low + high) / 2;
|
||||
if (sortedKeys[pos] <= EMPTY_SLOT) {
|
||||
low = pos + 1;
|
||||
} else {
|
||||
high = pos - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (low < sortedKeys.length && sortedKeys[low] <= EMPTY_SLOT) {
|
||||
low++;
|
||||
}
|
||||
|
||||
return low < sortedKeys.length && sortedKeys[low] > EMPTY_SLOT ? low : -1;
|
||||
}
|
||||
|
||||
private void growAndRehash() {
|
||||
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) {
|
||||
final long value = oldValues[i];
|
||||
putInternal(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// visible for test
|
||||
int spread(final long key) {
|
||||
return hash(key) % keys.length;
|
||||
}
|
||||
|
||||
private int hash(final long l) {
|
||||
return Math.abs(Long.hashCode(l));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user