package b.d;

import b.d.g;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final int[] f649a = new int[0];

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Object[] f650b = new Object[0];

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static Object[] f653e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static int f654f;
    public int[] g;
    public Object[] h;
    public int i;
    public g<E, E> j;

    public c() {
        this(0);
    }

    public c(int i) {
        if (i == 0) {
            this.g = f649a;
            this.h = f650b;
        } else {
            a(i);
        }
        this.i = 0;
    }

    public static void b(int[] iArr, Object[] objArr, int i) {
        if (iArr.length == 8) {
            synchronized (c.class) {
                if (f654f < 10) {
                    objArr[0] = f653e;
                    objArr[1] = iArr;
                    for (int i2 = i - 1; i2 >= 2; i2--) {
                        objArr[i2] = null;
                    }
                    f653e = objArr;
                    f654f++;
                }
            }
            return;
        }
        if (iArr.length == 4) {
            synchronized (c.class) {
                if (f652d < 10) {
                    objArr[0] = f651c;
                    objArr[1] = iArr;
                    for (int i3 = i - 1; i3 >= 2; i3--) {
                        objArr[i3] = null;
                    }
                    f651c = objArr;
                    f652d++;
                }
            }
        }
    }

    public final void a(int i) {
        if (i == 8) {
            synchronized (c.class) {
                Object[] objArr = f653e;
                if (objArr != null) {
                    this.h = objArr;
                    f653e = (Object[]) objArr[0];
                    this.g = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    f654f--;
                    return;
                }
            }
        } else if (i == 4) {
            synchronized (c.class) {
                Object[] objArr2 = f651c;
                if (objArr2 != null) {
                    this.h = objArr2;
                    f651c = (Object[]) objArr2[0];
                    this.g = (int[]) objArr2[1];
                    objArr2[1] = null;
                    objArr2[0] = null;
                    f652d--;
                    return;
                }
            }
        }
        this.g = new int[i];
        this.h = new Object[i];
    }

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

    @Override // java.util.Collection, java.util.Set
    public boolean addAll(Collection<? extends E> collection) {
        int size = collection.size() + this.i;
        int[] iArr = this.g;
        boolean zAdd = false;
        if (iArr.length < size) {
            Object[] objArr = this.h;
            a(size);
            int i = this.i;
            if (i > 0) {
                System.arraycopy(iArr, 0, this.g, 0, i);
                System.arraycopy(objArr, 0, this.h, 0, this.i);
            }
            b(iArr, objArr, this.i);
        }
        Iterator<? extends E> it = collection.iterator();
        while (it.hasNext()) {
            zAdd |= add(it.next());
        }
        return zAdd;
    }

    public final int c(Object obj, int i) {
        int i2 = this.i;
        if (i2 == 0) {
            return -1;
        }
        int iA = d.a(this.g, i2, i);
        if (iA < 0 || obj.equals(this.h[iA])) {
            return iA;
        }
        int i3 = iA + 1;
        while (i3 < i2 && this.g[i3] == i) {
            if (obj.equals(this.h[i3])) {
                return i3;
            }
            i3++;
        }
        for (int i4 = iA - 1; i4 >= 0 && this.g[i4] == i; i4--) {
            if (obj.equals(this.h[i4])) {
                return i4;
            }
        }
        return ~i3;
    }

    @Override // java.util.Collection, java.util.Set
    public void clear() {
        int i = this.i;
        if (i != 0) {
            b(this.g, this.h, i);
            this.g = f649a;
            this.h = f650b;
            this.i = 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 final int d() {
        int i = this.i;
        if (i == 0) {
            return -1;
        }
        int iA = d.a(this.g, i, 0);
        if (iA < 0 || this.h[iA] == null) {
            return iA;
        }
        int i2 = iA + 1;
        while (i2 < i && this.g[i2] == 0) {
            if (this.h[i2] == null) {
                return i2;
            }
            i2++;
        }
        for (int i3 = iA - 1; i3 >= 0 && this.g[i3] == 0; i3--) {
            if (this.h[i3] == null) {
                return i3;
            }
        }
        return ~i2;
    }

    public E e(int i) {
        Object[] objArr = this.h;
        E e2 = (E) objArr[i];
        int i2 = this.i;
        if (i2 <= 1) {
            b(this.g, objArr, i2);
            this.g = f649a;
            this.h = f650b;
            this.i = 0;
        } else {
            int[] iArr = this.g;
            if (iArr.length <= 8 || i2 >= iArr.length / 3) {
                int i3 = i2 - 1;
                this.i = i3;
                if (i < i3) {
                    int i4 = i + 1;
                    System.arraycopy(iArr, i4, iArr, i, i3 - i);
                    Object[] objArr2 = this.h;
                    System.arraycopy(objArr2, i4, objArr2, i, this.i - i);
                }
                this.h[this.i] = null;
            } else {
                a(i2 > 8 ? i2 + (i2 >> 1) : 8);
                this.i--;
                if (i > 0) {
                    System.arraycopy(iArr, 0, this.g, 0, i);
                    System.arraycopy(objArr, 0, this.h, 0, i);
                }
                int i5 = this.i;
                if (i < i5) {
                    int i6 = i + 1;
                    System.arraycopy(iArr, i6, this.g, i, i5 - i);
                    System.arraycopy(objArr, i6, this.h, i, this.i - i);
                }
            }
        }
        return e2;
    }

    @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 (this.i != set.size()) {
                return false;
            }
            for (int i = 0; i < this.i; i++) {
                try {
                    if (!set.contains(this.h[i])) {
                        return false;
                    }
                } catch (ClassCastException | NullPointerException unused) {
                }
            }
            return true;
        }
        return false;
    }

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

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

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

    @Override // java.util.Collection, java.lang.Iterable, java.util.Set
    public Iterator<E> iterator() {
        if (this.j == null) {
            this.j = new b(this);
        }
        g<E, E> gVar = this.j;
        if (gVar.f670b == null) {
            gVar.f670b = new g.c();
        }
        return gVar.f670b.iterator();
    }

    @Override // java.util.Collection, java.util.Set
    public boolean remove(Object obj) {
        int iIndexOf = indexOf(obj);
        if (iIndexOf < 0) {
            return false;
        }
        e(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 i = this.i - 1; i >= 0; i--) {
            if (!collection.contains(this.h[i])) {
                e(i);
                z = true;
            }
        }
        return z;
    }

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

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

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

    public String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder(this.i * 14);
        sb.append('{');
        for (int i = 0; i < this.i; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            Object obj = this.h[i];
            if (obj != this) {
                sb.append(obj);
            } else {
                sb.append("(this Set)");
            }
        }
        sb.append('}');
        return sb.toString();
    }
}
