content: package kotlinx.coroutines.internal;
  
  import java.lang.Comparable;
  import java.util.Arrays;
  import kotlin.Metadata;
  import kotlin.Unit;
  import kotlin.collections.ArraysKt;
  import kotlin.jvm.functions.Function1;
  import kotlin.jvm.internal.InlineMarker;
  import kotlin.jvm.internal.Intrinsics;
  import kotlinx.coroutines.DebugKt;
  import kotlinx.coroutines.internal.ThreadSafeHeapNode;
  
  @Metadata(d1 = {"\u0000<\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000f\n\u0002\b\u0004\n\u0002\u0010\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0010\u000b\n\u0002\b\u0007\n\u0002\u0010\u0011\n\u0002\b\u0004\n\u0002\u0010\b\n\u0002\b\u0018\n\u0002\u0010\u0000\n\u0002\u0018\u0002\b\u0017\u0018\u0000*\u0012\b\u0000\u0010\u0003*\u00020\u0001*\b\u0012\u0004\u0012\u00028\u00000\u00022\u000602j\u0002`3B\u0007¢\u0006\u0004\b\u0004\u0010\u0005J\u0017\u0010\b\u001a\u00020\u00072\u0006\u0010\u0006\u001a\u00028\u0000H\u0001¢\u0006\u0004\b\b\u0010\tJ\u0015\u0010\n\u001a\u00020\u00072\u0006\u0010\u0006\u001a\u00028\u0000¢\u0006\u0004\b\n\u0010\tJ.\u0010\u000e\u001a\u00020\f2\u0006\u0010\u0006\u001a\u00028\u00002\u0014\u0010\r\u001a\u0010\u0012\u0006\u0012\u0004\u0018\u00018\u0000\u0012\u0004\u0012\u00020\f0\u000bH\b¢\u0006\u0004\b\u000e\u0010\u000fJ\r\u0010\u0010\u001a\u00020\u0007¢\u0006\u0004\b\u0010\u0010\u0005J\u0011\u0010\u0011\u001a\u0004\u0018\u00018\u0000H\u0001¢\u0006\u0004\b\u0011\u0010\u0012J\u000f\u0010\u0013\u001a\u0004\u0018\u00018\u0000¢\u0006\u0004\b\u0013\u0010\u0012J\u0017\u0010\u0015\u001a\n\u0012\u0006\u0012\u0004\u0018\u00018\u00000\u0014H\u0002¢\u0006\u0004\b\u0015\u0010\u0016J\u0015\u0010\u0017\u001a\u00020\f2\u0006\u0010\u0006\u001a\u00028\u0000¢\u0006\u0004\b\u0017\u0010\u0018J\u0017\u0010\u001b\u001a\u00028\u00002\u0006\u0010\u001a\u001a\u00020\u0019H\u0001¢\u0006\u0004\b\u001b\u0010\u001cJ&\u0010\u001e\u001a\u0004\u0018\u00018\u00002\u0012\u0010\u001d\u001a\u000e\u0012\u0004\u0012\u00028\u0000\u0012\u0004\u0012\u00020\f0\u000bH\b¢\u0006\u0004\b\u001e\u0010\u001fJ\u000f\u0010 \u001a\u0004\u0018\u00018\u0000¢\u0006\u0004\b \u0010\u0012J\u0018\u0010\"\u001a\u00020\u00072\u0006\u0010!\u001a\u00020\u0019H\u0010¢\u0006\u0004\b\"\u0010#J\u0018\u0010$\u001a\u00020\u00072\u0006\u0010!\u001a\u00020\u0019H\u0010¢\u0006\u0004\b$\u0010#J\u001f\u0010&\u001a\u00020\u00072\u0006\u0010!\u001a\u00020\u00192\u0006\u0010%\u001a\u00020\u0019H\u0002¢\u0006\u0004\b&\u0010'R \u0010(\u001a\f\u0012\u0006\u0012\u0004\u0018\u00018\u0000\u0018\u00010\u00148\u0002@\u0002X\u000e¢\u0006\u0006\n\u0004\b(\u0010)R\u0011\u0010*\u001a\u00020\f8F¢\u0006\u0006\u001a\u0004\b*\u0010+R$\u00100\u001a\u00020\u00192\u0006\u0010,\u001a\u00020\u00198F@BX\u000e¢\u0006\f\u001a\u0004\b-\u0010.\"\u0004\b/\u0010#¨\u00061"}, d2 = {"Lkotlinx/coroutines/internal/ThreadSafeHeap;", "Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "", "T", "<init>", "()V", "node", "", "addImpl", "(Lkotlinx/coroutines/internal/ThreadSafeHeapNode;)V", "addLast", "Lkotlin/Function1;", "", "cond", "addLastIf", "(Lkotlinx/coroutines/internal/ThreadSafeHeapNode;Lkotlin/jvm/functions/Function1;)Z", "clear", "firstImpl", "()Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "peek", "", "realloc", "()[Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "remove", "(Lkotlinx/coroutines/internal/ThreadSafeHeapNode;)Z", "", "index", "removeAtImpl", "(I)Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "predicate", "removeFirstIf", "(Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "removeFirstOrNull", "i", "siftDownFrom", "(I)V", "siftUpFrom", "j", "swap", "(II)V", "a", "[Lkotlinx/coroutines/internal/ThreadSafeHeapNode;", "isEmpty", "()Z", "value", "getSize", "()I", "setSize", "size", "kotlinx-coroutines-core", "", "Lkotlinx/coroutines/internal/SynchronizedObject;"}, k = 1, mv = {1, 6, 0}, xi = 48)
  /* compiled from: ThreadSafeHeap.kt */
  public class ThreadSafeHeap<T extends ThreadSafeHeapNode & Comparable<? super T>> {
      private volatile /* synthetic */ int _size = 0;
      private T[] a;
  
      private final T[] realloc() {
          T[] tArr = this.a;
          if (tArr == null) {
              T[] tArr2 = new ThreadSafeHeapNode[4];
              this.a = tArr2;
              return tArr2;
          } else if (getSize() < tArr.length) {
              return tArr;
          } else {
              T[] copyOf = Arrays.copyOf(tArr, getSize() * 2);
              Intrinsics.checkNotNullExpressionValue(copyOf, "copyOf(this, newSize)");
              this.a = (ThreadSafeHeapNode[]) copyOf;
              return (ThreadSafeHeapNode[]) copyOf;
          }
      }
  
      private final void setSize(int value) {
          this._size = value;
      }
  
      private final void siftDownFrom(int i) {
          int i2 = i;
          while (true) {
              int i3 = (i2 * 2) + 1;
              if (i3 < getSize()) {
                  T[] tArr = this.a;
                  Intrinsics.checkNotNull(tArr);
                  if (i3 + 1 < getSize()) {
                      T t = tArr[i3 + 1];
                      Intrinsics.checkNotNull(t);
                      T t2 = tArr[i3];
                      Intrinsics.checkNotNull(t2);
                      if (((Comparable) t).compareTo(t2) < 0) {
                          i3++;
                      }
                  }
                  T t3 = tArr[i2];
                  Intrinsics.checkNotNull(t3);
                  T t4 = tArr[i3];
                  Intrinsics.checkNotNull(t4);
                  if (((Comparable) t3).compareTo(t4) > 0) {
                      swap(i2, i3);
                      i2 = i3;
                  } else {
                      return;
                  }
              } else {
                  return;
              }
          }
      }
  
      private final void siftUpFrom(int i) {
          int i2 = i;
          while (i2 > 0) {
              T[] tArr = this.a;
              Intrinsics.checkNotNull(tArr);
              int i3 = (i2 - 1) / 2;
              T t = tArr[i3];
              Intrinsics.checkNotNull(t);
              T t2 = tArr[i2];
              Intrinsics.checkNotNull(t2);
              if (((Comparable) t).compareTo(t2) > 0) {
                  swap(i2, i3);
                  i2 = i3;
              } else {
                  return;
              }
          }
      }
  
      private final void swap(int i, int j) {
          T[] tArr = this.a;
          Intrinsics.checkNotNull(tArr);
          T t = tArr[j];
          Intrinsics.checkNotNull(t);
          T t2 = tArr[i];
          Intrinsics.checkNotNull(t2);
          tArr[i] = t;
          tArr[j] = t2;
          t.setIndex(i);
          t2.setIndex(j);
      }
  
      public final void addImpl(T node) {
          if (DebugKt.getASSERTIONS_ENABLED()) {
              if (!(node.getHeap() == null)) {
                  throw new AssertionError();
              }
          }
          node.setHeap(this);
          ThreadSafeHeapNode[] realloc = realloc();
          int size = getSize();
          setSize(size + 1);
          realloc[size] = node;
          node.setIndex(size);
          siftUpFrom(size);
      }
  
      public final void addLast(T node) {
          synchronized (this) {
              addImpl(node);
              Unit unit = Unit.INSTANCE;
          }
      }
  
      public final boolean addLastIf(T node, Function1<? super T, Boolean> cond) {
          boolean z;
          synchronized (this) {
              try {
                  if (cond.invoke(firstImpl()).booleanValue()) {
                      addImpl(node);
                      z = true;
                  } else {
                      z = false;
                  }
                  InlineMarker.finallyStart(1);
              } catch (Throwable th) {
                  InlineMarker.finallyStart(1);
                  InlineMarker.finallyEnd(1);
                  throw th;
              }
          }
          InlineMarker.finallyEnd(1);
          return z;
      }
  
      public final void clear() {
          synchronized (this) {
              T[] tArr = this.a;
              if (tArr != null) {
                  ArraysKt.fill$default((Object[]) tArr, (Object) null, 0, 0, 6, (Object) null);
              }
              this._size = 0;
              Unit unit = Unit.INSTANCE;
          }
      }
  
      public final T firstImpl() {
          T[] tArr = this.a;
          if (tArr == null) {
              return null;
          }
          return tArr[0];
      }
  
      public final int getSize() {
          return this._size;
      }
  
      public final boolean isEmpty() {
          return getSize() == 0;
      }
  
      public final T peek() {
          T firstImpl;
          synchronized (this) {
              firstImpl = firstImpl();
          }
          return firstImpl;
      }
  
      public final boolean remove(T node) {
          boolean z;
          synchronized (this) {
              z = true;
              boolean z2 = false;
              if (node.getHeap() == null) {
                  z = false;
              } else {
                  int index = node.getIndex();
                  if (DebugKt.getASSERTIONS_ENABLED()) {
                      if (index >= 0) {
                          z2 = true;
                      }
                      if (!z2) {
                          throw new AssertionError();
                      }
                  }
                  removeAtImpl(index);
              }
          }
          return z;
      }
  
      public final T removeAtImpl(int index) {
          boolean z = false;
          if (DebugKt.getASSERTIONS_ENABLED()) {
              if (!(getSize() > 0)) {
                  throw new AssertionError();
              }
          }
          T[] tArr = this.a;
          Intrinsics.checkNotNull(tArr);
          setSize(getSize() - 1);
          if (index < getSize()) {
              swap(index, getSize());
              int i = (index - 1) / 2;
              if (index > 0) {
                  T t = tArr[index];
                  Intrinsics.checkNotNull(t);
                  T t2 = tArr[i];
                  Intrinsics.checkNotNull(t2);
                  if (((Comparable) t).compareTo(t2) < 0) {
                      swap(index, i);
                      siftUpFrom(i);
                  }
              }
              siftDownFrom(index);
          }
          T t3 = tArr[getSize()];
          Intrinsics.checkNotNull(t3);
          if (DebugKt.getASSERTIONS_ENABLED()) {
              if (t3.getHeap() == this) {
                  z = true;
              }
              if (!z) {
                  throw new AssertionError();
              }
          }
          t3.setHeap((ThreadSafeHeap<?>) null);
          t3.setIndex(-1);
          tArr[getSize()] = null;
          return t3;
      }
  
      public final T removeFirstIf(Function1<? super T, Boolean> predicate) {
          synchronized (this) {
              try {
                  ThreadSafeHeapNode firstImpl = firstImpl();
                  T t = null;
                  if (firstImpl == null) {
                      InlineMarker.finallyStart(2);
                      InlineMarker.finallyEnd(2);
                      return null;
                  }
                  if (predicate.invoke(firstImpl).booleanValue()) {
                      t = removeAtImpl(0);
                  } else {
                      ThreadSafeHeapNode threadSafeHeapNode = null;
                  }
                  InlineMarker.finallyStart(1);
                  InlineMarker.finallyEnd(1);
                  return t;
              } catch (Throwable th) {
                  InlineMarker.finallyStart(1);
                  InlineMarker.finallyEnd(1);
                  throw th;
              }
          }
      }
  
      public final T removeFirstOrNull() {
          T t;
          synchronized (this) {
              if (getSize() > 0) {
                  t = removeAtImpl(0);
              } else {
                  t = null;
                  ThreadSafeHeapNode threadSafeHeapNode = null;
              }
          }
          return t;
      }
  }
  