package kotlinx.coroutines.internal;

import java.lang.Comparable;
import java.util.Arrays;
import kotlinx.coroutines.internal.InterfaceC1359c0;
import p039m1.C1470g;

/* renamed from: kotlinx.coroutines.internal.b0 */
/* loaded from: classes.dex */
public class C1357b0<T extends InterfaceC1359c0 & Comparable<? super T>> {
    private volatile /* synthetic */ int _size = 0;

    /* renamed from: a */
    public T[] f3021a;

    /* renamed from: a */
    public final void m1565a(T t) {
        t.mo546a(this);
        T[] m1560f = m1560f();
        int m1563c = m1563c();
        m1556j(m1563c + 1);
        m1560f[m1563c] = t;
        t.setIndex(m1563c);
        m1554l(m1563c);
    }

    /* renamed from: b */
    public final T m1564b() {
        T[] tArr = this.f3021a;
        if (tArr == null) {
            return null;
        }
        return tArr[0];
    }

    /* renamed from: c */
    public final int m1563c() {
        return this._size;
    }

    /* renamed from: d */
    public final boolean m1562d() {
        return m1563c() == 0;
    }

    /* renamed from: e */
    public final T m1561e() {
        T m1564b;
        synchronized (this) {
            m1564b = m1564b();
        }
        return m1564b;
    }

    /* renamed from: f */
    public final T[] m1560f() {
        T[] tArr = this.f3021a;
        if (tArr == null) {
            T[] tArr2 = (T[]) new InterfaceC1359c0[4];
            this.f3021a = tArr2;
            return tArr2;
        } else if (m1563c() >= tArr.length) {
            Object[] copyOf = Arrays.copyOf(tArr, m1563c() * 2);
            C1470g.m1293d(copyOf, "copyOf(this, newSize)");
            T[] tArr3 = (T[]) ((InterfaceC1359c0[]) copyOf);
            this.f3021a = tArr3;
            return tArr3;
        } else {
            return tArr;
        }
    }

    /* renamed from: g */
    public final boolean m1559g(T t) {
        boolean z;
        synchronized (this) {
            if (t.mo544d() == null) {
                z = false;
            } else {
                m1558h(t.getIndex());
                z = true;
            }
        }
        return z;
    }

    /* renamed from: h */
    public final T m1558h(int i) {
        T[] tArr = this.f3021a;
        C1470g.m1295b(tArr);
        m1556j(m1563c() - 1);
        if (i < m1563c()) {
            m1553m(i, m1563c());
            int i2 = (i - 1) / 2;
            if (i > 0) {
                T t = tArr[i];
                C1470g.m1295b(t);
                T t2 = tArr[i2];
                C1470g.m1295b(t2);
                if (((Comparable) t).compareTo(t2) < 0) {
                    m1553m(i, i2);
                    m1554l(i2);
                }
            }
            m1555k(i);
        }
        T t3 = tArr[m1563c()];
        C1470g.m1295b(t3);
        t3.mo546a(null);
        t3.setIndex(-1);
        tArr[m1563c()] = null;
        return t3;
    }

    /* renamed from: i */
    public final T m1557i() {
        T m1558h;
        synchronized (this) {
            m1558h = m1563c() > 0 ? m1558h(0) : null;
        }
        return m1558h;
    }

    /* renamed from: j */
    public final void m1556j(int i) {
        this._size = i;
    }

    /* renamed from: k */
    public final void m1555k(int i) {
        while (true) {
            int i2 = (i * 2) + 1;
            if (i2 >= m1563c()) {
                return;
            }
            T[] tArr = this.f3021a;
            C1470g.m1295b(tArr);
            int i3 = i2 + 1;
            if (i3 < m1563c()) {
                T t = tArr[i3];
                C1470g.m1295b(t);
                T t2 = tArr[i2];
                C1470g.m1295b(t2);
                if (((Comparable) t).compareTo(t2) < 0) {
                    i2 = i3;
                }
            }
            T t3 = tArr[i];
            C1470g.m1295b(t3);
            T t4 = tArr[i2];
            C1470g.m1295b(t4);
            if (((Comparable) t3).compareTo(t4) <= 0) {
                return;
            }
            m1553m(i, i2);
            i = i2;
        }
    }

    /* renamed from: l */
    public final void m1554l(int i) {
        while (i > 0) {
            T[] tArr = this.f3021a;
            C1470g.m1295b(tArr);
            int i2 = (i - 1) / 2;
            T t = tArr[i2];
            C1470g.m1295b(t);
            T t2 = tArr[i];
            C1470g.m1295b(t2);
            if (((Comparable) t).compareTo(t2) <= 0) {
                return;
            }
            m1553m(i, i2);
            i = i2;
        }
    }

    /* renamed from: m */
    public final void m1553m(int i, int i2) {
        T[] tArr = this.f3021a;
        C1470g.m1295b(tArr);
        T t = tArr[i2];
        C1470g.m1295b(t);
        T t2 = tArr[i];
        C1470g.m1295b(t2);
        tArr[i] = t;
        tArr[i2] = t2;
        t.setIndex(i);
        t2.setIndex(i2);
    }
}