package at.yawk.numaec;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableLongBag;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction0;
import org.eclipse.collections.api.block.function.primitive.LongToLongFunction;
import org.eclipse.collections.api.block.function.primitive.LongToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.IntLongToLongFunction;
import org.eclipse.collections.api.block.function.primitive.IntToLongFunction;
import org.eclipse.collections.api.block.predicate.primitive.LongPredicate;
import org.eclipse.collections.api.block.predicate.primitive.IntLongPredicate;
import org.eclipse.collections.api.iterator.MutableLongIterator;
import org.eclipse.collections.api.map.primitive.MutableLongIntMap;
import org.eclipse.collections.api.map.primitive.MutableIntLongMap;
import org.eclipse.collections.api.map.primitive.IntLongMap;
class IntLongLinearHashMap extends BaseIntLongMap implements IntLongBufferMap {
private final float loadFactor;
private final long sipHashK0, sipHashK1;
private final long hashMask;
protected final LinearHashTable table;
protected int size;
IntLongLinearHashMap(LargeByteBufferAllocator allocator, LinearHashMapConfig config) {
this.sipHashK0 = config.sipHashK0.getAsLong();
this.sipHashK1 = config.sipHashK1.getAsLong();
this.loadFactor = config.loadFactor;
int hashLength = config.hashLength;
this.hashMask = hashLength == 0 ? -1L : ~(-1L >>> hashLength);
this.table = new LinearHashTable(allocator, config, hashLength + Integer.BYTES + Long.BYTES) {
@Override
protected void write(LargeByteBuffer lbb, long address, long hash, long key, long value) {
if (hashLength != 0) {
if ((hash & ~hashMask) != 0) {
throw new AssertionError();
}
BTree.uset(lbb, address, hashLength, Long.reverse(hash));
}
lbb.setInt(address + hashLength, fromKey(key));
lbb.setLong(address + hashLength + Integer.BYTES, fromValue(value));
}
@Override
protected long readHash(LargeByteBuffer lbb, long address) {
if (hashLength == 0) {
return hash(fromKey(readKey(lbb, address)));
} else {
return Long.reverse(BTree.uget(lbb, address, hashLength));
}
}
@Override
protected long readKey(LargeByteBuffer lbb, long address) {
return toKey(lbb.getInt(address + hashLength));
}
@Override
protected long readValue(LargeByteBuffer lbb, long address) {
return toValue(lbb.getLong(address + hashLength + Integer.BYTES));
}
};
}
protected void ensureCapacity(int capacity) {
table.expandToFullLoadCapacity((long) (capacity / loadFactor));
}
@Override
protected MapStoreCursor iterationCursor() {
return table.allocateCursor();
}
@Override
protected MapStoreCursor keyCursor(int key) {
LinearHashTable.Cursor cursor = table.allocateCursor();
cursor.seek(hash(key), toKey(key));
return cursor;
}
@DoNotMutate
@Override
void checkInvariants() {
super.checkInvariants();
table.checkInvariants();
}
protected long hash(int key) {
return SipHash.sipHash2_4_8_to_8(sipHashK0, sipHashK1, toKey(key)) & hashMask;
}
@Override
public void close() {
table.close();
}
@Override
public int size() {
return size;
}
public static class Mutable extends IntLongLinearHashMap implements MutableIntLongBufferMap {
Mutable(LargeByteBufferAllocator allocator, LinearHashMapConfig config) {
super(allocator, config);
}
@Override
public void put(int key, long value) {
ensureCapacity(1);
long h = hash(key);
long k = toKey(key);
long v = toValue(value);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
cursor.seek(h, k);
if (cursor.elementFound()) {
cursor.setValue(v);
} else {
cursor.insert(h, k, v);
size++;
ensureCapacity(size);
}
}
}
@Override
public void putAll(IntLongMap map) {
ensureCapacity(size + map.size());
map.forEachKeyValue(this::put);
}
@Override
public void updateValues(IntLongToLongFunction function) {
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
while (cursor.next()) {
long updated = function.valueOf(fromKey(cursor.getKey()), fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
}
}
}
@Override
public void removeKey(int key) {
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
cursor.seek(hash(key), toKey(key));
if (cursor.elementFound()) {
cursor.remove();
size--;
}
}
}
@Override
public void remove(int key) {
removeKey(key);
}
@Override
public long removeKeyIfAbsent(int key, long value) {
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
cursor.seek(hash(key), toKey(key));
if (cursor.elementFound()) {
long v = fromValue(cursor.getValue());
cursor.remove();
size--;
return v;
} else {
return value;
}
}
}
@Override
public long getIfAbsentPut(int key, long value) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
cursor.insert(hash, toKey(key), toValue(value));
size++;
ensureCapacity(size);
return value;
}
}
}
@Override
public long getIfAbsentPut(int key, LongFunction0 function) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
long v = function.value();
cursor.insert(hash, toKey(key), toValue(v));
size++;
ensureCapacity(size);
return v;
}
}
}
@Override
public long getIfAbsentPutWithKey(int key, IntToLongFunction function) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
long v = function.valueOf(key);
cursor.insert(hash, toKey(key), toValue(v));
size++;
ensureCapacity(size);
return v;
}
}
}
@Override
public <P> long getIfAbsentPutWith(int key, LongFunction<? super P> function, P parameter) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
long v = function.longValueOf(parameter);
cursor.insert(hash, toKey(key), toValue(v));
size++;
ensureCapacity(size);
return v;
}
}
}
@Override
public long updateValue(int key, long initialValueIfAbsent, LongToLongFunction function) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
long updated = function.valueOf(fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
return updated;
} else {
long updated = function.valueOf(initialValueIfAbsent);
cursor.insert(hash, toKey(key), toValue(updated));
size++;
ensureCapacity(size);
return updated;
}
}
}
@Override
public MutableIntLongMap withKeyValue(int key, long value) {
put(key, value);
return this;
}
@Override
public MutableIntLongMap withoutKey(int key) {
removeKey(key);
return this;
}
@Override
public MutableIntLongMap withoutAllKeys(IntIterable keys) {
keys.forEach(this::removeKey);
return this;
}
@Override
public MutableIntLongMap asUnmodifiable() {
throw new UnsupportedOperationException("Mutable.asUnmodifiable not implemented yet");
}
@Override
public MutableIntLongMap asSynchronized() {
throw new UnsupportedOperationException("Mutable.asSynchronized not implemented yet");
}
@Override
public long addToValue(int key, long toBeAdded) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
long updated = (long) (fromValue(cursor.getValue()) + toBeAdded);
cursor.setValue(toValue(updated));
return updated;
} else {
cursor.insert(hash, toKey(key), toValue(toBeAdded));
size++;
ensureCapacity(size);
return toBeAdded;
}
}
}
@Override
public void clear() {
table.clear();
size = 0;
}
@Override
public MutableLongIntMap flipUniqueValues() {
throw new UnsupportedOperationException("IntLongBufferMap.Mutable.flipUniqueValues not implemented yet");
}
@Override
public MutableIntLongMap select(IntLongPredicate predicate) {
throw new UnsupportedOperationException("IntLongBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableLongBag select(LongPredicate predicate) {
throw new UnsupportedOperationException("IntLongBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableIntLongMap reject(IntLongPredicate predicate) {
throw new UnsupportedOperationException("IntLongBufferMap.Mutable.reject not implemented yet");
}
@Override
public MutableLongBag reject(LongPredicate predicate) {
throw new UnsupportedOperationException("IntLongBufferMap.Mutable.reject not implemented yet");
}
@Override
public <V> MutableBag<V> collect(LongToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("IntLongBufferMap.Mutable.collect not implemented yet");
}
@Override
public MutableLongIterator longIterator() {
return super.longIterator();
}
}
}