package o;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class NW extends V implements Collection, InterfaceC3084zJ {
    public Y l;
    public Object[] m;
    public Object[] n;

    /* renamed from: o, reason: collision with root package name */
    public int f257o;
    public BP p = new BP(1);
    public Object[] q;
    public Object[] r;
    public int s;

    public NW(Y y, Object[] objArr, Object[] objArr2, int i) {
        this.l = y;
        this.m = objArr;
        this.n = objArr2;
        this.f257o = i;
        this.q = objArr;
        this.r = objArr2;
        this.s = y.a();
    }

    public static void d(Object[] objArr, int i, Iterator it) {
        while (i < 32 && it.hasNext()) {
            objArr[i] = it.next();
            i++;
        }
    }

    public final Object[] A(Object[] objArr, int i, int i2, G0 g0) {
        int iD = F3.D(i2, i);
        if (i == 0) {
            Object obj = objArr[iD];
            Object[] objArrM = m(objArr);
            int i3 = iD + 1;
            System.arraycopy(objArr, i3, objArrM, iD, 32 - i3);
            objArrM[31] = g0.m;
            g0.m = obj;
            return objArrM;
        }
        int iD2 = objArr[31] == null ? F3.D(C() - 1, i) : 31;
        Object[] objArrM2 = m(objArr);
        int i4 = i - 5;
        int i5 = iD + 1;
        if (i5 <= iD2) {
            while (true) {
                objArrM2[iD2] = A((Object[]) objArrM2[iD2], i4, 0, g0);
                if (iD2 == i5) {
                    break;
                }
                iD2--;
            }
        }
        objArrM2[iD] = A((Object[]) objArrM2[iD], i4, i2, g0);
        return objArrM2;
    }

    public final Object B(Object[] objArr, int i, int i2, int i3) {
        int iA = a() - i;
        if (iA == 1) {
            Object obj = this.r[0];
            s(objArr, i, i2);
            return obj;
        }
        Object[] objArr2 = this.r;
        Object obj2 = objArr2[i3];
        Object[] objArrM = m(objArr2);
        int i4 = i3 + 1;
        System.arraycopy(objArr2, i4, objArrM, i3, iA - i4);
        objArrM[iA - 1] = null;
        this.q = objArr;
        this.r = objArrM;
        this.s = (i + iA) - 1;
        this.f257o = i2;
        return obj2;
    }

    public final int C() {
        int i = this.s;
        if (i <= 32) {
            return 0;
        }
        return (i - 1) & (-32);
    }

    public final Object[] D(Object[] objArr, int i, int i2, Object obj, G0 g0) {
        int iD = F3.D(i2, i);
        Object[] objArrM = m(objArr);
        if (i != 0) {
            objArrM[iD] = D((Object[]) objArrM[iD], i - 5, i2, obj, g0);
            return objArrM;
        }
        if (objArrM != objArr) {
            ((AbstractList) this).modCount++;
        }
        g0.m = objArrM[iD];
        objArrM[iD] = obj;
        return objArrM;
    }

    public final void E(Collection collection, int i, Object[] objArr, int i2, Object[][] objArr2, int i3, Object[] objArr3) {
        Object[] objArrO;
        if (i3 < 1) {
            AbstractC1102eY.a("requires at least one nullBuffer");
        }
        Object[] objArrM = m(objArr);
        objArr2[0] = objArrM;
        int i4 = i & 31;
        int size = ((collection.size() + i) - 1) & 31;
        int i5 = (i2 - i4) + size;
        if (i5 < 32) {
            AbstractC2219q9.J(size + 1, i4, i2, objArrM, objArr3);
        } else {
            int i6 = i5 - 31;
            if (i3 == 1) {
                objArrO = objArrM;
            } else {
                objArrO = o();
                i3--;
                objArr2[i3] = objArrO;
            }
            int i7 = i2 - i6;
            AbstractC2219q9.J(0, i7, i2, objArrM, objArr3);
            AbstractC2219q9.J(size + 1, i4, i7, objArrM, objArrO);
            objArr3 = objArrO;
        }
        Iterator it = collection.iterator();
        d(objArrM, i4, it);
        for (int i8 = 1; i8 < i3; i8++) {
            Object[] objArrO2 = o();
            d(objArrO2, 0, it);
            objArr2[i8] = objArrO2;
        }
        d(objArr3, 0, it);
    }

    public final int F() {
        int i = this.s;
        return i <= 32 ? i : i - ((i - 1) & (-32));
    }

    @Override // o.V
    public final int a() {
        return this.s;
    }

    @Override // java.util.AbstractList, java.util.List
    public final void add(int i, Object obj) {
        AbstractC2798wI.i(i, a());
        if (i == a()) {
            add(obj);
            return;
        }
        ((AbstractList) this).modCount++;
        int iC = C();
        if (i >= iC) {
            i(i - iC, obj, this.q);
        } else {
            G0 g0 = new G0(5, null);
            i(0, g0.m, h(this.q, this.f257o, i, obj, g0));
        }
    }

    @Override // java.util.AbstractList, java.util.List
    public final boolean addAll(int i, Collection collection) {
        Collection collection2;
        NW nw;
        Object[] objArrO;
        AbstractC2798wI.i(i, this.s);
        if (i == this.s) {
            return addAll(collection);
        }
        if (collection.isEmpty()) {
            return false;
        }
        ((AbstractList) this).modCount++;
        int i2 = (i >> 5) << 5;
        int size = ((collection.size() + (this.s - i2)) - 1) / 32;
        if (size == 0) {
            int i3 = i & 31;
            int size2 = ((collection.size() + i) - 1) & 31;
            Object[] objArr = this.r;
            Object[] objArrM = m(objArr);
            System.arraycopy(objArr, i3, objArrM, size2 + 1, F() - i3);
            d(objArrM, i3, collection.iterator());
            this.r = objArrM;
            this.s = collection.size() + this.s;
            return true;
        }
        Object[][] objArr2 = new Object[size][];
        int iF = F();
        int size3 = collection.size() + this.s;
        if (size3 > 32) {
            size3 -= (size3 - 1) & (-32);
        }
        if (i >= C()) {
            objArrO = o();
            collection2 = collection;
            E(collection2, i, this.r, iF, objArr2, size, objArrO);
            nw = this;
            objArr2 = objArr2;
        } else {
            collection2 = collection;
            nw = this;
            if (size3 > iF) {
                int i4 = size3 - iF;
                Object[] objArrN = n(i4, nw.r);
                nw.f(collection2, i, i4, objArr2, size, objArrN);
                objArr2 = objArr2;
                objArrO = objArrN;
            } else {
                Object[] objArr3 = nw.r;
                objArrO = o();
                int i5 = iF - size3;
                System.arraycopy(objArr3, i5, objArrO, 0, iF - i5);
                int i6 = 32 - i5;
                Object[] objArrN2 = n(i6, nw.r);
                int i7 = size - 1;
                objArr2[i7] = objArrN2;
                nw.f(collection2, i, i6, objArr2, i7, objArrN2);
                collection2 = collection2;
            }
        }
        nw.q = u(nw.q, i2, objArr2);
        nw.r = objArrO;
        nw.s = collection2.size() + nw.s;
        return true;
    }

    @Override // o.V
    public final Object b(int i) {
        AbstractC2798wI.h(i, a());
        ((AbstractList) this).modCount++;
        int iC = C();
        if (i >= iC) {
            return B(this.q, iC, this.f257o, i - iC);
        }
        G0 g0 = new G0(5, this.r[0]);
        B(A(this.q, this.f257o, i, g0), iC, this.f257o, 0);
        return g0.m;
    }

    public final Y c() {
        Y c1555j90;
        Object[] objArr = this.q;
        if (objArr == this.m && this.r == this.n) {
            c1555j90 = this.l;
        } else {
            this.p = new BP(1);
            this.m = objArr;
            Object[] objArr2 = this.r;
            this.n = objArr2;
            c1555j90 = objArr == null ? objArr2.length == 0 ? C1555j90.m : new C1555j90(Arrays.copyOf(this.r, a())) : new MW(objArr, objArr2, a(), this.f257o);
        }
        this.l = c1555j90;
        return c1555j90;
    }

    public final int e() {
        return ((AbstractList) this).modCount;
    }

    public final void f(Collection collection, int i, int i2, Object[][] objArr, int i3, Object[] objArr2) {
        if (this.q == null) {
            throw new IllegalStateException("root is null");
        }
        int i4 = i >> 5;
        S sL = l(C() >> 5);
        int i5 = i3;
        Object[] objArrN = objArr2;
        while (sL.l - 1 != i4) {
            Object[] objArr3 = (Object[]) sL.previous();
            AbstractC2219q9.J(0, 32 - i2, 32, objArr3, objArrN);
            objArrN = n(i2, objArr3);
            i5--;
            objArr[i5] = objArrN;
        }
        Object[] objArr4 = (Object[]) sL.previous();
        int iC = i3 - (((C() >> 5) - 1) - i4);
        if (iC < i3) {
            objArr2 = objArr[iC];
        }
        E(collection, i, objArr4, 32, objArr, iC, objArr2);
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object get(int i) {
        Object[] objArr;
        AbstractC2798wI.h(i, a());
        if (C() <= i) {
            objArr = this.r;
        } else {
            objArr = this.q;
            for (int i2 = this.f257o; i2 > 0; i2 -= 5) {
                objArr = (Object[]) objArr[F3.D(i, i2)];
            }
        }
        return objArr[i & 31];
    }

    public final Object[] h(Object[] objArr, int i, int i2, Object obj, G0 g0) {
        Object obj2;
        int iD = F3.D(i2, i);
        if (i == 0) {
            g0.m = objArr[31];
            Object[] objArrM = m(objArr);
            System.arraycopy(objArr, iD, objArrM, iD + 1, 31 - iD);
            objArrM[iD] = obj;
            return objArrM;
        }
        Object[] objArrM2 = m(objArr);
        int i3 = i - 5;
        objArrM2[iD] = h((Object[]) objArrM2[iD], i3, i2, obj, g0);
        while (true) {
            iD++;
            if (iD >= 32 || (obj2 = objArrM2[iD]) == null) {
                break;
            }
            objArrM2[iD] = h((Object[]) obj2, i3, 0, g0.m, g0);
        }
        return objArrM2;
    }

    public final void i(int i, Object obj, Object[] objArr) {
        int iF = F();
        Object[] objArrM = m(this.r);
        if (iF >= 32) {
            Object[] objArr2 = this.r;
            Object obj2 = objArr2[31];
            AbstractC2219q9.J(i + 1, i, 31, objArr2, objArrM);
            objArrM[i] = obj;
            v(objArr, objArrM, p(obj2));
            return;
        }
        AbstractC2219q9.J(i + 1, i, iF, this.r, objArrM);
        objArrM[i] = obj;
        this.q = objArr;
        this.r = objArrM;
        this.s++;
    }

    @Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable, java.util.List
    public final Iterator iterator() {
        return listIterator(0);
    }

    public final boolean k(Object[] objArr) {
        return objArr.length == 33 && objArr[32] == this.p;
    }

    public final S l(int i) {
        Object[] objArr = this.q;
        if (objArr == null) {
            throw new IllegalStateException("Invalid root");
        }
        int iC = C() >> 5;
        AbstractC2798wI.i(i, iC);
        int i2 = this.f257o;
        return i2 == 0 ? new C0828bf(i, objArr) : new Zg0(objArr, i, iC, i2 / 5);
    }

    @Override // java.util.AbstractList, java.util.List
    public final ListIterator listIterator(int i) {
        AbstractC2798wI.i(i, this.s);
        return new PW(this, i);
    }

    public final Object[] m(Object[] objArr) {
        if (objArr == null) {
            return o();
        }
        if (k(objArr)) {
            return objArr;
        }
        Object[] objArrO = o();
        int length = objArr.length;
        if (length > 32) {
            length = 32;
        }
        AbstractC2219q9.M(0, length, 6, objArr, objArrO);
        return objArrO;
    }

    public final Object[] n(int i, Object[] objArr) {
        if (k(objArr)) {
            System.arraycopy(objArr, 0, objArr, i, 32 - i);
            return objArr;
        }
        Object[] objArrO = o();
        System.arraycopy(objArr, 0, objArrO, i, 32 - i);
        return objArrO;
    }

    public final Object[] o() {
        Object[] objArr = new Object[33];
        objArr[32] = this.p;
        return objArr;
    }

    public final Object[] p(Object obj) {
        Object[] objArr = new Object[33];
        objArr[0] = obj;
        objArr[32] = this.p;
        return objArr;
    }

    public final Object[] q(Object[] objArr, int i, int i2) {
        if (!(i2 >= 0)) {
            AbstractC1102eY.a("shift should be positive");
        }
        if (i2 == 0) {
            return objArr;
        }
        int iD = F3.D(i, i2);
        Object objQ = q((Object[]) objArr[iD], i, i2 - 5);
        if (iD < 31) {
            int i3 = iD + 1;
            if (objArr[i3] != null) {
                if (k(objArr)) {
                    Arrays.fill(objArr, i3, 32, (Object) null);
                }
                Object[] objArrO = o();
                System.arraycopy(objArr, 0, objArrO, 0, i3);
                objArr = objArrO;
            }
        }
        if (objQ == objArr[iD]) {
            return objArr;
        }
        Object[] objArrM = m(objArr);
        objArrM[iD] = objQ;
        return objArrM;
    }

    public final Object[] r(Object[] objArr, int i, int i2, G0 g0) {
        Object[] objArrR;
        int iD = F3.D(i2 - 1, i);
        if (i == 5) {
            g0.m = objArr[iD];
            objArrR = null;
        } else {
            objArrR = r((Object[]) objArr[iD], i - 5, i2, g0);
        }
        if (objArrR == null && iD == 0) {
            return null;
        }
        Object[] objArrM = m(objArr);
        objArrM[iD] = objArrR;
        return objArrM;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean removeAll(Collection collection) {
        return z(new X(1, collection));
    }

    public final void s(Object[] objArr, int i, int i2) {
        if (i2 == 0) {
            this.q = null;
            if (objArr == null) {
                objArr = new Object[0];
            }
            this.r = objArr;
            this.s = i;
            this.f257o = i2;
            return;
        }
        G0 g0 = new G0(5, null);
        Object[] objArrR = r(objArr, i2, i, g0);
        this.r = (Object[]) g0.m;
        this.s = i;
        if (objArrR[1] == null) {
            this.q = (Object[]) objArrR[0];
            this.f257o = i2 - 5;
        } else {
            this.q = objArrR;
            this.f257o = i2;
        }
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object set(int i, Object obj) {
        AbstractC2798wI.h(i, a());
        if (C() > i) {
            G0 g0 = new G0(5, null);
            this.q = D(this.q, this.f257o, i, obj, g0);
            return g0.m;
        }
        Object[] objArrM = m(this.r);
        if (objArrM != this.r) {
            ((AbstractList) this).modCount++;
        }
        int i2 = i & 31;
        Object obj2 = objArrM[i2];
        objArrM[i2] = obj;
        this.r = objArrM;
        return obj2;
    }

    public final Object[] t(Object[] objArr, int i, int i2, Iterator it) {
        if (!it.hasNext()) {
            AbstractC1102eY.a("invalid buffersIterator");
        }
        if (!(i2 >= 0)) {
            AbstractC1102eY.a("negative shift");
        }
        if (i2 == 0) {
            return (Object[]) it.next();
        }
        Object[] objArrM = m(objArr);
        int iD = F3.D(i, i2);
        int i3 = i2 - 5;
        objArrM[iD] = t((Object[]) objArrM[iD], i, i3, it);
        while (true) {
            iD++;
            if (iD >= 32 || !it.hasNext()) {
                break;
            }
            objArrM[iD] = t((Object[]) objArrM[iD], 0, i3, it);
        }
        return objArrM;
    }

    public final Object[] u(Object[] objArr, int i, Object[][] objArr2) {
        N n = new N(1, objArr2);
        int i2 = i >> 5;
        int i3 = this.f257o;
        Object[] objArrT = i2 < (1 << i3) ? t(objArr, i, i3, n) : m(objArr);
        while (n.hasNext()) {
            this.f257o += 5;
            objArrT = p(objArrT);
            int i4 = this.f257o;
            t(objArrT, 1 << i4, i4, n);
        }
        return objArrT;
    }

    public final void v(Object[] objArr, Object[] objArr2, Object[] objArr3) {
        int i = this.s;
        int i2 = i >> 5;
        int i3 = this.f257o;
        if (i2 > (1 << i3)) {
            this.q = w(this.f257o + 5, p(objArr), objArr2);
            this.r = objArr3;
            this.f257o += 5;
            this.s++;
            return;
        }
        if (objArr == null) {
            this.q = objArr2;
            this.r = objArr3;
            this.s = i + 1;
        } else {
            this.q = w(i3, objArr, objArr2);
            this.r = objArr3;
            this.s++;
        }
    }

    public final Object[] w(int i, Object[] objArr, Object[] objArr2) {
        int iD = F3.D(a() - 1, i);
        Object[] objArrM = m(objArr);
        if (i == 5) {
            objArrM[iD] = objArr2;
            return objArrM;
        }
        objArrM[iD] = w(i - 5, (Object[]) objArrM[iD], objArr2);
        return objArrM;
    }

    public final int x(OC oc, Object[] objArr, int i, int i2, G0 g0, ArrayList arrayList, ArrayList arrayList2) {
        if (k(objArr)) {
            arrayList.add(objArr);
        }
        Object[] objArr2 = (Object[]) g0.m;
        Object[] objArrO = objArr2;
        for (int i3 = 0; i3 < i; i3++) {
            Object obj = objArr[i3];
            if (!((Boolean) oc.g(obj)).booleanValue()) {
                if (i2 == 32) {
                    objArrO = !arrayList.isEmpty() ? (Object[]) arrayList.remove(arrayList.size() - 1) : o();
                    i2 = 0;
                }
                objArrO[i2] = obj;
                i2++;
            }
        }
        g0.m = objArrO;
        if (objArr2 != objArrO) {
            arrayList2.add(objArr2);
        }
        return i2;
    }

    public final int y(OC oc, Object[] objArr, int i, G0 g0) {
        Object[] objArrM = objArr;
        int i2 = i;
        boolean z = false;
        for (int i3 = 0; i3 < i; i3++) {
            Object obj = objArr[i3];
            if (((Boolean) oc.g(obj)).booleanValue()) {
                if (!z) {
                    objArrM = m(objArr);
                    z = true;
                    i2 = i3;
                }
            } else if (z) {
                objArrM[i2] = obj;
                i2++;
            }
        }
        g0.m = objArrM;
        return i2;
    }

    public final boolean z(OC oc) {
        int i;
        OC oc2 = oc;
        int iF = F();
        Object[] objArrQ = null;
        G0 g0 = new G0(5, null);
        boolean z = false;
        if (this.q == null) {
            int iY = y(oc2, this.r, iF, g0);
            if (iY == iF) {
                iY = iF;
            } else {
                Object[] objArr = (Object[]) g0.m;
                Arrays.fill(objArr, iY, iF, (Object) null);
                this.r = objArr;
                this.s -= iF - iY;
            }
            if (iY != iF) {
                z = true;
            }
        } else {
            S sL = l(0);
            int iY2 = 32;
            while (iY2 == 32 && sL.hasNext()) {
                iY2 = y(oc2, (Object[]) sL.next(), 32, g0);
            }
            if (iY2 == 32) {
                int iY3 = y(oc2, this.r, iF, g0);
                if (iY3 == iF) {
                    iY3 = iF;
                } else {
                    Object[] objArr2 = (Object[]) g0.m;
                    Arrays.fill(objArr2, iY3, iF, (Object) null);
                    this.r = objArr2;
                    this.s -= iF - iY3;
                }
                if (iY3 == 0) {
                    s(this.q, this.s, this.f257o);
                }
                if (iY3 != iF) {
                }
            } else {
                int i2 = (sL.l - 1) << 5;
                ArrayList arrayList = new ArrayList();
                ArrayList arrayList2 = new ArrayList();
                int iX = iY2;
                while (sL.hasNext()) {
                    iX = x(oc2, (Object[]) sL.next(), 32, iX, g0, arrayList2, arrayList);
                    oc2 = oc;
                }
                int iX2 = x(oc, this.r, iF, iX, g0, arrayList2, arrayList);
                Object[] objArr3 = (Object[]) g0.m;
                Arrays.fill(objArr3, iX2, 32, (Object) null);
                Object[] objArrT = arrayList.isEmpty() ? this.q : t(this.q, i2, this.f257o, arrayList.iterator());
                int size = i2 + (arrayList.size() << 5);
                if ((size & 31) != 0) {
                    AbstractC1102eY.a("invalid size");
                }
                if (size == 0) {
                    this.f257o = 0;
                } else {
                    int i3 = size - 1;
                    while (true) {
                        i = this.f257o;
                        if ((i3 >> i) != 0) {
                            break;
                        }
                        this.f257o = i - 5;
                        objArrT = objArrT[0];
                    }
                    objArrQ = q(objArrT, i3, i);
                }
                this.q = objArrQ;
                this.r = objArr3;
                this.s = size + iX2;
            }
            z = true;
        }
        if (z) {
            ((AbstractList) this).modCount++;
        }
        return z;
    }

    @Override // java.util.AbstractList, java.util.List
    public final ListIterator listIterator() {
        return listIterator(0);
    }

    @Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean add(Object obj) {
        ((AbstractList) this).modCount++;
        int iF = F();
        if (iF < 32) {
            Object[] objArrM = m(this.r);
            objArrM[iF] = obj;
            this.r = objArrM;
            this.s = a() + 1;
        } else {
            v(this.q, this.r, p(obj));
        }
        return true;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean addAll(Collection collection) {
        if (collection.isEmpty()) {
            return false;
        }
        ((AbstractList) this).modCount++;
        int iF = F();
        Iterator it = collection.iterator();
        if (32 - iF >= collection.size()) {
            Object[] objArrM = m(this.r);
            d(objArrM, iF, it);
            this.r = objArrM;
            this.s = collection.size() + this.s;
            return true;
        }
        int size = ((collection.size() + iF) - 1) / 32;
        Object[][] objArr = new Object[size][];
        Object[] objArrM2 = m(this.r);
        d(objArrM2, iF, it);
        objArr[0] = objArrM2;
        for (int i = 1; i < size; i++) {
            Object[] objArrO = o();
            d(objArrO, 0, it);
            objArr[i] = objArrO;
        }
        this.q = u(this.q, C(), objArr);
        Object[] objArrO2 = o();
        d(objArrO2, 0, it);
        this.r = objArrO2;
        this.s = collection.size() + this.s;
        return true;
    }
}
