package androidx.collection; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.lang.reflect.Array; import java.util.Collection; import java.util.Iterator; import java.util.Map; import java.util.Set; public final class ArraySet implements Collection, Set { private static final int BASE_SIZE = 4; private static final int CACHE_SIZE = 10; private static final boolean DEBUG = false; private static final int[] INT = new int[0]; private static final Object[] OBJECT = new Object[0]; private static final String TAG = "ArraySet"; @Nullable private static Object[] sBaseCache; private static int sBaseCacheSize; @Nullable private static Object[] sTwiceBaseCache; private static int sTwiceBaseCacheSize; public Object[] mArray; private MapCollections mCollections; private int[] mHashes; public int mSize; /* renamed from: androidx.collection.ArraySet$1 reason: invalid class name */ public class AnonymousClass1 extends MapCollections { public AnonymousClass1() { } @Override // androidx.collection.MapCollections public void colClear() { ArraySet.this.clear(); } @Override // androidx.collection.MapCollections public Object colGetEntry(int i, int i2) { return ArraySet.this.mArray[i]; } @Override // androidx.collection.MapCollections public Map colGetMap() { throw new UnsupportedOperationException("not a map"); } @Override // androidx.collection.MapCollections public int colGetSize() { return ArraySet.this.mSize; } @Override // androidx.collection.MapCollections public int colIndexOfKey(Object obj) { return ArraySet.this.indexOf(obj); } @Override // androidx.collection.MapCollections public int colIndexOfValue(Object obj) { return ArraySet.this.indexOf(obj); } @Override // androidx.collection.MapCollections public void colPut(E e, E e2) { ArraySet.this.add(e); } @Override // androidx.collection.MapCollections public void colRemoveAt(int i) { ArraySet.this.removeAt(i); } @Override // androidx.collection.MapCollections public E colSetValue(int i, E e) { throw new UnsupportedOperationException("not a map"); } } public ArraySet() { this(0); } public ArraySet(int i) { if (i == 0) { this.mHashes = INT; this.mArray = OBJECT; } else { allocArrays(i); } this.mSize = 0; } public ArraySet(@Nullable ArraySet arraySet) { this(); if (arraySet != null) { addAll((ArraySet) arraySet); } } public ArraySet(@Nullable Collection collection) { this(); if (collection != null) { addAll(collection); } } private void allocArrays(int i) { if (i == 8) { synchronized (ArraySet.class) { Object[] objArr = sTwiceBaseCache; if (objArr != null) { this.mArray = objArr; sTwiceBaseCache = (Object[]) objArr[0]; this.mHashes = (int[]) objArr[1]; objArr[1] = null; objArr[0] = null; sTwiceBaseCacheSize--; return; } } } else if (i == 4) { synchronized (ArraySet.class) { Object[] objArr2 = sBaseCache; if (objArr2 != null) { this.mArray = objArr2; sBaseCache = (Object[]) objArr2[0]; this.mHashes = (int[]) objArr2[1]; objArr2[1] = null; objArr2[0] = null; sBaseCacheSize--; return; } } } this.mHashes = new int[i]; this.mArray = new Object[i]; } private static void freeArrays(int[] iArr, Object[] objArr, int i) { if (iArr.length == 8) { synchronized (ArraySet.class) { if (sTwiceBaseCacheSize < 10) { objArr[0] = sTwiceBaseCache; objArr[1] = iArr; for (int i2 = i - 1; i2 >= 2; i2--) { objArr[i2] = null; } sTwiceBaseCache = objArr; sTwiceBaseCacheSize++; } } } else if (iArr.length == 4) { synchronized (ArraySet.class) { if (sBaseCacheSize < 10) { objArr[0] = sBaseCache; objArr[1] = iArr; for (int i3 = i - 1; i3 >= 2; i3--) { objArr[i3] = null; } sBaseCache = objArr; sBaseCacheSize++; } } } } private MapCollections getCollection() { if (this.mCollections == null) { this.mCollections = new AnonymousClass1(); } return this.mCollections; } private int indexOf(Object obj, int i) { int i2 = this.mSize; if (i2 == 0) { return -1; } int binarySearch = ContainerHelpers.binarySearch(this.mHashes, i2, i); if (binarySearch < 0 || obj.equals(this.mArray[binarySearch])) { return binarySearch; } int i3 = binarySearch + 1; while (i3 < i2 && this.mHashes[i3] == i) { if (obj.equals(this.mArray[i3])) { return i3; } i3++; } int i4 = binarySearch - 1; while (i4 >= 0 && this.mHashes[i4] == i) { if (obj.equals(this.mArray[i4])) { return i4; } i4--; } return ~i3; } private int indexOfNull() { int i = this.mSize; if (i == 0) { return -1; } int binarySearch = ContainerHelpers.binarySearch(this.mHashes, i, 0); if (binarySearch < 0 || this.mArray[binarySearch] == null) { return binarySearch; } int i2 = binarySearch + 1; while (i2 < i && this.mHashes[i2] == 0) { if (this.mArray[i2] == null) { return i2; } i2++; } int i3 = binarySearch - 1; while (i3 >= 0 && this.mHashes[i3] == 0) { if (this.mArray[i3] == null) { return i3; } i3--; } return ~i2; } @Override // java.util.Collection, java.util.Set public boolean add(@Nullable E e) { int i; int i2; if (e == null) { i2 = indexOfNull(); i = 0; } else { int hashCode = e.hashCode(); i = hashCode; i2 = indexOf(e, hashCode); } if (i2 >= 0) { return false; } int i3 = ~i2; int i4 = this.mSize; int[] iArr = this.mHashes; if (i4 >= iArr.length) { int i5 = 4; if (i4 >= 8) { i5 = (i4 >> 1) + i4; } else if (i4 >= 4) { i5 = 8; } Object[] objArr = this.mArray; allocArrays(i5); int[] iArr2 = this.mHashes; if (iArr2.length > 0) { System.arraycopy(iArr, 0, iArr2, 0, iArr.length); System.arraycopy(objArr, 0, this.mArray, 0, objArr.length); } freeArrays(iArr, objArr, this.mSize); } int i6 = this.mSize; if (i3 < i6) { int[] iArr3 = this.mHashes; int i7 = i3 + 1; System.arraycopy(iArr3, i3, iArr3, i7, i6 - i3); Object[] objArr2 = this.mArray; System.arraycopy(objArr2, i3, objArr2, i7, this.mSize - i3); } this.mHashes[i3] = i; this.mArray[i3] = e; this.mSize++; return true; } /* JADX DEBUG: Multi-variable search result rejected for r4v0, resolved type: androidx.collection.ArraySet */ /* JADX WARN: Multi-variable type inference failed */ public void addAll(@NonNull ArraySet arraySet) { int i = arraySet.mSize; ensureCapacity(this.mSize + i); if (this.mSize != 0) { for (int i2 = 0; i2 < i; i2++) { add(arraySet.valueAt(i2)); } } else if (i > 0) { System.arraycopy(arraySet.mHashes, 0, this.mHashes, 0, i); System.arraycopy(arraySet.mArray, 0, this.mArray, 0, i); this.mSize = i; } } /* JADX DEBUG: Multi-variable search result rejected for r2v0, resolved type: androidx.collection.ArraySet */ /* JADX WARN: Multi-variable type inference failed */ @Override // java.util.Collection, java.util.Set public boolean addAll(@NonNull Collection collection) { ensureCapacity(collection.size() + this.mSize); Iterator it = collection.iterator(); boolean z2 = false; while (it.hasNext()) { z2 |= add(it.next()); } return z2; } @Override // java.util.Collection, java.util.Set public void clear() { int i = this.mSize; if (i != 0) { freeArrays(this.mHashes, this.mArray, i); this.mHashes = INT; this.mArray = OBJECT; this.mSize = 0; } } @Override // java.util.Collection, java.util.Set public boolean contains(@Nullable Object obj) { return indexOf(obj) >= 0; } @Override // java.util.Collection, java.util.Set public boolean containsAll(@NonNull Collection collection) { Iterator it = collection.iterator(); while (it.hasNext()) { if (!contains(it.next())) { return false; } } return true; } public void ensureCapacity(int i) { int[] iArr = this.mHashes; if (iArr.length < i) { Object[] objArr = this.mArray; allocArrays(i); int i2 = this.mSize; if (i2 > 0) { System.arraycopy(iArr, 0, this.mHashes, 0, i2); System.arraycopy(objArr, 0, this.mArray, 0, this.mSize); } freeArrays(iArr, objArr, this.mSize); } } @Override // java.util.Collection, java.lang.Object, 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 i = 0; i < this.mSize; i++) { try { if (!set.contains(valueAt(i))) { return false; } } catch (ClassCastException | NullPointerException unused) { } } return true; } return false; } @Override // java.util.Collection, java.lang.Object, java.util.Set public int hashCode() { int[] iArr = this.mHashes; int i = this.mSize; int i2 = 0; for (int i3 = 0; i3 < i; i3++) { i2 += iArr[i3]; } return i2; } public int indexOf(@Nullable Object obj) { return obj == null ? indexOfNull() : indexOf(obj, obj.hashCode()); } @Override // java.util.Collection, java.util.Set public boolean isEmpty() { return this.mSize <= 0; } @Override // java.util.Collection, java.lang.Iterable, java.util.Set public Iterator iterator() { return getCollection().getKeySet().iterator(); } @Override // java.util.Collection, java.util.Set public boolean remove(@Nullable Object obj) { int indexOf = indexOf(obj); if (indexOf < 0) { return false; } removeAt(indexOf); return true; } public boolean removeAll(@NonNull ArraySet arraySet) { int i = arraySet.mSize; int i2 = this.mSize; for (int i3 = 0; i3 < i; i3++) { remove(arraySet.valueAt(i3)); } return i2 != this.mSize; } @Override // java.util.Collection, java.util.Set public boolean removeAll(@NonNull Collection collection) { Iterator it = collection.iterator(); boolean z2 = false; while (it.hasNext()) { z2 |= remove(it.next()); } return z2; } public E removeAt(int i) { Object[] objArr = this.mArray; E e = (E) objArr[i]; int i2 = this.mSize; if (i2 <= 1) { freeArrays(this.mHashes, objArr, i2); this.mHashes = INT; this.mArray = OBJECT; this.mSize = 0; } else { int[] iArr = this.mHashes; int i3 = 8; if (iArr.length <= 8 || i2 >= iArr.length / 3) { int i4 = i2 - 1; this.mSize = i4; if (i < i4) { int i5 = i + 1; System.arraycopy(iArr, i5, iArr, i, i4 - i); Object[] objArr2 = this.mArray; System.arraycopy(objArr2, i5, objArr2, i, this.mSize - i); } this.mArray[this.mSize] = null; } else { if (i2 > 8) { i3 = i2 + (i2 >> 1); } allocArrays(i3); this.mSize--; if (i > 0) { System.arraycopy(iArr, 0, this.mHashes, 0, i); System.arraycopy(objArr, 0, this.mArray, 0, i); } int i6 = this.mSize; if (i < i6) { int i7 = i + 1; System.arraycopy(iArr, i7, this.mHashes, i, i6 - i); System.arraycopy(objArr, i7, this.mArray, i, this.mSize - i); } } } return e; } @Override // java.util.Collection, java.util.Set public boolean retainAll(@NonNull Collection collection) { boolean z2 = false; for (int i = this.mSize - 1; i >= 0; i--) { if (!collection.contains(this.mArray[i])) { removeAt(i); z2 = true; } } return z2; } @Override // java.util.Collection, java.util.Set public int size() { return this.mSize; } @Override // java.util.Collection, java.util.Set @NonNull public Object[] toArray() { int i = this.mSize; Object[] objArr = new Object[i]; System.arraycopy(this.mArray, 0, objArr, 0, i); return objArr; } @Override // java.util.Collection, java.util.Set @NonNull public T[] toArray(@NonNull T[] tArr) { if (tArr.length < this.mSize) { tArr = (T[]) ((Object[]) Array.newInstance(tArr.getClass().getComponentType(), this.mSize)); } System.arraycopy(this.mArray, 0, tArr, 0, this.mSize); int length = tArr.length; int i = this.mSize; if (length > i) { tArr[i] = null; } return tArr; } @Override // java.lang.Object public String toString() { if (isEmpty()) { return "{}"; } StringBuilder sb = new StringBuilder(this.mSize * 14); sb.append('{'); for (int i = 0; i < this.mSize; i++) { if (i > 0) { sb.append(", "); } E valueAt = valueAt(i); if (valueAt != this) { sb.append((Object) valueAt); } else { sb.append("(this Set)"); } } sb.append('}'); return sb.toString(); } @Nullable public E valueAt(int i) { return (E) this.mArray[i]; } }