package o;

import com.google.android.gms.common.api.Api;
import java.lang.reflect.Array;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.f9, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C1172f9 extends V {

    /* renamed from: o, reason: collision with root package name */
    public static final Object[] f523o = new Object[0];
    public int l;
    public Object[] m = f523o;
    public int n;

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

    @Override // java.util.AbstractList, java.util.List
    public final void add(int i, Object obj) {
        int i2 = this.n;
        if (i < 0 || i > i2) {
            throw new IndexOutOfBoundsException(AbstractC0126Ew.i(i, i2, "index: ", ", size: "));
        }
        if (i == i2) {
            addLast(obj);
            return;
        }
        if (i == 0) {
            addFirst(obj);
            return;
        }
        k();
        d(this.n + 1);
        int i3 = i(this.l + i);
        int i4 = this.n;
        if (i < ((i4 + 1) >> 1)) {
            int length = i3 == 0 ? this.m.length - 1 : i3 - 1;
            int i5 = this.l;
            int length2 = i5 == 0 ? this.m.length - 1 : i5 - 1;
            if (length >= i5) {
                Object[] objArr = this.m;
                objArr[length2] = objArr[i5];
                AbstractC2219q9.J(i5, i5 + 1, length + 1, objArr, objArr);
            } else {
                Object[] objArr2 = this.m;
                AbstractC2219q9.J(i5 - 1, i5, objArr2.length, objArr2, objArr2);
                Object[] objArr3 = this.m;
                objArr3[objArr3.length - 1] = objArr3[0];
                AbstractC2219q9.J(0, 1, length + 1, objArr3, objArr3);
            }
            this.m[length] = obj;
            this.l = length2;
        } else {
            int i6 = i(i4 + this.l);
            if (i3 < i6) {
                Object[] objArr4 = this.m;
                AbstractC2219q9.J(i3 + 1, i3, i6, objArr4, objArr4);
            } else {
                Object[] objArr5 = this.m;
                AbstractC2219q9.J(1, 0, i6, objArr5, objArr5);
                Object[] objArr6 = this.m;
                objArr6[0] = objArr6[objArr6.length - 1];
                AbstractC2219q9.J(i3 + 1, i3, objArr6.length - 1, objArr6, objArr6);
            }
            this.m[i3] = obj;
        }
        this.n++;
    }

    @Override // java.util.AbstractList, java.util.List
    public final boolean addAll(int i, Collection collection) {
        int i2 = this.n;
        if (i < 0 || i > i2) {
            throw new IndexOutOfBoundsException(AbstractC0126Ew.i(i, i2, "index: ", ", size: "));
        }
        if (collection.isEmpty()) {
            return false;
        }
        if (i == this.n) {
            return addAll(collection);
        }
        k();
        d(collection.size() + this.n);
        int i3 = i(this.n + this.l);
        int i4 = i(this.l + i);
        int size = collection.size();
        if (i >= ((this.n + 1) >> 1)) {
            int i5 = i4 + size;
            if (i4 < i3) {
                int i6 = size + i3;
                Object[] objArr = this.m;
                if (i6 <= objArr.length) {
                    AbstractC2219q9.J(i5, i4, i3, objArr, objArr);
                } else if (i5 >= objArr.length) {
                    AbstractC2219q9.J(i5 - objArr.length, i4, i3, objArr, objArr);
                } else {
                    int length = i3 - (i6 - objArr.length);
                    AbstractC2219q9.J(0, length, i3, objArr, objArr);
                    Object[] objArr2 = this.m;
                    AbstractC2219q9.J(i5, i4, length, objArr2, objArr2);
                }
            } else {
                Object[] objArr3 = this.m;
                AbstractC2219q9.J(size, 0, i3, objArr3, objArr3);
                Object[] objArr4 = this.m;
                if (i5 >= objArr4.length) {
                    AbstractC2219q9.J(i5 - objArr4.length, i4, objArr4.length, objArr4, objArr4);
                } else {
                    AbstractC2219q9.J(0, objArr4.length - size, objArr4.length, objArr4, objArr4);
                    Object[] objArr5 = this.m;
                    AbstractC2219q9.J(i5, i4, objArr5.length - size, objArr5, objArr5);
                }
            }
            c(i4, collection);
            return true;
        }
        int i7 = this.l;
        int length2 = i7 - size;
        if (i4 < i7) {
            Object[] objArr6 = this.m;
            AbstractC2219q9.J(length2, i7, objArr6.length, objArr6, objArr6);
            if (size >= i4) {
                Object[] objArr7 = this.m;
                AbstractC2219q9.J(objArr7.length - size, 0, i4, objArr7, objArr7);
            } else {
                Object[] objArr8 = this.m;
                AbstractC2219q9.J(objArr8.length - size, 0, size, objArr8, objArr8);
                Object[] objArr9 = this.m;
                AbstractC2219q9.J(0, size, i4, objArr9, objArr9);
            }
        } else if (length2 >= 0) {
            Object[] objArr10 = this.m;
            AbstractC2219q9.J(length2, i7, i4, objArr10, objArr10);
        } else {
            Object[] objArr11 = this.m;
            length2 += objArr11.length;
            int i8 = i4 - i7;
            int length3 = objArr11.length - length2;
            if (length3 >= i8) {
                AbstractC2219q9.J(length2, i7, i4, objArr11, objArr11);
            } else {
                AbstractC2219q9.J(length2, i7, i7 + length3, objArr11, objArr11);
                Object[] objArr12 = this.m;
                AbstractC2219q9.J(0, this.l + length3, i4, objArr12, objArr12);
            }
        }
        this.l = length2;
        c(f(i4 - size), collection);
        return true;
    }

    public final void addFirst(Object obj) {
        k();
        d(this.n + 1);
        int length = this.l;
        if (length == 0) {
            length = this.m.length;
        }
        int i = length - 1;
        this.l = i;
        this.m[i] = obj;
        this.n++;
    }

    public final void addLast(Object obj) {
        k();
        d(a() + 1);
        this.m[i(a() + this.l)] = obj;
        this.n = a() + 1;
    }

    @Override // o.V
    public final Object b(int i) {
        int i2 = this.n;
        if (i < 0 || i >= i2) {
            throw new IndexOutOfBoundsException(AbstractC0126Ew.i(i, i2, "index: ", ", size: "));
        }
        if (i == AbstractC0008Ai.R(this)) {
            return removeLast();
        }
        if (i == 0) {
            return removeFirst();
        }
        k();
        int i3 = i(this.l + i);
        Object[] objArr = this.m;
        Object obj = objArr[i3];
        if (i < (this.n >> 1)) {
            int i4 = this.l;
            if (i3 >= i4) {
                AbstractC2219q9.J(i4 + 1, i4, i3, objArr, objArr);
            } else {
                AbstractC2219q9.J(1, 0, i3, objArr, objArr);
                Object[] objArr2 = this.m;
                objArr2[0] = objArr2[objArr2.length - 1];
                int i5 = this.l;
                AbstractC2219q9.J(i5 + 1, i5, objArr2.length - 1, objArr2, objArr2);
            }
            Object[] objArr3 = this.m;
            int i6 = this.l;
            objArr3[i6] = null;
            this.l = e(i6);
        } else {
            int i7 = i(AbstractC0008Ai.R(this) + this.l);
            if (i3 <= i7) {
                Object[] objArr4 = this.m;
                AbstractC2219q9.J(i3, i3 + 1, i7 + 1, objArr4, objArr4);
            } else {
                Object[] objArr5 = this.m;
                AbstractC2219q9.J(i3, i3 + 1, objArr5.length, objArr5, objArr5);
                Object[] objArr6 = this.m;
                objArr6[objArr6.length - 1] = objArr6[0];
                AbstractC2219q9.J(0, 1, i7 + 1, objArr6, objArr6);
            }
            this.m[i7] = null;
        }
        this.n--;
        return obj;
    }

    public final void c(int i, Collection collection) {
        Iterator it = collection.iterator();
        int length = this.m.length;
        while (i < length && it.hasNext()) {
            this.m[i] = it.next();
            i++;
        }
        int i2 = this.l;
        for (int i3 = 0; i3 < i2 && it.hasNext(); i3++) {
            this.m[i3] = it.next();
        }
        this.n = collection.size() + this.n;
    }

    @Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
    public final void clear() {
        if (!isEmpty()) {
            k();
            h(this.l, i(a() + this.l));
        }
        this.l = 0;
        this.n = 0;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean contains(Object obj) {
        return indexOf(obj) != -1;
    }

    public final void d(int i) {
        if (i < 0) {
            throw new IllegalStateException("Deque is too big.");
        }
        Object[] objArr = this.m;
        if (i <= objArr.length) {
            return;
        }
        if (objArr == f523o) {
            if (i < 10) {
                i = 10;
            }
            this.m = new Object[i];
            return;
        }
        int length = objArr.length;
        int i2 = length + (length >> 1);
        if (i2 - i < 0) {
            i2 = i;
        }
        if (i2 - 2147483639 > 0) {
            i2 = i > 2147483639 ? Api.BaseClientBuilder.API_PRIORITY_OTHER : 2147483639;
        }
        Object[] objArr2 = new Object[i2];
        AbstractC2219q9.J(0, this.l, objArr.length, objArr, objArr2);
        Object[] objArr3 = this.m;
        int length2 = objArr3.length;
        int i3 = this.l;
        AbstractC2219q9.J(length2 - i3, 0, i3, objArr3, objArr2);
        this.l = 0;
        this.m = objArr2;
    }

    public final int e(int i) {
        if (i == this.m.length - 1) {
            return 0;
        }
        return i + 1;
    }

    public final int f(int i) {
        return i < 0 ? i + this.m.length : i;
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object get(int i) {
        int iA = a();
        if (i < 0 || i >= iA) {
            throw new IndexOutOfBoundsException(AbstractC0126Ew.i(i, iA, "index: ", ", size: "));
        }
        return this.m[i(this.l + i)];
    }

    public final void h(int i, int i2) {
        if (i < i2) {
            Arrays.fill(this.m, i, i2, (Object) null);
            return;
        }
        Object[] objArr = this.m;
        Arrays.fill(objArr, i, objArr.length, (Object) null);
        Arrays.fill(this.m, 0, i2, (Object) null);
    }

    public final int i(int i) {
        Object[] objArr = this.m;
        return i >= objArr.length ? i - objArr.length : i;
    }

    @Override // java.util.AbstractList, java.util.List
    public final int indexOf(Object obj) {
        int i;
        int i2 = i(a() + this.l);
        int length = this.l;
        if (length < i2) {
            while (length < i2) {
                if (F3.c(obj, this.m[length])) {
                    i = this.l;
                } else {
                    length++;
                }
            }
            return -1;
        }
        if (length < i2) {
            return -1;
        }
        int length2 = this.m.length;
        while (true) {
            if (length >= length2) {
                for (int i3 = 0; i3 < i2; i3++) {
                    if (F3.c(obj, this.m[i3])) {
                        length = i3 + this.m.length;
                        i = this.l;
                    }
                }
                return -1;
            }
            if (F3.c(obj, this.m[length])) {
                i = this.l;
                break;
            }
            length++;
        }
        return length - i;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean isEmpty() {
        return a() == 0;
    }

    public final void k() {
        ((AbstractList) this).modCount++;
    }

    public final Object last() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        return this.m[i(AbstractC0008Ai.R(this) + this.l)];
    }

    @Override // java.util.AbstractList, java.util.List
    public final int lastIndexOf(Object obj) {
        int length;
        int i;
        int i2 = i(a() + this.l);
        int i3 = this.l;
        if (i3 < i2) {
            length = i2 - 1;
            if (i3 <= length) {
                while (!F3.c(obj, this.m[length])) {
                    if (length != i3) {
                        length--;
                    }
                }
                i = this.l;
                return length - i;
            }
            return -1;
        }
        if (i3 > i2) {
            int i4 = i2 - 1;
            while (true) {
                if (-1 >= i4) {
                    length = this.m.length - 1;
                    int i5 = this.l;
                    if (i5 <= length) {
                        while (!F3.c(obj, this.m[length])) {
                            if (length != i5) {
                                length--;
                            }
                        }
                        i = this.l;
                    }
                } else {
                    if (F3.c(obj, this.m[i4])) {
                        length = i4 + this.m.length;
                        i = this.l;
                        break;
                    }
                    i4--;
                }
            }
        }
        return -1;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean remove(Object obj) {
        int iIndexOf = indexOf(obj);
        if (iIndexOf == -1) {
            return false;
        }
        b(iIndexOf);
        return true;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean removeAll(Collection collection) {
        int i;
        boolean z = false;
        z = false;
        z = false;
        if (!isEmpty() && this.m.length != 0) {
            int i2 = i(a() + this.l);
            int i3 = this.l;
            if (i3 < i2) {
                i = i3;
                while (i3 < i2) {
                    Object obj = this.m[i3];
                    if (collection.contains(obj)) {
                        z = true;
                    } else {
                        this.m[i] = obj;
                        i++;
                    }
                    i3++;
                }
                Arrays.fill(this.m, i, i2, (Object) null);
            } else {
                int length = this.m.length;
                boolean z2 = false;
                int i4 = i3;
                while (i3 < length) {
                    Object[] objArr = this.m;
                    Object obj2 = objArr[i3];
                    objArr[i3] = null;
                    if (collection.contains(obj2)) {
                        z2 = true;
                    } else {
                        this.m[i4] = obj2;
                        i4++;
                    }
                    i3++;
                }
                i = i(i4);
                for (int i5 = 0; i5 < i2; i5++) {
                    Object[] objArr2 = this.m;
                    Object obj3 = objArr2[i5];
                    objArr2[i5] = null;
                    if (collection.contains(obj3)) {
                        z2 = true;
                    } else {
                        this.m[i] = obj3;
                        i = e(i);
                    }
                }
                z = z2;
            }
            if (z) {
                k();
                this.n = f(i - this.l);
            }
        }
        return z;
    }

    public final Object removeFirst() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        k();
        Object[] objArr = this.m;
        int i = this.l;
        Object obj = objArr[i];
        objArr[i] = null;
        this.l = e(i);
        this.n = a() - 1;
        return obj;
    }

    public final Object removeLast() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        k();
        int i = i(AbstractC0008Ai.R(this) + this.l);
        Object[] objArr = this.m;
        Object obj = objArr[i];
        objArr[i] = null;
        this.n = a() - 1;
        return obj;
    }

    @Override // java.util.AbstractList
    public final void removeRange(int i, int i2) {
        AbstractC0752ap.x(i, i2, this.n);
        int i3 = i2 - i;
        if (i3 == 0) {
            return;
        }
        if (i3 == this.n) {
            clear();
            return;
        }
        if (i3 == 1) {
            b(i);
            return;
        }
        k();
        if (i < this.n - i2) {
            int i4 = i((i - 1) + this.l);
            int i5 = i((i2 - 1) + this.l);
            while (i > 0) {
                int i6 = i4 + 1;
                int iMin = Math.min(i, Math.min(i6, i5 + 1));
                Object[] objArr = this.m;
                int i7 = i5 - iMin;
                int i8 = i4 - iMin;
                AbstractC2219q9.J(i7 + 1, i8 + 1, i6, objArr, objArr);
                i4 = f(i8);
                i5 = f(i7);
                i -= iMin;
            }
            int i9 = i(this.l + i3);
            h(this.l, i9);
            this.l = i9;
        } else {
            int i10 = i(this.l + i2);
            int i11 = i(this.l + i);
            int i12 = this.n;
            while (true) {
                i12 -= i2;
                if (i12 <= 0) {
                    break;
                }
                Object[] objArr2 = this.m;
                i2 = Math.min(i12, Math.min(objArr2.length - i10, objArr2.length - i11));
                Object[] objArr3 = this.m;
                int i13 = i10 + i2;
                AbstractC2219q9.J(i11, i10, i13, objArr3, objArr3);
                i10 = i(i13);
                i11 = i(i11 + i2);
            }
            int i14 = i(this.n + this.l);
            h(f(i14 - i3), i14);
        }
        this.n -= i3;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean retainAll(Collection collection) {
        int i;
        boolean z = false;
        z = false;
        z = false;
        if (!isEmpty() && this.m.length != 0) {
            int i2 = i(a() + this.l);
            int i3 = this.l;
            if (i3 < i2) {
                i = i3;
                while (i3 < i2) {
                    Object obj = this.m[i3];
                    if (collection.contains(obj)) {
                        this.m[i] = obj;
                        i++;
                    } else {
                        z = true;
                    }
                    i3++;
                }
                Arrays.fill(this.m, i, i2, (Object) null);
            } else {
                int length = this.m.length;
                boolean z2 = false;
                int i4 = i3;
                while (i3 < length) {
                    Object[] objArr = this.m;
                    Object obj2 = objArr[i3];
                    objArr[i3] = null;
                    if (collection.contains(obj2)) {
                        this.m[i4] = obj2;
                        i4++;
                    } else {
                        z2 = true;
                    }
                    i3++;
                }
                i = i(i4);
                for (int i5 = 0; i5 < i2; i5++) {
                    Object[] objArr2 = this.m;
                    Object obj3 = objArr2[i5];
                    objArr2[i5] = null;
                    if (collection.contains(obj3)) {
                        this.m[i] = obj3;
                        i = e(i);
                    } else {
                        z2 = true;
                    }
                }
                z = z2;
            }
            if (z) {
                k();
                this.n = f(i - this.l);
            }
        }
        return z;
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object set(int i, Object obj) {
        int iA = a();
        if (i < 0 || i >= iA) {
            throw new IndexOutOfBoundsException(AbstractC0126Ew.i(i, iA, "index: ", ", size: "));
        }
        int i2 = i(this.l + i);
        Object[] objArr = this.m;
        Object obj2 = objArr[i2];
        objArr[i2] = obj;
        return obj2;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final Object[] toArray() {
        return toArray(new Object[a()]);
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final Object[] toArray(Object[] objArr) {
        int length = objArr.length;
        int i = this.n;
        if (length < i) {
            objArr = (Object[]) Array.newInstance(objArr.getClass().getComponentType(), i);
        }
        int i2 = i(this.n + this.l);
        int i3 = this.l;
        if (i3 < i2) {
            AbstractC2219q9.M(i3, i2, 2, this.m, objArr);
        } else if (!isEmpty()) {
            Object[] objArr2 = this.m;
            AbstractC2219q9.J(0, this.l, objArr2.length, objArr2, objArr);
            Object[] objArr3 = this.m;
            AbstractC2219q9.J(objArr3.length - this.l, 0, i2, objArr3, objArr);
        }
        int i4 = this.n;
        if (i4 < objArr.length) {
            objArr[i4] = null;
        }
        return objArr;
    }

    @Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean add(Object obj) {
        addLast(obj);
        return true;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean addAll(Collection collection) {
        if (collection.isEmpty()) {
            return false;
        }
        k();
        d(collection.size() + a());
        c(i(a() + this.l), collection);
        return true;
    }
}
