package at.yawk.numaec;
import org.eclipse.collections.api.DoubleIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableFloatBag;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction0;
import org.eclipse.collections.api.block.function.primitive.FloatToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFloatToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleToFloatFunction;
import org.eclipse.collections.api.block.predicate.primitive.FloatPredicate;
import org.eclipse.collections.api.block.predicate.primitive.DoubleFloatPredicate;
import org.eclipse.collections.api.iterator.MutableFloatIterator;
import org.eclipse.collections.api.map.primitive.MutableFloatDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableDoubleFloatMap;
import org.eclipse.collections.api.map.primitive.DoubleFloatMap;
public class DoubleFloatBTreeMap extends BaseDoubleFloatMap implements DoubleFloatBufferMap {
protected final BTree bTree;
protected int size = 0;
DoubleFloatBTreeMap(LargeByteBufferAllocator allocator, BTreeConfig config) {
int leafSize = Double.BYTES + Float.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.setFloat(address + Double.BYTES, fromValue(value));
}
}
@Override
protected void writeLeafEntry(LargeByteBuffer lbb, long address, long key, long value) {
lbb.setDouble(address, fromKey(key));
lbb.setFloat(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.getFloat(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.getFloat(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 DoubleFloatBTreeMap implements MutableDoubleFloatBufferMap {
Mutable(LargeByteBufferAllocator allocator, BTreeConfig config) {
super(allocator, config);
}
@Override
public void put(double key, float 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(DoubleFloatMap map) {
map.forEachKeyValue(this::put);
}
@Override
public void updateValues(DoubleFloatToFloatFunction function) {
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToStart();
while (cursor.next()) {
float 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 float removeKeyIfAbsent(double key, float value) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
float v = fromValue(cursor.getValue());
cursor.simpleRemove();
cursor.balance();
size--;
return v;
} else {
return value;
}
}
}
@Override
public float getIfAbsentPut(double key, float 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 float getIfAbsentPut(double key, FloatFunction0 function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
float v = function.value();
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public float getIfAbsentPutWithKey(double key, DoubleToFloatFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
float v = function.valueOf(key);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public <P> float getIfAbsentPutWith(double key, FloatFunction<? 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 {
float v = function.floatValueOf(parameter);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public float updateValue(double key, float initialValueIfAbsent, FloatToFloatFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
float updated = function.valueOf(fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
return updated;
} else {
float updated = function.valueOf(initialValueIfAbsent);
cursor.simpleInsert(k, toValue(updated));
cursor.balance();
size++;
return updated;
}
}
}
@Override
public MutableDoubleFloatMap withKeyValue(double key, float value) {
put(key, value);
return this;
}
@Override
public MutableDoubleFloatMap withoutKey(double key) {
removeKey(key);
return this;
}
@Override
public MutableDoubleFloatMap withoutAllKeys(DoubleIterable keys) {
keys.forEach(this::removeKey);
return this;
}
@Override
public MutableDoubleFloatMap asUnmodifiable() {
throw new UnsupportedOperationException("Mutable.asUnmodifiable not implemented yet");
}
@Override
public MutableDoubleFloatMap asSynchronized() {
throw new UnsupportedOperationException("Mutable.asSynchronized not implemented yet");
}
@Override
public float addToValue(double key, float toBeAdded) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
float updated = (float) (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 MutableFloatDoubleMap flipUniqueValues() {
throw new UnsupportedOperationException("DoubleFloatBufferMap.Mutable.flipUniqueValues not implemented yet");
}
@Override
public MutableDoubleFloatMap select(DoubleFloatPredicate predicate) {
throw new UnsupportedOperationException("DoubleFloatBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableFloatBag select(FloatPredicate predicate) {
throw new UnsupportedOperationException("DoubleFloatBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableDoubleFloatMap reject(DoubleFloatPredicate predicate) {
throw new UnsupportedOperationException("DoubleFloatBufferMap.Mutable.reject not implemented yet");
}
@Override
public MutableFloatBag reject(FloatPredicate predicate) {
throw new UnsupportedOperationException("DoubleFloatBufferMap.Mutable.reject not implemented yet");
}
@Override
public <V> MutableBag<V> collect(FloatToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("DoubleFloatBufferMap.Mutable.collect not implemented yet");
}
@Override
public MutableFloatIterator floatIterator() {
return super.floatIterator();
}
}
}