package at.yawk.numaec;
import org.eclipse.collections.api.DoubleIterable;
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.DoubleShortToShortFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleToShortFunction;
import org.eclipse.collections.api.block.predicate.primitive.ShortPredicate;
import org.eclipse.collections.api.block.predicate.primitive.DoubleShortPredicate;
import org.eclipse.collections.api.iterator.MutableShortIterator;
import org.eclipse.collections.api.map.primitive.MutableShortDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableDoubleShortMap;
import org.eclipse.collections.api.map.primitive.DoubleShortMap;
public class DoubleShortBTreeMap extends BaseDoubleShortMap implements DoubleShortBufferMap {
protected final BTree bTree;
protected int size = 0;
DoubleShortBTreeMap(LargeByteBufferAllocator allocator, BTreeConfig config) {
int leafSize = Double.BYTES + Short.BYTES;
int branchSize = config.entryMustBeInLeaf ? Double.BYTES : leafSize;
this.bTree = new BTree(allocator, config, branchSize, leafSize) {
@Override
protected void writeBranchEntry(LargeByteBuffer lbb, long address, long key, long value) {
lbb.setDouble(address, fromKey(key));
if (!config.entryMustBeInLeaf) {
lbb.setShort(address + Double.BYTES, fromValue(value));
}
}
@Override
protected void writeLeafEntry(LargeByteBuffer lbb, long address, long key, long value) {
lbb.setDouble(address, fromKey(key));
lbb.setShort(address + Double.BYTES, fromValue(value));
}
@Override
protected long readBranchKey(LargeByteBuffer lbb, long address) {
return toKey(lbb.getDouble(address));
}
@Override
protected long readBranchValue(LargeByteBuffer lbb, long address) {
if (config.entryMustBeInLeaf) {
throw new AssertionError();
} else {
return toValue(lbb.getShort(address + Double.BYTES));
}
}
@Override
protected long readLeafKey(LargeByteBuffer lbb, long address) {
return toKey(lbb.getDouble(address));
}
@Override
protected long readLeafValue(LargeByteBuffer lbb, long address) {
return toValue(lbb.getShort(address + Double.BYTES));
}
};
}
@Override
protected MapStoreCursor iterationCursor() {
BTree.Cursor cursor = bTree.allocateCursor();
cursor.descendToStart();
return cursor;
}
@Override
protected MapStoreCursor keyCursor(double 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 DoubleShortBTreeMap implements MutableDoubleShortBufferMap {
Mutable(LargeByteBufferAllocator allocator, BTreeConfig config) {
super(allocator, config);
}
@Override
public void put(double key, short 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(DoubleShortMap map) {
map.forEachKeyValue(this::put);
}
@Override
public void updateValues(DoubleShortToShortFunction function) {
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToStart();
while (cursor.next()) {
short updated = function.valueOf(fromKey(cursor.getKey()), fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
}
}
}
@Override
public void removeKey(double 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(double key) {
removeKey(key);
}
@Override
public short removeKeyIfAbsent(double key, short value) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
short v = fromValue(cursor.getValue());
cursor.simpleRemove();
cursor.balance();
size--;
return v;
} else {
return value;
}
}
}
@Override
public short getIfAbsentPut(double key, short 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 short getIfAbsentPut(double key, ShortFunction0 function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
short v = function.value();
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public short getIfAbsentPutWithKey(double key, DoubleToShortFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
short v = function.valueOf(key);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public <P> short getIfAbsentPutWith(double key, ShortFunction<? 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 {
short v = function.shortValueOf(parameter);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public short updateValue(double key, short initialValueIfAbsent, ShortToShortFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
short updated = function.valueOf(fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
return updated;
} else {
short updated = function.valueOf(initialValueIfAbsent);
cursor.simpleInsert(k, toValue(updated));
cursor.balance();
size++;
return updated;
}
}
}
@Override
public MutableDoubleShortMap withKeyValue(double key, short value) {
put(key, value);
return this;
}
@Override
public MutableDoubleShortMap withoutKey(double key) {
removeKey(key);
return this;
}
@Override
public MutableDoubleShortMap withoutAllKeys(DoubleIterable keys) {
keys.forEach(this::removeKey);
return this;
}
@Override
public MutableDoubleShortMap asUnmodifiable() {
throw new UnsupportedOperationException("Mutable.asUnmodifiable not implemented yet");
}
@Override
public MutableDoubleShortMap asSynchronized() {
throw new UnsupportedOperationException("Mutable.asSynchronized not implemented yet");
}
@Override
public short addToValue(double key, short toBeAdded) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
short updated = (short) (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 MutableShortDoubleMap flipUniqueValues() {
throw new UnsupportedOperationException("DoubleShortBufferMap.Mutable.flipUniqueValues not implemented yet");
}
@Override
public MutableDoubleShortMap select(DoubleShortPredicate predicate) {
throw new UnsupportedOperationException("DoubleShortBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableShortBag select(ShortPredicate predicate) {
throw new UnsupportedOperationException("DoubleShortBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableDoubleShortMap reject(DoubleShortPredicate predicate) {
throw new UnsupportedOperationException("DoubleShortBufferMap.Mutable.reject not implemented yet");
}
@Override
public MutableShortBag reject(ShortPredicate predicate) {
throw new UnsupportedOperationException("DoubleShortBufferMap.Mutable.reject not implemented yet");
}
@Override
public <V> MutableBag<V> collect(ShortToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("DoubleShortBufferMap.Mutable.collect not implemented yet");
}
@Override
public MutableShortIterator shortIterator() {
return super.shortIterator();
}
}
}