jimu-decompiled/sources/androidx/collection/ArraySet.java
2025-05-13 19:24:51 +02:00

473 lines
13 KiB
Java

package androidx.collection;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/* loaded from: classes.dex */
public final class ArraySet<E> implements Collection<E>, Set<E> {
private static final int[] e = new int[0];
private static final Object[] f = new Object[0];
private static Object[] g;
private static int h;
private static Object[] i;
private static int j;
private int[] a;
Object[] b;
int c;
private MapCollections<E, E> d;
public ArraySet() {
this(0);
}
private int a(Object obj, int i2) {
int i3 = this.c;
if (i3 == 0) {
return -1;
}
int a = ContainerHelpers.a(this.a, i3, i2);
if (a < 0 || obj.equals(this.b[a])) {
return a;
}
int i4 = a + 1;
while (i4 < i3 && this.a[i4] == i2) {
if (obj.equals(this.b[i4])) {
return i4;
}
i4++;
}
for (int i5 = a - 1; i5 >= 0 && this.a[i5] == i2; i5--) {
if (obj.equals(this.b[i5])) {
return i5;
}
}
return ~i4;
}
private int b() {
int i2 = this.c;
if (i2 == 0) {
return -1;
}
int a = ContainerHelpers.a(this.a, i2, 0);
if (a < 0 || this.b[a] == null) {
return a;
}
int i3 = a + 1;
while (i3 < i2 && this.a[i3] == 0) {
if (this.b[i3] == null) {
return i3;
}
i3++;
}
for (int i4 = a - 1; i4 >= 0 && this.a[i4] == 0; i4--) {
if (this.b[i4] == null) {
return i4;
}
}
return ~i3;
}
private void f(int i2) {
if (i2 == 8) {
synchronized (ArraySet.class) {
if (i != null) {
Object[] objArr = i;
this.b = objArr;
i = (Object[]) objArr[0];
this.a = (int[]) objArr[1];
objArr[1] = null;
objArr[0] = null;
j--;
return;
}
}
} else if (i2 == 4) {
synchronized (ArraySet.class) {
if (g != null) {
Object[] objArr2 = g;
this.b = objArr2;
g = (Object[]) objArr2[0];
this.a = (int[]) objArr2[1];
objArr2[1] = null;
objArr2[0] = null;
h--;
return;
}
}
}
this.a = new int[i2];
this.b = new Object[i2];
}
@Override // java.util.Collection, java.util.Set
public boolean add(E e2) {
int i2;
int a;
if (e2 == null) {
a = b();
i2 = 0;
} else {
int hashCode = e2.hashCode();
i2 = hashCode;
a = a(e2, hashCode);
}
if (a >= 0) {
return false;
}
int i3 = ~a;
int i4 = this.c;
if (i4 >= this.a.length) {
int i5 = 4;
if (i4 >= 8) {
i5 = (i4 >> 1) + i4;
} else if (i4 >= 4) {
i5 = 8;
}
int[] iArr = this.a;
Object[] objArr = this.b;
f(i5);
int[] iArr2 = this.a;
if (iArr2.length > 0) {
System.arraycopy(iArr, 0, iArr2, 0, iArr.length);
System.arraycopy(objArr, 0, this.b, 0, objArr.length);
}
a(iArr, objArr, this.c);
}
int i6 = this.c;
if (i3 < i6) {
int[] iArr3 = this.a;
int i7 = i3 + 1;
System.arraycopy(iArr3, i3, iArr3, i7, i6 - i3);
Object[] objArr2 = this.b;
System.arraycopy(objArr2, i3, objArr2, i7, this.c - i3);
}
this.a[i3] = i2;
this.b[i3] = e2;
this.c++;
return true;
}
@Override // java.util.Collection, java.util.Set
public boolean addAll(Collection<? extends E> collection) {
c(this.c + collection.size());
Iterator<? extends E> it = collection.iterator();
boolean z = false;
while (it.hasNext()) {
z |= add(it.next());
}
return z;
}
public void c(int i2) {
int[] iArr = this.a;
if (iArr.length < i2) {
Object[] objArr = this.b;
f(i2);
int i3 = this.c;
if (i3 > 0) {
System.arraycopy(iArr, 0, this.a, 0, i3);
System.arraycopy(objArr, 0, this.b, 0, this.c);
}
a(iArr, objArr, this.c);
}
}
@Override // java.util.Collection, java.util.Set
public void clear() {
int i2 = this.c;
if (i2 != 0) {
a(this.a, this.b, i2);
this.a = e;
this.b = f;
this.c = 0;
}
}
@Override // java.util.Collection, java.util.Set
public boolean contains(Object obj) {
return indexOf(obj) >= 0;
}
@Override // java.util.Collection, java.util.Set
public boolean containsAll(Collection<?> collection) {
Iterator<?> it = collection.iterator();
while (it.hasNext()) {
if (!contains(it.next())) {
return false;
}
}
return true;
}
public E d(int i2) {
Object[] objArr = this.b;
E e2 = (E) objArr[i2];
int i3 = this.c;
if (i3 <= 1) {
a(this.a, objArr, i3);
this.a = e;
this.b = f;
this.c = 0;
} else {
int[] iArr = this.a;
if (iArr.length <= 8 || i3 >= iArr.length / 3) {
this.c--;
int i4 = this.c;
if (i2 < i4) {
int[] iArr2 = this.a;
int i5 = i2 + 1;
System.arraycopy(iArr2, i5, iArr2, i2, i4 - i2);
Object[] objArr2 = this.b;
System.arraycopy(objArr2, i5, objArr2, i2, this.c - i2);
}
this.b[this.c] = null;
} else {
int i6 = i3 > 8 ? i3 + (i3 >> 1) : 8;
int[] iArr3 = this.a;
Object[] objArr3 = this.b;
f(i6);
this.c--;
if (i2 > 0) {
System.arraycopy(iArr3, 0, this.a, 0, i2);
System.arraycopy(objArr3, 0, this.b, 0, i2);
}
int i7 = this.c;
if (i2 < i7) {
int i8 = i2 + 1;
System.arraycopy(iArr3, i8, this.a, i2, i7 - i2);
System.arraycopy(objArr3, i8, this.b, i2, this.c - i2);
}
}
}
return e2;
}
public E e(int i2) {
return (E) this.b[i2];
}
@Override // java.util.Collection, java.util.Set
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Set) {
Set set = (Set) obj;
if (size() != set.size()) {
return false;
}
for (int i2 = 0; i2 < this.c; i2++) {
try {
if (!set.contains(e(i2))) {
return false;
}
} catch (ClassCastException | NullPointerException unused) {
}
}
return true;
}
return false;
}
@Override // java.util.Collection, java.util.Set
public int hashCode() {
int[] iArr = this.a;
int i2 = this.c;
int i3 = 0;
for (int i4 = 0; i4 < i2; i4++) {
i3 += iArr[i4];
}
return i3;
}
public int indexOf(Object obj) {
return obj == null ? b() : a(obj, obj.hashCode());
}
@Override // java.util.Collection, java.util.Set
public boolean isEmpty() {
return this.c <= 0;
}
@Override // java.util.Collection, java.lang.Iterable, java.util.Set
public Iterator<E> iterator() {
return a().e().iterator();
}
@Override // java.util.Collection, java.util.Set
public boolean remove(Object obj) {
int indexOf = indexOf(obj);
if (indexOf < 0) {
return false;
}
d(indexOf);
return true;
}
@Override // java.util.Collection, java.util.Set
public boolean removeAll(Collection<?> collection) {
Iterator<?> it = collection.iterator();
boolean z = false;
while (it.hasNext()) {
z |= remove(it.next());
}
return z;
}
@Override // java.util.Collection, java.util.Set
public boolean retainAll(Collection<?> collection) {
boolean z = false;
for (int i2 = this.c - 1; i2 >= 0; i2--) {
if (!collection.contains(this.b[i2])) {
d(i2);
z = true;
}
}
return z;
}
@Override // java.util.Collection, java.util.Set
public int size() {
return this.c;
}
@Override // java.util.Collection, java.util.Set
public Object[] toArray() {
int i2 = this.c;
Object[] objArr = new Object[i2];
System.arraycopy(this.b, 0, objArr, 0, i2);
return objArr;
}
public String toString() {
if (isEmpty()) {
return "{}";
}
StringBuilder sb = new StringBuilder(this.c * 14);
sb.append('{');
for (int i2 = 0; i2 < this.c; i2++) {
if (i2 > 0) {
sb.append(", ");
}
E e2 = e(i2);
if (e2 != this) {
sb.append(e2);
} else {
sb.append("(this Set)");
}
}
sb.append('}');
return sb.toString();
}
public ArraySet(int i2) {
if (i2 == 0) {
this.a = e;
this.b = f;
} else {
f(i2);
}
this.c = 0;
}
@Override // java.util.Collection, java.util.Set
public <T> T[] toArray(T[] tArr) {
if (tArr.length < this.c) {
tArr = (T[]) ((Object[]) Array.newInstance(tArr.getClass().getComponentType(), this.c));
}
System.arraycopy(this.b, 0, tArr, 0, this.c);
int length = tArr.length;
int i2 = this.c;
if (length > i2) {
tArr[i2] = null;
}
return tArr;
}
private static void a(int[] iArr, Object[] objArr, int i2) {
if (iArr.length == 8) {
synchronized (ArraySet.class) {
if (j < 10) {
objArr[0] = i;
objArr[1] = iArr;
for (int i3 = i2 - 1; i3 >= 2; i3--) {
objArr[i3] = null;
}
i = objArr;
j++;
}
}
return;
}
if (iArr.length == 4) {
synchronized (ArraySet.class) {
if (h < 10) {
objArr[0] = g;
objArr[1] = iArr;
for (int i4 = i2 - 1; i4 >= 2; i4--) {
objArr[i4] = null;
}
g = objArr;
h++;
}
}
}
}
private MapCollections<E, E> a() {
if (this.d == null) {
this.d = new MapCollections<E, E>() { // from class: androidx.collection.ArraySet.1
@Override // androidx.collection.MapCollections
protected Object a(int i2, int i3) {
return ArraySet.this.b[i2];
}
@Override // androidx.collection.MapCollections
protected int b(Object obj) {
return ArraySet.this.indexOf(obj);
}
@Override // androidx.collection.MapCollections
protected int c() {
return ArraySet.this.c;
}
@Override // androidx.collection.MapCollections
protected int a(Object obj) {
return ArraySet.this.indexOf(obj);
}
@Override // androidx.collection.MapCollections
protected Map<E, E> b() {
throw new UnsupportedOperationException("not a map");
}
@Override // androidx.collection.MapCollections
protected void a(E e2, E e3) {
ArraySet.this.add(e2);
}
@Override // androidx.collection.MapCollections
protected E a(int i2, E e2) {
throw new UnsupportedOperationException("not a map");
}
@Override // androidx.collection.MapCollections
protected void a(int i2) {
ArraySet.this.d(i2);
}
@Override // androidx.collection.MapCollections
protected void a() {
ArraySet.this.clear();
}
};
}
return this.d;
}
}