/*
 * Copyright (c) 2018 Goldman Sachs and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.map.mutable.primitive;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.eclipse.collections.api.ByteIterable;
import org.eclipse.collections.api.LazyByteIterable;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.primitive.ByteToIntFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction0;
import org.eclipse.collections.api.block.function.primitive.IntToIntFunction;
import org.eclipse.collections.api.block.function.primitive.ByteIntToIntFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.ByteIntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ByteProcedure;
import org.eclipse.collections.api.block.procedure.primitive.ByteIntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.iterator.ByteIterator;
import org.eclipse.collections.api.iterator.MutableByteIterator;
import org.eclipse.collections.api.iterator.MutableIntIterator;
import org.eclipse.collections.api.map.primitive.ByteIntMap;
import org.eclipse.collections.api.map.primitive.ImmutableByteIntMap;
import org.eclipse.collections.api.map.primitive.MutableByteIntMap;
import org.eclipse.collections.api.map.primitive.MutableIntByteMap;
import org.eclipse.collections.api.set.primitive.ByteSet;
import org.eclipse.collections.api.set.primitive.IntSet;
import org.eclipse.collections.api.set.primitive.MutableByteSet;
import org.eclipse.collections.api.tuple.primitive.ByteIntPair;
import org.eclipse.collections.impl.bag.mutable.primitive.IntHashBag;
import org.eclipse.collections.impl.factory.primitive.ByteIntMaps;
import org.eclipse.collections.impl.factory.primitive.IntByteMaps;
import org.eclipse.collections.impl.iterator.UnmodifiableByteIterator;
import org.eclipse.collections.impl.lazy.AbstractLazyIterable;
import org.eclipse.collections.impl.lazy.primitive.AbstractLazyByteIterable;
import org.eclipse.collections.impl.set.mutable.primitive.ByteHashSet;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;

This file was automatically generated from template file primitivePrimitiveHashMap.stg.
Since:3.0.
/** * This file was automatically generated from template file primitivePrimitiveHashMap.stg. * * @since 3.0. */
public class ByteIntHashMap extends AbstractMutableIntValuesMap implements MutableByteIntMap, Externalizable, MutableByteKeysMap { private static final int EMPTY_VALUE = 0; private static final long serialVersionUID = 1L; private static final byte EMPTY_KEY = (byte) 0; private static final byte REMOVED_KEY = (byte) 1; private static final int CACHE_LINE_SIZE = 64; private static final int KEY_SIZE = 1; private static final int INITIAL_LINEAR_PROBE = CACHE_LINE_SIZE / KEY_SIZE / 2; /* half a cache line */ private static final int DEFAULT_INITIAL_CAPACITY = 8; private byte[] keys; private int[] values; private int occupiedWithData; private int occupiedWithSentinels; private SentinelValues sentinelValues; private boolean copyKeysOnWrite; public ByteIntHashMap() { this.allocateTable(DEFAULT_INITIAL_CAPACITY << 1); } public ByteIntHashMap(int initialCapacity) { if (initialCapacity < 0) { throw new IllegalArgumentException("initial capacity cannot be less than 0"); } int capacity = this.smallestPowerOfTwoGreaterThan(initialCapacity << 1); this.allocateTable(capacity); } public ByteIntHashMap(ByteIntMap map) { if (map instanceof ByteIntHashMap && ((ByteIntHashMap) map).occupiedWithSentinels == 0) { ByteIntHashMap hashMap = (ByteIntHashMap) map; this.occupiedWithData = hashMap.occupiedWithData; if (hashMap.sentinelValues != null) { this.sentinelValues = hashMap.sentinelValues.copy(); } this.keys = Arrays.copyOf(hashMap.keys, hashMap.keys.length); this.values = Arrays.copyOf(hashMap.values, hashMap.values.length); } else { int capacity = this.smallestPowerOfTwoGreaterThan(Math.max(map.size(), DEFAULT_INITIAL_CAPACITY) << 1); this.allocateTable(capacity); this.putAll(map); } } public static ByteIntHashMap newWithKeysValues(byte key1, int value1) { return new ByteIntHashMap(1).withKeyValue(key1, value1); } public static ByteIntHashMap newWithKeysValues(byte key1, int value1, byte key2, int value2) { return new ByteIntHashMap(2).withKeysValues(key1, value1, key2, value2); } public static ByteIntHashMap newWithKeysValues(byte key1, int value1, byte key2, int value2, byte key3, int value3) { return new ByteIntHashMap(3).withKeysValues(key1, value1, key2, value2, key3, value3); } public static ByteIntHashMap newWithKeysValues(byte key1, int value1, byte key2, int value2, byte key3, int value3, byte key4, int value4) { return new ByteIntHashMap(4).withKeysValues(key1, value1, key2, value2, key3, value3, key4, value4); } private int smallestPowerOfTwoGreaterThan(int n) { return n > 1 ? Integer.highestOneBit(n - 1) << 1 : 1; } @Override protected int getOccupiedWithData() { return this.occupiedWithData; } @Override protected SentinelValues getSentinelValues() { return this.sentinelValues; } @Override protected void setSentinelValuesNull() { this.sentinelValues = null; } @Override protected int getEmptyValue() { return EMPTY_VALUE; } @Override protected int getTableSize() { return this.values.length; } @Override protected int getValueAtIndex(int index) { return this.values[index]; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof ByteIntMap)) { return false; } ByteIntMap other = (ByteIntMap) obj; if (this.size() != other.size()) { return false; } if (this.sentinelValues == null) { if (other.containsKey(EMPTY_KEY) || other.containsKey(REMOVED_KEY)) { return false; } } else { if (this.sentinelValues.containsZeroKey && (!other.containsKey(EMPTY_KEY) || this.sentinelValues.zeroValue != other.getOrThrow(EMPTY_KEY))) { return false; } if (this.sentinelValues.containsOneKey && (!other.containsKey(REMOVED_KEY) || this.sentinelValues.oneValue != other.getOrThrow(REMOVED_KEY))) { return false; } } for (int i = 0; i < this.keys.length; i++) { byte key = this.keys[i]; if (isNonSentinel(key) && (!other.containsKey(key) || this.values[i] != other.getOrThrow(key))) { return false; } } return true; } @Override public int hashCode() { int result = 0; if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey) { result += (int) EMPTY_KEY ^ this.sentinelValues.zeroValue; } if (this.sentinelValues.containsOneKey) { result += (int) REMOVED_KEY ^ this.sentinelValues.oneValue; } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { result += (int) this.keys[i] ^ this.values[i]; } } return result; } @Override public String toString() { StringBuilder appendable = new StringBuilder(); appendable.append("{"); boolean first = true; if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey) { appendable.append(EMPTY_KEY).append("=").append(this.sentinelValues.zeroValue); first = false; } if (this.sentinelValues.containsOneKey) { if (!first) { appendable.append(", "); } appendable.append(REMOVED_KEY).append("=").append(this.sentinelValues.oneValue); first = false; } } for (int i = 0; i < this.keys.length; i++) { byte key = this.keys[i]; if (isNonSentinel(key)) { if (!first) { appendable.append(", "); } appendable.append(key).append("=").append(this.values[i]); first = false; } } appendable.append("}"); return appendable.toString(); } @Override public MutableIntIterator intIterator() { return new InternalIntIterator(); } @Override public <V> V injectInto(V injectedValue, ObjectIntToObjectFunction<? super V, ? extends V> function) { V result = injectedValue; if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey) { result = function.valueOf(result, this.sentinelValues.zeroValue); } if (this.sentinelValues.containsOneKey) { result = function.valueOf(result, this.sentinelValues.oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { result = function.valueOf(result, this.values[i]); } } return result; } @Override public void clear() { this.sentinelValues = null; this.occupiedWithData = 0; this.occupiedWithSentinels = 0; if (this.copyKeysOnWrite) { this.copyKeys(); } Arrays.fill(this.keys, EMPTY_KEY); Arrays.fill(this.values, EMPTY_VALUE); } @Override public void put(byte key, int value) { if (isEmptyKey(key)) { this.putForEmptySentinel(value); return; } if (isRemovedKey(key)) { this.putForRemovedSentinel(value); return; } int index = this.probe(key); byte keyAtIndex = this.keys[index]; if (keyAtIndex == key) { this.values[index] = value; } else { this.addKeyValueAtIndex(key, value, index); } } private void putForRemovedSentinel(int value) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); } this.addRemovedKeyValue(value); } private void putForEmptySentinel(int value) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); } this.addEmptyKeyValue(value); } @Override public void putAll(ByteIntMap map) { map.forEachKeyValue(this::put); } @Override public void updateValues(ByteIntToIntFunction function) { if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey) { this.sentinelValues.zeroValue = function.valueOf(EMPTY_KEY, this.sentinelValues.zeroValue); } if (this.sentinelValues.containsOneKey) { this.sentinelValues.oneValue = function.valueOf(REMOVED_KEY, this.sentinelValues.oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { this.values[i] = function.valueOf(this.keys[i], this.values[i]); } } } @Override public void removeKey(byte key) { if (isEmptyKey(key)) { if (this.sentinelValues == null || !this.sentinelValues.containsZeroKey) { return; } this.removeEmptyKey(); return; } if (isRemovedKey(key)) { if (this.sentinelValues == null || !this.sentinelValues.containsOneKey) { return; } this.removeRemovedKey(); return; } int index = this.probe(key); if (this.keys[index] == key) { this.removeKeyAtIndex(index); } } @Override public void remove(byte key) { this.removeKey(key); } @Override public int removeKeyIfAbsent(byte key, int value) { if (isEmptyKey(key)) { if (this.sentinelValues == null || !this.sentinelValues.containsZeroKey) { return value; } int oldValue = this.sentinelValues.zeroValue; this.removeEmptyKey(); return oldValue; } if (isRemovedKey(key)) { if (this.sentinelValues == null || !this.sentinelValues.containsOneKey) { return value; } int oldValue = this.sentinelValues.oneValue; this.removeRemovedKey(); return oldValue; } int index = this.probe(key); if (this.keys[index] == key) { int oldValue = this.values[index]; this.removeKeyAtIndex(index); return oldValue; } return value; } @Override public int getIfAbsentPut(byte key, int value) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(value); return value; } if (this.sentinelValues.containsZeroKey) { return this.sentinelValues.zeroValue; } this.addEmptyKeyValue(value); return value; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(value); return value; } if (this.sentinelValues.containsOneKey) { return this.sentinelValues.oneValue; } this.addRemovedKeyValue(value); return value; } int index = this.probe(key); if (this.keys[index] == key) { return this.values[index]; } this.addKeyValueAtIndex(key, value, index); return value; } @Override public int getIfAbsentPut(byte key, IntFunction0 function) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { int value = function.value(); this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(value); return value; } if (this.sentinelValues.containsZeroKey) { return this.sentinelValues.zeroValue; } int value = function.value(); this.addEmptyKeyValue(value); return value; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { int value = function.value(); this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(value); return value; } if (this.sentinelValues.containsOneKey) { return this.sentinelValues.oneValue; } int value = function.value(); this.addRemovedKeyValue(value); return value; } int index = this.probe(key); if (this.keys[index] == key) { return this.values[index]; } int value = function.value(); this.addKeyValueAtIndex(key, value, index); return value; } @Override public <P> int getIfAbsentPutWith(byte key, IntFunction<? super P> function, P parameter) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { int value = function.intValueOf(parameter); this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(value); return value; } if (this.sentinelValues.containsZeroKey) { return this.sentinelValues.zeroValue; } int value = function.intValueOf(parameter); this.addEmptyKeyValue(value); return value; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { int value = function.intValueOf(parameter); this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(value); return value; } if (this.sentinelValues.containsOneKey) { return this.sentinelValues.oneValue; } int value = function.intValueOf(parameter); this.addRemovedKeyValue(value); return value; } int index = this.probe(key); if (this.keys[index] == key) { return this.values[index]; } int value = function.intValueOf(parameter); this.addKeyValueAtIndex(key, value, index); return value; } @Override public int getIfAbsentPutWithKey(byte key, ByteToIntFunction function) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { int value = function.valueOf(key); this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(value); return value; } if (this.sentinelValues.containsZeroKey) { return this.sentinelValues.zeroValue; } int value = function.valueOf(key); this.addEmptyKeyValue(value); return value; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { int value = function.valueOf(key); this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(value); return value; } if (this.sentinelValues.containsOneKey) { return this.sentinelValues.oneValue; } int value = function.valueOf(key); this.addRemovedKeyValue(value); return value; } int index = this.probe(key); if (this.keys[index] == key) { return this.values[index]; } int value = function.valueOf(key); this.addKeyValueAtIndex(key, value, index); return value; } @Override public int addToValue(byte key, int toBeAdded) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(toBeAdded); } else if (this.sentinelValues.containsZeroKey) { this.sentinelValues.zeroValue += toBeAdded; } else { this.addEmptyKeyValue(toBeAdded); } return this.sentinelValues.zeroValue; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(toBeAdded); } else if (this.sentinelValues.containsOneKey) { this.sentinelValues.oneValue += toBeAdded; } else { this.addRemovedKeyValue(toBeAdded); } return this.sentinelValues.oneValue; } int index = this.probe(key); if (this.keys[index] == key) { this.values[index] += toBeAdded; return this.values[index]; } this.addKeyValueAtIndex(key, toBeAdded, index); return toBeAdded; } private void addKeyValueAtIndex(byte key, int value, int index) { if (this.keys[index] == REMOVED_KEY) { this.occupiedWithSentinels--; } if (this.copyKeysOnWrite) { this.copyKeys(); } this.keys[index] = key; this.values[index] = value; this.occupiedWithData++; if (this.occupiedWithData + this.occupiedWithSentinels > this.maxOccupiedWithData()) { this.rehashAndGrow(); } } private void removeKeyAtIndex(int index) { if (this.copyKeysOnWrite) { this.copyKeys(); } this.keys[index] = REMOVED_KEY; this.values[index] = EMPTY_VALUE; this.occupiedWithData--; this.occupiedWithSentinels++; } private void copyKeys() { byte[] copy = new byte[this.keys.length]; System.arraycopy(this.keys, 0, copy, 0, this.keys.length); this.keys = copy; this.copyKeysOnWrite = false; } @Override public int updateValue(byte key, int initialValueIfAbsent, IntToIntFunction function) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(function.valueOf(initialValueIfAbsent)); } else if (this.sentinelValues.containsZeroKey) { this.sentinelValues.zeroValue = function.valueOf(this.sentinelValues.zeroValue); } else { this.addEmptyKeyValue(function.valueOf(initialValueIfAbsent)); } return this.sentinelValues.zeroValue; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(function.valueOf(initialValueIfAbsent)); } else if (this.sentinelValues.containsOneKey) { this.sentinelValues.oneValue = function.valueOf(this.sentinelValues.oneValue); } else { this.addRemovedKeyValue(function.valueOf(initialValueIfAbsent)); } return this.sentinelValues.oneValue; } int index = this.probe(key); if (this.keys[index] == key) { this.values[index] = function.valueOf(this.values[index]); return this.values[index]; } int value = function.valueOf(initialValueIfAbsent); this.addKeyValueAtIndex(key, value, index); return value; } @Override public ByteIntHashMap withKeyValue(byte key1, int value1) { this.put(key1, value1); return this; } public ByteIntHashMap withKeysValues(byte key1, int value1, byte key2, int value2) { this.put(key1, value1); this.put(key2, value2); return this; } public ByteIntHashMap withKeysValues(byte key1, int value1, byte key2, int value2, byte key3, int value3) { this.put(key1, value1); this.put(key2, value2); this.put(key3, value3); return this; } public ByteIntHashMap withKeysValues(byte key1, int value1, byte key2, int value2, byte key3, int value3, byte key4, int value4) { this.put(key1, value1); this.put(key2, value2); this.put(key3, value3); this.put(key4, value4); return this; } @Override public ByteIntHashMap withoutKey(byte key) { this.removeKey(key); return this; } @Override public ByteIntHashMap withoutAllKeys(ByteIterable keys) { keys.forEach(this::removeKey); return this; } @Override public MutableByteIntMap asUnmodifiable() { return new UnmodifiableByteIntMap(this); } @Override public MutableByteIntMap asSynchronized() { return new SynchronizedByteIntMap(this); } @Override public ImmutableByteIntMap toImmutable() { return ByteIntMaps.immutable.ofAll(this); } @Override public int get(byte key) { return this.getIfAbsent(key, EMPTY_VALUE); } @Override public int getIfAbsent(byte key, int ifAbsent) { if (isEmptyKey(key) || isRemovedKey(key)) { return this.getForSentinel(key, ifAbsent); } return this.slowGetIfAbsent(key, ifAbsent); } private int getForSentinel(byte key, int ifAbsent) { if (isEmptyKey(key)) { if (this.sentinelValues == null || !this.sentinelValues.containsZeroKey) { return ifAbsent; } return this.sentinelValues.zeroValue; } if (this.sentinelValues == null || !this.sentinelValues.containsOneKey) { return ifAbsent; } return this.sentinelValues.oneValue; } private int slowGetIfAbsent(byte key, int ifAbsent) { int index = this.probe(key); if (this.keys[index] == key) { return this.values[index]; } return ifAbsent; } @Override public int getOrThrow(byte key) { if (isEmptyKey(key)) { if (this.sentinelValues == null || !this.sentinelValues.containsZeroKey) { throw new IllegalStateException("Key " + key + " not present."); } return this.sentinelValues.zeroValue; } if (isRemovedKey(key)) { if (this.sentinelValues == null || !this.sentinelValues.containsOneKey) { throw new IllegalStateException("Key " + key + " not present."); } return this.sentinelValues.oneValue; } int index = this.probe(key); if (isNonSentinel(this.keys[index])) { return this.values[index]; } throw new IllegalStateException("Key " + key + " not present."); } @Override public boolean containsKey(byte key) { if (isEmptyKey(key)) { return this.sentinelValues != null && this.sentinelValues.containsZeroKey; } if (isRemovedKey(key)) { return this.sentinelValues != null && this.sentinelValues.containsOneKey; } return this.keys[this.probe(key)] == key; } @Override public void forEachKey(ByteProcedure procedure) { if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey) { procedure.value(EMPTY_KEY); } if (this.sentinelValues.containsOneKey) { procedure.value(REMOVED_KEY); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { procedure.value(this.keys[i]); } } } @Override public void forEachKeyValue(ByteIntProcedure procedure) { if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey) { procedure.value(EMPTY_KEY, this.sentinelValues.zeroValue); } if (this.sentinelValues.containsOneKey) { procedure.value(REMOVED_KEY, this.sentinelValues.oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { procedure.value(this.keys[i], this.values[i]); } } } @Override public LazyByteIterable keysView() { return new KeysView(); } @Override public RichIterable<ByteIntPair> keyValuesView() { return new KeyValuesView(); } @Override public MutableIntByteMap flipUniqueValues() { MutableIntByteMap result = IntByteMaps.mutable.empty(); this.forEachKeyValue((key, value) -> { if (result.containsKey(value)) { throw new IllegalStateException("Duplicate value: " + value + " found at key: " + result.get(value) + " and key: " + key); } result.put(value, key); }); return result; } @Override public ByteIntHashMap select(ByteIntPredicate predicate) { ByteIntHashMap result = new ByteIntHashMap(); if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey && predicate.accept(EMPTY_KEY, this.sentinelValues.zeroValue)) { result.put(EMPTY_KEY, this.sentinelValues.zeroValue); } if (this.sentinelValues.containsOneKey && predicate.accept(REMOVED_KEY, this.sentinelValues.oneValue)) { result.put(REMOVED_KEY, this.sentinelValues.oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i], this.values[i])) { result.put(this.keys[i], this.values[i]); } } return result; } @Override public ByteIntHashMap reject(ByteIntPredicate predicate) { ByteIntHashMap result = new ByteIntHashMap(); if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey && !predicate.accept(EMPTY_KEY, this.sentinelValues.zeroValue)) { result.put(EMPTY_KEY, this.sentinelValues.zeroValue); } if (this.sentinelValues.containsOneKey && !predicate.accept(REMOVED_KEY, this.sentinelValues.oneValue)) { result.put(REMOVED_KEY, this.sentinelValues.oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i]) && !predicate.accept(this.keys[i], this.values[i])) { result.put(this.keys[i], this.values[i]); } } return result; } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(this.size()); if (this.sentinelValues != null) { if (this.sentinelValues.containsZeroKey) { out.writeByte(EMPTY_KEY); out.writeInt(this.sentinelValues.zeroValue); } if (this.sentinelValues.containsOneKey) { out.writeByte(REMOVED_KEY); out.writeInt(this.sentinelValues.oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { out.writeByte(this.keys[i]); out.writeInt(this.values[i]); } } } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { int size = in.readInt(); for (int i = 0; i < size; i++) { this.put(in.readByte(), in.readInt()); } }
Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
/** * Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels. */
public void compact() { this.rehash(this.smallestPowerOfTwoGreaterThan(this.size())); } private void rehashAndGrow() { int max = this.maxOccupiedWithData(); int newCapacity = Math.max(max, smallestPowerOfTwoGreaterThan((this.occupiedWithData + 1) << 1)); if (this.occupiedWithSentinels > 0 && (max >> 1) + (max >> 2) < this.occupiedWithData) { newCapacity <<= 1; } this.rehash(newCapacity); } private void rehash(int newCapacity) { int oldLength = this.keys.length; byte[] old = this.keys; int[] oldValues = this.values; this.allocateTable(newCapacity); this.occupiedWithData = 0; this.occupiedWithSentinels = 0; for (int i = 0; i < oldLength; i++) { if (isNonSentinel(old[i])) { this.put(old[i], oldValues[i]); } } } // exposed for testing int probe(byte element) { int index = this.spreadAndMask(element); byte keyAtIndex = this.keys[index]; if (keyAtIndex == element || keyAtIndex == EMPTY_KEY) { return index; } int removedIndex = keyAtIndex == REMOVED_KEY ? index : -1; int nextIndex = index; int probe = 17; // loop until an empty slot is reached while (true) { // Probe algorithm: 17*n*(n+1)/2 where n = number of collisions nextIndex += probe; probe += 17; nextIndex &= this.keys.length - 1; if (this.keys[nextIndex] == element) { return nextIndex; } if (this.keys[nextIndex] == REMOVED_KEY) { if (removedIndex == -1) { removedIndex = nextIndex; } } else if (this.keys[nextIndex] == EMPTY_KEY) { return removedIndex == -1 ? nextIndex : removedIndex; } } } // exposed for testing int spreadAndMask(byte element) { // No spreading necessary for 8-bit types return this.mask(element); } private int mask(int spread) { return spread & (this.keys.length - 1); } private void allocateTable(int sizeToAllocate) { this.keys = new byte[sizeToAllocate]; this.values = new int[sizeToAllocate]; } private static boolean isEmptyKey(byte key) { return key == EMPTY_KEY; } private static boolean isRemovedKey(byte key) { return key == REMOVED_KEY; } private static boolean isNonSentinel(byte key) { return !isEmptyKey(key) && !isRemovedKey(key); } @Override protected boolean isNonSentinelAtIndex(int index) { return !isEmptyKey(this.keys[index]) && !isRemovedKey(this.keys[index]); } private int maxOccupiedWithData() { return this.keys.length >> 1; } private class InternalIntIterator implements MutableIntIterator { private int count; private int position; private byte lastKey; private boolean handledZero; private boolean handledOne; private boolean canRemove; @Override public boolean hasNext() { return this.count < ByteIntHashMap.this.size(); } @Override public int next() { if (!this.hasNext()) { throw new NoSuchElementException("next() called, but the iterator is exhausted"); } this.count++; this.canRemove = true; if (!this.handledZero) { this.handledZero = true; if (ByteIntHashMap.this.containsKey(EMPTY_KEY)) { this.lastKey = EMPTY_KEY; return ByteIntHashMap.this.get(EMPTY_KEY); } } if (!this.handledOne) { this.handledOne = true; if (ByteIntHashMap.this.containsKey(REMOVED_KEY)) { this.lastKey = REMOVED_KEY; return ByteIntHashMap.this.get(REMOVED_KEY); } } byte[] keys = ByteIntHashMap.this.keys; while (!isNonSentinel(keys[this.position])) { this.position++; } this.lastKey = keys[this.position]; int result = ByteIntHashMap.this.values[this.position]; this.position++; return result; } @Override public void remove() { if (!this.canRemove) { throw new IllegalStateException(); } ByteIntHashMap.this.removeKey(this.lastKey); this.count--; this.canRemove = false; } } private class KeysView extends AbstractLazyByteIterable { @Override public ByteIterator byteIterator() { return new UnmodifiableByteIterator(new KeySetIterator()); }
Since:7.0.
/** * @since 7.0. */
@Override public void each(ByteProcedure procedure) { ByteIntHashMap.this.forEachKey(procedure); } } private class KeySetIterator implements MutableByteIterator { private int count; private int position; private byte lastKey; private boolean handledZero; private boolean handledOne; private boolean canRemove; @Override public boolean hasNext() { return this.count < ByteIntHashMap.this.size(); } @Override public byte next() { if (!this.hasNext()) { throw new NoSuchElementException("next() called, but the iterator is exhausted"); } this.count++; this.canRemove = true; if (!this.handledZero) { this.handledZero = true; if (ByteIntHashMap.this.containsKey(EMPTY_KEY)) { this.lastKey = EMPTY_KEY; return this.lastKey; } } if (!this.handledOne) { this.handledOne = true; if (ByteIntHashMap.this.containsKey(REMOVED_KEY)) { this.lastKey = REMOVED_KEY; return this.lastKey; } } byte[] keys = ByteIntHashMap.this.keys; while (!isNonSentinel(keys[this.position])) { this.position++; } this.lastKey = keys[this.position]; this.position++; return this.lastKey; } @Override public void remove() { if (!this.canRemove) { throw new IllegalStateException(); } ByteIntHashMap.this.removeKey(this.lastKey); this.count--; this.canRemove = false; } } @Override public MutableByteSet keySet() { return new KeySet(); } private class KeySet extends AbstractMutableByteKeySet { @Override protected MutableByteKeysMap getOuter() { return ByteIntHashMap.this; } @Override protected SentinelValues getSentinelValues() { return ByteIntHashMap.this.sentinelValues; } @Override protected byte getKeyAtIndex(int index) { return ByteIntHashMap.this.keys[index]; } @Override protected int getTableSize() { return ByteIntHashMap.this.keys.length; } @Override public MutableByteIterator byteIterator() { return new KeySetIterator(); } @Override public boolean retainAll(ByteIterable source) { int oldSize = ByteIntHashMap.this.size(); ByteSet sourceSet = source instanceof ByteSet ? (ByteSet) source : source.toSet(); ByteIntHashMap retained = ByteIntHashMap.this.select((byte key, int value) -> sourceSet.contains(key)); if (retained.size() != oldSize) { ByteIntHashMap.this.keys = retained.keys; ByteIntHashMap.this.values = retained.values; ByteIntHashMap.this.sentinelValues = retained.sentinelValues; ByteIntHashMap.this.occupiedWithData = retained.occupiedWithData; ByteIntHashMap.this.occupiedWithSentinels = retained.occupiedWithSentinels; return true; } return false; } @Override public boolean retainAll(byte... source) { return this.retainAll(ByteHashSet.newSetWith(source)); } @Override public ByteSet freeze() { ByteIntHashMap.this.copyKeysOnWrite = true; boolean containsZeroKey = false; boolean containsOneKey = false; if (ByteIntHashMap.this.sentinelValues != null) { containsZeroKey = ByteIntHashMap.this.sentinelValues.containsZeroKey; containsOneKey = ByteIntHashMap.this.sentinelValues.containsOneKey; } return new ImmutableByteMapKeySet(ByteIntHashMap.this.keys, ByteIntHashMap.this.occupiedWithData, containsZeroKey, containsOneKey); }
Since:9.2.
/** * @since 9.2. */
@Override public MutableByteSet newEmpty() { return new ByteHashSet(); } } @Override public MutableIntCollection values() { return new ValuesCollection(); } private class ValuesCollection extends AbstractIntValuesCollection { @Override public MutableIntIterator intIterator() { return ByteIntHashMap.this.intIterator(); } @Override public boolean remove(int item) { int oldSize = ByteIntHashMap.this.size(); if (ByteIntHashMap.this.sentinelValues != null) { if (ByteIntHashMap.this.sentinelValues.containsZeroKey && item == ByteIntHashMap.this.sentinelValues.zeroValue) { ByteIntHashMap.this.removeKey(EMPTY_KEY); } if (ByteIntHashMap.this.sentinelValues.containsOneKey && item == ByteIntHashMap.this.sentinelValues.oneValue) { ByteIntHashMap.this.removeKey(REMOVED_KEY); } } for (int i = 0; i < ByteIntHashMap.this.keys.length; i++) { if (isNonSentinel(ByteIntHashMap.this.keys[i]) && item == ByteIntHashMap.this.values[i]) { ByteIntHashMap.this.removeKey(ByteIntHashMap.this.keys[i]); } } return oldSize != ByteIntHashMap.this.size(); } @Override public boolean retainAll(IntIterable source) { int oldSize = ByteIntHashMap.this.size(); IntSet sourceSet = source instanceof IntSet ? (IntSet) source : source.toSet(); ByteIntHashMap retained = ByteIntHashMap.this.select((byte key, int value) -> sourceSet.contains(value)); if (retained.size() != oldSize) { ByteIntHashMap.this.keys = retained.keys; ByteIntHashMap.this.values = retained.values; ByteIntHashMap.this.sentinelValues = retained.sentinelValues; ByteIntHashMap.this.occupiedWithData = retained.occupiedWithData; ByteIntHashMap.this.occupiedWithSentinels = retained.occupiedWithSentinels; return true; } return false; }
Since:9.2.
/** * @since 9.2. */
@Override public MutableIntCollection newEmpty() { return new IntHashBag(); } } private class KeyValuesView extends AbstractLazyIterable<ByteIntPair> { @Override public void each(Procedure<? super ByteIntPair> procedure) { if (ByteIntHashMap.this.sentinelValues != null) { if (ByteIntHashMap.this.sentinelValues.containsZeroKey) { procedure.value(PrimitiveTuples.pair(EMPTY_KEY, ByteIntHashMap.this.sentinelValues.zeroValue)); } if (ByteIntHashMap.this.sentinelValues.containsOneKey) { procedure.value(PrimitiveTuples.pair(REMOVED_KEY, ByteIntHashMap.this.sentinelValues.oneValue)); } } for (int i = 0; i < ByteIntHashMap.this.keys.length; i++) { if (isNonSentinel(ByteIntHashMap.this.keys[i])) { procedure.value(PrimitiveTuples.pair(ByteIntHashMap.this.keys[i], ByteIntHashMap.this.values[i])); } } } @Override public void forEachWithIndex(ObjectIntProcedure<? super ByteIntPair> objectIntProcedure) { int index = 0; if (ByteIntHashMap.this.sentinelValues != null) { if (ByteIntHashMap.this.sentinelValues.containsZeroKey) { objectIntProcedure.value(PrimitiveTuples.pair(EMPTY_KEY, ByteIntHashMap.this.sentinelValues.zeroValue), index); index++; } if (ByteIntHashMap.this.sentinelValues.containsOneKey) { objectIntProcedure.value(PrimitiveTuples.pair(REMOVED_KEY, ByteIntHashMap.this.sentinelValues.oneValue), index); index++; } } for (int i = 0; i < ByteIntHashMap.this.keys.length; i++) { if (isNonSentinel(ByteIntHashMap.this.keys[i])) { objectIntProcedure.value(PrimitiveTuples.pair(ByteIntHashMap.this.keys[i], ByteIntHashMap.this.values[i]), index); index++; } } } @Override public <P> void forEachWith(Procedure2<? super ByteIntPair, ? super P> procedure, P parameter) { if (ByteIntHashMap.this.sentinelValues != null) { if (ByteIntHashMap.this.sentinelValues.containsZeroKey) { procedure.value(PrimitiveTuples.pair(EMPTY_KEY, ByteIntHashMap.this.sentinelValues.zeroValue), parameter); } if (ByteIntHashMap.this.sentinelValues.containsOneKey) { procedure.value(PrimitiveTuples.pair(REMOVED_KEY, ByteIntHashMap.this.sentinelValues.oneValue), parameter); } } for (int i = 0; i < ByteIntHashMap.this.keys.length; i++) { if (isNonSentinel(ByteIntHashMap.this.keys[i])) { procedure.value(PrimitiveTuples.pair(ByteIntHashMap.this.keys[i], ByteIntHashMap.this.values[i]), parameter); } } } @Override public Iterator<ByteIntPair> iterator() { return new InternalKeyValuesIterator(); } public class InternalKeyValuesIterator implements Iterator<ByteIntPair> { private int count; private int position; private boolean handledZero; private boolean handledOne; @Override public ByteIntPair next() { if (!this.hasNext()) { throw new NoSuchElementException("next() called, but the iterator is exhausted"); } this.count++; if (!this.handledZero) { this.handledZero = true; if (ByteIntHashMap.this.containsKey(EMPTY_KEY)) { return PrimitiveTuples.pair(EMPTY_KEY, ByteIntHashMap.this.sentinelValues.zeroValue); } } if (!this.handledOne) { this.handledOne = true; if (ByteIntHashMap.this.containsKey(REMOVED_KEY)) { return PrimitiveTuples.pair(REMOVED_KEY, ByteIntHashMap.this.sentinelValues.oneValue); } } byte[] keys = ByteIntHashMap.this.keys; while (!isNonSentinel(keys[this.position])) { this.position++; } ByteIntPair result = PrimitiveTuples.pair(keys[this.position], ByteIntHashMap.this.values[this.position]); this.position++; return result; } @Override public void remove() { throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName()); } @Override public boolean hasNext() { return this.count != ByteIntHashMap.this.size(); } } } }