package at.yawk.numaec;
import org.eclipse.collections.api.FloatIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction0;
import org.eclipse.collections.api.block.function.primitive.ShortToShortFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.FloatShortToShortFunction;
import org.eclipse.collections.api.block.function.primitive.FloatToShortFunction;
import org.eclipse.collections.api.block.predicate.primitive.ShortPredicate;
import org.eclipse.collections.api.block.predicate.primitive.FloatShortPredicate;
import org.eclipse.collections.api.iterator.MutableShortIterator;
import org.eclipse.collections.api.map.primitive.MutableShortFloatMap;
import org.eclipse.collections.api.map.primitive.MutableFloatShortMap;
import org.eclipse.collections.api.map.primitive.FloatShortMap;
class FloatShortLinearHashMap extends BaseFloatShortMap implements FloatShortBufferMap {
private final float loadFactor;
private final long sipHashK0, sipHashK1;
private final long hashMask;
protected final LinearHashTable table;
protected int size;
FloatShortLinearHashMap(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 + Float.BYTES + Short.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.setFloat(address + hashLength, fromKey(key));
lbb.setShort(address + hashLength + Float.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.getFloat(address + hashLength));
}
@Override
protected long readValue(LargeByteBuffer lbb, long address) {
return toValue(lbb.getShort(address + hashLength + Float.BYTES));
}
};
}
protected void ensureCapacity(int capacity) {
table.expandToFullLoadCapacity((long) (capacity / loadFactor));
}
@Override
protected MapStoreCursor iterationCursor() {
return table.allocateCursor();
}
@Override
protected MapStoreCursor keyCursor(float 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(float 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 FloatShortLinearHashMap implements MutableFloatShortBufferMap {
Mutable(LargeByteBufferAllocator allocator, LinearHashMapConfig config) {
super(allocator, config);
}
@Override
public void put(float key, short 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(FloatShortMap map) {
ensureCapacity(size + map.size());
map.forEachKeyValue(this::put);
}
@Override
public void updateValues(FloatShortToShortFunction function) {
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
while (cursor.next()) {
short updated = function.valueOf(fromKey(cursor.getKey()), fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
}
}
}
@Override
public void removeKey(float key) {
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
cursor.seek(hash(key), toKey(key));
if (cursor.elementFound()) {
cursor.remove();
size--;
}
}
}
@Override
public void remove(float key) {
removeKey(key);
}
@Override
public short removeKeyIfAbsent(float key, short value) {
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
cursor.seek(hash(key), toKey(key));
if (cursor.elementFound()) {
short v = fromValue(cursor.getValue());
cursor.remove();
size--;
return v;
} else {
return value;
}
}
}
@Override
public short getIfAbsentPut(float key, short 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 short getIfAbsentPut(float key, ShortFunction0 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 {
short v = function.value();
cursor.insert(hash, toKey(key), toValue(v));
size++;
ensureCapacity(size);
return v;
}
}
}
@Override
public short getIfAbsentPutWithKey(float key, FloatToShortFunction 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 {
short v = function.valueOf(key);
cursor.insert(hash, toKey(key), toValue(v));
size++;
ensureCapacity(size);
return v;
}
}
}
@Override
public <P> short getIfAbsentPutWith(float key, ShortFunction<? 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 {
short v = function.shortValueOf(parameter);
cursor.insert(hash, toKey(key), toValue(v));
size++;
ensureCapacity(size);
return v;
}
}
}
@Override
public short updateValue(float key, short initialValueIfAbsent, ShortToShortFunction function) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
short updated = function.valueOf(fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
return updated;
} else {
short updated = function.valueOf(initialValueIfAbsent);
cursor.insert(hash, toKey(key), toValue(updated));
size++;
ensureCapacity(size);
return updated;
}
}
}
@Override
public MutableFloatShortMap withKeyValue(float key, short value) {
put(key, value);
return this;
}
@Override
public MutableFloatShortMap withoutKey(float key) {
removeKey(key);
return this;
}
@Override
public MutableFloatShortMap withoutAllKeys(FloatIterable keys) {
keys.forEach(this::removeKey);
return this;
}
@Override
public MutableFloatShortMap asUnmodifiable() {
throw new UnsupportedOperationException("Mutable.asUnmodifiable not implemented yet");
}
@Override
public MutableFloatShortMap asSynchronized() {
throw new UnsupportedOperationException("Mutable.asSynchronized not implemented yet");
}
@Override
public short addToValue(float key, short toBeAdded) {
ensureCapacity(1);
try (LinearHashTable.Cursor cursor = table.allocateCursor()) {
long hash = hash(key);
cursor.seek(hash, toKey(key));
if (cursor.elementFound()) {
short updated = (short) (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 MutableShortFloatMap flipUniqueValues() {
throw new UnsupportedOperationException("FloatShortBufferMap.Mutable.flipUniqueValues not implemented yet");
}
@Override
public MutableFloatShortMap select(FloatShortPredicate predicate) {
throw new UnsupportedOperationException("FloatShortBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableShortBag select(ShortPredicate predicate) {
throw new UnsupportedOperationException("FloatShortBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableFloatShortMap reject(FloatShortPredicate predicate) {
throw new UnsupportedOperationException("FloatShortBufferMap.Mutable.reject not implemented yet");
}
@Override
public MutableShortBag reject(ShortPredicate predicate) {
throw new UnsupportedOperationException("FloatShortBufferMap.Mutable.reject not implemented yet");
}
@Override
public <V> MutableBag<V> collect(ShortToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("FloatShortBufferMap.Mutable.collect not implemented yet");
}
@Override
public MutableShortIterator shortIterator() {
return super.shortIterator();
}
}
}