package o;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Set;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.p9, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2124p9 implements Collection, Set, InterfaceC3084zJ, BJ {
    public int[] l;
    public Object[] m;
    public int n;

    public C2124p9(int i) {
        this.l = AbstractC0940cn0.a;
        this.m = AbstractC0940cn0.c;
        if (i > 0) {
            this.l = new int[i];
            this.m = new Object[i];
        }
    }

    public final Object a(int i) {
        int i2 = this.n;
        Object[] objArr = this.m;
        Object obj = objArr[i];
        if (i2 <= 1) {
            clear();
            return obj;
        }
        int i3 = i2 - 1;
        int[] iArr = this.l;
        if (iArr.length <= 8 || i2 >= iArr.length / 3) {
            if (i < i3) {
                int i4 = i + 1;
                AbstractC2219q9.I(i, i4, i2, iArr, iArr);
                Object[] objArr2 = this.m;
                AbstractC2219q9.J(i, i4, i2, objArr2, objArr2);
            }
            this.m[i3] = null;
        } else {
            int i5 = i2 > 8 ? i2 + (i2 >> 1) : 8;
            int[] iArr2 = new int[i5];
            this.l = iArr2;
            this.m = new Object[i5];
            if (i > 0) {
                AbstractC2219q9.L(0, i, 6, iArr, iArr2);
                AbstractC2219q9.M(0, i, 6, objArr, this.m);
            }
            if (i < i3) {
                int i6 = i + 1;
                AbstractC2219q9.I(i, i6, i2, iArr, this.l);
                AbstractC2219q9.J(i, i6, i2, objArr, this.m);
            }
        }
        if (i2 != this.n) {
            throw new ConcurrentModificationException();
        }
        this.n = i3;
        return obj;
    }

    @Override // java.util.Collection, java.util.Set
    public final boolean add(Object obj) {
        int i;
        int iY;
        int i2 = this.n;
        if (obj == null) {
            iY = AbstractC1692kh0.y(this, null, 0);
            i = 0;
        } else {
            int iHashCode = obj.hashCode();
            i = iHashCode;
            iY = AbstractC1692kh0.y(this, obj, iHashCode);
        }
        if (iY >= 0) {
            return false;
        }
        int i3 = ~iY;
        int[] iArr = this.l;
        if (i2 >= iArr.length) {
            int i4 = 8;
            if (i2 >= 8) {
                i4 = (i2 >> 1) + i2;
            } else if (i2 < 4) {
                i4 = 4;
            }
            Object[] objArr = this.m;
            int[] iArr2 = new int[i4];
            this.l = iArr2;
            this.m = new Object[i4];
            if (i2 != this.n) {
                throw new ConcurrentModificationException();
            }
            if (iArr2.length != 0) {
                AbstractC2219q9.L(0, iArr.length, 6, iArr, iArr2);
                AbstractC2219q9.M(0, objArr.length, 6, objArr, this.m);
            }
        }
        if (i3 < i2) {
            int[] iArr3 = this.l;
            int i5 = i3 + 1;
            AbstractC2219q9.I(i5, i3, i2, iArr3, iArr3);
            Object[] objArr2 = this.m;
            AbstractC2219q9.J(i5, i3, i2, objArr2, objArr2);
        }
        int i6 = this.n;
        if (i2 == i6) {
            int[] iArr4 = this.l;
            if (i3 < iArr4.length) {
                iArr4[i3] = i;
                this.m[i3] = obj;
                this.n = i6 + 1;
                return true;
            }
        }
        throw new ConcurrentModificationException();
    }

    @Override // java.util.Collection, java.util.Set
    public final boolean addAll(Collection collection) {
        int size = collection.size() + this.n;
        int i = this.n;
        int[] iArr = this.l;
        boolean zAdd = false;
        if (iArr.length < size) {
            Object[] objArr = this.m;
            int[] iArr2 = new int[size];
            this.l = iArr2;
            this.m = new Object[size];
            if (i > 0) {
                AbstractC2219q9.L(0, i, 6, iArr, iArr2);
                AbstractC2219q9.M(0, this.n, 6, objArr, this.m);
            }
        }
        if (this.n != i) {
            throw new ConcurrentModificationException();
        }
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            zAdd |= add(it.next());
        }
        return zAdd;
    }

    @Override // java.util.Collection, java.util.Set
    public final void clear() {
        if (this.n != 0) {
            this.l = AbstractC0940cn0.a;
            this.m = AbstractC0940cn0.c;
            this.n = 0;
        }
        if (this.n != 0) {
            throw new ConcurrentModificationException();
        }
    }

    @Override // java.util.Collection, java.util.Set
    public final boolean contains(Object obj) {
        return (obj == null ? AbstractC1692kh0.y(this, null, 0) : AbstractC1692kh0.y(this, obj, obj.hashCode())) >= 0;
    }

    @Override // java.util.Collection, java.util.Set
    public final 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 final boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Set) || this.n != ((Set) obj).size()) {
            return false;
        }
        try {
            int i = this.n;
            for (int i2 = 0; i2 < i; i2++) {
                if (!((Set) obj).contains(this.m[i2])) {
                    return false;
                }
            }
            return true;
        } catch (ClassCastException | NullPointerException unused) {
            return false;
        }
    }

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

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

    @Override // java.util.Collection, java.lang.Iterable, java.util.Set
    public final Iterator iterator() {
        return new C1460i9(this);
    }

    @Override // java.util.Collection, java.util.Set
    public final boolean remove(Object obj) {
        int iY = obj == null ? AbstractC1692kh0.y(this, null, 0) : AbstractC1692kh0.y(this, obj, obj.hashCode());
        if (iY < 0) {
            return false;
        }
        a(iY);
        return true;
    }

    @Override // java.util.Collection, java.util.Set
    public final 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 final boolean retainAll(Collection collection) {
        boolean z = false;
        for (int i = this.n - 1; -1 < i; i--) {
            if (!AbstractC3117zi.b0(collection, this.m[i])) {
                a(i);
                z = true;
            }
        }
        return z;
    }

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

    @Override // java.util.Collection, java.util.Set
    public final Object[] toArray() {
        Object[] objArr = this.m;
        int i = this.n;
        AbstractC2461sm0.d(i, objArr.length);
        return Arrays.copyOfRange(objArr, 0, i);
    }

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

    @Override // java.util.Collection, java.util.Set
    public final Object[] toArray(Object[] objArr) {
        int i = this.n;
        if (objArr.length < i) {
            objArr = (Object[]) Array.newInstance(objArr.getClass().getComponentType(), i);
        } else if (objArr.length > i) {
            objArr[i] = null;
        }
        AbstractC2219q9.J(0, 0, this.n, this.m, objArr);
        return objArr;
    }
}
