package c.d;

import c.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: b, reason: collision with root package name */
    public static final int[] f5107b = new int[0];

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int[] f5113h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public Object[] f5114i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f5115j;
    public g<E, E> k;

    public c() {
        this(0);
    }

    public c(int i2) {
        if (i2 == 0) {
            this.f5113h = f5107b;
            this.f5114i = f5108c;
        } else {
            a(i2);
        }
        this.f5115j = 0;
    }

    public static void b(int[] iArr, Object[] objArr, int i2) {
        if (iArr.length == 8) {
            synchronized (c.class) {
                if (f5112g < 10) {
                    objArr[0] = f5111f;
                    objArr[1] = iArr;
                    for (int i3 = i2 - 1; i3 >= 2; i3--) {
                        objArr[i3] = null;
                    }
                    f5111f = objArr;
                    f5112g++;
                }
            }
            return;
        }
        if (iArr.length == 4) {
            synchronized (c.class) {
                if (f5110e < 10) {
                    objArr[0] = f5109d;
                    objArr[1] = iArr;
                    for (int i4 = i2 - 1; i4 >= 2; i4--) {
                        objArr[i4] = null;
                    }
                    f5109d = objArr;
                    f5110e++;
                }
            }
        }
    }

    public final void a(int i2) {
        if (i2 == 8) {
            synchronized (c.class) {
                Object[] objArr = f5111f;
                if (objArr != null) {
                    this.f5114i = objArr;
                    f5111f = (Object[]) objArr[0];
                    this.f5113h = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    f5112g--;
                    return;
                }
            }
        } else if (i2 == 4) {
            synchronized (c.class) {
                Object[] objArr2 = f5109d;
                if (objArr2 != null) {
                    this.f5114i = objArr2;
                    f5109d = (Object[]) objArr2[0];
                    this.f5113h = (int[]) objArr2[1];
                    objArr2[1] = null;
                    objArr2[0] = null;
                    f5110e--;
                    return;
                }
            }
        }
        this.f5113h = new int[i2];
        this.f5114i = new Object[i2];
    }

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

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

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

    @Override // java.util.Collection, java.util.Set
    public void clear() {
        int i2 = this.f5115j;
        if (i2 != 0) {
            b(this.f5113h, this.f5114i, i2);
            this.f5113h = f5107b;
            this.f5114i = f5108c;
            this.f5115j = 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 i2 = this.f5115j;
        if (i2 == 0) {
            return -1;
        }
        int iA = d.a(this.f5113h, i2, 0);
        if (iA < 0 || this.f5114i[iA] == null) {
            return iA;
        }
        int i3 = iA + 1;
        while (i3 < i2 && this.f5113h[i3] == 0) {
            if (this.f5114i[i3] == null) {
                return i3;
            }
            i3++;
        }
        for (int i4 = iA - 1; i4 >= 0 && this.f5113h[i4] == 0; i4--) {
            if (this.f5114i[i4] == null) {
                return i4;
            }
        }
        return ~i3;
    }

    public E e(int i2) {
        Object[] objArr = this.f5114i;
        E e2 = (E) objArr[i2];
        int i3 = this.f5115j;
        if (i3 <= 1) {
            b(this.f5113h, objArr, i3);
            this.f5113h = f5107b;
            this.f5114i = f5108c;
            this.f5115j = 0;
        } else {
            int[] iArr = this.f5113h;
            if (iArr.length <= 8 || i3 >= iArr.length / 3) {
                int i4 = i3 - 1;
                this.f5115j = i4;
                if (i2 < i4) {
                    int i5 = i2 + 1;
                    System.arraycopy(iArr, i5, iArr, i2, i4 - i2);
                    Object[] objArr2 = this.f5114i;
                    System.arraycopy(objArr2, i5, objArr2, i2, this.f5115j - i2);
                }
                this.f5114i[this.f5115j] = null;
            } else {
                a(i3 > 8 ? i3 + (i3 >> 1) : 8);
                this.f5115j--;
                if (i2 > 0) {
                    System.arraycopy(iArr, 0, this.f5113h, 0, i2);
                    System.arraycopy(objArr, 0, this.f5114i, 0, i2);
                }
                int i6 = this.f5115j;
                if (i2 < i6) {
                    int i7 = i2 + 1;
                    System.arraycopy(iArr, i7, this.f5113h, i2, i6 - i2);
                    System.arraycopy(objArr, i7, this.f5114i, i2, this.f5115j - i2);
                }
            }
        }
        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.f5115j != set.size()) {
                return false;
            }
            for (int i2 = 0; i2 < this.f5115j; i2++) {
                try {
                    if (!set.contains(this.f5114i[i2])) {
                        return false;
                    }
                } catch (ClassCastException | NullPointerException unused) {
                }
            }
            return true;
        }
        return false;
    }

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

    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.f5115j <= 0;
    }

    @Override // java.util.Collection, java.lang.Iterable, java.util.Set
    public Iterator<E> iterator() {
        if (this.k == null) {
            this.k = new b(this);
        }
        g<E, E> gVar = this.k;
        if (gVar.f5132b == null) {
            gVar.f5132b = new g.c();
        }
        return gVar.f5132b.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 i2 = this.f5115j - 1; i2 >= 0; i2--) {
            if (!collection.contains(this.f5114i[i2])) {
                e(i2);
                z = true;
            }
        }
        return z;
    }

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

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

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

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