package androidx.collection;

import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Set;

/* loaded from: classes.dex */
public final class b implements Collection, Set, en.b, en.e {

    /* renamed from: p, reason: collision with root package name */
    private int[] f1286p;

    /* renamed from: q, reason: collision with root package name */
    private Object[] f1287q;

    /* renamed from: r, reason: collision with root package name */
    private int f1288r;

    /* loaded from: classes.dex */
    private final class a extends h {
        public a() {
            super(b.this.r());
        }

        @Override // androidx.collection.h
        protected Object a(int i10) {
            return b.this.D(i10);
        }

        @Override // androidx.collection.h
        protected void b(int i10) {
            b.this.x(i10);
        }
    }

    public b() {
        this(0, 1, null);
    }

    public final void B(int[] iArr) {
        kotlin.jvm.internal.t.g(iArr, "<set-?>");
        this.f1286p = iArr;
    }

    public final void C(int i10) {
        this.f1288r = i10;
    }

    public final Object D(int i10) {
        return g()[i10];
    }

    @Override // java.util.Collection, java.util.Set
    public boolean add(Object obj) {
        int i10;
        int c10;
        int r10 = r();
        if (obj == null) {
            c10 = d.d(this);
            i10 = 0;
        } else {
            int hashCode = obj.hashCode();
            i10 = hashCode;
            c10 = d.c(this, obj, hashCode);
        }
        if (c10 >= 0) {
            return false;
        }
        int i11 = ~c10;
        if (r10 >= i().length) {
            int i12 = 8;
            if (r10 >= 8) {
                i12 = (r10 >> 1) + r10;
            } else if (r10 < 4) {
                i12 = 4;
            }
            int[] i13 = i();
            Object[] g10 = g();
            d.a(this, i12);
            if (r10 != r()) {
                throw new ConcurrentModificationException();
            }
            if (!(i().length == 0)) {
                sm.o.o(i13, i(), 0, 0, i13.length, 6, null);
                sm.o.p(g10, g(), 0, 0, g10.length, 6, null);
            }
        }
        if (i11 < r10) {
            int i14 = i11 + 1;
            sm.o.j(i(), i(), i14, i11, r10);
            sm.o.l(g(), g(), i14, i11, r10);
        }
        if (r10 != r() || i11 >= i().length) {
            throw new ConcurrentModificationException();
        }
        i()[i11] = i10;
        g()[i11] = obj;
        C(r() + 1);
        return true;
    }

    @Override // java.util.Collection, java.util.Set
    public boolean addAll(Collection elements) {
        kotlin.jvm.internal.t.g(elements, "elements");
        e(r() + elements.size());
        Iterator it = elements.iterator();
        boolean z10 = false;
        while (it.hasNext()) {
            z10 |= add(it.next());
        }
        return z10;
    }

    @Override // java.util.Collection, java.util.Set
    public void clear() {
        if (r() != 0) {
            B(w.a.f40528a);
            z(w.a.f40530c);
            C(0);
        }
        if (r() != 0) {
            throw new ConcurrentModificationException();
        }
    }

    @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 elements) {
        kotlin.jvm.internal.t.g(elements, "elements");
        Iterator it = elements.iterator();
        while (it.hasNext()) {
            if (!contains(it.next())) {
                return false;
            }
        }
        return true;
    }

    public final void e(int i10) {
        int r10 = r();
        if (i().length < i10) {
            int[] i11 = i();
            Object[] g10 = g();
            d.a(this, i10);
            if (r() > 0) {
                sm.o.o(i11, i(), 0, 0, r(), 6, null);
                sm.o.p(g10, g(), 0, 0, r(), 6, null);
            }
        }
        if (r() != r10) {
            throw new ConcurrentModificationException();
        }
    }

    @Override // java.util.Collection, java.util.Set
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if ((obj instanceof Set) && size() == ((Set) obj).size()) {
            try {
                int r10 = r();
                for (int i10 = 0; i10 < r10; i10++) {
                    if (((Set) obj).contains(D(i10))) {
                    }
                }
                return true;
            } catch (ClassCastException | NullPointerException unused) {
            }
        }
        return false;
    }

    public final Object[] g() {
        return this.f1287q;
    }

    @Override // java.util.Collection, java.util.Set
    public int hashCode() {
        int[] i10 = i();
        int r10 = r();
        int i11 = 0;
        for (int i12 = 0; i12 < r10; i12++) {
            i11 += i10[i12];
        }
        return i11;
    }

    public final int[] i() {
        return this.f1286p;
    }

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

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

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

    public int m() {
        return this.f1288r;
    }

    public final int r() {
        return this.f1288r;
    }

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

    @Override // java.util.Collection, java.util.Set
    public boolean removeAll(Collection elements) {
        kotlin.jvm.internal.t.g(elements, "elements");
        Iterator it = elements.iterator();
        boolean z10 = false;
        while (it.hasNext()) {
            z10 |= remove(it.next());
        }
        return z10;
    }

    @Override // java.util.Collection, java.util.Set
    public boolean retainAll(Collection elements) {
        boolean U;
        kotlin.jvm.internal.t.g(elements, "elements");
        boolean z10 = false;
        for (int r10 = r() - 1; -1 < r10; r10--) {
            U = sm.c0.U(elements, g()[r10]);
            if (!U) {
                x(r10);
                z10 = true;
            }
        }
        return z10;
    }

    @Override // java.util.Collection, java.util.Set
    public final /* bridge */ int size() {
        return m();
    }

    @Override // java.util.Collection, java.util.Set
    public final Object[] toArray() {
        Object[] r10;
        r10 = sm.o.r(this.f1287q, 0, this.f1288r);
        return r10;
    }

    public String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder sb2 = new StringBuilder(r() * 14);
        sb2.append('{');
        int r10 = r();
        for (int i10 = 0; i10 < r10; i10++) {
            if (i10 > 0) {
                sb2.append(", ");
            }
            Object D = D(i10);
            if (D != this) {
                sb2.append(D);
            } else {
                sb2.append("(this Set)");
            }
        }
        sb2.append('}');
        String sb3 = sb2.toString();
        kotlin.jvm.internal.t.f(sb3, "StringBuilder(capacity).…builderAction).toString()");
        return sb3;
    }

    public final Object x(int i10) {
        int r10 = r();
        Object obj = g()[i10];
        if (r10 <= 1) {
            clear();
        } else {
            int i11 = r10 - 1;
            if (i().length <= 8 || r() >= i().length / 3) {
                if (i10 < i11) {
                    int i12 = i10 + 1;
                    int i13 = i11 + 1;
                    sm.o.j(i(), i(), i10, i12, i13);
                    sm.o.l(g(), g(), i10, i12, i13);
                }
                g()[i11] = null;
            } else {
                int r11 = r() > 8 ? r() + (r() >> 1) : 8;
                int[] i14 = i();
                Object[] g10 = g();
                d.a(this, r11);
                if (i10 > 0) {
                    sm.o.o(i14, i(), 0, 0, i10, 6, null);
                    sm.o.p(g10, g(), 0, 0, i10, 6, null);
                }
                if (i10 < i11) {
                    int i15 = i10 + 1;
                    int i16 = i11 + 1;
                    sm.o.j(i14, i(), i10, i15, i16);
                    sm.o.l(g10, g(), i10, i15, i16);
                }
            }
            if (r10 != r()) {
                throw new ConcurrentModificationException();
            }
            C(i11);
        }
        return obj;
    }

    public final void z(Object[] objArr) {
        kotlin.jvm.internal.t.g(objArr, "<set-?>");
        this.f1287q = objArr;
    }

    public b(int i10) {
        this.f1286p = w.a.f40528a;
        this.f1287q = w.a.f40530c;
        if (i10 > 0) {
            d.a(this, i10);
        }
    }

    @Override // java.util.Collection, java.util.Set
    public final Object[] toArray(Object[] array) {
        kotlin.jvm.internal.t.g(array, "array");
        Object[] result = c.a(array, this.f1288r);
        sm.o.l(this.f1287q, result, 0, 0, this.f1288r);
        kotlin.jvm.internal.t.f(result, "result");
        return result;
    }

    public /* synthetic */ b(int i10, int i11, kotlin.jvm.internal.k kVar) {
        this((i11 & 1) != 0 ? 0 : i10);
    }
}
