what-the-bank/sources/com/google/common/collect/CompactHashMap.java

808 lines
27 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.collect;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class CompactHashMap<K, V> extends AbstractMap<K, V> implements Serializable {
static final double HASH_FLOODING_FPP = 0.001d;
private static final int MAX_HASH_BUCKET_LENGTH = 9;
private static final Object NOT_FOUND = new Object();
transient int[] entries;
private transient Set<Map.Entry<K, V>> entrySetView;
private transient Set<K> keySetView;
transient Object[] keys;
private transient int metadata;
private transient int size;
private transient Object table;
transient Object[] values;
private transient Collection<V> valuesView;
void accessEntry(int i) {
}
int adjustAfterRemove(int i, int i2) {
return i - 1;
}
static /* synthetic */ int access$710(CompactHashMap compactHashMap) {
int i = compactHashMap.size;
compactHashMap.size = i - 1;
return i;
}
public static <K, V> CompactHashMap<K, V> create() {
return new CompactHashMap<>();
}
public static <K, V> CompactHashMap<K, V> createWithExpectedSize(int i) {
return new CompactHashMap<>(i);
}
CompactHashMap() {
init(3);
}
/* JADX INFO: Access modifiers changed from: package-private */
public CompactHashMap(int i) {
init(i);
}
/* JADX INFO: Access modifiers changed from: package-private */
public void init(int i) {
Preconditions.checkArgument(i >= 0, "Expected size must be >= 0");
this.metadata = Ints.constrainToRange(i, 1, 1073741823);
}
/* JADX INFO: Access modifiers changed from: package-private */
public int allocArrays() {
Preconditions.checkState(needsAllocArrays(), "Arrays already allocated");
int i = this.metadata;
int tableSize = CompactHashing.tableSize(i);
this.table = CompactHashing.createTable(tableSize);
setHashTableMask(tableSize - 1);
this.entries = new int[i];
this.keys = new Object[i];
this.values = new Object[i];
return i;
}
Map<K, V> delegateOrNull() {
Object obj = this.table;
if (obj instanceof Map) {
return (Map) obj;
}
return null;
}
Map<K, V> createHashFloodingResistantDelegate(int i) {
return new LinkedHashMap(i, 1.0f);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* JADX WARN: Multi-variable type inference failed */
public Map<K, V> convertToHashFloodingResistantImplementation() {
Map<K, V> createHashFloodingResistantDelegate = createHashFloodingResistantDelegate(hashTableMask() + 1);
int firstEntryIndex = firstEntryIndex();
while (firstEntryIndex >= 0) {
createHashFloodingResistantDelegate.put(this.keys[firstEntryIndex], this.values[firstEntryIndex]);
firstEntryIndex = getSuccessor(firstEntryIndex);
}
this.table = createHashFloodingResistantDelegate;
this.entries = null;
this.keys = null;
this.values = null;
incrementModCount();
return createHashFloodingResistantDelegate;
}
private void setHashTableMask(int i) {
this.metadata = CompactHashing.maskCombine(this.metadata, 32 - Integer.numberOfLeadingZeros(i), 31);
}
@Override // java.util.AbstractMap, java.util.Map
public V put(K k, V v) {
int resizeTable;
int i;
if (needsAllocArrays()) {
allocArrays();
}
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.put(k, v);
}
int[] iArr = this.entries;
Object[] objArr = this.keys;
Object[] objArr2 = this.values;
int i2 = this.size;
int i3 = i2 + 1;
int smearedHash = Hashing.smearedHash(k);
int hashTableMask = hashTableMask();
int i4 = smearedHash & hashTableMask;
int tableGet = CompactHashing.tableGet(this.table, i4);
if (tableGet != 0) {
int hashPrefix = CompactHashing.getHashPrefix(smearedHash, hashTableMask);
int i5 = 0;
while (true) {
int i6 = tableGet - 1;
int i7 = iArr[i6];
if (CompactHashing.getHashPrefix(i7, hashTableMask) == hashPrefix && Objects.equal(k, objArr[i6])) {
V v2 = (V) objArr2[i6];
objArr2[i6] = v;
accessEntry(i6);
return v2;
}
int next = CompactHashing.getNext(i7, hashTableMask);
i5++;
if (next != 0) {
tableGet = next;
} else {
if (i5 >= 9) {
return convertToHashFloodingResistantImplementation().put(k, v);
}
if (i3 > hashTableMask) {
resizeTable = resizeTable(hashTableMask, CompactHashing.newCapacity(hashTableMask), smearedHash, i2);
} else {
iArr[i6] = CompactHashing.maskCombine(i7, i3, hashTableMask);
}
}
}
} else if (i3 > hashTableMask) {
resizeTable = resizeTable(hashTableMask, CompactHashing.newCapacity(hashTableMask), smearedHash, i2);
i = resizeTable;
} else {
CompactHashing.tableSet(this.table, i4, i3);
i = hashTableMask;
}
resizeMeMaybe(i3);
insertEntry(i2, k, v, smearedHash, i);
this.size = i3;
incrementModCount();
return null;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void insertEntry(int i, K k, V v, int i2, int i3) {
this.entries[i] = CompactHashing.maskCombine(i2, 0, i3);
this.keys[i] = k;
this.values[i] = v;
}
private void resizeMeMaybe(int i) {
int min;
int length = this.entries.length;
if (i <= length || (min = Math.min(1073741823, (Math.max(1, length >>> 1) + length) | 1)) == length) {
return;
}
resizeEntries(min);
}
/* JADX INFO: Access modifiers changed from: package-private */
public void resizeEntries(int i) {
this.entries = Arrays.copyOf(this.entries, i);
this.keys = Arrays.copyOf(this.keys, i);
this.values = Arrays.copyOf(this.values, i);
}
private int resizeTable(int i, int i2, int i3, int i4) {
Object createTable = CompactHashing.createTable(i2);
int i5 = i2 - 1;
if (i4 != 0) {
CompactHashing.tableSet(createTable, i3 & i5, i4 + 1);
}
Object obj = this.table;
int[] iArr = this.entries;
for (int i6 = 0; i6 <= i; i6++) {
int tableGet = CompactHashing.tableGet(obj, i6);
while (tableGet != 0) {
int i7 = tableGet - 1;
int i8 = iArr[i7];
int hashPrefix = CompactHashing.getHashPrefix(i8, i) | i6;
int i9 = hashPrefix & i5;
int tableGet2 = CompactHashing.tableGet(createTable, i9);
CompactHashing.tableSet(createTable, i9, tableGet);
iArr[i7] = CompactHashing.maskCombine(hashPrefix, tableGet2, i5);
tableGet = CompactHashing.getNext(i8, i);
}
}
this.table = createTable;
setHashTableMask(i5);
return i5;
}
/* JADX INFO: Access modifiers changed from: private */
public int indexOf(Object obj) {
if (needsAllocArrays()) {
return -1;
}
int smearedHash = Hashing.smearedHash(obj);
int hashTableMask = hashTableMask();
int tableGet = CompactHashing.tableGet(this.table, smearedHash & hashTableMask);
if (tableGet == 0) {
return -1;
}
int hashPrefix = CompactHashing.getHashPrefix(smearedHash, hashTableMask);
do {
int i = tableGet - 1;
int i2 = this.entries[i];
if (CompactHashing.getHashPrefix(i2, hashTableMask) == hashPrefix && Objects.equal(obj, this.keys[i])) {
return i;
}
tableGet = CompactHashing.getNext(i2, hashTableMask);
} while (tableGet != 0);
return -1;
}
@Override // java.util.AbstractMap, java.util.Map
public boolean containsKey(Object obj) {
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.containsKey(obj);
}
return indexOf(obj) != -1;
}
@Override // java.util.AbstractMap, java.util.Map
public V get(Object obj) {
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.get(obj);
}
int indexOf = indexOf(obj);
if (indexOf == -1) {
return null;
}
accessEntry(indexOf);
return (V) this.values[indexOf];
}
@Override // java.util.AbstractMap, java.util.Map
public V remove(Object obj) {
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.remove(obj);
}
V v = (V) removeHelper(obj);
if (v == NOT_FOUND) {
return null;
}
return v;
}
/* JADX INFO: Access modifiers changed from: private */
public Object removeHelper(Object obj) {
if (needsAllocArrays()) {
return NOT_FOUND;
}
int hashTableMask = hashTableMask();
int remove = CompactHashing.remove(obj, null, hashTableMask, this.table, this.entries, this.keys, null);
if (remove == -1) {
return NOT_FOUND;
}
Object obj2 = this.values[remove];
moveLastEntry(remove, hashTableMask);
this.size--;
incrementModCount();
return obj2;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void moveLastEntry(int i, int i2) {
int size = size();
int i3 = size - 1;
if (i < i3) {
Object[] objArr = this.keys;
Object obj = objArr[i3];
objArr[i] = obj;
Object[] objArr2 = this.values;
objArr2[i] = objArr2[i3];
objArr[i3] = null;
objArr2[i3] = null;
int[] iArr = this.entries;
iArr[i] = iArr[i3];
iArr[i3] = 0;
int smearedHash = Hashing.smearedHash(obj) & i2;
int tableGet = CompactHashing.tableGet(this.table, smearedHash);
if (tableGet == size) {
CompactHashing.tableSet(this.table, smearedHash, i + 1);
return;
}
while (true) {
int i4 = tableGet - 1;
int i5 = this.entries[i4];
int next = CompactHashing.getNext(i5, i2);
if (next == size) {
this.entries[i4] = CompactHashing.maskCombine(i5, i + 1, i2);
return;
}
tableGet = next;
}
} else {
this.keys[i] = null;
this.values[i] = null;
this.entries[i] = 0;
}
}
int firstEntryIndex() {
return isEmpty() ? -1 : 0;
}
/* loaded from: classes2.dex */
abstract class Itr<T> implements Iterator<T> {
int currentIndex;
int expectedMetadata;
int indexToRemove;
final CompactHashMap this$0;
abstract T getOutput(int i);
private Itr(CompactHashMap compactHashMap) {
this.this$0 = compactHashMap;
this.expectedMetadata = compactHashMap.metadata;
this.currentIndex = compactHashMap.firstEntryIndex();
this.indexToRemove = -1;
}
@Override // java.util.Iterator
public T next() {
checkForConcurrentModification();
if (!hasNext()) {
throw new NoSuchElementException();
}
int i = this.currentIndex;
this.indexToRemove = i;
T output = getOutput(i);
this.currentIndex = this.this$0.getSuccessor(this.currentIndex);
return output;
}
@Override // java.util.Iterator
public void remove() {
checkForConcurrentModification();
CollectPreconditions.checkRemove(this.indexToRemove >= 0);
incrementExpectedModCount();
CompactHashMap compactHashMap = this.this$0;
compactHashMap.remove(compactHashMap.keys[this.indexToRemove]);
this.currentIndex = this.this$0.adjustAfterRemove(this.currentIndex, this.indexToRemove);
this.indexToRemove = -1;
}
private void checkForConcurrentModification() {
if (this.this$0.metadata != this.expectedMetadata) {
throw new ConcurrentModificationException();
}
}
void incrementExpectedModCount() {
this.expectedMetadata += 32;
}
@Override // java.util.Iterator
public boolean hasNext() {
return this.currentIndex >= 0;
}
}
@Override // java.util.AbstractMap, java.util.Map
public Set<K> keySet() {
Set<K> set = this.keySetView;
if (set != null) {
return set;
}
Set<K> createKeySet = createKeySet();
this.keySetView = createKeySet;
return createKeySet;
}
Set<K> createKeySet() {
return new KeySetView(this);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class KeySetView extends AbstractSet<K> {
final CompactHashMap this$0;
KeySetView(CompactHashMap compactHashMap) {
this.this$0 = compactHashMap;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public int size() {
return this.this$0.size();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return this.this$0.containsKey(obj);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean remove(Object obj) {
Map<K, V> delegateOrNull = this.this$0.delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.keySet().remove(obj);
}
return this.this$0.removeHelper(obj) != CompactHashMap.NOT_FOUND;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public Iterator<K> iterator() {
return this.this$0.keySetIterator();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public void clear() {
this.this$0.clear();
}
}
Iterator<K> keySetIterator() {
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.keySet().iterator();
}
return new Itr(this) { // from class: com.google.common.collect.CompactHashMap.1
final CompactHashMap this$0;
{
this.this$0 = this;
}
@Override // com.google.common.collect.CompactHashMap.Itr
K getOutput(int i) {
return (K) this.this$0.keys[i];
}
};
}
@Override // java.util.AbstractMap, java.util.Map
public Set<Map.Entry<K, V>> entrySet() {
Set<Map.Entry<K, V>> set = this.entrySetView;
if (set != null) {
return set;
}
Set<Map.Entry<K, V>> createEntrySet = createEntrySet();
this.entrySetView = createEntrySet;
return createEntrySet;
}
Set<Map.Entry<K, V>> createEntrySet() {
return new EntrySetView(this);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class EntrySetView extends AbstractSet<Map.Entry<K, V>> {
final CompactHashMap this$0;
EntrySetView(CompactHashMap compactHashMap) {
this.this$0 = compactHashMap;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public int size() {
return this.this$0.size();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public void clear() {
this.this$0.clear();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public Iterator<Map.Entry<K, V>> iterator() {
return this.this$0.entrySetIterator();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean contains(Object obj) {
Map<K, V> delegateOrNull = this.this$0.delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.entrySet().contains(obj);
}
if (!(obj instanceof Map.Entry)) {
return false;
}
Map.Entry entry = (Map.Entry) obj;
int indexOf = this.this$0.indexOf(entry.getKey());
return indexOf != -1 && Objects.equal(this.this$0.values[indexOf], entry.getValue());
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public boolean remove(Object obj) {
Map<K, V> delegateOrNull = this.this$0.delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.entrySet().remove(obj);
}
if (!(obj instanceof Map.Entry)) {
return false;
}
Map.Entry entry = (Map.Entry) obj;
if (this.this$0.needsAllocArrays()) {
return false;
}
int hashTableMask = this.this$0.hashTableMask();
int remove = CompactHashing.remove(entry.getKey(), entry.getValue(), hashTableMask, this.this$0.table, this.this$0.entries, this.this$0.keys, this.this$0.values);
if (remove == -1) {
return false;
}
this.this$0.moveLastEntry(remove, hashTableMask);
CompactHashMap.access$710(this.this$0);
this.this$0.incrementModCount();
return true;
}
}
Iterator<Map.Entry<K, V>> entrySetIterator() {
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.entrySet().iterator();
}
return new Itr(this) { // from class: com.google.common.collect.CompactHashMap.2
final CompactHashMap this$0;
{
this.this$0 = this;
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // com.google.common.collect.CompactHashMap.Itr
public Map.Entry<K, V> getOutput(int i) {
return new MapEntry(this.this$0, i);
}
};
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class MapEntry extends AbstractMapEntry<K, V> {
private final K key;
private int lastKnownIndex;
final CompactHashMap this$0;
MapEntry(CompactHashMap compactHashMap, int i) {
this.this$0 = compactHashMap;
this.key = (K) compactHashMap.keys[i];
this.lastKnownIndex = i;
}
private void updateLastKnownIndex() {
int i = this.lastKnownIndex;
if (i == -1 || i >= this.this$0.size() || !Objects.equal(this.key, this.this$0.keys[this.lastKnownIndex])) {
this.lastKnownIndex = this.this$0.indexOf(this.key);
}
}
@Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry
public final V getValue() {
Map<K, V> delegateOrNull = this.this$0.delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.get(this.key);
}
updateLastKnownIndex();
if (this.lastKnownIndex == -1) {
return null;
}
return (V) this.this$0.values[this.lastKnownIndex];
}
@Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry
public final V setValue(V v) {
Map<K, V> delegateOrNull = this.this$0.delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.put(this.key, v);
}
updateLastKnownIndex();
if (this.lastKnownIndex == -1) {
this.this$0.put(this.key, v);
return null;
}
V v2 = (V) this.this$0.values[this.lastKnownIndex];
this.this$0.values[this.lastKnownIndex] = v;
return v2;
}
@Override // com.google.common.collect.AbstractMapEntry, java.util.Map.Entry
public final K getKey() {
return this.key;
}
}
@Override // java.util.AbstractMap, java.util.Map
public int size() {
Map<K, V> delegateOrNull = delegateOrNull();
return delegateOrNull != null ? delegateOrNull.size() : this.size;
}
@Override // java.util.AbstractMap, java.util.Map
public boolean isEmpty() {
return size() == 0;
}
@Override // java.util.AbstractMap, java.util.Map
public boolean containsValue(Object obj) {
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.containsValue(obj);
}
for (int i = 0; i < this.size; i++) {
if (Objects.equal(obj, this.values[i])) {
return true;
}
}
return false;
}
@Override // java.util.AbstractMap, java.util.Map
public Collection<V> values() {
Collection<V> collection = this.valuesView;
if (collection != null) {
return collection;
}
Collection<V> createValues = createValues();
this.valuesView = createValues;
return createValues;
}
Collection<V> createValues() {
return new ValuesView(this);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class ValuesView extends AbstractCollection<V> {
final CompactHashMap this$0;
ValuesView(CompactHashMap compactHashMap) {
this.this$0 = compactHashMap;
}
@Override // java.util.AbstractCollection, java.util.Collection
public int size() {
return this.this$0.size();
}
@Override // java.util.AbstractCollection, java.util.Collection
public void clear() {
this.this$0.clear();
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable
public Iterator<V> iterator() {
return this.this$0.valuesIterator();
}
}
Iterator<V> valuesIterator() {
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
return delegateOrNull.values().iterator();
}
return new Itr(this) { // from class: com.google.common.collect.CompactHashMap.3
final CompactHashMap this$0;
{
this.this$0 = this;
}
@Override // com.google.common.collect.CompactHashMap.Itr
V getOutput(int i) {
return (V) this.this$0.values[i];
}
};
}
public void trimToSize() {
if (needsAllocArrays()) {
return;
}
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
Map<K, V> createHashFloodingResistantDelegate = createHashFloodingResistantDelegate(size());
createHashFloodingResistantDelegate.putAll(delegateOrNull);
this.table = createHashFloodingResistantDelegate;
return;
}
int i = this.size;
if (i < this.entries.length) {
resizeEntries(i);
}
int tableSize = CompactHashing.tableSize(i);
int hashTableMask = hashTableMask();
if (tableSize < hashTableMask) {
resizeTable(hashTableMask, tableSize, 0, 0);
}
}
@Override // java.util.AbstractMap, java.util.Map
public void clear() {
if (needsAllocArrays()) {
return;
}
incrementModCount();
Map<K, V> delegateOrNull = delegateOrNull();
if (delegateOrNull != null) {
this.metadata = Ints.constrainToRange(size(), 3, 1073741823);
delegateOrNull.clear();
this.table = null;
this.size = 0;
return;
}
Arrays.fill(this.keys, 0, this.size, (Object) null);
Arrays.fill(this.values, 0, this.size, (Object) null);
CompactHashing.tableClear(this.table);
Arrays.fill(this.entries, 0, this.size, 0);
this.size = 0;
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
objectOutputStream.writeInt(size());
Iterator<Map.Entry<K, V>> entrySetIterator = entrySetIterator();
while (entrySetIterator.hasNext()) {
Map.Entry<K, V> next = entrySetIterator.next();
objectOutputStream.writeObject(next.getKey());
objectOutputStream.writeObject(next.getValue());
}
}
/* JADX WARN: Multi-variable type inference failed */
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
int readInt = objectInputStream.readInt();
if (readInt < 0) {
StringBuilder sb = new StringBuilder(25);
sb.append("Invalid size: ");
sb.append(readInt);
throw new InvalidObjectException(sb.toString());
}
init(readInt);
for (int i = 0; i < readInt; i++) {
put(objectInputStream.readObject(), objectInputStream.readObject());
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean needsAllocArrays() {
return this.table == null;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void incrementModCount() {
this.metadata += 32;
}
int getSuccessor(int i) {
int i2 = i + 1;
if (i2 >= this.size) {
return -1;
}
return i2;
}
/* JADX INFO: Access modifiers changed from: private */
public int hashTableMask() {
return (1 << (this.metadata & 31)) - 1;
}
}