package g.a.n2;

import g.a.l0;
import g.a.n2.h0;
import java.lang.Comparable;
import java.util.Arrays;

/* 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;
    private T[] a;

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

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

    private final void k(int i2) {
        while (true) {
            int i3 = (i2 * 2) + 1;
            if (i3 >= c()) {
                return;
            }
            T[] tArr = this.a;
            f.z.d.k.c(tArr);
            int i4 = i3 + 1;
            if (i4 < c()) {
                T t = tArr[i4];
                f.z.d.k.c(t);
                T t2 = tArr[i3];
                f.z.d.k.c(t2);
                if (((Comparable) t).compareTo(t2) < 0) {
                    i3 = i4;
                }
            }
            T t3 = tArr[i2];
            f.z.d.k.c(t3);
            T t4 = tArr[i3];
            f.z.d.k.c(t4);
            if (((Comparable) t3).compareTo(t4) <= 0) {
                return;
            }
            m(i2, i3);
            i2 = i3;
        }
    }

    private final void l(int i2) {
        while (i2 > 0) {
            T[] tArr = this.a;
            f.z.d.k.c(tArr);
            int i3 = (i2 - 1) / 2;
            T t = tArr[i3];
            f.z.d.k.c(t);
            T t2 = tArr[i2];
            f.z.d.k.c(t2);
            if (((Comparable) t).compareTo(t2) <= 0) {
                return;
            }
            m(i2, i3);
            i2 = i3;
        }
    }

    private final void m(int i2, int i3) {
        T[] tArr = this.a;
        f.z.d.k.c(tArr);
        T t = tArr[i3];
        f.z.d.k.c(t);
        T t2 = tArr[i2];
        f.z.d.k.c(t2);
        tArr[i2] = t;
        tArr[i3] = t2;
        t.a(i2);
        t2.a(i3);
    }

    public final void a(T t) {
        if (l0.a()) {
            if (!(t.d() == null)) {
                throw new AssertionError();
            }
        }
        t.b(this);
        h0[] h0VarArrF = f();
        int iC = c();
        j(iC + 1);
        h0VarArrF[iC] = t;
        t.a(iC);
        l(iC);
    }

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

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

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

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

    public final boolean g(T t) {
        boolean z;
        synchronized (this) {
            z = true;
            if (t.d() == null) {
                z = false;
            } else {
                int iC = t.c();
                if (l0.a()) {
                    if (!(iC >= 0)) {
                        throw new AssertionError();
                    }
                }
                h(iC);
            }
        }
        return z;
    }

    /* 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 = g.a.l0.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 r8 = new java.lang.AssertionError
            r8.<init>()
            throw r8
        L1a:
            T extends g.a.n2.h0 & java.lang.Comparable<? super T>[] r0 = r7.a
            f.z.d.k.c(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]
            f.z.d.k.c(r5)
            java.lang.Comparable r5 = (java.lang.Comparable) r5
            r6 = r0[r3]
            f.z.d.k.c(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]
            f.z.d.k.c(r8)
            boolean r3 = g.a.l0.a()
            if (r3 == 0) goto L76
            g.a.n2.g0 r3 = r8.d()
            if (r3 != r7) goto L6d
            r1 = r2
        L6d:
            if (r1 == 0) goto L70
            goto L76
        L70:
            java.lang.AssertionError r8 = new java.lang.AssertionError
            r8.<init>()
            throw r8
        L76:
            r1 = 0
            r8.b(r1)
            r8.a(r4)
            int r2 = r7.c()
            r0[r2] = r1
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: g.a.n2.g0.h(int):g.a.n2.h0");
    }

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