package kotlin.collections;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;

/* JADX INFO: loaded from: classes.dex */
public final class h extends e {

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f29213a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Object[] f29214b = f29212d;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f29215c;

    @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) {
        nb.g.i(collection, "elements");
        if (collection.isEmpty()) {
            return false;
        }
        i(collection.size() + getLength());
        h(k(getLength() + this.f29213a), collection);
        return true;
    }

    public final void addFirst(Object obj) {
        i(this.f29215c + 1);
        int length = this.f29213a;
        if (length == 0) {
            Object[] objArr = this.f29214b;
            nb.g.i(objArr, "<this>");
            length = objArr.length;
        }
        int i10 = length - 1;
        this.f29213a = i10;
        this.f29214b[i10] = obj;
        this.f29215c++;
    }

    public final void addLast(Object obj) {
        i(getLength() + 1);
        this.f29214b[k(getLength() + this.f29213a)] = obj;
        this.f29215c = getLength() + 1;
    }

    @Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
    public final void clear() {
        int iK = k(this.f29215c + this.f29213a);
        int i10 = this.f29213a;
        if (i10 < iK) {
            Object[] objArr = this.f29214b;
            nb.g.i(objArr, "<this>");
            Arrays.fill(objArr, i10, iK, (Object) null);
        } else if (!isEmpty()) {
            Object[] objArr2 = this.f29214b;
            Arrays.fill(objArr2, this.f29213a, objArr2.length, (Object) null);
            Object[] objArr3 = this.f29214b;
            nb.g.i(objArr3, "<this>");
            Arrays.fill(objArr3, 0, iK, (Object) null);
        }
        this.f29213a = 0;
        this.f29215c = 0;
    }

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

    @Override // kotlin.collections.e
    /* JADX INFO: renamed from: f */
    public final int getLength() {
        return this.f29215c;
    }

    @Override // kotlin.collections.e
    public final Object g(int i10) {
        a aVar = d.Companion;
        int i11 = this.f29215c;
        aVar.getClass();
        a.a(i10, i11);
        if (i10 == hb.a.b0(this)) {
            return removeLast();
        }
        if (i10 == 0) {
            return removeFirst();
        }
        int iK = k(this.f29213a + i10);
        Object[] objArr = this.f29214b;
        Object obj = objArr[iK];
        if (i10 < (this.f29215c >> 1)) {
            int i12 = this.f29213a;
            if (iK >= i12) {
                q.R(objArr, i12 + 1, objArr, i12, iK);
            } else {
                q.R(objArr, 1, objArr, 0, iK);
                Object[] objArr2 = this.f29214b;
                objArr2[0] = objArr2[objArr2.length - 1];
                int i13 = this.f29213a;
                q.R(objArr2, i13 + 1, objArr2, i13, objArr2.length - 1);
            }
            Object[] objArr3 = this.f29214b;
            int i14 = this.f29213a;
            objArr3[i14] = null;
            this.f29213a = j(i14);
        } else {
            int iK2 = k(hb.a.b0(this) + this.f29213a);
            if (iK <= iK2) {
                Object[] objArr4 = this.f29214b;
                q.R(objArr4, iK, objArr4, iK + 1, iK2 + 1);
            } else {
                Object[] objArr5 = this.f29214b;
                q.R(objArr5, iK, objArr5, iK + 1, objArr5.length);
                Object[] objArr6 = this.f29214b;
                objArr6[objArr6.length - 1] = objArr6[0];
                q.R(objArr6, 0, objArr6, 1, iK2 + 1);
            }
            this.f29214b[iK2] = null;
        }
        this.f29215c--;
        return obj;
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object get(int i10) {
        a aVar = d.Companion;
        int i11 = this.f29215c;
        aVar.getClass();
        a.a(i10, i11);
        return this.f29214b[k(this.f29213a + i10)];
    }

    public final void h(int i10, Collection collection) {
        Iterator it = collection.iterator();
        int length = this.f29214b.length;
        while (i10 < length && it.hasNext()) {
            this.f29214b[i10] = it.next();
            i10++;
        }
        int i11 = this.f29213a;
        for (int i12 = 0; i12 < i11 && it.hasNext(); i12++) {
            this.f29214b[i12] = it.next();
        }
        this.f29215c = collection.size() + getLength();
    }

    public final void i(int i10) {
        if (i10 < 0) {
            throw new IllegalStateException("Deque is too big.");
        }
        Object[] objArr = this.f29214b;
        if (i10 <= objArr.length) {
            return;
        }
        if (objArr == f29212d) {
            if (i10 < 10) {
                i10 = 10;
            }
            this.f29214b = new Object[i10];
            return;
        }
        int length = objArr.length;
        int i11 = length + (length >> 1);
        if (i11 - i10 < 0) {
            i11 = i10;
        }
        if (i11 - 2147483639 > 0) {
            i11 = i10 > 2147483639 ? Integer.MAX_VALUE : 2147483639;
        }
        Object[] objArr2 = new Object[i11];
        q.R(objArr, 0, objArr2, this.f29213a, objArr.length);
        Object[] objArr3 = this.f29214b;
        int length2 = objArr3.length;
        int i12 = this.f29213a;
        q.R(objArr3, length2 - i12, objArr2, 0, i12);
        this.f29213a = 0;
        this.f29214b = objArr2;
    }

    @Override // java.util.AbstractList, java.util.List
    public final int indexOf(Object obj) {
        int i10;
        int iK = k(getLength() + this.f29213a);
        int length = this.f29213a;
        if (length < iK) {
            while (length < iK) {
                if (nb.g.c(obj, this.f29214b[length])) {
                    i10 = this.f29213a;
                } else {
                    length++;
                }
            }
            return -1;
        }
        if (length < iK) {
            return -1;
        }
        int length2 = this.f29214b.length;
        while (true) {
            if (length >= length2) {
                for (int i11 = 0; i11 < iK; i11++) {
                    if (nb.g.c(obj, this.f29214b[i11])) {
                        length = i11 + this.f29214b.length;
                        i10 = this.f29213a;
                    }
                }
                return -1;
            }
            if (nb.g.c(obj, this.f29214b[length])) {
                i10 = this.f29213a;
                break;
            }
            length++;
        }
        return length - i10;
    }

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

    public final int j(int i10) {
        nb.g.i(this.f29214b, "<this>");
        if (i10 == r0.length - 1) {
            return 0;
        }
        return i10 + 1;
    }

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

    @Override // java.util.AbstractList, java.util.List
    public final int lastIndexOf(Object obj) {
        int length;
        int i10;
        int iK = k(this.f29215c + this.f29213a);
        int i11 = this.f29213a;
        if (i11 < iK) {
            length = iK - 1;
            if (i11 <= length) {
                while (!nb.g.c(obj, this.f29214b[length])) {
                    if (length != i11) {
                        length--;
                    }
                }
                i10 = this.f29213a;
                return length - i10;
            }
            return -1;
        }
        if (i11 > iK) {
            int i12 = iK - 1;
            while (true) {
                if (-1 >= i12) {
                    Object[] objArr = this.f29214b;
                    nb.g.i(objArr, "<this>");
                    length = objArr.length - 1;
                    int i13 = this.f29213a;
                    if (i13 <= length) {
                        while (!nb.g.c(obj, this.f29214b[length])) {
                            if (length != i13) {
                                length--;
                            }
                        }
                        i10 = this.f29213a;
                    }
                } else {
                    if (nb.g.c(obj, this.f29214b[i12])) {
                        length = i12 + this.f29214b.length;
                        i10 = this.f29213a;
                        break;
                    }
                    i12--;
                }
            }
        }
        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;
        }
        g(iIndexOf);
        return true;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean removeAll(Collection collection) {
        int iK;
        nb.g.i(collection, "elements");
        boolean z10 = false;
        z10 = false;
        z10 = false;
        if (!isEmpty() && this.f29214b.length != 0) {
            int iK2 = k(this.f29215c + this.f29213a);
            int i10 = this.f29213a;
            if (i10 < iK2) {
                iK = i10;
                while (i10 < iK2) {
                    Object obj = this.f29214b[i10];
                    if (!collection.contains(obj)) {
                        this.f29214b[iK] = obj;
                        iK++;
                    } else {
                        z10 = true;
                    }
                    i10++;
                }
                Object[] objArr = this.f29214b;
                nb.g.i(objArr, "<this>");
                Arrays.fill(objArr, iK, iK2, (Object) null);
            } else {
                int length = this.f29214b.length;
                boolean z11 = false;
                int i11 = i10;
                while (i10 < length) {
                    Object[] objArr2 = this.f29214b;
                    Object obj2 = objArr2[i10];
                    objArr2[i10] = null;
                    if (!collection.contains(obj2)) {
                        this.f29214b[i11] = obj2;
                        i11++;
                    } else {
                        z11 = true;
                    }
                    i10++;
                }
                iK = k(i11);
                for (int i12 = 0; i12 < iK2; i12++) {
                    Object[] objArr3 = this.f29214b;
                    Object obj3 = objArr3[i12];
                    objArr3[i12] = null;
                    if (!collection.contains(obj3)) {
                        this.f29214b[iK] = obj3;
                        iK = j(iK);
                    } else {
                        z11 = true;
                    }
                }
                z10 = z11;
            }
            if (z10) {
                int length2 = iK - this.f29213a;
                if (length2 < 0) {
                    length2 += this.f29214b.length;
                }
                this.f29215c = length2;
            }
        }
        return z10;
    }

    public final Object removeFirst() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        Object[] objArr = this.f29214b;
        int i10 = this.f29213a;
        Object obj = objArr[i10];
        objArr[i10] = null;
        this.f29213a = j(i10);
        this.f29215c = getLength() - 1;
        return obj;
    }

    public final Object removeLast() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        int iK = k(hb.a.b0(this) + this.f29213a);
        Object[] objArr = this.f29214b;
        Object obj = objArr[iK];
        objArr[iK] = null;
        this.f29215c = getLength() - 1;
        return obj;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean retainAll(Collection collection) {
        int iK;
        nb.g.i(collection, "elements");
        boolean z10 = false;
        z10 = false;
        z10 = false;
        if (!isEmpty() && this.f29214b.length != 0) {
            int iK2 = k(this.f29215c + this.f29213a);
            int i10 = this.f29213a;
            if (i10 < iK2) {
                iK = i10;
                while (i10 < iK2) {
                    Object obj = this.f29214b[i10];
                    if (collection.contains(obj)) {
                        this.f29214b[iK] = obj;
                        iK++;
                    } else {
                        z10 = true;
                    }
                    i10++;
                }
                Object[] objArr = this.f29214b;
                nb.g.i(objArr, "<this>");
                Arrays.fill(objArr, iK, iK2, (Object) null);
            } else {
                int length = this.f29214b.length;
                boolean z11 = false;
                int i11 = i10;
                while (i10 < length) {
                    Object[] objArr2 = this.f29214b;
                    Object obj2 = objArr2[i10];
                    objArr2[i10] = null;
                    if (collection.contains(obj2)) {
                        this.f29214b[i11] = obj2;
                        i11++;
                    } else {
                        z11 = true;
                    }
                    i10++;
                }
                iK = k(i11);
                for (int i12 = 0; i12 < iK2; i12++) {
                    Object[] objArr3 = this.f29214b;
                    Object obj3 = objArr3[i12];
                    objArr3[i12] = null;
                    if (collection.contains(obj3)) {
                        this.f29214b[iK] = obj3;
                        iK = j(iK);
                    } else {
                        z11 = true;
                    }
                }
                z10 = z11;
            }
            if (z10) {
                int length2 = iK - this.f29213a;
                if (length2 < 0) {
                    length2 += this.f29214b.length;
                }
                this.f29215c = length2;
            }
        }
        return z10;
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object set(int i10, Object obj) {
        a aVar = d.Companion;
        int i11 = this.f29215c;
        aVar.getClass();
        a.a(i10, i11);
        int iK = k(this.f29213a + i10);
        Object[] objArr = this.f29214b;
        Object obj2 = objArr[iK];
        objArr[iK] = obj;
        return obj2;
    }

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

    @Override // java.util.AbstractList, java.util.List
    public final void add(int i10, Object obj) {
        int length;
        a aVar = d.Companion;
        int i11 = this.f29215c;
        aVar.getClass();
        a.b(i10, i11);
        int i12 = this.f29215c;
        if (i10 == i12) {
            addLast(obj);
            return;
        }
        if (i10 == 0) {
            addFirst(obj);
            return;
        }
        i(i12 + 1);
        int iK = k(this.f29213a + i10);
        int i13 = this.f29215c;
        if (i10 < ((i13 + 1) >> 1)) {
            if (iK == 0) {
                Object[] objArr = this.f29214b;
                nb.g.i(objArr, "<this>");
                iK = objArr.length;
            }
            int i14 = iK - 1;
            int i15 = this.f29213a;
            if (i15 == 0) {
                Object[] objArr2 = this.f29214b;
                nb.g.i(objArr2, "<this>");
                length = objArr2.length - 1;
            } else {
                length = i15 - 1;
            }
            int i16 = this.f29213a;
            if (i14 >= i16) {
                Object[] objArr3 = this.f29214b;
                objArr3[length] = objArr3[i16];
                q.R(objArr3, i16, objArr3, i16 + 1, i14 + 1);
            } else {
                Object[] objArr4 = this.f29214b;
                q.R(objArr4, i16 - 1, objArr4, i16, objArr4.length);
                Object[] objArr5 = this.f29214b;
                objArr5[objArr5.length - 1] = objArr5[0];
                q.R(objArr5, 0, objArr5, 1, i14 + 1);
            }
            this.f29214b[i14] = obj;
            this.f29213a = length;
        } else {
            int iK2 = k(i13 + this.f29213a);
            if (iK < iK2) {
                Object[] objArr6 = this.f29214b;
                q.R(objArr6, iK + 1, objArr6, iK, iK2);
            } else {
                Object[] objArr7 = this.f29214b;
                q.R(objArr7, 1, objArr7, 0, iK2);
                Object[] objArr8 = this.f29214b;
                objArr8[0] = objArr8[objArr8.length - 1];
                q.R(objArr8, iK + 1, objArr8, iK, objArr8.length - 1);
            }
            this.f29214b[iK] = obj;
        }
        this.f29215c++;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final Object[] toArray(Object[] objArr) {
        nb.g.i(objArr, "array");
        int length = objArr.length;
        int i10 = this.f29215c;
        if (length < i10) {
            Object objNewInstance = Array.newInstance(objArr.getClass().getComponentType(), i10);
            nb.g.g(objNewInstance, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.arrayOfNulls>");
            objArr = (Object[]) objNewInstance;
        }
        int iK = k(this.f29215c + this.f29213a);
        int i11 = this.f29213a;
        if (i11 < iK) {
            q.S(this.f29214b, objArr, i11, iK, 2);
        } else if (!isEmpty()) {
            Object[] objArr2 = this.f29214b;
            q.R(objArr2, 0, objArr, this.f29213a, objArr2.length);
            Object[] objArr3 = this.f29214b;
            q.R(objArr3, objArr3.length - this.f29213a, objArr, 0, iK);
        }
        int length2 = objArr.length;
        int i12 = this.f29215c;
        if (length2 > i12) {
            objArr[i12] = null;
        }
        return objArr;
    }

    @Override // java.util.AbstractList, java.util.List
    public final boolean addAll(int i10, Collection collection) {
        nb.g.i(collection, "elements");
        a aVar = d.Companion;
        int i11 = this.f29215c;
        aVar.getClass();
        a.b(i10, i11);
        if (collection.isEmpty()) {
            return false;
        }
        int i12 = this.f29215c;
        if (i10 == i12) {
            return addAll(collection);
        }
        i(collection.size() + i12);
        int iK = k(this.f29215c + this.f29213a);
        int iK2 = k(this.f29213a + i10);
        int size = collection.size();
        if (i10 < ((this.f29215c + 1) >> 1)) {
            int i13 = this.f29213a;
            int length = i13 - size;
            if (iK2 < i13) {
                Object[] objArr = this.f29214b;
                q.R(objArr, length, objArr, i13, objArr.length);
                if (size >= iK2) {
                    Object[] objArr2 = this.f29214b;
                    q.R(objArr2, objArr2.length - size, objArr2, 0, iK2);
                } else {
                    Object[] objArr3 = this.f29214b;
                    q.R(objArr3, objArr3.length - size, objArr3, 0, size);
                    Object[] objArr4 = this.f29214b;
                    q.R(objArr4, 0, objArr4, size, iK2);
                }
            } else if (length >= 0) {
                Object[] objArr5 = this.f29214b;
                q.R(objArr5, length, objArr5, i13, iK2);
            } else {
                Object[] objArr6 = this.f29214b;
                length += objArr6.length;
                int i14 = iK2 - i13;
                int length2 = objArr6.length - length;
                if (length2 >= i14) {
                    q.R(objArr6, length, objArr6, i13, iK2);
                } else {
                    q.R(objArr6, length, objArr6, i13, i13 + length2);
                    Object[] objArr7 = this.f29214b;
                    q.R(objArr7, 0, objArr7, this.f29213a + length2, iK2);
                }
            }
            this.f29213a = length;
            int length3 = iK2 - size;
            if (length3 < 0) {
                length3 += this.f29214b.length;
            }
            h(length3, collection);
        } else {
            int i15 = iK2 + size;
            if (iK2 < iK) {
                int i16 = size + iK;
                Object[] objArr8 = this.f29214b;
                if (i16 <= objArr8.length) {
                    q.R(objArr8, i15, objArr8, iK2, iK);
                } else if (i15 >= objArr8.length) {
                    q.R(objArr8, i15 - objArr8.length, objArr8, iK2, iK);
                } else {
                    int length4 = iK - (i16 - objArr8.length);
                    q.R(objArr8, 0, objArr8, length4, iK);
                    Object[] objArr9 = this.f29214b;
                    q.R(objArr9, i15, objArr9, iK2, length4);
                }
            } else {
                Object[] objArr10 = this.f29214b;
                q.R(objArr10, size, objArr10, 0, iK);
                Object[] objArr11 = this.f29214b;
                if (i15 >= objArr11.length) {
                    q.R(objArr11, i15 - objArr11.length, objArr11, iK2, objArr11.length);
                } else {
                    q.R(objArr11, 0, objArr11, objArr11.length - size, objArr11.length);
                    Object[] objArr12 = this.f29214b;
                    q.R(objArr12, i15, objArr12, iK2, objArr12.length - size);
                }
            }
            h(iK2, collection);
        }
        return true;
    }
}
