package q;

import java.util.Arrays;
import r.AbstractC1481a;

/* JADX INFO: loaded from: classes.dex */
public final class g implements Cloneable {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public /* synthetic */ boolean f16182q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public /* synthetic */ long[] f16183r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public /* synthetic */ Object[] f16184s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public /* synthetic */ int f16185t;

    public g() {
        int i;
        int i5 = 4;
        while (true) {
            i = 80;
            if (i5 >= 32) {
                break;
            }
            int i8 = (1 << i5) - 12;
            if (80 <= i8) {
                i = i8;
                break;
            }
            i5++;
        }
        int i9 = i / 8;
        this.f16183r = new long[i9];
        this.f16184s = new Object[i9];
    }

    public final void c(long j8, Long l8) {
        int i = this.f16185t;
        if (i != 0 && j8 <= this.f16183r[i - 1]) {
            i(j8, l8);
            return;
        }
        if (this.f16182q) {
            long[] jArr = this.f16183r;
            if (i >= jArr.length) {
                Object[] objArr = this.f16184s;
                int i5 = 0;
                for (int i8 = 0; i8 < i; i8++) {
                    Object obj = objArr[i8];
                    if (obj != h.f16186a) {
                        if (i8 != i5) {
                            jArr[i5] = jArr[i8];
                            objArr[i5] = obj;
                            objArr[i8] = null;
                        }
                        i5++;
                    }
                }
                this.f16182q = false;
                this.f16185t = i5;
            }
        }
        int i9 = this.f16185t;
        if (i9 >= this.f16183r.length) {
            int i10 = (i9 + 1) * 8;
            int i11 = 4;
            while (true) {
                if (i11 >= 32) {
                    break;
                }
                int i12 = (1 << i11) - 12;
                if (i10 <= i12) {
                    i10 = i12;
                    break;
                }
                i11++;
            }
            int i13 = i10 / 8;
            long[] jArrCopyOf = Arrays.copyOf(this.f16183r, i13);
            P6.g.d(jArrCopyOf, "copyOf(this, newSize)");
            this.f16183r = jArrCopyOf;
            Object[] objArrCopyOf = Arrays.copyOf(this.f16184s, i13);
            P6.g.d(objArrCopyOf, "copyOf(this, newSize)");
            this.f16184s = objArrCopyOf;
        }
        this.f16183r[i9] = j8;
        this.f16184s[i9] = l8;
        this.f16185t = i9 + 1;
    }

    public final void d() {
        int i = this.f16185t;
        Object[] objArr = this.f16184s;
        for (int i5 = 0; i5 < i; i5++) {
            objArr[i5] = null;
        }
        this.f16185t = 0;
        this.f16182q = false;
    }

    /* JADX INFO: renamed from: e, reason: merged with bridge method [inline-methods] */
    public final g clone() throws CloneNotSupportedException {
        Object objClone = super.clone();
        P6.g.c(objClone, "null cannot be cast to non-null type androidx.collection.LongSparseArray<E of androidx.collection.LongSparseArray>");
        g gVar = (g) objClone;
        gVar.f16183r = (long[]) this.f16183r.clone();
        gVar.f16184s = (Object[]) this.f16184s.clone();
        return gVar;
    }

    public final Object f(long j8) {
        Object obj;
        int iB = AbstractC1481a.b(this.f16183r, this.f16185t, j8);
        if (iB < 0 || (obj = this.f16184s[iB]) == h.f16186a) {
            return null;
        }
        return obj;
    }

    public final Object h(long j8) {
        Object obj;
        int iB = AbstractC1481a.b(this.f16183r, this.f16185t, j8);
        if (iB < 0 || (obj = this.f16184s[iB]) == h.f16186a) {
            return -1L;
        }
        return obj;
    }

    public final void i(long j8, Object obj) {
        int iB = AbstractC1481a.b(this.f16183r, this.f16185t, j8);
        if (iB >= 0) {
            this.f16184s[iB] = obj;
            return;
        }
        int i = ~iB;
        int i5 = this.f16185t;
        Object obj2 = h.f16186a;
        if (i < i5) {
            Object[] objArr = this.f16184s;
            if (objArr[i] == obj2) {
                this.f16183r[i] = j8;
                objArr[i] = obj;
                return;
            }
        }
        if (this.f16182q) {
            long[] jArr = this.f16183r;
            if (i5 >= jArr.length) {
                Object[] objArr2 = this.f16184s;
                int i8 = 0;
                for (int i9 = 0; i9 < i5; i9++) {
                    Object obj3 = objArr2[i9];
                    if (obj3 != obj2) {
                        if (i9 != i8) {
                            jArr[i8] = jArr[i9];
                            objArr2[i8] = obj3;
                            objArr2[i9] = null;
                        }
                        i8++;
                    }
                }
                this.f16182q = false;
                this.f16185t = i8;
                i = ~AbstractC1481a.b(this.f16183r, i8, j8);
            }
        }
        int i10 = this.f16185t;
        if (i10 >= this.f16183r.length) {
            int i11 = (i10 + 1) * 8;
            int i12 = 4;
            while (true) {
                if (i12 >= 32) {
                    break;
                }
                int i13 = (1 << i12) - 12;
                if (i11 <= i13) {
                    i11 = i13;
                    break;
                }
                i12++;
            }
            int i14 = i11 / 8;
            long[] jArrCopyOf = Arrays.copyOf(this.f16183r, i14);
            P6.g.d(jArrCopyOf, "copyOf(this, newSize)");
            this.f16183r = jArrCopyOf;
            Object[] objArrCopyOf = Arrays.copyOf(this.f16184s, i14);
            P6.g.d(objArrCopyOf, "copyOf(this, newSize)");
            this.f16184s = objArrCopyOf;
        }
        int i15 = this.f16185t - i;
        if (i15 != 0) {
            long[] jArr2 = this.f16183r;
            int i16 = i + 1;
            P6.g.e(jArr2, "<this>");
            System.arraycopy(jArr2, i, jArr2, i16, i15);
            Object[] objArr3 = this.f16184s;
            B6.j.H(i16, i, this.f16185t, objArr3, objArr3);
        }
        this.f16183r[i] = j8;
        this.f16184s[i] = obj;
        this.f16185t++;
    }

    public final int k() {
        if (this.f16182q) {
            int i = this.f16185t;
            long[] jArr = this.f16183r;
            Object[] objArr = this.f16184s;
            int i5 = 0;
            for (int i8 = 0; i8 < i; i8++) {
                Object obj = objArr[i8];
                if (obj != h.f16186a) {
                    if (i8 != i5) {
                        jArr[i5] = jArr[i8];
                        objArr[i5] = obj;
                        objArr[i8] = null;
                    }
                    i5++;
                }
            }
            this.f16182q = false;
            this.f16185t = i5;
        }
        return this.f16185t;
    }

    public final Object l(int i) {
        if (!(i >= 0 && i < this.f16185t)) {
            AbstractC1481a.c("Expected index to be within 0..size()-1, but was " + i);
            throw null;
        }
        if (this.f16182q) {
            int i5 = this.f16185t;
            long[] jArr = this.f16183r;
            Object[] objArr = this.f16184s;
            int i8 = 0;
            for (int i9 = 0; i9 < i5; i9++) {
                Object obj = objArr[i9];
                if (obj != h.f16186a) {
                    if (i9 != i8) {
                        jArr[i8] = jArr[i9];
                        objArr[i8] = obj;
                        objArr[i9] = null;
                    }
                    i8++;
                }
            }
            this.f16182q = false;
            this.f16185t = i8;
        }
        return this.f16184s[i];
    }

    public final String toString() {
        if (k() <= 0) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder(this.f16185t * 28);
        sb.append('{');
        int i = this.f16185t;
        int i5 = 0;
        while (i5 < i) {
            if (i5 > 0) {
                sb.append(", ");
            }
            if (!(i5 >= 0 && i5 < this.f16185t)) {
                AbstractC1481a.c("Expected index to be within 0..size()-1, but was " + i5);
                throw null;
            }
            if (this.f16182q) {
                int i8 = this.f16185t;
                long[] jArr = this.f16183r;
                Object[] objArr = this.f16184s;
                int i9 = 0;
                for (int i10 = 0; i10 < i8; i10++) {
                    Object obj = objArr[i10];
                    if (obj != h.f16186a) {
                        if (i10 != i9) {
                            jArr[i9] = jArr[i10];
                            objArr[i9] = obj;
                            objArr[i10] = null;
                        }
                        i9++;
                    }
                }
                this.f16182q = false;
                this.f16185t = i9;
            }
            sb.append(this.f16183r[i5]);
            sb.append('=');
            Object objL = l(i5);
            if (objL != sb) {
                sb.append(objL);
            } else {
                sb.append("(this Map)");
            }
            i5++;
        }
        sb.append('}');
        String string = sb.toString();
        P6.g.d(string, "StringBuilder(capacity).…builderAction).toString()");
        return string;
    }
}
