package at.yawk.numaec;
import org.eclipse.collections.api.CharIterable;
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.CharLongToLongFunction;
import org.eclipse.collections.api.block.function.primitive.CharToLongFunction;
import org.eclipse.collections.api.block.predicate.primitive.LongPredicate;
import org.eclipse.collections.api.block.predicate.primitive.CharLongPredicate;
import org.eclipse.collections.api.iterator.MutableLongIterator;
import org.eclipse.collections.api.map.primitive.MutableLongCharMap;
import org.eclipse.collections.api.map.primitive.MutableCharLongMap;
import org.eclipse.collections.api.map.primitive.CharLongMap;
public class CharLongBTreeMap extends BaseCharLongMap implements CharLongBufferMap {
protected final BTree bTree;
protected int size = 0;
CharLongBTreeMap(LargeByteBufferAllocator allocator, BTreeConfig config) {
int leafSize = Character.BYTES + Long.BYTES;
int branchSize = config.entryMustBeInLeaf ? Character.BYTES : leafSize;
this.bTree = new BTree(allocator, config, branchSize, leafSize) {
@Override
protected void writeBranchEntry(LargeByteBuffer lbb, long address, long key, long value) {
lbb.setChar(address, fromKey(key));
if (!config.entryMustBeInLeaf) {
lbb.setLong(address + Character.BYTES, fromValue(value));
}
}
@Override
protected void writeLeafEntry(LargeByteBuffer lbb, long address, long key, long value) {
lbb.setChar(address, fromKey(key));
lbb.setLong(address + Character.BYTES, fromValue(value));
}
@Override
protected long readBranchKey(LargeByteBuffer lbb, long address) {
return toKey(lbb.getChar(address));
}
@Override
protected long readBranchValue(LargeByteBuffer lbb, long address) {
if (config.entryMustBeInLeaf) {
throw new AssertionError();
} else {
return toValue(lbb.getLong(address + Character.BYTES));
}
}
@Override
protected long readLeafKey(LargeByteBuffer lbb, long address) {
return toKey(lbb.getChar(address));
}
@Override
protected long readLeafValue(LargeByteBuffer lbb, long address) {
return toValue(lbb.getLong(address + Character.BYTES));
}
};
}
@Override
protected MapStoreCursor iterationCursor() {
BTree.Cursor cursor = bTree.allocateCursor();
cursor.descendToStart();
return cursor;
}
@Override
protected MapStoreCursor keyCursor(char 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 CharLongBTreeMap implements MutableCharLongBufferMap {
Mutable(LargeByteBufferAllocator allocator, BTreeConfig config) {
super(allocator, config);
}
@Override
public void put(char key, long 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(CharLongMap map) {
map.forEachKeyValue(this::put);
}
@Override
public void updateValues(CharLongToLongFunction function) {
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToStart();
while (cursor.next()) {
long updated = function.valueOf(fromKey(cursor.getKey()), fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
}
}
}
@Override
public void removeKey(char 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(char key) {
removeKey(key);
}
@Override
public long removeKeyIfAbsent(char key, long value) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
long v = fromValue(cursor.getValue());
cursor.simpleRemove();
cursor.balance();
size--;
return v;
} else {
return value;
}
}
}
@Override
public long getIfAbsentPut(char key, long 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 long getIfAbsentPut(char key, LongFunction0 function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
long v = function.value();
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public long getIfAbsentPutWithKey(char key, CharToLongFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
long v = function.valueOf(key);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public <P> long getIfAbsentPutWith(char key, LongFunction<? 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 {
long v = function.longValueOf(parameter);
cursor.simpleInsert(k, toValue(v));
cursor.balance();
size++;
return v;
}
}
}
@Override
public long updateValue(char key, long initialValueIfAbsent, LongToLongFunction function) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
long updated = function.valueOf(fromValue(cursor.getValue()));
cursor.setValue(toValue(updated));
return updated;
} else {
long updated = function.valueOf(initialValueIfAbsent);
cursor.simpleInsert(k, toValue(updated));
cursor.balance();
size++;
return updated;
}
}
}
@Override
public MutableCharLongMap withKeyValue(char key, long value) {
put(key, value);
return this;
}
@Override
public MutableCharLongMap withoutKey(char key) {
removeKey(key);
return this;
}
@Override
public MutableCharLongMap withoutAllKeys(CharIterable keys) {
keys.forEach(this::removeKey);
return this;
}
@Override
public MutableCharLongMap asUnmodifiable() {
throw new UnsupportedOperationException("Mutable.asUnmodifiable not implemented yet");
}
@Override
public MutableCharLongMap asSynchronized() {
throw new UnsupportedOperationException("Mutable.asSynchronized not implemented yet");
}
@Override
public long addToValue(char key, long toBeAdded) {
long k = toKey(key);
try (BTree.Cursor cursor = bTree.allocateCursor()) {
cursor.descendToKey(k);
if (cursor.elementFound()) {
long updated = (long) (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 MutableLongCharMap flipUniqueValues() {
throw new UnsupportedOperationException("CharLongBufferMap.Mutable.flipUniqueValues not implemented yet");
}
@Override
public MutableCharLongMap select(CharLongPredicate predicate) {
throw new UnsupportedOperationException("CharLongBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableLongBag select(LongPredicate predicate) {
throw new UnsupportedOperationException("CharLongBufferMap.Mutable.select not implemented yet");
}
@Override
public MutableCharLongMap reject(CharLongPredicate predicate) {
throw new UnsupportedOperationException("CharLongBufferMap.Mutable.reject not implemented yet");
}
@Override
public MutableLongBag reject(LongPredicate predicate) {
throw new UnsupportedOperationException("CharLongBufferMap.Mutable.reject not implemented yet");
}
@Override
public <V> MutableBag<V> collect(LongToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("CharLongBufferMap.Mutable.collect not implemented yet");
}
@Override
public MutableLongIterator longIterator() {
return super.longIterator();
}
}
}