content: package androidx.collection;
  
  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<E> implements Collection<E>, Set<E> {
      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";
      private static Object[] sBaseCache;
      private static int sBaseCacheSize;
      private static Object[] sTwiceBaseCache;
      private static int sTwiceBaseCacheSize;
      Object[] mArray;
      private MapCollections<E, E> mCollections;
      private int[] mHashes;
      int mSize;
  
      public ArraySet() {
          this(0);
      }
  
      public ArraySet(int capacity) {
          if (capacity == 0) {
              this.mHashes = INT;
              this.mArray = OBJECT;
          } else {
              allocArrays(capacity);
          }
          this.mSize = 0;
      }
  
      public ArraySet(ArraySet<E> arraySet) {
          this();
          if (arraySet != null) {
              addAll(arraySet);
          }
      }
  
      public ArraySet(Collection<E> collection) {
          this();
          if (collection != null) {
              addAll(collection);
          }
      }
  
      private void allocArrays(int size) {
          Class<ArraySet> cls = ArraySet.class;
          if (size == 8) {
              synchronized (cls) {
                  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 (size == 4) {
              synchronized (cls) {
                  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[size];
          this.mArray = new Object[size];
      }
  
      private static void freeArrays(int[] hashes, Object[] array, int size) {
          Class<ArraySet> cls = ArraySet.class;
          if (hashes.length == 8) {
              synchronized (cls) {
                  if (sTwiceBaseCacheSize < 10) {
                      array[0] = sTwiceBaseCache;
                      array[1] = hashes;
                      for (int i = size - 1; i >= 2; i--) {
                          array[i] = null;
                      }
                      sTwiceBaseCache = array;
                      sTwiceBaseCacheSize++;
                  }
              }
          } else if (hashes.length == 4) {
              synchronized (cls) {
                  if (sBaseCacheSize < 10) {
                      array[0] = sBaseCache;
                      array[1] = hashes;
                      for (int i2 = size - 1; i2 >= 2; i2--) {
                          array[i2] = null;
                      }
                      sBaseCache = array;
                      sBaseCacheSize++;
                  }
              }
          }
      }
  
      private MapCollections<E, E> getCollection() {
          if (this.mCollections == null) {
              this.mCollections = new MapCollections<E, E>() {
                  /* access modifiers changed from: protected */
                  public void colClear() {
                      ArraySet.this.clear();
                  }
  
                  /* access modifiers changed from: protected */
                  public Object colGetEntry(int index, int offset) {
                      return ArraySet.this.mArray[index];
                  }
  
                  /* access modifiers changed from: protected */
                  public Map<E, E> colGetMap() {
                      throw new UnsupportedOperationException("not a map");
                  }
  
                  /* access modifiers changed from: protected */
                  public int colGetSize() {
                      return ArraySet.this.mSize;
                  }
  
                  /* access modifiers changed from: protected */
                  public int colIndexOfKey(Object key) {
                      return ArraySet.this.indexOf(key);
                  }
  
                  /* access modifiers changed from: protected */
                  public int colIndexOfValue(Object value) {
                      return ArraySet.this.indexOf(value);
                  }
  
                  /* access modifiers changed from: protected */
                  public void colPut(E e, E e2) {
                      ArraySet.this.add(e);
                  }
  
                  /* access modifiers changed from: protected */
                  public void colRemoveAt(int index) {
                      ArraySet.this.removeAt(index);
                  }
  
                  /* access modifiers changed from: protected */
                  public E colSetValue(int index, E e) {
                      throw new UnsupportedOperationException("not a map");
                  }
              };
          }
          return this.mCollections;
      }
  
      private int indexOf(Object key, int hash) {
          int i = this.mSize;
          if (i == 0) {
              return -1;
          }
          int binarySearch = ContainerHelpers.binarySearch(this.mHashes, i, hash);
          if (binarySearch < 0 || key.equals(this.mArray[binarySearch])) {
              return binarySearch;
          }
          int i2 = binarySearch + 1;
          while (i2 < i && this.mHashes[i2] == hash) {
              if (key.equals(this.mArray[i2])) {
                  return i2;
              }
              i2++;
          }
          int i3 = binarySearch - 1;
          while (i3 >= 0 && this.mHashes[i3] == hash) {
              if (key.equals(this.mArray[i3])) {
                  return i3;
              }
              i3--;
          }
          return ~i2;
      }
  
      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;
      }
  
      public boolean add(E e) {
          int i;
          int i2;
          if (e == null) {
              i2 = 0;
              i = indexOfNull();
          } else {
              i2 = e.hashCode();
              i = indexOf(e, i2);
          }
          if (i >= 0) {
              return false;
          }
          int i3 = ~i;
          int i4 = this.mSize;
          if (i4 >= this.mHashes.length) {
              int i5 = 4;
              if (i4 >= 8) {
                  i5 = (i4 >> 1) + i4;
              } else if (i4 >= 4) {
                  i5 = 8;
              }
              int i6 = i5;
              int[] iArr = this.mHashes;
              Object[] objArr = this.mArray;
              allocArrays(i6);
              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 i7 = this.mSize;
          if (i3 < i7) {
              int[] iArr3 = this.mHashes;
              System.arraycopy(iArr3, i3, iArr3, i3 + 1, i7 - i3);
              Object[] objArr2 = this.mArray;
              System.arraycopy(objArr2, i3, objArr2, i3 + 1, this.mSize - i3);
          }
          this.mHashes[i3] = i2;
          this.mArray[i3] = e;
          this.mSize++;
          return true;
      }
  
      public void addAll(ArraySet<? extends E> 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;
          }
      }
  
      public boolean addAll(Collection<? extends E> collection) {
          ensureCapacity(this.mSize + collection.size());
          boolean z = false;
          for (Object add : collection) {
              z |= add(add);
          }
          return z;
      }
  
      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;
          }
      }
  
      public boolean contains(Object key) {
          return indexOf(key) >= 0;
      }
  
      public boolean containsAll(Collection<?> collection) {
          for (Object contains : collection) {
              if (!contains(contains)) {
                  return false;
              }
          }
          return true;
      }
  
      public void ensureCapacity(int minimumCapacity) {
          if (this.mHashes.length < minimumCapacity) {
              int[] iArr = this.mHashes;
              Object[] objArr = this.mArray;
              allocArrays(minimumCapacity);
              int i = this.mSize;
              if (i > 0) {
                  System.arraycopy(iArr, 0, this.mHashes, 0, i);
                  System.arraycopy(objArr, 0, this.mArray, 0, this.mSize);
              }
              freeArrays(iArr, objArr, this.mSize);
          }
      }
  
      public boolean equals(Object object) {
          if (this == object) {
              return true;
          }
          if (!(object instanceof Set)) {
              return false;
          }
          Set set = (Set) object;
          if (size() != set.size()) {
              return false;
          }
          int i = 0;
          while (i < this.mSize) {
              try {
                  if (!set.contains(valueAt(i))) {
                      return false;
                  }
                  i++;
              } catch (NullPointerException e) {
                  return false;
              } catch (ClassCastException e2) {
                  return false;
              }
          }
          return true;
      }
  
      public int hashCode() {
          int[] iArr = this.mHashes;
          int i = 0;
          int i2 = this.mSize;
          for (int i3 = 0; i3 < i2; i3++) {
              i += iArr[i3];
          }
          return i;
      }
  
      public int indexOf(Object key) {
          return key == null ? indexOfNull() : indexOf(key, key.hashCode());
      }
  
      public boolean isEmpty() {
          return this.mSize <= 0;
      }
  
      public Iterator<E> iterator() {
          return getCollection().getKeySet().iterator();
      }
  
      public boolean remove(Object object) {
          int indexOf = indexOf(object);
          if (indexOf < 0) {
              return false;
          }
          removeAt(indexOf);
          return true;
      }
  
      public boolean removeAll(ArraySet<? extends E> arraySet) {
          int i = arraySet.mSize;
          int i2 = this.mSize;
          for (int i3 = 0; i3 < i; i3++) {
              remove(arraySet.valueAt(i3));
          }
          return i2 != this.mSize;
      }
  
      public boolean removeAll(Collection<?> collection) {
          boolean z = false;
          for (Object remove : collection) {
              z |= remove(remove);
          }
          return z;
      }
  
      public E removeAt(int index) {
          E[] eArr = this.mArray;
          E e = eArr[index];
          int i = this.mSize;
          if (i <= 1) {
              freeArrays(this.mHashes, eArr, i);
              this.mHashes = INT;
              this.mArray = OBJECT;
              this.mSize = 0;
          } else {
              int[] iArr = this.mHashes;
              int i2 = 8;
              if (iArr.length <= 8 || i >= iArr.length / 3) {
                  int i3 = i - 1;
                  this.mSize = i3;
                  if (index < i3) {
                      System.arraycopy(iArr, index + 1, iArr, index, i3 - index);
                      Object[] objArr = this.mArray;
                      System.arraycopy(objArr, index + 1, objArr, index, this.mSize - index);
                  }
                  this.mArray[this.mSize] = null;
              } else {
                  if (i > 8) {
                      i2 = i + (i >> 1);
                  }
                  int[] iArr2 = this.mHashes;
                  Object[] objArr2 = this.mArray;
                  allocArrays(i2);
                  this.mSize--;
                  if (index > 0) {
                      System.arraycopy(iArr2, 0, this.mHashes, 0, index);
                      System.arraycopy(objArr2, 0, this.mArray, 0, index);
                  }
                  int i4 = this.mSize;
                  if (index < i4) {
                      System.arraycopy(iArr2, index + 1, this.mHashes, index, i4 - index);
                      System.arraycopy(objArr2, index + 1, this.mArray, index, this.mSize - index);
                  }
              }
          }
          return e;
      }
  
      public boolean retainAll(Collection<?> collection) {
          boolean z = false;
          for (int i = this.mSize - 1; i >= 0; i--) {
              if (!collection.contains(this.mArray[i])) {
                  removeAt(i);
                  z = true;
              }
          }
          return z;
      }
  
      public int size() {
          return this.mSize;
      }
  
      public Object[] toArray() {
          int i = this.mSize;
          Object[] objArr = new Object[i];
          System.arraycopy(this.mArray, 0, objArr, 0, i);
          return objArr;
      }
  
      public <T> T[] toArray(T[] tArr) {
          if (tArr.length < this.mSize) {
              tArr = (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;
      }
  
      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(", ");
              }
              Object valueAt = valueAt(i);
              if (valueAt != this) {
                  sb.append(valueAt);
              } else {
                  sb.append("(this Set)");
              }
          }
          sb.append('}');
          return sb.toString();
      }
  
      public E valueAt(int index) {
          return this.mArray[index];
      }
  }
  