package at.yawk.numaec;
import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction0;
import org.eclipse.collections.api.block.function.primitive.IntToIntFunction;
import org.eclipse.collections.api.block.function.primitive.IntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ShortIntToIntFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToIntFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.ShortIntPredicate;
import org.eclipse.collections.api.iterator.MutableIntIterator;
import org.eclipse.collections.api.map.primitive.MutableIntShortMap;
import org.eclipse.collections.api.map.primitive.MutableShortIntMap;
import org.eclipse.collections.api.map.primitive.ShortIntMap;
public class ShortIntBTreeMap extends BaseShortIntMap implements ShortIntBufferMap {
protected final BTree bTree;
protected int size = 0;
ShortIntBTreeMap(LargeByteBufferAllocator allocator, BTreeConfig config) {
int leafSize = Short.BYTES + Integer.BYTES;
int branchSize = config.entryMustBeInLeaf ? Short.BYTES : leafSize;
this.bTree = new BTree(allocator, config, branchSize, leafSize) {
@Override
protected void writeBranchEntry(LargeByteBuffer lbb, long address, long key, long value) {
lbb.setShort(address, fromKey(key));
if (!config.entryMustBeInLeaf) {
lbb.setInt(address + Short.BYTES, fromValue(value));
}
}
@Override
protected void writeLeafEntry(LargeByteBuffer lbb, long address, long key, long value) {
lbb.setShort(address, fromKey(key));
lbb.setInt(address + Short.BYTES, fromValue(value));
}
@Override
protected long readBranchKey(LargeByteBuffer lbb, long address) {
return toKey(lbb.getShort(address));
}
@Override
protected long readBranchValue(LargeByteBuffer lbb, long address) {
if (config.entryMustBeInLeaf) {
throw new AssertionError();
} else {
return toValue(lbb.getInt(address + Short.BYTES));
}
}
@Override
protected long readLeafKey(LargeByteBuffer lbb, long address) {
return toKey(lbb.getShort(address));
}
@Override
protected long readLeafValue(LargeByteBuffer lbb, long address) {
return toValue(lbb.getInt(address + Short.BYTES));
}
};
}
@Override
protected MapStoreCursor iterationCursor() {
BTree.Cursor cursor = bTree.allocateCursor();
cursor.descendToStart();
return cursor;
}
@Override
protected MapStoreCursor keyCursor(short key) {
BTree.Cursor cursor = bTree.allocateCursor();
cursor.descendToKey(toKey(key));
return cursor;
}
@DoNotMutate
@Override
void checkInvariants() {
super.checkInvariants();
bTree.checkInvariants();
}
@Override
public int size() {
return size;
}
@Override
public void close() {
bTree.close();
}
public static class Mutable extends ShortIntBTreeMap implements MutableShortIntBufferMap {
Mutable(LargeByteBufferAllocator allocator, BTreeConfig config) {
super(allocator, config);
}
@Override
public void put(short key, int value) {
long k = toKey(key);
long v = toValue(value);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
cursor.setValue(v);
} else {
cursor.simpleInsert(k, v);
cursor.balance();
size++;
}
}
}
@Override
public void putAll(ShortIntMap map) {
map.forEachKeyValue(this::put);
}
@Override
public void updateValues(ShortIntToIntFunction function) {
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToStart();
while (cursor.next()) {
int updated = function.valueOf(fromKey(cursor.getKey()), fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
}
}
}
@Override
public void removeKey(short key) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
cursor.simpleRemove();
cursor.balance();
size--;
}
}
}
@Override
public void remove(short key) {
removeKey(key);
}
@Override
public int removeKeyIfAbsent(short key, int value) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
int v = fromValue(cursor.getValue());
cursor.simpleRemove();
cursor.balance();
size--;
return v;
} else {
return value;
}
}
}
@Override
public int getIfAbsentPut(short key, int value) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
cursor.simpleInsert(k, toValue(value));
cursor.balance();
size++;
return value;
}
}
}
@Override
public int getIfAbsentPut(short key, IntFunction0 function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
int v = function.value();
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public int getIfAbsentPutWithKey(short key, ShortToIntFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
int v = function.valueOf(key);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public <P> int getIfAbsentPutWith(short key, IntFunction<? super P> function, P parameter) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
int v = function.intValueOf(parameter);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public int updateValue(short key, int initialValueIfAbsent, IntToIntFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
int updated = function.valueOf(fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
return updated;
} else {
int updated = function.valueOf(initialValueIfAbsent);
cursor.simpleInsert(k, toValue(updated));
cursor.balance();
size++;
return updated;
}
}
}
@Override
public MutableShortIntMap withKeyValue(short key, int value) {
put(key, value);
return this;
}
@Override
public MutableShortIntMap withoutKey(short key) {
removeKey(key);
return this;
}
@Override
public MutableShortIntMap withoutAllKeys(ShortIterable keys) {
keys.forEach(this::removeKey);
return this;
}
@Override
public MutableShortIntMap asUnmodifiable() {
throw new UnsupportedOperationException("Mutable.asUnmodifiable not implemented yet");
}
@Override
public MutableShortIntMap asSynchronized() {
throw new UnsupportedOperationException("Mutable.asSynchronized not implemented yet");
}
@Override
public int addToValue(short key, int toBeAdded) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
int updated = (int) (fromValue(cursor.getValue()) + toBeAdded);
cursor.setValue(toValue(updated));
return updated;
} else {
cursor.simpleInsert(k, toValue(toBeAdded));
cursor.balance();
size++;
return toBeAdded;
}
}
}
@Override
public void clear() {
bTree.clear();
size = 0;
}
@Override
public MutableIntShortMap flipUniqueValues() {
throw new UnsupportedOperationException("ShortIntBufferMap.Mutable.flipUniqueValues not implemented yet");
}
@Override
public MutableShortIntMap select(ShortIntPredicate predicate) {
throw new UnsupportedOperationException("ShortIntBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableIntBag select(IntPredicate predicate) {
throw new UnsupportedOperationException("ShortIntBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableShortIntMap reject(ShortIntPredicate predicate) {
throw new UnsupportedOperationException("ShortIntBufferMap.Mutable.reject not implemented yet");
}
@Override
public MutableIntBag reject(IntPredicate predicate) {
throw new UnsupportedOperationException("ShortIntBufferMap.Mutable.reject not implemented yet");
}
@Override
public <V> MutableBag<V> collect(IntToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("ShortIntBufferMap.Mutable.collect not implemented yet");
}
@Override
public MutableIntIterator intIterator() {
return super.intIterator();
}
}
}