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.LazyShortIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.primitive.FloatBag;
import org.eclipse.collections.api.block.function.primitive.FloatToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectFloatToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.FloatPredicate;
import org.eclipse.collections.api.block.predicate.primitive.ShortFloatPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.primitive.FloatProcedure;
import org.eclipse.collections.api.block.procedure.primitive.ShortFloatProcedure;
import org.eclipse.collections.api.block.procedure.primitive.ShortProcedure;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.iterator.FloatIterator;
import org.eclipse.collections.api.iterator.MutableFloatIterator;
import org.eclipse.collections.api.iterator.ShortIterator;
import org.eclipse.collections.api.map.primitive.FloatShortMap;
import org.eclipse.collections.api.map.primitive.ImmutableShortFloatMap;
import org.eclipse.collections.api.map.primitive.ShortFloatMap;
import org.eclipse.collections.api.set.primitive.MutableShortSet;
import org.eclipse.collections.api.tuple.primitive.ShortFloatPair;
import org.eclipse.collections.impl.lazy.AbstractLazyIterable;
import org.eclipse.collections.impl.lazy.primitive.AbstractLazyShortIterable;
import org.eclipse.collections.impl.primitive.AbstractFloatIterable;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
abstract class BaseShortFloatMap extends AbstractFloatIterable implements ShortFloatMap {
static final long KEY_MASK = -1L >>> (64 - (Short.BYTES * 8));
static final long VALUE_MASK = -1L >>> (64 - (Float.BYTES * 8));
static long toKey(short key) {
return key & KEY_MASK;
}
static long toValue(float value) {
return Float.floatToRawIntBits(value) & VALUE_MASK;
}
@SuppressWarnings("ConstantConditions")
static short fromKey(long key) {
if (KEY_MASK != -1) { if (key < 0 || key > KEY_MASK) { throw new IllegalArgumentException(); } }
return (short) key;
}
@SuppressWarnings("ConstantConditions")
static float fromValue(long value) {
if (VALUE_MASK != -1) { if (value < 0 || value > VALUE_MASK) { throw new IllegalArgumentException(); } }
return Float.intBitsToFloat((int) value);
}
protected abstract MapStoreCursor iterationCursor();
protected abstract MapStoreCursor keyCursor(short key);
@DoNotMutate
void checkInvariants() {
int count = count(x -> true);
if (count != size()) { throw new AssertionError(); }
}
@Override
public float getIfAbsent(short key, float ifAbsent) {
try (MapStoreCursor cursor = keyCursor(key)) {
if (cursor.elementFound()) {
return fromValue(cursor.getValue());
} else {
return ifAbsent;
}
}
}
@Override
public float getOrThrow(short 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(short key) {
try (MapStoreCursor cursor = keyCursor(key)) {
return cursor.elementFound();
}
}
@Override
public void forEachKey(ShortProcedure procedure) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
procedure.value(fromKey(cursor.getKey()));
}
}
}
@Override
public void forEachKeyValue(ShortFloatProcedure procedure) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
procedure.value(fromKey(cursor.getKey()),
fromValue(cursor.getValue()));
}
}
}
@Override
public LazyShortIterable keysView() {
return new AbstractLazyShortIterable() {
@Override
public ShortIterator shortIterator() {
return new KeyIterator(iterationCursor());
}
@Override
public void each(ShortProcedure procedure) {
forEachKey(procedure);
}
};
}
@Override
public RichIterable<ShortFloatPair> keyValuesView() {
return new AbstractLazyIterable<ShortFloatPair>() {
@Override
public Iterator<ShortFloatPair> iterator() {
return new KeyValueIterator(iterationCursor());
}
@Override
public void each(Procedure<? super ShortFloatPair> procedure) {
iterator().forEachRemaining(procedure);
}
};
}
@Override
public boolean containsValue(float value) {
long needle = toValue(value);
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
if (cursor.getValue() == needle) {
return true;
}
}
}
return false;
}
@Override
public void forEachValue(FloatProcedure procedure) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
procedure.value(fromValue(cursor.getValue()));
}
}
}
@Override
public MutableFloatIterator floatIterator() {
return new ValueIterator(iterationCursor());
}
@Override
public float[] toArray() {
float[] data = new float[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 float detectIfNone(FloatPredicate predicate, float ifNone) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
float value = fromValue(cursor.getValue());
if (predicate.accept(value)) {
return value;
}
}
}
return ifNone;
}
@Override
public int count(FloatPredicate predicate) {
int count = 0;
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
float value = fromValue(cursor.getValue());
if (predicate.accept(value)) {
count++;
}
}
}
return count;
}
@Override
public boolean anySatisfy(FloatPredicate predicate) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
float value = fromValue(cursor.getValue());
if (predicate.accept(value)) {
return true;
}
}
}
return false;
}
@Override
public boolean allSatisfy(FloatPredicate predicate) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
float value = fromValue(cursor.getValue());
if (!predicate.accept(value)) {
return false;
}
}
}
return true;
}
@Override
public <T> T injectInto(T injectedValue, ObjectFloatToObjectFunction<? super T, ? extends T> function) {
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
injectedValue = function.valueOf(injectedValue, fromValue(cursor.getValue()));
}
}
return injectedValue;
}
@Override
public double
sum() {
double
sum = 0;
try (MapStoreCursor cursor = iterationCursor()) {
while (cursor.next()) {
sum += fromValue(cursor.getValue());
}
}
return sum;
}
@Override
public float max() {
try (MapStoreCursor cursor = iterationCursor()) {
if (!cursor.next()) { throw new NoSuchElementException(); }
float max = fromValue(cursor.getValue());
while (cursor.next()) {
max = (float) Math.max(max, fromValue(cursor.getValue()));
}
return max;
}
}
@Override
public float min() {
try (MapStoreCursor cursor = iterationCursor()) {
if (!cursor.next()) { throw new NoSuchElementException(); }
float min = fromValue(cursor.getValue());
while (cursor.next()) {
min = (float) 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 float get(short key) {
return getIfAbsent(key, (float) 0);
}
@Override
public FloatShortMap flipUniqueValues() {
throw new UnsupportedOperationException("ShortFloatBufferMap.flipUniqueValues not implemented yet");
}
@Override
public ShortFloatMap select(ShortFloatPredicate predicate) {
throw new UnsupportedOperationException("ShortFloatBufferMap.select not implemented yet");
}
@Override
public ShortFloatMap reject(ShortFloatPredicate predicate) {
throw new UnsupportedOperationException("ShortFloatBufferMap.reject not implemented yet");
}
@Override
public ImmutableShortFloatMap toImmutable() {
throw new UnsupportedOperationException("ShortFloatBufferMap.toImmutable not implemented yet");
}
@Override
public MutableShortSet keySet() {
throw new UnsupportedOperationException("ShortFloatBufferMap.keySet not implemented yet");
}
@Override
public MutableFloatCollection values() {
throw new UnsupportedOperationException("ShortFloatBufferMap.values not implemented yet");
}
@Override
public FloatBag select(FloatPredicate predicate) {
throw new UnsupportedOperationException("ShortFloatBufferMap.select not implemented yet");
}
@Override
public FloatBag reject(FloatPredicate predicate) {
throw new UnsupportedOperationException("ShortFloatBufferMap.reject not implemented yet");
}
@Override
public <V> Bag<V> collect(FloatToObjectFunction<? extends V> function) {
throw new UnsupportedOperationException("ShortFloatBufferMap.collect not implemented yet");
}
@Override
public boolean contains(float value) {
return containsValue(value);
}
@Override
public void forEach(FloatProcedure procedure) {
forEachValue(procedure);
}
@Override
public void each(FloatProcedure procedure) {
forEachValue(procedure);
}
@Override
public boolean noneSatisfy(FloatPredicate 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 ShortIterator {
KeyIterator(MapStoreCursor cursor) {
super(cursor);
}
@Override
public short next() {
next0();
return fromKey(cursor.getKey());
}
}
private static class KeyValueIterator extends BaseIterator implements Iterator<ShortFloatPair> {
KeyValueIterator(MapStoreCursor cursor) {
super(cursor);
}
@Override
public ShortFloatPair next() {
next0();
return PrimitiveTuples.pair(fromKey(cursor.getKey()), fromValue(cursor.getValue()));
}
}
protected static class ValueIterator extends BaseIterator implements FloatIterator, MutableFloatIterator {
ValueIterator(MapStoreCursor cursor) {
super(cursor);
}
@Override
public float next() {
next0();
return fromValue(cursor.getValue());
}
@Override
public void remove() {
throw new UnsupportedOperationException("ValueIterator.remove not implemented yet");
}
}
}