what-the-bank/sources/com/google/gson/internal/LinkedHashTreeMap.java

799 lines
26 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.gson.internal;
import com.huawei.hms.framework.common.ContainerUtils;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
/* loaded from: classes2.dex */
public final class LinkedHashTreeMap<K, V> extends AbstractMap<K, V> implements Serializable {
static final boolean $assertionsDisabled = false;
private static final Comparator<Comparable> NATURAL_ORDER = new Comparator<Comparable>() { // from class: com.google.gson.internal.LinkedHashTreeMap.1
@Override // java.util.Comparator
public int compare(Comparable comparable, Comparable comparable2) {
return comparable.compareTo(comparable2);
}
};
Comparator<? super K> comparator;
private LinkedHashTreeMap<K, V>.EntrySet entrySet;
final Node<K, V> header;
private LinkedHashTreeMap<K, V>.KeySet keySet;
int modCount;
int size;
Node<K, V>[] table;
int threshold;
private static int secondaryHash(int i) {
int i2 = i ^ ((i >>> 20) ^ (i >>> 12));
return (i2 ^ (i2 >>> 7)) ^ (i2 >>> 4);
}
public LinkedHashTreeMap() {
this(NATURAL_ORDER);
}
public LinkedHashTreeMap(Comparator<? super K> comparator) {
this.size = 0;
this.modCount = 0;
this.comparator = comparator == null ? NATURAL_ORDER : comparator;
this.header = new Node<>();
this.table = new Node[16];
int i = 16 / 2;
int i2 = 16 / 4;
this.threshold = 12;
}
@Override // java.util.AbstractMap, java.util.Map
public final V get(Object obj) {
Node<K, V> findByObject = findByObject(obj);
if (findByObject != null) {
return findByObject.value;
}
return null;
}
@Override // java.util.AbstractMap, java.util.Map
public final boolean containsKey(Object obj) {
return findByObject(obj) != null;
}
@Override // java.util.AbstractMap, java.util.Map
public final V put(K k, V v) {
if (k == null) {
throw new NullPointerException("key == null");
}
Node<K, V> find = find(k, true);
V v2 = find.value;
find.value = v;
return v2;
}
@Override // java.util.AbstractMap, java.util.Map
public final void clear() {
Arrays.fill(this.table, (Object) null);
this.size = 0;
this.modCount++;
Node<K, V> node = this.header;
Node<K, V> node2 = node.next;
while (node2 != node) {
Node<K, V> node3 = node2.next;
node2.prev = null;
node2.next = null;
node2 = node3;
}
node.prev = node;
node.next = node;
}
@Override // java.util.AbstractMap, java.util.Map
public final V remove(Object obj) {
Node<K, V> removeInternalByKey = removeInternalByKey(obj);
if (removeInternalByKey != null) {
return removeInternalByKey.value;
}
return null;
}
final Node<K, V> find(K k, boolean z) {
int i;
Node<K, V> node;
Comparator<? super K> comparator = this.comparator;
Node<K, V>[] nodeArr = this.table;
int secondaryHash = secondaryHash(k.hashCode());
int length = (nodeArr.length - 1) & secondaryHash;
Node<K, V> node2 = nodeArr[length];
if (node2 != null) {
Comparable comparable = comparator == NATURAL_ORDER ? (Comparable) k : null;
while (true) {
if (comparable != null) {
i = comparable.compareTo(node2.key);
} else {
i = comparator.compare(k, node2.key);
}
if (i != 0) {
Node<K, V> node3 = i < 0 ? node2.left : node2.right;
if (node3 == null) {
break;
}
node2 = node3;
} else {
return node2;
}
}
} else {
i = 0;
}
Node<K, V> node4 = node2;
int i2 = i;
if (!z) {
return null;
}
Node<K, V> node5 = this.header;
if (node4 == null) {
if (comparator == NATURAL_ORDER && !(k instanceof Comparable)) {
StringBuilder sb = new StringBuilder();
sb.append(k.getClass().getName());
sb.append(" is not Comparable");
throw new ClassCastException(sb.toString());
}
node = new Node<>(node4, k, secondaryHash, node5, node5.prev);
nodeArr[length] = node;
} else {
node = new Node<>(node4, k, secondaryHash, node5, node5.prev);
if (i2 < 0) {
node4.left = node;
} else {
node4.right = node;
}
rebalance(node4, true);
}
int i3 = this.size;
this.size = i3 + 1;
if (i3 > this.threshold) {
doubleCapacity();
}
this.modCount++;
return node;
}
/* JADX WARN: Multi-variable type inference failed */
final Node<K, V> findByObject(Object obj) {
if (obj == 0) {
return null;
}
try {
return find(obj, false);
} catch (ClassCastException unused) {
return null;
}
}
final Node<K, V> findByEntry(Map.Entry<?, ?> entry) {
Node<K, V> findByObject = findByObject(entry.getKey());
if (findByObject == null || !equal(findByObject.value, entry.getValue())) {
return null;
}
return findByObject;
}
private boolean equal(Object obj, Object obj2) {
return obj == obj2 || (obj != null && obj.equals(obj2));
}
final void removeInternal(Node<K, V> node, boolean z) {
int i;
if (z) {
node.prev.next = node.next;
node.next.prev = node.prev;
node.prev = null;
node.next = null;
}
Node<K, V> node2 = node.left;
Node<K, V> node3 = node.right;
Node<K, V> node4 = node.parent;
int i2 = 0;
if (node2 != null && node3 != null) {
Node<K, V> last = node2.height > node3.height ? node2.last() : node3.first();
removeInternal(last, false);
Node<K, V> node5 = node.left;
if (node5 != null) {
i = node5.height;
last.left = node5;
node5.parent = last;
node.left = null;
} else {
i = 0;
}
Node<K, V> node6 = node.right;
if (node6 != null) {
i2 = node6.height;
last.right = node6;
node6.parent = last;
node.right = null;
}
last.height = Math.max(i, i2) + 1;
replaceInParent(node, last);
return;
}
if (node2 != null) {
replaceInParent(node, node2);
node.left = null;
} else if (node3 != null) {
replaceInParent(node, node3);
node.right = null;
} else {
replaceInParent(node, null);
}
rebalance(node4, false);
this.size--;
this.modCount++;
}
final Node<K, V> removeInternalByKey(Object obj) {
Node<K, V> findByObject = findByObject(obj);
if (findByObject != null) {
removeInternal(findByObject, true);
}
return findByObject;
}
private void replaceInParent(Node<K, V> node, Node<K, V> node2) {
Node<K, V> node3 = node.parent;
node.parent = null;
if (node2 != null) {
node2.parent = node3;
}
if (node3 != null) {
if (node3.left == node) {
node3.left = node2;
return;
} else {
node3.right = node2;
return;
}
}
int i = node.hash;
this.table[i & (r0.length - 1)] = node2;
}
private void rebalance(Node<K, V> node, boolean z) {
while (node != null) {
Node<K, V> node2 = node.left;
Node<K, V> node3 = node.right;
int i = node2 != null ? node2.height : 0;
int i2 = node3 != null ? node3.height : 0;
int i3 = i - i2;
if (i3 == -2) {
Node<K, V> node4 = node3.left;
Node<K, V> node5 = node3.right;
int i4 = (node4 != null ? node4.height : 0) - (node5 != null ? node5.height : 0);
if (i4 != -1 && (i4 != 0 || z)) {
rotateRight(node3);
}
rotateLeft(node);
if (z) {
return;
}
} else if (i3 == 2) {
Node<K, V> node6 = node2.left;
Node<K, V> node7 = node2.right;
int i5 = (node6 != null ? node6.height : 0) - (node7 != null ? node7.height : 0);
if (i5 != 1 && (i5 != 0 || z)) {
rotateLeft(node2);
}
rotateRight(node);
if (z) {
return;
}
} else if (i3 == 0) {
node.height = i + 1;
if (z) {
return;
}
} else {
node.height = Math.max(i, i2) + 1;
if (!z) {
return;
}
}
node = node.parent;
}
}
private void rotateLeft(Node<K, V> node) {
Node<K, V> node2 = node.left;
Node<K, V> node3 = node.right;
Node<K, V> node4 = node3.left;
Node<K, V> node5 = node3.right;
node.right = node4;
if (node4 != null) {
node4.parent = node;
}
replaceInParent(node, node3);
node3.left = node;
node.parent = node3;
node.height = Math.max(node2 != null ? node2.height : 0, node4 != null ? node4.height : 0) + 1;
node3.height = Math.max(node.height, node5 != null ? node5.height : 0) + 1;
}
private void rotateRight(Node<K, V> node) {
Node<K, V> node2 = node.left;
Node<K, V> node3 = node.right;
Node<K, V> node4 = node2.left;
Node<K, V> node5 = node2.right;
node.left = node5;
if (node5 != null) {
node5.parent = node;
}
replaceInParent(node, node2);
node2.right = node;
node.parent = node2;
node.height = Math.max(node3 != null ? node3.height : 0, node5 != null ? node5.height : 0) + 1;
node2.height = Math.max(node.height, node4 != null ? node4.height : 0) + 1;
}
@Override // java.util.AbstractMap, java.util.Map
public final Set<Map.Entry<K, V>> entrySet() {
LinkedHashTreeMap<K, V>.EntrySet entrySet = this.entrySet;
if (entrySet != null) {
return entrySet;
}
LinkedHashTreeMap<K, V>.EntrySet entrySet2 = new EntrySet(this);
this.entrySet = entrySet2;
return entrySet2;
}
@Override // java.util.AbstractMap, java.util.Map
public final Set<K> keySet() {
LinkedHashTreeMap<K, V>.KeySet keySet = this.keySet;
if (keySet != null) {
return keySet;
}
LinkedHashTreeMap<K, V>.KeySet keySet2 = new KeySet(this);
this.keySet = keySet2;
return keySet2;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class Node<K, V> implements Map.Entry<K, V> {
final int hash;
int height;
final K key;
Node<K, V> left;
Node<K, V> next;
Node<K, V> parent;
Node<K, V> prev;
Node<K, V> right;
V value;
Node() {
this.key = null;
this.hash = -1;
this.prev = this;
this.next = this;
}
Node(Node<K, V> node, K k, int i, Node<K, V> node2, Node<K, V> node3) {
this.parent = node;
this.key = k;
this.hash = i;
this.height = 1;
this.next = node2;
this.prev = node3;
node3.next = this;
node2.prev = this;
}
@Override // java.util.Map.Entry
public final boolean equals(Object obj) {
if (!(obj instanceof Map.Entry)) {
return false;
}
Map.Entry entry = (Map.Entry) obj;
K k = this.key;
if (k == null) {
if (entry.getKey() != null) {
return false;
}
} else if (!k.equals(entry.getKey())) {
return false;
}
V v = this.value;
if (v == null) {
if (entry.getValue() != null) {
return false;
}
} else if (!v.equals(entry.getValue())) {
return false;
}
return true;
}
@Override // java.util.Map.Entry
public final int hashCode() {
K k = this.key;
int hashCode = k == null ? 0 : k.hashCode();
V v = this.value;
return hashCode ^ (v != null ? v.hashCode() : 0);
}
public final String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.key);
sb.append(ContainerUtils.KEY_VALUE_DELIMITER);
sb.append(this.value);
return sb.toString();
}
public final Node<K, V> first() {
Node<K, V> node = this;
for (Node<K, V> node2 = this.left; node2 != null; node2 = node2.left) {
node = node2;
}
return node;
}
public final Node<K, V> last() {
Node<K, V> node = this;
for (Node<K, V> node2 = this.right; node2 != null; node2 = node2.right) {
node = node2;
}
return node;
}
@Override // java.util.Map.Entry
public final V setValue(V v) {
V v2 = this.value;
this.value = v;
return v2;
}
@Override // java.util.Map.Entry
public final V getValue() {
return this.value;
}
@Override // java.util.Map.Entry
public final K getKey() {
return this.key;
}
}
private void doubleCapacity() {
Node<K, V>[] doubleCapacity = doubleCapacity(this.table);
this.table = doubleCapacity;
this.threshold = (doubleCapacity.length / 2) + (doubleCapacity.length / 4);
}
static <K, V> Node<K, V>[] doubleCapacity(Node<K, V>[] nodeArr) {
int length = nodeArr.length;
Node<K, V>[] nodeArr2 = new Node[length << 1];
AvlIterator avlIterator = new AvlIterator();
AvlBuilder avlBuilder = new AvlBuilder();
AvlBuilder avlBuilder2 = new AvlBuilder();
for (int i = 0; i < length; i++) {
Node<K, V> node = nodeArr[i];
if (node != null) {
avlIterator.reset(node);
int i2 = 0;
int i3 = 0;
while (true) {
Node<K, V> next = avlIterator.next();
if (next == null) {
break;
}
if ((next.hash & length) == 0) {
i2++;
} else {
i3++;
}
}
avlBuilder.reset(i2);
avlBuilder2.reset(i3);
avlIterator.reset(node);
while (true) {
Node<K, V> next2 = avlIterator.next();
if (next2 == null) {
break;
}
if ((next2.hash & length) == 0) {
avlBuilder.add(next2);
} else {
avlBuilder2.add(next2);
}
}
nodeArr2[i] = i2 > 0 ? avlBuilder.root() : null;
nodeArr2[i + length] = i3 > 0 ? avlBuilder2.root() : null;
}
}
return nodeArr2;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static class AvlIterator<K, V> {
private Node<K, V> stackTop;
AvlIterator() {
}
void reset(Node<K, V> node) {
Node<K, V> node2 = null;
while (node != null) {
node.parent = node2;
node2 = node;
node = node.left;
}
this.stackTop = node2;
}
public Node<K, V> next() {
Node<K, V> node = this.stackTop;
if (node == null) {
return null;
}
Node<K, V> node2 = node.parent;
node.parent = null;
for (Node<K, V> node3 = node.right; node3 != null; node3 = node3.left) {
node3.parent = node2;
node2 = node3;
}
this.stackTop = node2;
return node;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class AvlBuilder<K, V> {
private int leavesSkipped;
private int leavesToSkip;
private int size;
private Node<K, V> stack;
AvlBuilder() {
}
final void reset(int i) {
this.leavesToSkip = ((Integer.highestOneBit(i) << 1) - 1) - i;
this.size = 0;
this.leavesSkipped = 0;
this.stack = null;
}
final void add(Node<K, V> node) {
node.right = null;
node.parent = null;
node.left = null;
node.height = 1;
int i = this.leavesToSkip;
if (i > 0) {
int i2 = this.size;
if ((i2 & 1) == 0) {
this.size = i2 + 1;
this.leavesToSkip = i - 1;
this.leavesSkipped++;
}
}
node.parent = this.stack;
this.stack = node;
int i3 = this.size;
int i4 = i3 + 1;
this.size = i4;
int i5 = this.leavesToSkip;
if (i5 > 0 && (i4 & 1) == 0) {
this.size = i3 + 2;
this.leavesToSkip = i5 - 1;
this.leavesSkipped++;
}
int i6 = 4;
while (true) {
int i7 = i6 - 1;
if ((this.size & i7) != i7) {
return;
}
int i8 = this.leavesSkipped;
if (i8 == 0) {
Node<K, V> node2 = this.stack;
Node<K, V> node3 = node2.parent;
Node<K, V> node4 = node3.parent;
node3.parent = node4.parent;
this.stack = node3;
node3.left = node4;
node3.right = node2;
node3.height = node2.height + 1;
node4.parent = node3;
node2.parent = node3;
} else {
if (i8 == 1) {
Node<K, V> node5 = this.stack;
Node<K, V> node6 = node5.parent;
this.stack = node6;
node6.right = node5;
node6.height = node5.height + 1;
node5.parent = node6;
} else if (i8 != 2) {
}
this.leavesSkipped = 0;
}
i6 <<= 1;
}
}
final Node<K, V> root() {
Node<K, V> node = this.stack;
if (node.parent == null) {
return node;
}
throw new IllegalStateException();
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public abstract class LinkedTreeMapIterator<T> implements Iterator<T> {
int expectedModCount;
Node<K, V> lastReturned = null;
Node<K, V> next;
final LinkedHashTreeMap this$0;
LinkedTreeMapIterator(LinkedHashTreeMap linkedHashTreeMap) {
this.this$0 = linkedHashTreeMap;
this.next = linkedHashTreeMap.header.next;
this.expectedModCount = linkedHashTreeMap.modCount;
}
@Override // java.util.Iterator
public final boolean hasNext() {
return this.next != this.this$0.header;
}
final Node<K, V> nextNode() {
Node<K, V> node = this.next;
if (node == this.this$0.header) {
throw new NoSuchElementException();
}
if (this.this$0.modCount != this.expectedModCount) {
throw new ConcurrentModificationException();
}
this.next = node.next;
this.lastReturned = node;
return node;
}
@Override // java.util.Iterator
public final void remove() {
Node<K, V> node = this.lastReturned;
if (node == null) {
throw new IllegalStateException();
}
this.this$0.removeInternal(node, true);
this.lastReturned = null;
this.expectedModCount = this.this$0.modCount;
}
}
/* loaded from: classes2.dex */
final class EntrySet extends AbstractSet<Map.Entry<K, V>> {
final LinkedHashTreeMap this$0;
EntrySet(LinkedHashTreeMap linkedHashTreeMap) {
this.this$0 = linkedHashTreeMap;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final int size() {
return this.this$0.size;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public final Iterator<Map.Entry<K, V>> iterator() {
return new LinkedTreeMapIterator(this) { // from class: com.google.gson.internal.LinkedHashTreeMap.EntrySet.1
final EntrySet this$1;
{
this.this$1 = this;
LinkedHashTreeMap linkedHashTreeMap = this.this$0;
}
@Override // java.util.Iterator
public Map.Entry<K, V> next() {
return nextNode();
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final boolean contains(Object obj) {
return (obj instanceof Map.Entry) && this.this$0.findByEntry((Map.Entry) obj) != null;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final boolean remove(Object obj) {
Node<K, V> findByEntry;
if (!(obj instanceof Map.Entry) || (findByEntry = this.this$0.findByEntry((Map.Entry) obj)) == null) {
return false;
}
this.this$0.removeInternal(findByEntry, true);
return true;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final void clear() {
this.this$0.clear();
}
}
/* loaded from: classes2.dex */
final class KeySet extends AbstractSet<K> {
final LinkedHashTreeMap this$0;
KeySet(LinkedHashTreeMap linkedHashTreeMap) {
this.this$0 = linkedHashTreeMap;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final int size() {
return this.this$0.size;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.Set
public final Iterator<K> iterator() {
return new LinkedTreeMapIterator(this) { // from class: com.google.gson.internal.LinkedHashTreeMap.KeySet.1
final KeySet this$1;
{
this.this$1 = this;
LinkedHashTreeMap linkedHashTreeMap = this.this$0;
}
@Override // java.util.Iterator
public K next() {
return nextNode().key;
}
};
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final boolean contains(Object obj) {
return this.this$0.containsKey(obj);
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final boolean remove(Object obj) {
return this.this$0.removeInternalByKey(obj) != null;
}
@Override // java.util.AbstractCollection, java.util.Collection, java.util.Set
public final void clear() {
this.this$0.clear();
}
}
private Object writeReplace() throws ObjectStreamException {
return new LinkedHashMap(this);
}
private void readObject(ObjectInputStream objectInputStream) throws IOException {
throw new InvalidObjectException("Deserialization is unsupported");
}
@Override // java.util.AbstractMap, java.util.Map
public final int size() {
return this.size;
}
}