package at.yawk.numaec;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.LazyLongIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.primitive.IntBag;
import org.eclipse.collections.api.block.function.primitive.IntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.LongIntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.primitive.IntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.LongIntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.LongProcedure;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.iterator.IntIterator;
import org.eclipse.collections.api.iterator.MutableIntIterator;
import org.eclipse.collections.api.iterator.LongIterator;
import org.eclipse.collections.api.map.primitive.IntLongMap;
import org.eclipse.collections.api.map.primitive.ImmutableLongIntMap;
import org.eclipse.collections.api.map.primitive.LongIntMap;
import org.eclipse.collections.api.set.primitive.MutableLongSet;
import org.eclipse.collections.api.tuple.primitive.LongIntPair;
import org.eclipse.collections.impl.lazy.AbstractLazyIterable;
import org.eclipse.collections.impl.lazy.primitive.AbstractLazyLongIterable;
import org.eclipse.collections.impl.primitive.AbstractIntIterable;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
abstract class BaseLongIntMap extends AbstractIntIterable implements LongIntMap {
static final long KEY_MASK = -1L >>> (64 - (Long.BYTES * 8));
static final long VALUE_MASK = -1L >>> (64 - (Integer.BYTES * 8));
static long toKey(long key) {
return key & KEY_MASK;
}
static long toValue(int value) {
return value & VALUE_MASK;
}
@SuppressWarnings("ConstantConditions")
static long fromKey(long key) {
if (KEY_MASK != -1) { if (key < 0 || key > KEY_MASK) { throw new IllegalArgumentException(); } }
return (long) key;
}
@SuppressWarnings("ConstantConditions")
static int fromValue(long value) {
if (VALUE_MASK != -1) { if (value < 0 || value > VALUE_MASK) { throw new IllegalArgumentException(); } }
return (int) value;
}
protected abstract MapStoreCursor iterationCursor();
protected abstract MapStoreCursor keyCursor(long key);
@DoNotMutate
void checkInvariants() {
int count = count(x -> true);
if (count != size()) { throw new AssertionError(); }
}
@Override
public int getIfAbsent(long key, int ifAbsent) {
try (MapStoreCursor cursor = keyCursor(key)) {
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
return ifAbsent;
}
}
}
@Override
public int getOrThrow(long key) {
try (MapStoreCursor cursor = keyCursor(key)) {
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
throw new IllegalStateException("Key " + key + " not present");
}
}
}
@Override
public boolean containsKey(long key) {
try (MapStoreCursor cursor = keyCursor(key)) {
return cursor.elementFound();
}
}
@Override
public void forEachKey(LongProcedure procedure) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
procedure.value(fromKey(cursor.getKey()));
}
}
}
@Override
public void forEachKeyValue(LongIntProcedure procedure) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
procedure.value(fromKey(cursor.getKey()),
fromValue(cursor.getValue()));
}
}
}
@Override
public LazyLongIterable keysView() {
return new AbstractLazyLongIterable() {
@Override
public LongIterator longIterator() {
return new KeyIterator(iterationCursor());
}
@Override
public void each(LongProcedure procedure) {
forEachKey(procedure);
}
};
}
@Override
public RichIterable<LongIntPair> keyValuesView() {
return new AbstractLazyIterable<LongIntPair>() {
@Override
public Iterator<LongIntPair> iterator() {
return new KeyValueIterator(iterationCursor());
}
@Override
public void each(Procedure<? super LongIntPair> procedure) {
iterator().forEachRemaining(procedure);
}
};
}
@Override
public boolean containsValue(int value) {
long needle = toValue(value);
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
if (cursor.getValue() == needle) {
return true;
}
}
}
return false;
}
@Override
public void forEachValue(IntProcedure procedure) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
procedure.value(fromValue(cursor.getValue()));
}
}
}
@Override
public MutableIntIterator intIterator() {
return new ValueIterator(iterationCursor());
}
@Override
public int[] toArray() {
int[] data = new int[size()];
try (MapStoreCursor cursor = iterationCursor()) {
int i = 0;
while (cursor.next()) {
if (i >= data.length) { throw new ConcurrentModificationException(); }
data[i++] = fromValue(cursor.getValue());
}
if (i < data.length) { throw new ConcurrentModificationException(); }
return data;
}
}
@Override
public int detectIfNone(IntPredicate predicate, int ifNone) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
int value = fromValue(cursor.getValue());
if (predicate.accept(value)) {
return value;
}
}
}
return ifNone;
}
@Override
public int count(IntPredicate predicate) {
int count = 0;
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
int value = fromValue(cursor.getValue());
if (predicate.accept(value)) {
count++;
}
}
}
return count;
}
@Override
public boolean anySatisfy(IntPredicate predicate) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
int value = fromValue(cursor.getValue());
if (predicate.accept(value)) {
return true;
}
}
}
return false;
}
@Override
public boolean allSatisfy(IntPredicate predicate) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
int value = fromValue(cursor.getValue());
if (!predicate.accept(value)) {
return false;
}
}
}
return true;
}
@Override
public <T> T injectInto(T injectedValue, ObjectIntToObjectFunction<? super T, ? extends T> function) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
injectedValue = function.valueOf(injectedValue, fromValue(cursor.getValue()));
}
}
return injectedValue;
}
@Override
public long
sum() {
long
sum = 0;
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
sum += fromValue(cursor.getValue());
}
}
return sum;
}
@Override
public int max() {
try (MapStoreCursor cursor = iterationCursor()) {
if (!cursor.next()) { throw new NoSuchElementException(); }
int max = fromValue(cursor.getValue());
while (cursor.next()) {
max = (int) Math.max(max, fromValue(cursor.getValue()));
}
return max;
}
}
@Override
public int min() {
try (MapStoreCursor cursor = iterationCursor()) {
if (!cursor.next()) { throw new NoSuchElementException(); }
int min = fromValue(cursor.getValue());
while (cursor.next()) {
min = (int) Math.min(min, fromValue(cursor.getValue()));
}
return min;
}
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end) {
try (MapStoreCursor cursor = iterationCursor()) {
appendable.append(start);
boolean first = true;
while (cursor.next()) {
if (first) {
first = false;
} else {
appendable.append(separator);
}
appendable.append(String.valueOf(fromValue(cursor.getValue())));
}
appendable.append(end);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("{");
try (MapStoreCursor cursor = iterationCursor()) {
boolean first = true;
while (cursor.next()) {
if (first) {
first = false;
} else {
builder.append(", ");
}
builder.append(fromKey(cursor.getKey()))
.append('=')
.append(fromValue(cursor.getValue()));
}
return builder.append('}').toString();
}
}
@Override
public int get(long key) {
return getIfAbsent(key, (int) 0);
}
@Override
public IntLongMap flipUniqueValues() {
throw new UnsupportedOperationException("LongIntBufferMap.flipUniqueValues not implemented yet");
}
@Override
public LongIntMap select(LongIntPredicate predicate) {
throw new UnsupportedOperationException("LongIntBufferMap.select not implemented yet");
}
@Override
public LongIntMap reject(LongIntPredicate predicate) {
throw new UnsupportedOperationException("LongIntBufferMap.reject not implemented yet");
}
@Override
public ImmutableLongIntMap toImmutable() {
throw new UnsupportedOperationException("LongIntBufferMap.toImmutable not implemented yet");
}
@Override
public MutableLongSet keySet() {
throw new UnsupportedOperationException("LongIntBufferMap.keySet not implemented yet");
}
@Override
public MutableIntCollection values() {
throw new UnsupportedOperationException("LongIntBufferMap.values not implemented yet");
}
@Override
public IntBag select(IntPredicate predicate) {
throw new UnsupportedOperationException("LongIntBufferMap.select not implemented yet");
}
@Override
public IntBag reject(IntPredicate predicate) {
throw new UnsupportedOperationException("LongIntBufferMap.reject not implemented yet");
}
@Override
public <V> Bag<V> collect(IntToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("LongIntBufferMap.collect not implemented yet");
}
@Override
public boolean contains(int value) {
return containsValue(value);
}
@Override
public void forEach(IntProcedure procedure) {
forEachValue(procedure);
}
@Override
public void each(IntProcedure procedure) {
forEachValue(procedure);
}
@Override
public boolean noneSatisfy(IntPredicate predicate) {
return !anySatisfy(predicate);
}
private static abstract class BaseIterator {
protected final MapStoreCursor cursor;
private boolean peeked;
BaseIterator(MapStoreCursor cursor) {
this.cursor = cursor;
}
protected void next0() {
if (!hasNext()) { throw new NoSuchElementException(); }
peeked = false;
}
public boolean hasNext() {
if (peeked) {
return true;
} else {
return peeked = cursor.next();
}
}
}
private static class KeyIterator extends BaseIterator implements LongIterator {
KeyIterator(MapStoreCursor cursor) {
super(cursor);
}
@Override
public long next() {
next0();
return fromKey(cursor.getKey());
}
}
private static class KeyValueIterator extends BaseIterator implements Iterator<LongIntPair> {
KeyValueIterator(MapStoreCursor cursor) {
super(cursor);
}
@Override
public LongIntPair next() {
next0();
return PrimitiveTuples.pair(fromKey(cursor.getKey()), fromValue(cursor.getValue()));
}
}
protected static class ValueIterator extends BaseIterator implements IntIterator, MutableIntIterator {
ValueIterator(MapStoreCursor cursor) {
super(cursor);
}
@Override
public int next() {
next0();
return fromValue(cursor.getValue());
}
@Override
public void remove() {
throw new UnsupportedOperationException("ValueIterator.remove not implemented yet");
}
}
}