package kotlinx.coroutines.internal;

import java.lang.Comparable;
import java.util.Arrays;
import kotlinx.coroutines.internal.h0;

/* JADX INFO: compiled from: ThreadSafeHeap.kt */
/* JADX INFO: loaded from: classes.dex */
public class g0<T extends h0 & Comparable<? super T>> {
    private volatile /* synthetic */ int _size = 0;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private T[] f5196a;

    private final T[] f() {
        T[] tArr = this.f5196a;
        if (tArr == null) {
            T[] tArr2 = (T[]) new h0[4];
            this.f5196a = tArr2;
            return tArr2;
        }
        if (c() < tArr.length) {
            return tArr;
        }
        Object[] objArrCopyOf = Arrays.copyOf(tArr, c() * 2);
        w1.g.d(objArrCopyOf, "copyOf(this, newSize)");
        T[] tArr3 = (T[]) ((h0[]) objArrCopyOf);
        this.f5196a = tArr3;
        return tArr3;
    }

    private final void j(int i3) {
        this._size = i3;
    }

    private final void k(int i3) {
        while (true) {
            int i4 = (i3 * 2) + 1;
            if (i4 >= c()) {
                return;
            }
            T[] tArr = this.f5196a;
            w1.g.b(tArr);
            int i5 = i4 + 1;
            if (i5 < c()) {
                T t2 = tArr[i5];
                w1.g.b(t2);
                T t3 = tArr[i4];
                w1.g.b(t3);
                if (((Comparable) t2).compareTo(t3) < 0) {
                    i4 = i5;
                }
            }
            T t4 = tArr[i3];
            w1.g.b(t4);
            T t5 = tArr[i4];
            w1.g.b(t5);
            if (((Comparable) t4).compareTo(t5) <= 0) {
                return;
            }
            m(i3, i4);
            i3 = i4;
        }
    }

    private final void l(int i3) {
        while (i3 > 0) {
            T[] tArr = this.f5196a;
            w1.g.b(tArr);
            int i4 = (i3 - 1) / 2;
            T t2 = tArr[i4];
            w1.g.b(t2);
            T t3 = tArr[i3];
            w1.g.b(t3);
            if (((Comparable) t2).compareTo(t3) <= 0) {
                return;
            }
            m(i3, i4);
            i3 = i4;
        }
    }

    private final void m(int i3, int i4) {
        T[] tArr = this.f5196a;
        w1.g.b(tArr);
        T t2 = tArr[i4];
        w1.g.b(t2);
        T t3 = tArr[i3];
        w1.g.b(t3);
        tArr[i3] = t2;
        tArr[i4] = t3;
        t2.a(i3);
        t3.a(i4);
    }

    public final void a(T t2) {
        if (d2.c0.a()) {
            if (!(t2.e() == null)) {
                throw new AssertionError();
            }
        }
        t2.c(this);
        h0[] h0VarArrF = f();
        int iC = c();
        j(iC + 1);
        h0VarArrF[iC] = t2;
        t2.a(iC);
        l(iC);
    }

    public final T b() {
        T[] tArr = this.f5196a;
        if (tArr == null) {
            return null;
        }
        return tArr[0];
    }

    public final int c() {
        return this._size;
    }

    public final boolean d() {
        return c() == 0;
    }

    public final T e() {
        T t2;
        synchronized (this) {
            t2 = (T) b();
        }
        return t2;
    }

    public final boolean g(T t2) {
        boolean z2;
        synchronized (this) {
            z2 = true;
            if (t2.e() == null) {
                z2 = false;
            } else {
                int iF = t2.f();
                if (d2.c0.a()) {
                    if (!(iF >= 0)) {
                        throw new AssertionError();
                    }
                }
                h(iF);
            }
        }
        return z2;
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x0054  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final T h(int r8) {
        /*
            r7 = this;
            boolean r0 = d2.c0.a()
            r1 = 0
            r2 = 1
            if (r0 == 0) goto L1a
            int r0 = r7.c()
            if (r0 <= 0) goto L10
            r0 = r2
            goto L11
        L10:
            r0 = r1
        L11:
            if (r0 == 0) goto L14
            goto L1a
        L14:
            java.lang.AssertionError r7 = new java.lang.AssertionError
            r7.<init>()
            throw r7
        L1a:
            T extends kotlinx.coroutines.internal.h0 & java.lang.Comparable<? super T>[] r0 = r7.f5196a
            w1.g.b(r0)
            int r3 = r7.c()
            r4 = -1
            int r3 = r3 + r4
            r7.j(r3)
            int r3 = r7.c()
            if (r8 >= r3) goto L57
            int r3 = r7.c()
            r7.m(r8, r3)
            int r3 = r8 + (-1)
            int r3 = r3 / 2
            if (r8 <= 0) goto L54
            r5 = r0[r8]
            w1.g.b(r5)
            java.lang.Comparable r5 = (java.lang.Comparable) r5
            r6 = r0[r3]
            w1.g.b(r6)
            int r5 = r5.compareTo(r6)
            if (r5 >= 0) goto L54
            r7.m(r8, r3)
            r7.l(r3)
            goto L57
        L54:
            r7.k(r8)
        L57:
            int r8 = r7.c()
            r8 = r0[r8]
            w1.g.b(r8)
            boolean r3 = d2.c0.a()
            if (r3 == 0) goto L76
            kotlinx.coroutines.internal.g0 r3 = r8.e()
            if (r3 != r7) goto L6d
            r1 = r2
        L6d:
            if (r1 == 0) goto L70
            goto L76
        L70:
            java.lang.AssertionError r7 = new java.lang.AssertionError
            r7.<init>()
            throw r7
        L76:
            r1 = 0
            r8.c(r1)
            r8.a(r4)
            int r7 = r7.c()
            r0[r7] = r1
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: kotlinx.coroutines.internal.g0.h(int):kotlinx.coroutines.internal.h0");
    }

    public final T i() {
        T t2;
        synchronized (this) {
            t2 = c() > 0 ? (T) h(0) : null;
        }
        return t2;
    }
}
