in graphjet-core/src/main/java/com/twitter/graphjet/hashing/ArrayBasedLongToInternalIntFixedLengthBiMap.java [171:209]
public int put(long key) {
long primaryHashValue = primaryHashFunction(key);
int bucket = (int) (primaryHashValue & bitMask);
long currentKey = array.get(bucket);
if (currentKey == defaultGetReturnValue) {
if (isAtCapacity()) {
throw new RuntimeException("Exceeded the maximum number of insertions");
}
// If the bucket is empty, set the value
array.set(bucket, key);
size.getAndIncrement();
numStoredKeysCounter.incr();
return bucket;
} else if (currentKey == key) {
// otherwise if the bucket already has the key, return
return bucket;
}
// If the primary location has some other element, then open address using double hashing
// Double hashed probing is given by: h1(key) + i * h2(key)
int secondaryHashKey = secondaryHashFunction(key, bitMask);
do {
bucket = (int) (((long) bucket + secondaryHashKey) & bitMask);
currentKey = array.get(bucket);
} while ((currentKey != defaultGetReturnValue) && (currentKey != key));
// At this point we either find an empty bucket or the bucket that currently contains the key
if (currentKey == defaultGetReturnValue) {
if (isAtCapacity()) {
throw new RuntimeException("Exceeded the maximum number of insertions");
}
// If the bucket is empty, set the value
array.set(bucket, key);
size.getAndIncrement();
numStoredKeysCounter.incr();
return bucket;
} else {
// otherwise if the bucket already has the key, return
return bucket;
}
}