001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021import static com.google.common.base.Preconditions.checkPositionIndex;
022import static com.google.common.base.Preconditions.checkState;
023import static com.google.common.collect.CollectPreconditions.checkRemove;
024import static java.lang.Math.max;
025import static java.lang.Math.min;
026import static java.lang.System.arraycopy;
027import static java.util.Objects.requireNonNull;
028
029import com.google.common.annotations.GwtCompatible;
030import com.google.common.annotations.J2ktIncompatible;
031import com.google.common.annotations.VisibleForTesting;
032import com.google.common.math.IntMath;
033import com.google.errorprone.annotations.CanIgnoreReturnValue;
034import com.google.j2objc.annotations.Weak;
035import com.google.j2objc.annotations.WeakOuter;
036import java.util.AbstractQueue;
037import java.util.ArrayDeque;
038import java.util.ArrayList;
039import java.util.Collection;
040import java.util.Collections;
041import java.util.Comparator;
042import java.util.ConcurrentModificationException;
043import java.util.Iterator;
044import java.util.List;
045import java.util.NoSuchElementException;
046import java.util.PriorityQueue;
047import java.util.Queue;
048import org.jspecify.annotations.Nullable;
049
050/**
051 * A double-ended priority queue, which provides constant-time access to both its least element and
052 * its greatest element, as determined by the queue's specified comparator. If no comparator is
053 * given at creation time, the natural order of elements is used. If no maximum size is given at
054 * creation time, the queue is unbounded.
055 *
056 * <p>Usage example:
057 *
058 * <pre>{@code
059 * MinMaxPriorityQueue<User> users = MinMaxPriorityQueue.orderedBy(userComparator)
060 *     .maximumSize(1000)
061 *     .create();
062 * }</pre>
063 *
064 * <p>As a {@link Queue} it functions exactly as a {@link PriorityQueue}: its head element -- the
065 * implicit target of the methods {@link #peek()}, {@link #poll()} and {@link #remove()} -- is
066 * defined as the <i>least</i> element in the queue according to the queue's comparator. But unlike
067 * a regular priority queue, the methods {@link #peekLast}, {@link #pollLast} and {@link
068 * #removeLast} are also provided, to act on the <i>greatest</i> element in the queue instead.
069 *
070 * <p>A min-max priority queue can be configured with a maximum size. If so, each time the size of
071 * the queue exceeds that value, the queue automatically removes its greatest element according to
072 * its comparator (which might be the element that was just added). This is different from
073 * conventional bounded queues, which either block or reject new elements when full.
074 *
075 * <p>This implementation is based on the <a
076 * href="http://portal.acm.org/citation.cfm?id=6621">min-max heap</a> developed by Atkinson, et al.
077 * Unlike many other double-ended priority queues, it stores elements in a single array, as compact
078 * as the traditional heap data structure used in {@link PriorityQueue}.
079 *
080 * <p>This class is not thread-safe, and does not accept null elements.
081 *
082 * <p><i>Performance notes:</i>
083 *
084 * <ul>
085 *   <li>If you only access one end of the queue, and do use a maximum size, this class will perform
086 *       significantly worse than a {@code PriorityQueue} with manual eviction above the maximum
087 *       size. In many cases {@link Ordering#leastOf} may work for your use case with significantly
088 *       improved (and asymptotically superior) performance.
089 *   <li>The retrieval operations {@link #peek}, {@link #peekFirst}, {@link #peekLast}, {@link
090 *       #element}, and {@link #size} are constant-time.
091 *   <li>The enqueuing and dequeuing operations ({@link #offer}, {@link #add}, and all the forms of
092 *       {@link #poll} and {@link #remove()}) run in {@code O(log n) time}.
093 *   <li>The {@link #remove(Object)} and {@link #contains} operations require linear ({@code O(n)})
094 *       time.
095 *   <li>If you only access one end of the queue, and don't use a maximum size, this class is
096 *       functionally equivalent to {@link PriorityQueue}, but significantly slower.
097 * </ul>
098 *
099 * @author Sverre Sundsdal
100 * @author Torbjorn Gannholm
101 * @since 8.0
102 */
103@GwtCompatible
104public final class MinMaxPriorityQueue<E> extends AbstractQueue<E> {
105
106  /**
107   * Creates a new min-max priority queue with default settings: natural order, no maximum size, no
108   * initial contents, and an initial expected size of 11.
109   */
110  public static <E extends Comparable<E>> MinMaxPriorityQueue<E> create() {
111    return new Builder<Comparable<E>>(Ordering.natural()).create();
112  }
113
114  /**
115   * Creates a new min-max priority queue using natural order, no maximum size, and initially
116   * containing the given elements.
117   */
118  public static <E extends Comparable<E>> MinMaxPriorityQueue<E> create(
119      Iterable<? extends E> initialContents) {
120    return new Builder<E>(Ordering.<E>natural()).create(initialContents);
121  }
122
123  /**
124   * Creates and returns a new builder, configured to build {@code MinMaxPriorityQueue} instances
125   * that use {@code comparator} to determine the least and greatest elements.
126   */
127  /*
128   * TODO(cpovirk): Change to Comparator<? super B> to permit Comparator<@Nullable ...> and
129   * Comparator<SupertypeOfB>? What we have here matches the immutable collections, but those also
130   * expose a public Builder constructor that accepts "? super." So maybe we should do *that*
131   * instead.
132   */
133  public static <B> Builder<B> orderedBy(Comparator<B> comparator) {
134    return new Builder<>(comparator);
135  }
136
137  /**
138   * Creates and returns a new builder, configured to build {@code MinMaxPriorityQueue} instances
139   * sized appropriately to hold {@code expectedSize} elements.
140   */
141  @SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
142  public static Builder<Comparable> expectedSize(int expectedSize) {
143    return new Builder<Comparable>(Ordering.natural()).expectedSize(expectedSize);
144  }
145
146  /**
147   * Creates and returns a new builder, configured to build {@code MinMaxPriorityQueue} instances
148   * that are limited to {@code maximumSize} elements. Each time a queue grows beyond this bound, it
149   * immediately removes its greatest element (according to its comparator), which might be the
150   * element that was just added.
151   */
152  @SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
153  public static Builder<Comparable> maximumSize(int maximumSize) {
154    return new Builder<Comparable>(Ordering.natural()).maximumSize(maximumSize);
155  }
156
157  /**
158   * The builder class used in creation of min-max priority queues. Instead of constructing one
159   * directly, use {@link MinMaxPriorityQueue#orderedBy(Comparator)}, {@link
160   * MinMaxPriorityQueue#expectedSize(int)} or {@link MinMaxPriorityQueue#maximumSize(int)}.
161   *
162   * @param <B> the upper bound on the eventual type that can be produced by this builder (for
163   *     example, a {@code Builder<Number>} can produce a {@code Queue<Number>} or {@code
164   *     Queue<Integer>} but not a {@code Queue<Object>}).
165   * @since 8.0
166   */
167  public static final class Builder<B> {
168    /*
169     * TODO(kevinb): when the dust settles, see if we still need this or can
170     * just default to DEFAULT_CAPACITY.
171     */
172    private static final int UNSET_EXPECTED_SIZE = -1;
173
174    private final Comparator<B> comparator;
175    private int expectedSize = UNSET_EXPECTED_SIZE;
176    private int maximumSize = Integer.MAX_VALUE;
177
178    private Builder(Comparator<B> comparator) {
179      this.comparator = checkNotNull(comparator);
180    }
181
182    /**
183     * Configures this builder to build min-max priority queues with an initial expected size of
184     * {@code expectedSize}.
185     */
186    @CanIgnoreReturnValue
187    public Builder<B> expectedSize(int expectedSize) {
188      checkArgument(expectedSize >= 0);
189      this.expectedSize = expectedSize;
190      return this;
191    }
192
193    /**
194     * Configures this builder to build {@code MinMaxPriorityQueue} instances that are limited to
195     * {@code maximumSize} elements. Each time a queue grows beyond this bound, it immediately
196     * removes its greatest element (according to its comparator), which might be the element that
197     * was just added.
198     */
199    @CanIgnoreReturnValue
200    public Builder<B> maximumSize(int maximumSize) {
201      checkArgument(maximumSize > 0);
202      this.maximumSize = maximumSize;
203      return this;
204    }
205
206    /**
207     * Builds a new min-max priority queue using the previously specified options, and having no
208     * initial contents.
209     */
210    public <T extends B> MinMaxPriorityQueue<T> create() {
211      return create(Collections.<T>emptySet());
212    }
213
214    /**
215     * Builds a new min-max priority queue using the previously specified options, and having the
216     * given initial elements.
217     */
218    public <T extends B> MinMaxPriorityQueue<T> create(Iterable<? extends T> initialContents) {
219      MinMaxPriorityQueue<T> queue =
220          new MinMaxPriorityQueue<>(
221              this, initialQueueSize(expectedSize, maximumSize, initialContents));
222      for (T element : initialContents) {
223        queue.offer(element);
224      }
225      return queue;
226    }
227
228    @SuppressWarnings("unchecked") // safe "contravariant cast"
229    private <T extends B> Ordering<T> ordering() {
230      return Ordering.from((Comparator<T>) comparator);
231    }
232  }
233
234  private final Heap minHeap;
235  private final Heap maxHeap;
236  @VisibleForTesting final int maximumSize;
237  private @Nullable Object[] queue;
238  private int size;
239  private int modCount;
240
241  private MinMaxPriorityQueue(Builder<? super E> builder, int queueSize) {
242    Ordering<E> ordering = builder.ordering();
243    this.minHeap = new Heap(ordering);
244    this.maxHeap = new Heap(ordering.reverse());
245    minHeap.otherHeap = maxHeap;
246    maxHeap.otherHeap = minHeap;
247
248    this.maximumSize = builder.maximumSize;
249    // TODO(kevinb): pad?
250    this.queue = new Object[queueSize];
251  }
252
253  @Override
254  public int size() {
255    return size;
256  }
257
258  /**
259   * Adds the given element to this queue. If this queue has a maximum size, after adding {@code
260   * element} the queue will automatically evict its greatest element (according to its comparator),
261   * which may be {@code element} itself.
262   *
263   * @return {@code true} always
264   */
265  @CanIgnoreReturnValue
266  @Override
267  public boolean add(E element) {
268    offer(element);
269    return true;
270  }
271
272  @CanIgnoreReturnValue
273  @Override
274  public boolean addAll(Collection<? extends E> newElements) {
275    boolean modified = false;
276    for (E element : newElements) {
277      offer(element);
278      modified = true;
279    }
280    return modified;
281  }
282
283  /**
284   * Adds the given element to this queue. If this queue has a maximum size, after adding {@code
285   * element} the queue will automatically evict its greatest element (according to its comparator),
286   * which may be {@code element} itself.
287   */
288  @CanIgnoreReturnValue
289  @Override
290  public boolean offer(E element) {
291    checkNotNull(element);
292    modCount++;
293    int insertIndex = size++;
294
295    growIfNeeded();
296
297    // Adds the element to the end of the heap and bubbles it up to the correct
298    // position.
299    heapForIndex(insertIndex).bubbleUp(insertIndex, element);
300    return size <= maximumSize || pollLast() != element;
301  }
302
303  @CanIgnoreReturnValue
304  @Override
305  public @Nullable E poll() {
306    return isEmpty() ? null : removeAndGet(0);
307  }
308
309  @SuppressWarnings("unchecked") // we must carefully only allow Es to get in
310  E elementData(int index) {
311    /*
312     * requireNonNull is safe as long as we're careful to call this method only with populated
313     * indexes.
314     */
315    return (E) requireNonNull(queue[index]);
316  }
317
318  @Override
319  public @Nullable E peek() {
320    return isEmpty() ? null : elementData(0);
321  }
322
323  /** Returns the index of the max element. */
324  private int getMaxElementIndex() {
325    switch (size) {
326      case 1:
327        return 0; // The lone element in the queue is the maximum.
328      case 2:
329        return 1; // The lone element in the maxHeap is the maximum.
330      default:
331        // The max element must sit on the first level of the maxHeap. It is
332        // actually the *lesser* of the two from the maxHeap's perspective.
333        return (maxHeap.compareElements(1, 2) <= 0) ? 1 : 2;
334    }
335  }
336
337  /**
338   * Removes and returns the least element of this queue, or returns {@code null} if the queue is
339   * empty.
340   */
341  @CanIgnoreReturnValue
342  public @Nullable E pollFirst() {
343    return poll();
344  }
345
346  /**
347   * Removes and returns the least element of this queue.
348   *
349   * @throws NoSuchElementException if the queue is empty
350   */
351  @CanIgnoreReturnValue
352  public E removeFirst() {
353    return remove();
354  }
355
356  /**
357   * Retrieves, but does not remove, the least element of this queue, or returns {@code null} if the
358   * queue is empty.
359   */
360  public @Nullable E peekFirst() {
361    return peek();
362  }
363
364  /**
365   * Removes and returns the greatest element of this queue, or returns {@code null} if the queue is
366   * empty.
367   */
368  @CanIgnoreReturnValue
369  public @Nullable E pollLast() {
370    return isEmpty() ? null : removeAndGet(getMaxElementIndex());
371  }
372
373  /**
374   * Removes and returns the greatest element of this queue.
375   *
376   * @throws NoSuchElementException if the queue is empty
377   */
378  @CanIgnoreReturnValue
379  public E removeLast() {
380    if (isEmpty()) {
381      throw new NoSuchElementException();
382    }
383    return removeAndGet(getMaxElementIndex());
384  }
385
386  /**
387   * Retrieves, but does not remove, the greatest element of this queue, or returns {@code null} if
388   * the queue is empty.
389   */
390  public @Nullable E peekLast() {
391    return isEmpty() ? null : elementData(getMaxElementIndex());
392  }
393
394  /**
395   * Removes the element at position {@code index}.
396   *
397   * <p>Normally this method leaves the elements at up to {@code index - 1}, inclusive, untouched.
398   * Under these circumstances, it returns {@code null}.
399   *
400   * <p>Occasionally, in order to maintain the heap invariant, it must swap a later element of the
401   * list with one before {@code index}. Under these circumstances it returns a pair of elements as
402   * a {@link MoveDesc}. The first one is the element that was previously at the end of the heap and
403   * is now at some position before {@code index}. The second element is the one that was swapped
404   * down to replace the element at {@code index}. This fact is used by iterator.remove so as to
405   * visit elements during a traversal once and only once.
406   */
407  @VisibleForTesting
408  @CanIgnoreReturnValue
409  @Nullable MoveDesc<E> removeAt(int index) {
410    checkPositionIndex(index, size);
411    modCount++;
412    size--;
413    if (size == index) {
414      queue[size] = null;
415      return null;
416    }
417    E actualLastElement = elementData(size);
418    int lastElementAt = heapForIndex(size).swapWithConceptuallyLastElement(actualLastElement);
419    if (lastElementAt == index) {
420      // 'actualLastElement' is now at 'lastElementAt', and the element that was at 'lastElementAt'
421      // is now at the end of queue. If that's the element we wanted to remove in the first place,
422      // don't try to (incorrectly) trickle it. Instead, just delete it and we're done.
423      queue[size] = null;
424      return null;
425    }
426    E toTrickle = elementData(size);
427    queue[size] = null;
428    MoveDesc<E> changes = fillHole(index, toTrickle);
429    if (lastElementAt < index) {
430      // Last element is moved to before index, swapped with trickled element.
431      if (changes == null) {
432        // The trickled element is still after index.
433        return new MoveDesc<>(actualLastElement, toTrickle);
434      } else {
435        // The trickled element is back before index, but the replaced element
436        // has now been moved after index.
437        return new MoveDesc<>(actualLastElement, changes.replaced);
438      }
439    }
440    // Trickled element was after index to begin with, no adjustment needed.
441    return changes;
442  }
443
444  private @Nullable MoveDesc<E> fillHole(int index, E toTrickle) {
445    Heap heap = heapForIndex(index);
446    // We consider elementData(index) a "hole", and we want to fill it
447    // with the last element of the heap, toTrickle.
448    // Since the last element of the heap is from the bottom level, we
449    // optimistically fill index position with elements from lower levels,
450    // moving the hole down. In most cases this reduces the number of
451    // comparisons with toTrickle, but in some cases we will need to bubble it
452    // all the way up again.
453    int vacated = heap.fillHoleAt(index);
454    // Try to see if toTrickle can be bubbled up min levels.
455    int bubbledTo = heap.bubbleUpAlternatingLevels(vacated, toTrickle);
456    if (bubbledTo == vacated) {
457      // Could not bubble toTrickle up min levels, try moving
458      // it from min level to max level (or max to min level) and bubble up
459      // there.
460      return heap.tryCrossOverAndBubbleUp(index, vacated, toTrickle);
461    } else {
462      return (bubbledTo < index) ? new MoveDesc<E>(toTrickle, elementData(index)) : null;
463    }
464  }
465
466  // Returned from removeAt() to iterator.remove()
467  static class MoveDesc<E> {
468    final E toTrickle;
469    final E replaced;
470
471    MoveDesc(E toTrickle, E replaced) {
472      this.toTrickle = toTrickle;
473      this.replaced = replaced;
474    }
475  }
476
477  /** Removes and returns the value at {@code index}. */
478  private E removeAndGet(int index) {
479    E value = elementData(index);
480    removeAt(index);
481    return value;
482  }
483
484  private Heap heapForIndex(int i) {
485    return isEvenLevel(i) ? minHeap : maxHeap;
486  }
487
488  private static final int EVEN_POWERS_OF_TWO = 0x55555555;
489  private static final int ODD_POWERS_OF_TWO = 0xaaaaaaaa;
490
491  @VisibleForTesting
492  static boolean isEvenLevel(int index) {
493    int oneBased = ~~(index + 1); // for GWT
494    checkState(oneBased > 0, "negative index");
495    return (oneBased & EVEN_POWERS_OF_TWO) > (oneBased & ODD_POWERS_OF_TWO);
496  }
497
498  /**
499   * Returns {@code true} if the MinMax heap structure holds. This is only used in testing.
500   *
501   * <p>TODO(kevinb): move to the test class?
502   */
503  @VisibleForTesting
504  boolean isIntact() {
505    for (int i = 1; i < size; i++) {
506      if (!heapForIndex(i).verifyIndex(i)) {
507        return false;
508      }
509    }
510    return true;
511  }
512
513  /**
514   * Each instance of MinMaxPriorityQueue encapsulates two instances of Heap: a min-heap and a
515   * max-heap. Conceptually, these might each have their own array for storage, but for efficiency's
516   * sake they are stored interleaved on alternate heap levels in the same array (MMPQ.queue).
517   */
518  @WeakOuter
519  class Heap {
520    final Ordering<E> ordering;
521
522    @SuppressWarnings("nullness:initialization.field.uninitialized")
523    @Weak
524    Heap otherHeap; // always initialized immediately after construction
525
526    Heap(Ordering<E> ordering) {
527      this.ordering = ordering;
528    }
529
530    int compareElements(int a, int b) {
531      return ordering.compare(elementData(a), elementData(b));
532    }
533
534    /**
535     * Tries to move {@code toTrickle} from a min to a max level and bubble up there. If it moved
536     * before {@code removeIndex} this method returns a pair as described in {@link #removeAt}.
537     */
538    @Nullable MoveDesc<E> tryCrossOverAndBubbleUp(int removeIndex, int vacated, E toTrickle) {
539      int crossOver = crossOver(vacated, toTrickle);
540      if (crossOver == vacated) {
541        return null;
542      }
543      // Successfully crossed over from min to max.
544      // Bubble up max levels.
545      E parent;
546      // If toTrickle is moved up to a parent of removeIndex, the parent is
547      // placed in removeIndex position. We must return that to the iterator so
548      // that it knows to skip it.
549      if (crossOver < removeIndex) {
550        // We crossed over to the parent level in crossOver, so the parent
551        // has already been moved.
552        parent = elementData(removeIndex);
553      } else {
554        parent = elementData(getParentIndex(removeIndex));
555      }
556      // bubble it up the opposite heap
557      if (otherHeap.bubbleUpAlternatingLevels(crossOver, toTrickle) < removeIndex) {
558        return new MoveDesc<>(toTrickle, parent);
559      } else {
560        return null;
561      }
562    }
563
564    /** Bubbles a value from {@code index} up the appropriate heap if required. */
565    void bubbleUp(int index, E x) {
566      int crossOver = crossOverUp(index, x);
567
568      Heap heap;
569      if (crossOver == index) {
570        heap = this;
571      } else {
572        index = crossOver;
573        heap = otherHeap;
574      }
575      heap.bubbleUpAlternatingLevels(index, x);
576    }
577
578    /**
579     * Bubbles a value from {@code index} up the levels of this heap, and returns the index the
580     * element ended up at.
581     */
582    @CanIgnoreReturnValue
583    int bubbleUpAlternatingLevels(int index, E x) {
584      while (index > 2) {
585        int grandParentIndex = getGrandparentIndex(index);
586        E e = elementData(grandParentIndex);
587        if (ordering.compare(e, x) <= 0) {
588          break;
589        }
590        queue[index] = e;
591        index = grandParentIndex;
592      }
593      queue[index] = x;
594      return index;
595    }
596
597    /**
598     * Returns the index of minimum value between {@code index} and {@code index + len}, or {@code
599     * -1} if {@code index} is greater than {@code size}.
600     */
601    int findMin(int index, int len) {
602      if (index >= size) {
603        return -1;
604      }
605      checkState(index > 0);
606      int limit = min(index, size - len) + len;
607      int minIndex = index;
608      for (int i = index + 1; i < limit; i++) {
609        if (compareElements(i, minIndex) < 0) {
610          minIndex = i;
611        }
612      }
613      return minIndex;
614    }
615
616    /** Returns the minimum child or {@code -1} if no child exists. */
617    int findMinChild(int index) {
618      return findMin(getLeftChildIndex(index), 2);
619    }
620
621    /** Returns the minimum grand child or -1 if no grand child exists. */
622    int findMinGrandChild(int index) {
623      int leftChildIndex = getLeftChildIndex(index);
624      if (leftChildIndex < 0) {
625        return -1;
626      }
627      return findMin(getLeftChildIndex(leftChildIndex), 4);
628    }
629
630    /**
631     * Moves an element one level up from a min level to a max level (or vice versa). Returns the
632     * new position of the element.
633     */
634    int crossOverUp(int index, E x) {
635      if (index == 0) {
636        queue[0] = x;
637        return 0;
638      }
639      int parentIndex = getParentIndex(index);
640      E parentElement = elementData(parentIndex);
641      if (parentIndex != 0) {
642        /*
643         * This is a guard for the case of the childless aunt node. Since the end of the array is
644         * actually the middle of the heap, a smaller childless aunt node can become a child of x
645         * when we bubble up alternate levels, violating the invariant.
646         */
647        int grandparentIndex = getParentIndex(parentIndex);
648        int auntIndex = getRightChildIndex(grandparentIndex);
649        if (auntIndex != parentIndex && getLeftChildIndex(auntIndex) >= size) {
650          E auntElement = elementData(auntIndex);
651          if (ordering.compare(auntElement, parentElement) < 0) {
652            parentIndex = auntIndex;
653            parentElement = auntElement;
654          }
655        }
656      }
657      if (ordering.compare(parentElement, x) < 0) {
658        queue[index] = parentElement;
659        queue[parentIndex] = x;
660        return parentIndex;
661      }
662      queue[index] = x;
663      return index;
664    }
665
666    // About the term "aunt node": it's better to leave gender out of it, but for this the English
667    // language has nothing for us. Except for the whimsical neologism "pibling" (!) which we
668    // obviously could not expect to increase anyone's understanding of the code.
669
670    /**
671     * Swap {@code actualLastElement} with the conceptually correct last element of the heap.
672     * Returns the index that {@code actualLastElement} now resides in.
673     *
674     * <p>Since the last element of the array is actually in the middle of the sorted structure, a
675     * childless aunt node could be smaller, which would corrupt the invariant if this element
676     * becomes the new parent of the aunt node. In that case, we first switch the last element with
677     * its aunt node, before returning.
678     */
679    int swapWithConceptuallyLastElement(E actualLastElement) {
680      int parentIndex = getParentIndex(size);
681      if (parentIndex != 0) {
682        int grandparentIndex = getParentIndex(parentIndex);
683        int auntIndex = getRightChildIndex(grandparentIndex);
684        if (auntIndex != parentIndex && getLeftChildIndex(auntIndex) >= size) {
685          E auntElement = elementData(auntIndex);
686          if (ordering.compare(auntElement, actualLastElement) < 0) {
687            queue[auntIndex] = actualLastElement;
688            queue[size] = auntElement;
689            return auntIndex;
690          }
691        }
692      }
693      return size;
694    }
695
696    /**
697     * Crosses an element over to the opposite heap by moving it one level down (or up if there are
698     * no elements below it).
699     *
700     * <p>Returns the new position of the element.
701     */
702    int crossOver(int index, E x) {
703      int minChildIndex = findMinChild(index);
704      // TODO(kevinb): split the && into two if's and move crossOverUp so it's
705      // only called when there's no child.
706      if ((minChildIndex > 0) && (ordering.compare(elementData(minChildIndex), x) < 0)) {
707        queue[index] = elementData(minChildIndex);
708        queue[minChildIndex] = x;
709        return minChildIndex;
710      }
711      return crossOverUp(index, x);
712    }
713
714    /**
715     * Fills the hole at {@code index} by moving in the least of its grandchildren to this position,
716     * then recursively filling the new hole created.
717     *
718     * @return the position of the new hole (where the lowest grandchild moved from, that had no
719     *     grandchild to replace it)
720     */
721    int fillHoleAt(int index) {
722      int minGrandchildIndex;
723      while ((minGrandchildIndex = findMinGrandChild(index)) > 0) {
724        queue[index] = elementData(minGrandchildIndex);
725        index = minGrandchildIndex;
726      }
727      return index;
728    }
729
730    private boolean verifyIndex(int i) {
731      if ((getLeftChildIndex(i) < size) && (compareElements(i, getLeftChildIndex(i)) > 0)) {
732        return false;
733      }
734      if ((getRightChildIndex(i) < size) && (compareElements(i, getRightChildIndex(i)) > 0)) {
735        return false;
736      }
737      if ((i > 0) && (compareElements(i, getParentIndex(i)) > 0)) {
738        return false;
739      }
740      if ((i > 2) && (compareElements(getGrandparentIndex(i), i) > 0)) {
741        return false;
742      }
743      return true;
744    }
745
746    // These would be static if inner classes could have static members.
747
748    private int getLeftChildIndex(int i) {
749      return i * 2 + 1;
750    }
751
752    private int getRightChildIndex(int i) {
753      return i * 2 + 2;
754    }
755
756    private int getParentIndex(int i) {
757      return (i - 1) / 2;
758    }
759
760    private int getGrandparentIndex(int i) {
761      return getParentIndex(getParentIndex(i)); // (i - 3) / 4
762    }
763  }
764
765  /**
766   * Iterates the elements of the queue in no particular order.
767   *
768   * <p>If the underlying queue is modified during iteration an exception will be thrown.
769   */
770  private class QueueIterator implements Iterator<E> {
771    private int cursor = -1;
772    private int nextCursor = -1;
773    private int expectedModCount = modCount;
774    // The same element is not allowed in both forgetMeNot and skipMe, but duplicates are allowed in
775    // either of them, up to the same multiplicity as the queue.
776    private @Nullable Queue<E> forgetMeNot;
777    private @Nullable List<E> skipMe;
778    private @Nullable E lastFromForgetMeNot;
779    private boolean canRemove;
780
781    @Override
782    public boolean hasNext() {
783      checkModCount();
784      nextNotInSkipMe(cursor + 1);
785      return (nextCursor < size()) || ((forgetMeNot != null) && !forgetMeNot.isEmpty());
786    }
787
788    @Override
789    public E next() {
790      checkModCount();
791      nextNotInSkipMe(cursor + 1);
792      if (nextCursor < size()) {
793        cursor = nextCursor;
794        canRemove = true;
795        return elementData(cursor);
796      } else if (forgetMeNot != null) {
797        cursor = size();
798        lastFromForgetMeNot = forgetMeNot.poll();
799        if (lastFromForgetMeNot != null) {
800          canRemove = true;
801          return lastFromForgetMeNot;
802        }
803      }
804      throw new NoSuchElementException("iterator moved past last element in queue.");
805    }
806
807    @Override
808    public void remove() {
809      checkRemove(canRemove);
810      checkModCount();
811      canRemove = false;
812      expectedModCount++;
813      if (cursor < size()) {
814        MoveDesc<E> moved = removeAt(cursor);
815        if (moved != null) {
816          // Either both are null or neither is, but we check both to satisfy the nullness checker.
817          if (forgetMeNot == null || skipMe == null) {
818            forgetMeNot = new ArrayDeque<>();
819            skipMe = new ArrayList<>(3);
820          }
821          if (!foundAndRemovedExactReference(skipMe, moved.toTrickle)) {
822            forgetMeNot.add(moved.toTrickle);
823          }
824          if (!foundAndRemovedExactReference(forgetMeNot, moved.replaced)) {
825            skipMe.add(moved.replaced);
826          }
827        }
828        cursor--;
829        nextCursor--;
830      } else { // we must have set lastFromForgetMeNot in next()
831        checkState(removeExact(requireNonNull(lastFromForgetMeNot)));
832        lastFromForgetMeNot = null;
833      }
834    }
835
836    /** Returns true if an exact reference (==) was found and removed from the supplied iterable. */
837    private boolean foundAndRemovedExactReference(Iterable<E> elements, E target) {
838      for (Iterator<E> it = elements.iterator(); it.hasNext(); ) {
839        E element = it.next();
840        if (element == target) {
841          it.remove();
842          return true;
843        }
844      }
845      return false;
846    }
847
848    /** Removes only this exact instance, not others that are equals() */
849    private boolean removeExact(Object target) {
850      for (int i = 0; i < size; i++) {
851        if (queue[i] == target) {
852          removeAt(i);
853          return true;
854        }
855      }
856      return false;
857    }
858
859    private void checkModCount() {
860      if (modCount != expectedModCount) {
861        throw new ConcurrentModificationException();
862      }
863    }
864
865    /**
866     * Advances nextCursor to the index of the first element after {@code c} that is not in {@code
867     * skipMe} and returns {@code size()} if there is no such element.
868     */
869    private void nextNotInSkipMe(int c) {
870      if (nextCursor < c) {
871        if (skipMe != null) {
872          while (c < size() && foundAndRemovedExactReference(skipMe, elementData(c))) {
873            c++;
874          }
875        }
876        nextCursor = c;
877      }
878    }
879  }
880
881  /**
882   * Returns an iterator over the elements contained in this collection, <i>in no particular
883   * order</i>.
884   *
885   * <p>The iterator is <i>fail-fast</i>: If the MinMaxPriorityQueue is modified at any time after
886   * the iterator is created, in any way except through the iterator's own remove method, the
887   * iterator will generally throw a {@link ConcurrentModificationException}. Thus, in the face of
888   * concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary,
889   * non-deterministic behavior at an undetermined time in the future.
890   *
891   * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally
892   * speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent
893   * modification. Fail-fast iterators throw {@code ConcurrentModificationException} on a
894   * best-effort basis. Therefore, it would be wrong to write a program that depended on this
895   * exception for its correctness: <i>the fail-fast behavior of iterators should be used only to
896   * detect bugs.</i>
897   *
898   * @return an iterator over the elements contained in this collection
899   */
900  @Override
901  public Iterator<E> iterator() {
902    return new QueueIterator();
903  }
904
905  @Override
906  public void clear() {
907    for (int i = 0; i < size; i++) {
908      queue[i] = null;
909    }
910    size = 0;
911  }
912
913  @Override
914  @J2ktIncompatible // Incompatible return type change. Use inherited (unoptimized) implementation
915  public Object[] toArray() {
916    Object[] copyTo = new Object[size];
917    arraycopy(queue, 0, copyTo, 0, size);
918    return copyTo;
919  }
920
921  /**
922   * Returns the comparator used to order the elements in this queue. Obeys the general contract of
923   * {@link PriorityQueue#comparator}, but returns {@link Ordering#natural} instead of {@code null}
924   * to indicate natural ordering.
925   */
926  public Comparator<? super E> comparator() {
927    return minHeap.ordering;
928  }
929
930  @VisibleForTesting
931  int capacity() {
932    return queue.length;
933  }
934
935  // Size/capacity-related methods
936
937  private static final int DEFAULT_CAPACITY = 11;
938
939  @VisibleForTesting
940  static int initialQueueSize(
941      int configuredExpectedSize, int maximumSize, Iterable<?> initialContents) {
942    // Start with what they said, if they said it, otherwise DEFAULT_CAPACITY
943    int result =
944        (configuredExpectedSize == Builder.UNSET_EXPECTED_SIZE)
945            ? DEFAULT_CAPACITY
946            : configuredExpectedSize;
947
948    // Enlarge to contain initial contents
949    if (initialContents instanceof Collection) {
950      int initialSize = ((Collection<?>) initialContents).size();
951      result = max(result, initialSize);
952    }
953
954    // Now cap it at maxSize + 1
955    return capAtMaximumSize(result, maximumSize);
956  }
957
958  private void growIfNeeded() {
959    if (size > queue.length) {
960      int newCapacity = calculateNewCapacity();
961      Object[] newQueue = new Object[newCapacity];
962      arraycopy(queue, 0, newQueue, 0, queue.length);
963      queue = newQueue;
964    }
965  }
966
967  /** Returns ~2x the old capacity if small; ~1.5x otherwise. */
968  private int calculateNewCapacity() {
969    int oldCapacity = queue.length;
970    int newCapacity =
971        (oldCapacity < 64) ? (oldCapacity + 1) * 2 : IntMath.checkedMultiply(oldCapacity / 2, 3);
972    return capAtMaximumSize(newCapacity, maximumSize);
973  }
974
975  /** There's no reason for the queueSize to ever be more than maxSize + 1 */
976  private static int capAtMaximumSize(int queueSize, int maximumSize) {
977    return min(queueSize - 1, maximumSize) + 1; // don't overflow
978  }
979}