package it.unimi.dsi.fastutil.shorts;
import it.unimi.dsi.fastutil.Hash;
import it.unimi.dsi.fastutil.HashCommon;
import static it.unimi.dsi.fastutil.HashCommon.arraySize;
import static it.unimi.dsi.fastutil.HashCommon.maxFill;
import java.util.Map;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import it.unimi.dsi.fastutil.doubles.DoubleCollection;
import it.unimi.dsi.fastutil.doubles.AbstractDoubleCollection;
import it.unimi.dsi.fastutil.doubles.DoubleIterator;
import java.util.Comparator;
import it.unimi.dsi.fastutil.doubles.DoubleListIterator;
import it.unimi.dsi.fastutil.objects.AbstractObjectSortedSet;
import it.unimi.dsi.fastutil.objects.ObjectListIterator;
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator;
import it.unimi.dsi.fastutil.objects.ObjectSortedSet;
public class Short2DoubleLinkedOpenHashMap extends AbstractShort2DoubleSortedMap
implements
java.io.Serializable,
Cloneable,
Hash {
private static final long serialVersionUID = 0L;
private static final boolean ASSERTS = false;
protected transient short[] key;
protected transient double[] value;
protected transient int mask;
protected transient boolean containsNullKey;
protected transient int first = -1;
protected transient int last = -1;
protected transient long[] link;
protected transient int n;
protected transient int maxFill;
protected final transient int minN;
protected int size;
protected final float f;
protected transient FastSortedEntrySet entries;
protected transient ShortSortedSet keys;
protected transient DoubleCollection values;
public Short2DoubleLinkedOpenHashMap(final int expected, final float f) {
if (f <= 0 || f > 1)
throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than or equal to 1");
if (expected < 0)
throw new IllegalArgumentException("The expected number of elements must be nonnegative");
this.f = f;
minN = n = arraySize(expected, f);
mask = n - 1;
maxFill = maxFill(n, f);
key = new short[n + 1];
value = new double[n + 1];
link = new long[n + 1];
}
public Short2DoubleLinkedOpenHashMap(final int expected) {
this(expected, DEFAULT_LOAD_FACTOR);
}
public Short2DoubleLinkedOpenHashMap() {
this(DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR);
}
public Short2DoubleLinkedOpenHashMap(final Map<? extends Short, ? extends Double> m, final float f) {
this(m.size(), f);
putAll(m);
}
public Short2DoubleLinkedOpenHashMap(final Map<? extends Short, ? extends Double> m) {
this(m, DEFAULT_LOAD_FACTOR);
}
public Short2DoubleLinkedOpenHashMap(final Short2DoubleMap m, final float f) {
this(m.size(), f);
putAll(m);
}
public Short2DoubleLinkedOpenHashMap(final Short2DoubleMap m) {
this(m, DEFAULT_LOAD_FACTOR);
}
public Short2DoubleLinkedOpenHashMap(final short[] k, final double[] v, final float f) {
this(k.length, f);
if (k.length != v.length)
throw new IllegalArgumentException(
"The key array and the value array have different lengths (" + k.length + " and " + v.length + ")");
for (int i = 0; i < k.length; i++)
this.put(k[i], v[i]);
}
public Short2DoubleLinkedOpenHashMap(final short[] k, final double[] v) {
this(k, v, DEFAULT_LOAD_FACTOR);
}
private int realSize() {
return containsNullKey ? size - 1 : size;
}
private void ensureCapacity(final int capacity) {
final int needed = arraySize(capacity, f);
if (needed > n)
rehash(needed);
}
private void tryCapacity(final long capacity) {
final int needed = (int) Math.min(1 << 30,
Math.max(2, HashCommon.nextPowerOfTwo((long) Math.ceil(capacity / f))));
if (needed > n)
rehash(needed);
}
private double removeEntry(final int pos) {
final double oldValue = value[pos];
size--;
fixPointers(pos);
shiftKeys(pos);
if (n > minN && size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
rehash(n / 2);
return oldValue;
}
private double removeNullEntry() {
containsNullKey = false;
final double oldValue = value[n];
size--;
fixPointers(n);
if (n > minN && size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
rehash(n / 2);
return oldValue;
}
@Override
public void putAll(Map<? extends Short, ? extends Double> m) {
if (f <= .5)
ensureCapacity(m.size());
else
tryCapacity(size() + m.size());
super.putAll(m);
}
private int find(final short k) {
if (((k) == ((short) 0)))
return containsNullKey ? n : -(n + 1);
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return -(pos + 1);
if (((k) == (curr)))
return pos;
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return -(pos + 1);
if (((k) == (curr)))
return pos;
}
}
private void insert(final int pos, final short k, final double v) {
if (pos == n)
containsNullKey = true;
key[pos] = k;
value[pos] = v;
if (size == 0) {
first = last = pos;
link[pos] = -1L;
} else {
link[last] ^= ((link[last] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[pos] = ((last & 0xFFFFFFFFL) << 32) | (-1 & 0xFFFFFFFFL);
last = pos;
}
if (size++ >= maxFill)
rehash(arraySize(size + 1, f));
if (ASSERTS)
checkTable();
}
@Override
public double put(final short k, final double v) {
final int pos = find(k);
if (pos < 0) {
insert(-pos - 1, k, v);
return defRetValue;
}
final double oldValue = value[pos];
value[pos] = v;
return oldValue;
}
private double addToValue(final int pos, final double incr) {
final double oldValue = value[pos];
value[pos] = oldValue + incr;
return oldValue;
}
public double addTo(final short k, final double incr) {
int pos;
if (((k) == ((short) 0))) {
if (containsNullKey)
return addToValue(n, incr);
pos = n;
containsNullKey = true;
} else {
short curr;
final short[] key = this.key;
if (!((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0))) {
if (((curr) == (k)))
return addToValue(pos, incr);
while (!((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
if (((curr) == (k)))
return addToValue(pos, incr);
}
}
key[pos] = k;
value[pos] = defRetValue + incr;
if (size == 0) {
first = last = pos;
link[pos] = -1L;
} else {
link[last] ^= ((link[last] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[pos] = ((last & 0xFFFFFFFFL) << 32) | (-1 & 0xFFFFFFFFL);
last = pos;
}
if (size++ >= maxFill)
rehash(arraySize(size + 1, f));
if (ASSERTS)
checkTable();
return defRetValue;
}
protected final void shiftKeys(int pos) {
int last, slot;
short curr;
final short[] key = this.key;
for (;;) {
pos = ((last = pos) + 1) & mask;
for (;;) {
if (((curr = key[pos]) == ((short) 0))) {
key[last] = ((short) 0);
return;
}
slot = (it.unimi.dsi.fastutil.HashCommon.mix((curr))) & mask;
if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
break;
pos = (pos + 1) & mask;
}
key[last] = curr;
value[last] = value[pos];
fixPointers(pos, last);
}
}
@Override
public double remove(final short k) {
if (((k) == ((short) 0))) {
if (containsNullKey)
return removeNullEntry();
return defRetValue;
}
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr)))
return removeEntry(pos);
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr)))
return removeEntry(pos);
}
}
private double setValue(final int pos, final double v) {
final double oldValue = value[pos];
value[pos] = v;
return oldValue;
}
public double removeFirstDouble() {
if (size == 0)
throw new NoSuchElementException();
final int pos = first;
first = (int) link[pos];
if (0 <= first) {
link[first] |= (-1 & 0xFFFFFFFFL) << 32;
}
size--;
final double v = value[pos];
if (pos == n) {
containsNullKey = false;
} else
shiftKeys(pos);
if (n > minN && size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
rehash(n / 2);
return v;
}
public double removeLastDouble() {
if (size == 0)
throw new NoSuchElementException();
final int pos = last;
last = (int) (link[pos] >>> 32);
if (0 <= last) {
link[last] |= -1 & 0xFFFFFFFFL;
}
size--;
final double v = value[pos];
if (pos == n) {
containsNullKey = false;
} else
shiftKeys(pos);
if (n > minN && size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
rehash(n / 2);
return v;
}
private void moveIndexToFirst(final int i) {
if (size == 1 || first == i)
return;
if (last == i) {
last = (int) (link[i] >>> 32);
link[last] |= -1 & 0xFFFFFFFFL;
} else {
final long linki = link[i];
final int prev = (int) (linki >>> 32);
final int next = (int) linki;
link[prev] ^= ((link[prev] ^ (linki & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[next] ^= ((link[next] ^ (linki & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
}
link[first] ^= ((link[first] ^ ((i & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
link[i] = ((-1 & 0xFFFFFFFFL) << 32) | (first & 0xFFFFFFFFL);
first = i;
}
private void moveIndexToLast(final int i) {
if (size == 1 || last == i)
return;
if (first == i) {
first = (int) link[i];
link[first] |= (-1 & 0xFFFFFFFFL) << 32;
} else {
final long linki = link[i];
final int prev = (int) (linki >>> 32);
final int next = (int) linki;
link[prev] ^= ((link[prev] ^ (linki & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[next] ^= ((link[next] ^ (linki & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
}
link[last] ^= ((link[last] ^ (i & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[i] = ((last & 0xFFFFFFFFL) << 32) | (-1 & 0xFFFFFFFFL);
last = i;
}
public double getAndMoveToFirst(final short k) {
if (((k) == ((short) 0))) {
if (containsNullKey) {
moveIndexToFirst(n);
return value[n];
}
return defRetValue;
}
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr))) {
moveIndexToFirst(pos);
return value[pos];
}
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr))) {
moveIndexToFirst(pos);
return value[pos];
}
}
}
public double getAndMoveToLast(final short k) {
if (((k) == ((short) 0))) {
if (containsNullKey) {
moveIndexToLast(n);
return value[n];
}
return defRetValue;
}
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr))) {
moveIndexToLast(pos);
return value[pos];
}
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr))) {
moveIndexToLast(pos);
return value[pos];
}
}
}
public double putAndMoveToFirst(final short k, final double v) {
int pos;
if (((k) == ((short) 0))) {
if (containsNullKey) {
moveIndexToFirst(n);
return setValue(n, v);
}
containsNullKey = true;
pos = n;
} else {
short curr;
final short[] key = this.key;
if (!((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0))) {
if (((curr) == (k))) {
moveIndexToFirst(pos);
return setValue(pos, v);
}
while (!((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
if (((curr) == (k))) {
moveIndexToFirst(pos);
return setValue(pos, v);
}
}
}
key[pos] = k;
value[pos] = v;
if (size == 0) {
first = last = pos;
link[pos] = -1L;
} else {
link[first] ^= ((link[first] ^ ((pos & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
link[pos] = ((-1 & 0xFFFFFFFFL) << 32) | (first & 0xFFFFFFFFL);
first = pos;
}
if (size++ >= maxFill)
rehash(arraySize(size, f));
if (ASSERTS)
checkTable();
return defRetValue;
}
public double putAndMoveToLast(final short k, final double v) {
int pos;
if (((k) == ((short) 0))) {
if (containsNullKey) {
moveIndexToLast(n);
return setValue(n, v);
}
containsNullKey = true;
pos = n;
} else {
short curr;
final short[] key = this.key;
if (!((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0))) {
if (((curr) == (k))) {
moveIndexToLast(pos);
return setValue(pos, v);
}
while (!((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
if (((curr) == (k))) {
moveIndexToLast(pos);
return setValue(pos, v);
}
}
}
key[pos] = k;
value[pos] = v;
if (size == 0) {
first = last = pos;
link[pos] = -1L;
} else {
link[last] ^= ((link[last] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[pos] = ((last & 0xFFFFFFFFL) << 32) | (-1 & 0xFFFFFFFFL);
last = pos;
}
if (size++ >= maxFill)
rehash(arraySize(size, f));
if (ASSERTS)
checkTable();
return defRetValue;
}
@Override
public double get(final short k) {
if (((k) == ((short) 0)))
return containsNullKey ? value[n] : defRetValue;
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr)))
return value[pos];
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return defRetValue;
if (((k) == (curr)))
return value[pos];
}
}
@Override
public boolean containsKey(final short k) {
if (((k) == ((short) 0)))
return containsNullKey;
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return false;
if (((k) == (curr)))
return true;
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return false;
if (((k) == (curr)))
return true;
}
}
@Override
public boolean containsValue(final double v) {
final double value[] = this.value;
final short key[] = this.key;
if (containsNullKey && (Double.doubleToLongBits(value[n]) == Double.doubleToLongBits(v)))
return true;
for (int i = n; i-- != 0;)
if (!((key[i]) == ((short) 0)) && (Double.doubleToLongBits(value[i]) == Double.doubleToLongBits(v)))
return true;
return false;
}
@Override
public double getOrDefault(final short k, final double defaultValue) {
if (((k) == ((short) 0)))
return containsNullKey ? value[n] : defaultValue;
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return defaultValue;
if (((k) == (curr)))
return value[pos];
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return defaultValue;
if (((k) == (curr)))
return value[pos];
}
}
@Override
public double putIfAbsent(final short k, final double v) {
final int pos = find(k);
if (pos >= 0)
return value[pos];
insert(-pos - 1, k, v);
return defRetValue;
}
@Override
public boolean remove(final short k, final double v) {
if (((k) == ((short) 0))) {
if (containsNullKey && (Double.doubleToLongBits(v) == Double.doubleToLongBits(value[n]))) {
removeNullEntry();
return true;
}
return false;
}
short curr;
final short[] key = this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return false;
if (((k) == (curr)) && (Double.doubleToLongBits(v) == Double.doubleToLongBits(value[pos]))) {
removeEntry(pos);
return true;
}
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return false;
if (((k) == (curr)) && (Double.doubleToLongBits(v) == Double.doubleToLongBits(value[pos]))) {
removeEntry(pos);
return true;
}
}
}
@Override
public boolean replace(final short k, final double oldValue, final double v) {
final int pos = find(k);
if (pos < 0 || !(Double.doubleToLongBits(oldValue) == Double.doubleToLongBits(value[pos])))
return false;
value[pos] = v;
return true;
}
@Override
public double replace(final short k, final double v) {
final int pos = find(k);
if (pos < 0)
return defRetValue;
final double oldValue = value[pos];
value[pos] = v;
return oldValue;
}
@Override
public double computeIfAbsent(final short k, final java.util.function.IntToDoubleFunction mappingFunction) {
java.util.Objects.requireNonNull(mappingFunction);
final int pos = find(k);
if (pos >= 0)
return value[pos];
final double newValue = mappingFunction.applyAsDouble(k);
insert(-pos - 1, k, newValue);
return newValue;
}
@Override
public double computeIfAbsentNullable(final short k,
final java.util.function.IntFunction<? extends Double> mappingFunction) {
java.util.Objects.requireNonNull(mappingFunction);
final int pos = find(k);
if (pos >= 0)
return value[pos];
final Double newValue = mappingFunction.apply(k);
if (newValue == null)
return defRetValue;
final double v = (newValue).doubleValue();
insert(-pos - 1, k, v);
return v;
}
@Override
public double computeIfPresent(final short k,
final java.util.function.BiFunction<? super Short, ? super Double, ? extends Double> remappingFunction) {
java.util.Objects.requireNonNull(remappingFunction);
final int pos = find(k);
if (pos < 0)
return defRetValue;
final Double newValue = remappingFunction.apply(Short.valueOf(k), Double.valueOf(value[pos]));
if (newValue == null) {
if (((k) == ((short) 0)))
removeNullEntry();
else
removeEntry(pos);
return defRetValue;
}
return value[pos] = (newValue).doubleValue();
}
@Override
public double compute(final short k,
final java.util.function.BiFunction<? super Short, ? super Double, ? extends Double> remappingFunction) {
java.util.Objects.requireNonNull(remappingFunction);
final int pos = find(k);
final Double newValue = remappingFunction.apply(Short.valueOf(k), pos >= 0 ? Double.valueOf(value[pos]) : null);
if (newValue == null) {
if (pos >= 0) {
if (((k) == ((short) 0)))
removeNullEntry();
else
removeEntry(pos);
}
return defRetValue;
}
double newVal = (newValue).doubleValue();
if (pos < 0) {
insert(-pos - 1, k, newVal);
return newVal;
}
return value[pos] = newVal;
}
@Override
public double merge(final short k, final double v,
final java.util.function.BiFunction<? super Double, ? super Double, ? extends Double> remappingFunction) {
java.util.Objects.requireNonNull(remappingFunction);
final int pos = find(k);
if (pos < 0) {
insert(-pos - 1, k, v);
return v;
}
final Double newValue = remappingFunction.apply(Double.valueOf(value[pos]), Double.valueOf(v));
if (newValue == null) {
if (((k) == ((short) 0)))
removeNullEntry();
else
removeEntry(pos);
return defRetValue;
}
return value[pos] = (newValue).doubleValue();
}
@Override
public void clear() {
if (size == 0)
return;
size = 0;
containsNullKey = false;
Arrays.fill(key, ((short) 0));
first = last = -1;
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
final class MapEntry implements Short2DoubleMap.Entry, Map.Entry<Short, Double> {
int index;
MapEntry(final int index) {
this.index = index;
}
MapEntry() {
}
@Override
public short getShortKey() {
return key[index];
}
@Override
public double getDoubleValue() {
return value[index];
}
@Override
public double setValue(final double v) {
final double oldValue = value[index];
value[index] = v;
return oldValue;
}
@Deprecated
@Override
public Short getKey() {
return Short.valueOf(key[index]);
}
@Deprecated
@Override
public Double getValue() {
return Double.valueOf(value[index]);
}
@Deprecated
@Override
public Double setValue(final Double v) {
return Double.valueOf(setValue((v).doubleValue()));
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(final Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<Short, Double> e = (Map.Entry<Short, Double>) o;
return ((key[index]) == ((e.getKey()).shortValue()))
&& (Double.doubleToLongBits(value[index]) == Double.doubleToLongBits((e.getValue()).doubleValue()));
}
@Override
public int hashCode() {
return (key[index]) ^ it.unimi.dsi.fastutil.HashCommon.double2int(value[index]);
}
@Override
public String toString() {
return key[index] + "=>" + value[index];
}
}
protected void fixPointers(final int i) {
if (size == 0) {
first = last = -1;
return;
}
if (first == i) {
first = (int) link[i];
if (0 <= first) {
link[first] |= (-1 & 0xFFFFFFFFL) << 32;
}
return;
}
if (last == i) {
last = (int) (link[i] >>> 32);
if (0 <= last) {
link[last] |= -1 & 0xFFFFFFFFL;
}
return;
}
final long linki = link[i];
final int prev = (int) (linki >>> 32);
final int next = (int) linki;
link[prev] ^= ((link[prev] ^ (linki & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[next] ^= ((link[next] ^ (linki & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
}
protected void fixPointers(int s, int d) {
if (size == 1) {
first = last = d;
link[d] = -1L;
return;
}
if (first == s) {
first = d;
link[(int) link[s]] ^= ((link[(int) link[s]] ^ ((d & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
link[d] = link[s];
return;
}
if (last == s) {
last = d;
link[(int) (link[s] >>> 32)] ^= ((link[(int) (link[s] >>> 32)] ^ (d & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[d] = link[s];
return;
}
final long links = link[s];
final int prev = (int) (links >>> 32);
final int next = (int) links;
link[prev] ^= ((link[prev] ^ (d & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[next] ^= ((link[next] ^ ((d & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
link[d] = links;
}
@Override
public short firstShortKey() {
if (size == 0)
throw new NoSuchElementException();
return key[first];
}
@Override
public short lastShortKey() {
if (size == 0)
throw new NoSuchElementException();
return key[last];
}
@Override
public Short2DoubleSortedMap tailMap(short from) {
throw new UnsupportedOperationException();
}
@Override
public Short2DoubleSortedMap headMap(short to) {
throw new UnsupportedOperationException();
}
@Override
public Short2DoubleSortedMap subMap(short from, short to) {
throw new UnsupportedOperationException();
}
@Override
public ShortComparator comparator() {
return null;
}
private class MapIterator {
int prev = -1;
int next = -1;
int curr = -1;
int index = -1;
protected MapIterator() {
next = first;
index = 0;
}
private MapIterator(final short from) {
if (((from) == ((short) 0))) {
if (Short2DoubleLinkedOpenHashMap.this.containsNullKey) {
next = (int) link[n];
prev = n;
return;
} else
throw new NoSuchElementException("The key " + from + " does not belong to this map.");
}
if (((key[last]) == (from))) {
prev = last;
index = size;
return;
}
int pos = (it.unimi.dsi.fastutil.HashCommon.mix((from))) & mask;
while (!((key[pos]) == ((short) 0))) {
if (((key[pos]) == (from))) {
next = (int) link[pos];
prev = pos;
return;
}
pos = (pos + 1) & mask;
}
throw new NoSuchElementException("The key " + from + " does not belong to this map.");
}
public boolean hasNext() {
return next != -1;
}
public boolean hasPrevious() {
return prev != -1;
}
private final void ensureIndexKnown() {
if (index >= 0)
return;
if (prev == -1) {
index = 0;
return;
}
if (next == -1) {
index = size;
return;
}
int pos = first;
index = 1;
while (pos != prev) {
pos = (int) link[pos];
index++;
}
}
public int nextIndex() {
ensureIndexKnown();
return index;
}
public int previousIndex() {
ensureIndexKnown();
return index - 1;
}
public int nextEntry() {
if (!hasNext())
throw new NoSuchElementException();
curr = next;
next = (int) link[curr];
prev = curr;
if (index >= 0)
index++;
return curr;
}
public int previousEntry() {
if (!hasPrevious())
throw new NoSuchElementException();
curr = prev;
prev = (int) (link[curr] >>> 32);
next = curr;
if (index >= 0)
index--;
return curr;
}
public void remove() {
ensureIndexKnown();
if (curr == -1)
throw new IllegalStateException();
if (curr == prev) {
index--;
prev = (int) (link[curr] >>> 32);
} else
next = (int) link[curr];
size--;
if (prev == -1)
first = next;
else
link[prev] ^= ((link[prev] ^ (next & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
if (next == -1)
last = prev;
else
link[next] ^= ((link[next] ^ ((prev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
int last, slot, pos = curr;
curr = -1;
if (pos == n) {
Short2DoubleLinkedOpenHashMap.this.containsNullKey = false;
} else {
short curr;
final short[] key = Short2DoubleLinkedOpenHashMap.this.key;
for (;;) {
pos = ((last = pos) + 1) & mask;
for (;;) {
if (((curr = key[pos]) == ((short) 0))) {
key[last] = ((short) 0);
return;
}
slot = (it.unimi.dsi.fastutil.HashCommon.mix((curr))) & mask;
if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos)
break;
pos = (pos + 1) & mask;
}
key[last] = curr;
value[last] = value[pos];
if (next == pos)
next = last;
if (prev == pos)
prev = last;
fixPointers(pos, last);
}
}
}
public int skip(final int n) {
int i = n;
while (i-- != 0 && hasNext())
nextEntry();
return n - i - 1;
}
public int back(final int n) {
int i = n;
while (i-- != 0 && hasPrevious())
previousEntry();
return n - i - 1;
}
public void set(@SuppressWarnings("unused") Short2DoubleMap.Entry ok) {
throw new UnsupportedOperationException();
}
public void add(@SuppressWarnings("unused") Short2DoubleMap.Entry ok) {
throw new UnsupportedOperationException();
}
}
private class EntryIterator extends MapIterator implements ObjectListIterator<Short2DoubleMap.Entry> {
private MapEntry entry;
public EntryIterator() {
}
public EntryIterator(short from) {
super(from);
}
@Override
public MapEntry next() {
return entry = new MapEntry(nextEntry());
}
@Override
public MapEntry previous() {
return entry = new MapEntry(previousEntry());
}
@Override
public void remove() {
super.remove();
entry.index = -1;
}
}
private class FastEntryIterator extends MapIterator implements ObjectListIterator<Short2DoubleMap.Entry> {
final MapEntry entry = new MapEntry();
public FastEntryIterator() {
}
public FastEntryIterator(short from) {
super(from);
}
@Override
public MapEntry next() {
entry.index = nextEntry();
return entry;
}
@Override
public MapEntry previous() {
entry.index = previousEntry();
return entry;
}
}
private final class MapEntrySet extends AbstractObjectSortedSet<Short2DoubleMap.Entry>
implements
FastSortedEntrySet {
@Override
public ObjectBidirectionalIterator<Short2DoubleMap.Entry> iterator() {
return new EntryIterator();
}
@Override
public Comparator<? super Short2DoubleMap.Entry> comparator() {
return null;
}
@Override
public ObjectSortedSet<Short2DoubleMap.Entry> subSet(Short2DoubleMap.Entry fromElement,
Short2DoubleMap.Entry toElement) {
throw new UnsupportedOperationException();
}
@Override
public ObjectSortedSet<Short2DoubleMap.Entry> headSet(Short2DoubleMap.Entry toElement) {
throw new UnsupportedOperationException();
}
@Override
public ObjectSortedSet<Short2DoubleMap.Entry> tailSet(Short2DoubleMap.Entry fromElement) {
throw new UnsupportedOperationException();
}
@Override
public Short2DoubleMap.Entry first() {
if (size == 0)
throw new NoSuchElementException();
return new MapEntry(Short2DoubleLinkedOpenHashMap.this.first);
}
@Override
public Short2DoubleMap.Entry last() {
if (size == 0)
throw new NoSuchElementException();
return new MapEntry(Short2DoubleLinkedOpenHashMap.this.last);
}
@Override
public boolean contains(final Object o) {
if (!(o instanceof Map.Entry))
return false;
final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
if (e.getKey() == null || !(e.getKey() instanceof Short))
return false;
if (e.getValue() == null || !(e.getValue() instanceof Double))
return false;
final short k = ((Short) (e.getKey())).shortValue();
final double v = ((Double) (e.getValue())).doubleValue();
if (((k) == ((short) 0)))
return Short2DoubleLinkedOpenHashMap.this.containsNullKey
&& (Double.doubleToLongBits(value[n]) == Double.doubleToLongBits(v));
short curr;
final short[] key = Short2DoubleLinkedOpenHashMap.this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return false;
if (((k) == (curr)))
return (Double.doubleToLongBits(value[pos]) == Double.doubleToLongBits(v));
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return false;
if (((k) == (curr)))
return (Double.doubleToLongBits(value[pos]) == Double.doubleToLongBits(v));
}
}
@Override
public boolean remove(final Object o) {
if (!(o instanceof Map.Entry))
return false;
final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
if (e.getKey() == null || !(e.getKey() instanceof Short))
return false;
if (e.getValue() == null || !(e.getValue() instanceof Double))
return false;
final short k = ((Short) (e.getKey())).shortValue();
final double v = ((Double) (e.getValue())).doubleValue();
if (((k) == ((short) 0))) {
if (containsNullKey && (Double.doubleToLongBits(value[n]) == Double.doubleToLongBits(v))) {
removeNullEntry();
return true;
}
return false;
}
short curr;
final short[] key = Short2DoubleLinkedOpenHashMap.this.key;
int pos;
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask]) == ((short) 0)))
return false;
if (((curr) == (k))) {
if ((Double.doubleToLongBits(value[pos]) == Double.doubleToLongBits(v))) {
removeEntry(pos);
return true;
}
return false;
}
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == ((short) 0)))
return false;
if (((curr) == (k))) {
if ((Double.doubleToLongBits(value[pos]) == Double.doubleToLongBits(v))) {
removeEntry(pos);
return true;
}
}
}
}
@Override
public int size() {
return size;
}
@Override
public void clear() {
Short2DoubleLinkedOpenHashMap.this.clear();
}
@Override
public ObjectListIterator<Short2DoubleMap.Entry> iterator(final Short2DoubleMap.Entry from) {
return new EntryIterator(from.getShortKey());
}
@Override
public ObjectListIterator<Short2DoubleMap.Entry> fastIterator() {
return new FastEntryIterator();
}
@Override
public ObjectListIterator<Short2DoubleMap.Entry> fastIterator(final Short2DoubleMap.Entry from) {
return new FastEntryIterator(from.getShortKey());
}
@Override
public void forEach(final Consumer<? super Short2DoubleMap.Entry> consumer) {
for (int i = size, curr, next = first; i-- != 0;) {
curr = next;
next = (int) link[curr];
consumer.accept(new AbstractShort2DoubleMap.BasicEntry(key[curr], value[curr]));
}
}
@Override
public void fastForEach(final Consumer<? super Short2DoubleMap.Entry> consumer) {
final AbstractShort2DoubleMap.BasicEntry entry = new AbstractShort2DoubleMap.BasicEntry();
for (int i = size, curr, next = first; i-- != 0;) {
curr = next;
next = (int) link[curr];
entry.key = key[curr];
entry.value = value[curr];
consumer.accept(entry);
}
}
}
@Override
public FastSortedEntrySet short2DoubleEntrySet() {
if (entries == null)
entries = new MapEntrySet();
return entries;
}
private final class KeyIterator extends MapIterator implements ShortListIterator {
public KeyIterator(final short k) {
super(k);
}
@Override
public short previousShort() {
return key[previousEntry()];
}
public KeyIterator() {
super();
}
@Override
public short nextShort() {
return key[nextEntry()];
}
}
private final class KeySet extends AbstractShortSortedSet {
@Override
public ShortListIterator iterator(final short from) {
return new KeyIterator(from);
}
@Override
public ShortListIterator iterator() {
return new KeyIterator();
}
@Override
public void forEach(final java.util.function.IntConsumer consumer) {
if (containsNullKey)
consumer.accept(key[n]);
for (int pos = n; pos-- != 0;) {
final short k = key[pos];
if (!((k) == ((short) 0)))
consumer.accept(k);
}
}
@Override
public int size() {
return size;
}
@Override
public boolean contains(short k) {
return containsKey(k);
}
@Override
public boolean remove(short k) {
final int oldSize = size;
Short2DoubleLinkedOpenHashMap.this.remove(k);
return size != oldSize;
}
@Override
public void clear() {
Short2DoubleLinkedOpenHashMap.this.clear();
}
@Override
public short firstShort() {
if (size == 0)
throw new NoSuchElementException();
return key[first];
}
@Override
public short lastShort() {
if (size == 0)
throw new NoSuchElementException();
return key[last];
}
@Override
public ShortComparator comparator() {
return null;
}
@Override
public ShortSortedSet tailSet(short from) {
throw new UnsupportedOperationException();
}
@Override
public ShortSortedSet headSet(short to) {
throw new UnsupportedOperationException();
}
@Override
public ShortSortedSet subSet(short from, short to) {
throw new UnsupportedOperationException();
}
}
@Override
public ShortSortedSet keySet() {
if (keys == null)
keys = new KeySet();
return keys;
}
private final class ValueIterator extends MapIterator implements DoubleListIterator {
@Override
public double previousDouble() {
return value[previousEntry()];
}
public ValueIterator() {
super();
}
@Override
public double nextDouble() {
return value[nextEntry()];
}
}
@Override
public DoubleCollection values() {
if (values == null)
values = new AbstractDoubleCollection() {
@Override
public DoubleIterator iterator() {
return new ValueIterator();
}
@Override
public int size() {
return size;
}
@Override
public boolean contains(double v) {
return containsValue(v);
}
@Override
public void clear() {
Short2DoubleLinkedOpenHashMap.this.clear();
}
@Override
public void forEach(final java.util.function.DoubleConsumer consumer) {
if (containsNullKey)
consumer.accept(value[n]);
for (int pos = n; pos-- != 0;)
if (!((key[pos]) == ((short) 0)))
consumer.accept(value[pos]);
}
};
return values;
}
public boolean trim() {
return trim(size);
}
public boolean trim(final int n) {
final int l = HashCommon.nextPowerOfTwo((int) Math.ceil(n / f));
if (l >= this.n || size > maxFill(l, f))
return true;
try {
rehash(l);
} catch (OutOfMemoryError cantDoIt) {
return false;
}
return true;
}
protected void rehash(final int newN) {
final short key[] = this.key;
final double value[] = this.value;
final int mask = newN - 1;
final short newKey[] = new short[newN + 1];
final double newValue[] = new double[newN + 1];
int i = first, prev = -1, newPrev = -1, t, pos;
final long link[] = this.link;
final long newLink[] = new long[newN + 1];
first = -1;
for (int j = size; j-- != 0;) {
if (((key[i]) == ((short) 0)))
pos = newN;
else {
pos = (it.unimi.dsi.fastutil.HashCommon.mix((key[i]))) & mask;
while (!((newKey[pos]) == ((short) 0)))
pos = (pos + 1) & mask;
}
newKey[pos] = key[i];
newValue[pos] = value[i];
if (prev != -1) {
newLink[newPrev] ^= ((newLink[newPrev] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
newLink[pos] ^= ((newLink[pos] ^ ((newPrev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
newPrev = pos;
} else {
newPrev = first = pos;
newLink[pos] = -1L;
}
t = i;
i = (int) link[i];
prev = t;
}
this.link = newLink;
this.last = newPrev;
if (newPrev != -1)
newLink[newPrev] |= -1 & 0xFFFFFFFFL;
n = newN;
this.mask = mask;
maxFill = maxFill(n, f);
this.key = newKey;
this.value = newValue;
}
@Override
public Short2DoubleLinkedOpenHashMap clone() {
Short2DoubleLinkedOpenHashMap c;
try {
c = (Short2DoubleLinkedOpenHashMap) super.clone();
} catch (CloneNotSupportedException cantHappen) {
throw new InternalError();
}
c.keys = null;
c.values = null;
c.entries = null;
c.containsNullKey = containsNullKey;
c.key = key.clone();
c.value = value.clone();
c.link = link.clone();
return c;
}
@Override
public int hashCode() {
int h = 0;
for (int j = realSize(), i = 0, t = 0; j-- != 0;) {
while (((key[i]) == ((short) 0)))
i++;
t = (key[i]);
t ^= it.unimi.dsi.fastutil.HashCommon.double2int(value[i]);
h += t;
i++;
}
if (containsNullKey)
h += it.unimi.dsi.fastutil.HashCommon.double2int(value[n]);
return h;
}
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
final short key[] = this.key;
final double value[] = this.value;
final MapIterator i = new MapIterator();
s.defaultWriteObject();
for (int j = size, e; j-- != 0;) {
e = i.nextEntry();
s.writeShort(key[e]);
s.writeDouble(value[e]);
}
}
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
n = arraySize(size, f);
maxFill = maxFill(n, f);
mask = n - 1;
final short key[] = this.key = new short[n + 1];
final double value[] = this.value = new double[n + 1];
final long link[] = this.link = new long[n + 1];
int prev = -1;
first = last = -1;
short k;
double v;
for (int i = size, pos; i-- != 0;) {
k = s.readShort();
v = s.readDouble();
if (((k) == ((short) 0))) {
pos = n;
containsNullKey = true;
} else {
pos = (it.unimi.dsi.fastutil.HashCommon.mix((k))) & mask;
while (!((key[pos]) == ((short) 0)))
pos = (pos + 1) & mask;
}
key[pos] = k;
value[pos] = v;
if (first != -1) {
link[prev] ^= ((link[prev] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
link[pos] ^= ((link[pos] ^ ((prev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
prev = pos;
} else {
prev = first = pos;
link[pos] |= (-1L & 0xFFFFFFFFL) << 32;
}
}
last = prev;
if (prev != -1)
link[prev] |= -1 & 0xFFFFFFFFL;
if (ASSERTS)
checkTable();
}
private void checkTable() {
}
}