package a.b.c.f;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/* JADX INFO: compiled from: ArraySet.java */
/* JADX INFO: loaded from: classes.dex */
public final class b<E> implements Collection<E>, Set<E> {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static final int[] f177f = new int[0];
    private static final Object[] g = new Object[0];
    private static Object[] h;
    private static int i;
    private static Object[] j;
    private static int k;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private int[] f178b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    Object[] f179c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    int f180d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private h<E, E> f181e;

    /* JADX INFO: compiled from: ArraySet.java */
    class a extends h<E, E> {
        a() {
        }

        @Override // a.b.c.f.h
        protected void a() {
            b.this.clear();
        }

        @Override // a.b.c.f.h
        protected Object b(int i, int i2) {
            return b.this.f179c[i];
        }

        @Override // a.b.c.f.h
        protected Map<E, E> c() {
            throw new UnsupportedOperationException("not a map");
        }

        @Override // a.b.c.f.h
        protected int d() {
            return b.this.f180d;
        }

        @Override // a.b.c.f.h
        protected int e(Object obj) {
            return b.this.indexOf(obj);
        }

        @Override // a.b.c.f.h
        protected int f(Object obj) {
            return b.this.indexOf(obj);
        }

        @Override // a.b.c.f.h
        protected void g(E e2, E e3) {
            b.this.add(e2);
        }

        @Override // a.b.c.f.h
        protected void h(int i) {
            b.this.g(i);
        }

        @Override // a.b.c.f.h
        protected E i(int i, E e2) {
            throw new UnsupportedOperationException("not a map");
        }
    }

    public b() {
        this(0);
    }

    private void a(int i2) {
        if (i2 == 8) {
            synchronized (b.class) {
                if (j != null) {
                    Object[] objArr = j;
                    this.f179c = objArr;
                    j = (Object[]) objArr[0];
                    this.f178b = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    k--;
                    return;
                }
            }
        } else if (i2 == 4) {
            synchronized (b.class) {
                if (h != null) {
                    Object[] objArr2 = h;
                    this.f179c = objArr2;
                    h = (Object[]) objArr2[0];
                    this.f178b = (int[]) objArr2[1];
                    objArr2[1] = null;
                    objArr2[0] = null;
                    i--;
                    return;
                }
            }
        }
        this.f178b = new int[i2];
        this.f179c = new Object[i2];
    }

    private static void c(int[] iArr, Object[] objArr, int i2) {
        if (iArr.length == 8) {
            synchronized (b.class) {
                if (k < 10) {
                    objArr[0] = j;
                    objArr[1] = iArr;
                    for (int i3 = i2 - 1; i3 >= 2; i3--) {
                        objArr[i3] = null;
                    }
                    j = objArr;
                    k++;
                }
            }
            return;
        }
        if (iArr.length == 4) {
            synchronized (b.class) {
                if (i < 10) {
                    objArr[0] = h;
                    objArr[1] = iArr;
                    for (int i4 = i2 - 1; i4 >= 2; i4--) {
                        objArr[i4] = null;
                    }
                    h = objArr;
                    i++;
                }
            }
        }
    }

    private h<E, E> d() {
        if (this.f181e == null) {
            this.f181e = new a();
        }
        return this.f181e;
    }

    private int e(Object obj, int i2) {
        int i3 = this.f180d;
        if (i3 == 0) {
            return -1;
        }
        int iA = c.a(this.f178b, i3, i2);
        if (iA < 0 || obj.equals(this.f179c[iA])) {
            return iA;
        }
        int i4 = iA + 1;
        while (i4 < i3 && this.f178b[i4] == i2) {
            if (obj.equals(this.f179c[i4])) {
                return i4;
            }
            i4++;
        }
        for (int i5 = iA - 1; i5 >= 0 && this.f178b[i5] == i2; i5--) {
            if (obj.equals(this.f179c[i5])) {
                return i5;
            }
        }
        return ~i4;
    }

    private int f() {
        int i2 = this.f180d;
        if (i2 == 0) {
            return -1;
        }
        int iA = c.a(this.f178b, i2, 0);
        if (iA < 0 || this.f179c[iA] == null) {
            return iA;
        }
        int i3 = iA + 1;
        while (i3 < i2 && this.f178b[i3] == 0) {
            if (this.f179c[i3] == null) {
                return i3;
            }
            i3++;
        }
        for (int i4 = iA - 1; i4 >= 0 && this.f178b[i4] == 0; i4--) {
            if (this.f179c[i4] == null) {
                return i4;
            }
        }
        return ~i3;
    }

    @Override // java.util.Collection, java.util.Set
    public boolean add(E e2) {
        int i2;
        int iE;
        if (e2 == null) {
            iE = f();
            i2 = 0;
        } else {
            int iHashCode = e2.hashCode();
            i2 = iHashCode;
            iE = e(e2, iHashCode);
        }
        if (iE >= 0) {
            return false;
        }
        int i3 = ~iE;
        int i4 = this.f180d;
        if (i4 >= this.f178b.length) {
            int i5 = 4;
            if (i4 >= 8) {
                i5 = (i4 >> 1) + i4;
            } else if (i4 >= 4) {
                i5 = 8;
            }
            int[] iArr = this.f178b;
            Object[] objArr = this.f179c;
            a(i5);
            int[] iArr2 = this.f178b;
            if (iArr2.length > 0) {
                System.arraycopy(iArr, 0, iArr2, 0, iArr.length);
                System.arraycopy(objArr, 0, this.f179c, 0, objArr.length);
            }
            c(iArr, objArr, this.f180d);
        }
        int i6 = this.f180d;
        if (i3 < i6) {
            int[] iArr3 = this.f178b;
            int i7 = i3 + 1;
            System.arraycopy(iArr3, i3, iArr3, i7, i6 - i3);
            Object[] objArr2 = this.f179c;
            System.arraycopy(objArr2, i3, objArr2, i7, this.f180d - i3);
        }
        this.f178b[i3] = i2;
        this.f179c[i3] = e2;
        this.f180d++;
        return true;
    }

    @Override // java.util.Collection, java.util.Set
    public boolean addAll(Collection<? extends E> collection) {
        b(this.f180d + collection.size());
        Iterator<? extends E> it = collection.iterator();
        boolean zAdd = false;
        while (it.hasNext()) {
            zAdd |= add(it.next());
        }
        return zAdd;
    }

    public void b(int i2) {
        int[] iArr = this.f178b;
        if (iArr.length < i2) {
            Object[] objArr = this.f179c;
            a(i2);
            int i3 = this.f180d;
            if (i3 > 0) {
                System.arraycopy(iArr, 0, this.f178b, 0, i3);
                System.arraycopy(objArr, 0, this.f179c, 0, this.f180d);
            }
            c(iArr, objArr, this.f180d);
        }
    }

    @Override // java.util.Collection, java.util.Set
    public void clear() {
        int i2 = this.f180d;
        if (i2 != 0) {
            c(this.f178b, this.f179c, i2);
            this.f178b = f177f;
            this.f179c = g;
            this.f180d = 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;
    }

    @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.f180d; i2++) {
                try {
                    if (!set.contains(h(i2))) {
                        return false;
                    }
                } catch (ClassCastException | NullPointerException unused) {
                }
            }
            return true;
        }
        return false;
    }

    public E g(int i2) {
        Object[] objArr = this.f179c;
        E e2 = (E) objArr[i2];
        int i3 = this.f180d;
        if (i3 <= 1) {
            c(this.f178b, objArr, i3);
            this.f178b = f177f;
            this.f179c = g;
            this.f180d = 0;
        } else {
            int[] iArr = this.f178b;
            if (iArr.length <= 8 || i3 >= iArr.length / 3) {
                int i4 = this.f180d - 1;
                this.f180d = i4;
                if (i2 < i4) {
                    int[] iArr2 = this.f178b;
                    int i5 = i2 + 1;
                    System.arraycopy(iArr2, i5, iArr2, i2, i4 - i2);
                    Object[] objArr2 = this.f179c;
                    System.arraycopy(objArr2, i5, objArr2, i2, this.f180d - i2);
                }
                this.f179c[this.f180d] = null;
            } else {
                int i6 = i3 > 8 ? i3 + (i3 >> 1) : 8;
                int[] iArr3 = this.f178b;
                Object[] objArr3 = this.f179c;
                a(i6);
                this.f180d--;
                if (i2 > 0) {
                    System.arraycopy(iArr3, 0, this.f178b, 0, i2);
                    System.arraycopy(objArr3, 0, this.f179c, 0, i2);
                }
                int i7 = this.f180d;
                if (i2 < i7) {
                    int i8 = i2 + 1;
                    System.arraycopy(iArr3, i8, this.f178b, i2, i7 - i2);
                    System.arraycopy(objArr3, i8, this.f179c, i2, this.f180d - i2);
                }
            }
        }
        return e2;
    }

    public E h(int i2) {
        return (E) this.f179c[i2];
    }

    @Override // java.util.Collection, java.util.Set
    public int hashCode() {
        int[] iArr = this.f178b;
        int i2 = this.f180d;
        int i3 = 0;
        for (int i4 = 0; i4 < i2; i4++) {
            i3 += iArr[i4];
        }
        return i3;
    }

    public int indexOf(Object obj) {
        return obj == null ? f() : e(obj, obj.hashCode());
    }

    @Override // java.util.Collection, java.util.Set
    public boolean isEmpty() {
        return this.f180d <= 0;
    }

    @Override // java.util.Collection, java.lang.Iterable, java.util.Set
    public Iterator<E> iterator() {
        return d().m().iterator();
    }

    @Override // java.util.Collection, java.util.Set
    public boolean remove(Object obj) {
        int iIndexOf = indexOf(obj);
        if (iIndexOf < 0) {
            return false;
        }
        g(iIndexOf);
        return true;
    }

    @Override // java.util.Collection, java.util.Set
    public boolean removeAll(Collection<?> collection) {
        Iterator<?> it = collection.iterator();
        boolean zRemove = false;
        while (it.hasNext()) {
            zRemove |= remove(it.next());
        }
        return zRemove;
    }

    @Override // java.util.Collection, java.util.Set
    public boolean retainAll(Collection<?> collection) {
        boolean z = false;
        for (int i2 = this.f180d - 1; i2 >= 0; i2--) {
            if (!collection.contains(this.f179c[i2])) {
                g(i2);
                z = true;
            }
        }
        return z;
    }

    @Override // java.util.Collection, java.util.Set
    public int size() {
        return this.f180d;
    }

    @Override // java.util.Collection, java.util.Set
    public Object[] toArray() {
        int i2 = this.f180d;
        Object[] objArr = new Object[i2];
        System.arraycopy(this.f179c, 0, objArr, 0, i2);
        return objArr;
    }

    public String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder(this.f180d * 14);
        sb.append('{');
        for (int i2 = 0; i2 < this.f180d; i2++) {
            if (i2 > 0) {
                sb.append(", ");
            }
            E eH = h(i2);
            if (eH != this) {
                sb.append(eH);
            } else {
                sb.append("(this Set)");
            }
        }
        sb.append('}');
        return sb.toString();
    }

    public b(int i2) {
        if (i2 == 0) {
            this.f178b = f177f;
            this.f179c = g;
        } else {
            a(i2);
        }
        this.f180d = 0;
    }

    @Override // java.util.Collection, java.util.Set
    public <T> T[] toArray(T[] tArr) {
        if (tArr.length < this.f180d) {
            tArr = (T[]) ((Object[]) Array.newInstance(tArr.getClass().getComponentType(), this.f180d));
        }
        System.arraycopy(this.f179c, 0, tArr, 0, this.f180d);
        int length = tArr.length;
        int i2 = this.f180d;
        if (length > i2) {
            tArr[i2] = null;
        }
        return tArr;
    }
}
