1 /* 2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 import android.compat.Compatibility; 29 import android.compat.annotation.ChangeId; 30 import android.compat.annotation.EnabledSince; 31 32 import dalvik.annotation.compat.VersionCodes; 33 import dalvik.system.VMRuntime; 34 35 import java.util.function.Consumer; 36 import java.util.function.Predicate; 37 import jdk.internal.access.SharedSecrets; 38 import jdk.internal.util.ArraysSupport; 39 40 /** 41 * An unbounded priority {@linkplain Queue queue} based on a priority heap. 42 * The elements of the priority queue are ordered according to their 43 * {@linkplain Comparable natural ordering}, or by a {@link Comparator} 44 * provided at queue construction time, depending on which constructor is 45 * used. A priority queue does not permit {@code null} elements. 46 * A priority queue relying on natural ordering also does not permit 47 * insertion of non-comparable objects (doing so may result in 48 * {@code ClassCastException}). 49 * 50 * <p>The <em>head</em> of this queue is the <em>least</em> element 51 * with respect to the specified ordering. If multiple elements are 52 * tied for least value, the head is one of those elements -- ties are 53 * broken arbitrarily. The queue retrieval operations {@code poll}, 54 * {@code remove}, {@code peek}, and {@code element} access the 55 * element at the head of the queue. 56 * 57 * <p>A priority queue is unbounded, but has an internal 58 * <i>capacity</i> governing the size of an array used to store the 59 * elements on the queue. It is always at least as large as the queue 60 * size. As elements are added to a priority queue, its capacity 61 * grows automatically. The details of the growth policy are not 62 * specified. 63 * 64 * <p>This class and its iterator implement all of the 65 * <em>optional</em> methods of the {@link Collection} and {@link 66 * Iterator} interfaces. The Iterator provided in method {@link 67 * #iterator()} and the Spliterator provided in method {@link #spliterator()} 68 * are <em>not</em> guaranteed to traverse the elements of 69 * the priority queue in any particular order. If you need ordered 70 * traversal, consider using {@code Arrays.sort(pq.toArray())}. 71 * 72 * <p><strong>Note that this implementation is not synchronized.</strong> 73 * Multiple threads should not access a {@code PriorityQueue} 74 * instance concurrently if any of the threads modifies the queue. 75 * Instead, use the thread-safe {@link 76 * java.util.concurrent.PriorityBlockingQueue} class. 77 * 78 * <p>Implementation note: this implementation provides 79 * O(log(n)) time for the enqueuing and dequeuing methods 80 * ({@code offer}, {@code poll}, {@code remove()} and {@code add}); 81 * linear time for the {@code remove(Object)} and {@code contains(Object)} 82 * methods; and constant time for the retrieval methods 83 * ({@code peek}, {@code element}, and {@code size}). 84 * 85 * <p>This class is a member of the 86 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> 87 * Java Collections Framework</a>. 88 * 89 * @since 1.5 90 * @author Josh Bloch, Doug Lea 91 * @param <E> the type of elements held in this queue 92 */ 93 @SuppressWarnings("unchecked") 94 public class PriorityQueue<E> extends AbstractQueue<E> 95 implements java.io.Serializable { 96 97 @java.io.Serial 98 private static final long serialVersionUID = -7720805057305804111L; 99 100 private static final int DEFAULT_INITIAL_CAPACITY = 11; 101 102 /** 103 * Priority queue represented as a balanced binary heap: the two 104 * children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The 105 * priority queue is ordered by comparator, or by the elements' 106 * natural ordering, if comparator is null: For each node n in the 107 * heap and each descendant d of n, n <= d. The element with the 108 * lowest value is in queue[0], assuming the queue is nonempty. 109 */ 110 transient Object[] queue; // non-private to simplify nested class access 111 112 /** 113 * The number of elements in the priority queue. 114 */ 115 int size; 116 117 /** 118 * The comparator, or null if priority queue uses elements' 119 * natural ordering. 120 */ 121 @SuppressWarnings("serial") // Conditionally serializable 122 private final Comparator<? super E> comparator; 123 124 /** 125 * The number of times this priority queue has been 126 * <i>structurally modified</i>. See AbstractList for gory details. 127 */ 128 transient int modCount; // non-private to simplify nested class access 129 130 /** 131 * Creates a {@code PriorityQueue} with the default initial 132 * capacity (11) that orders its elements according to their 133 * {@linkplain Comparable natural ordering}. 134 */ PriorityQueue()135 public PriorityQueue() { 136 this(DEFAULT_INITIAL_CAPACITY, null); 137 } 138 139 /** 140 * Creates a {@code PriorityQueue} with the specified initial 141 * capacity that orders its elements according to their 142 * {@linkplain Comparable natural ordering}. 143 * 144 * @param initialCapacity the initial capacity for this priority queue 145 * @throws IllegalArgumentException if {@code initialCapacity} is less 146 * than 1 147 */ PriorityQueue(int initialCapacity)148 public PriorityQueue(int initialCapacity) { 149 this(initialCapacity, null); 150 } 151 152 /** 153 * Creates a {@code PriorityQueue} with the default initial capacity and 154 * whose elements are ordered according to the specified comparator. 155 * 156 * @param comparator the comparator that will be used to order this 157 * priority queue. If {@code null}, the {@linkplain Comparable 158 * natural ordering} of the elements will be used. 159 * @since 1.8 160 */ PriorityQueue(Comparator<? super E> comparator)161 public PriorityQueue(Comparator<? super E> comparator) { 162 this(DEFAULT_INITIAL_CAPACITY, comparator); 163 } 164 165 /** 166 * Creates a {@code PriorityQueue} with the specified initial capacity 167 * that orders its elements according to the specified comparator. 168 * 169 * @param initialCapacity the initial capacity for this priority queue 170 * @param comparator the comparator that will be used to order this 171 * priority queue. If {@code null}, the {@linkplain Comparable 172 * natural ordering} of the elements will be used. 173 * @throws IllegalArgumentException if {@code initialCapacity} is 174 * less than 1 175 */ PriorityQueue(int initialCapacity, Comparator<? super E> comparator)176 public PriorityQueue(int initialCapacity, 177 Comparator<? super E> comparator) { 178 // Note: This restriction of at least one is not actually needed, 179 // but continues for 1.5 compatibility 180 if (initialCapacity < 1) 181 throw new IllegalArgumentException(); 182 this.queue = new Object[initialCapacity]; 183 this.comparator = comparator; 184 } 185 186 /** 187 * Creates a {@code PriorityQueue} containing the elements in the 188 * specified collection. If the specified collection is an instance of 189 * a {@link SortedSet} or is another {@code PriorityQueue}, this 190 * priority queue will be ordered according to the same ordering. 191 * Otherwise, this priority queue will be ordered according to the 192 * {@linkplain Comparable natural ordering} of its elements. 193 * 194 * @param c the collection whose elements are to be placed 195 * into this priority queue 196 * @throws ClassCastException if elements of the specified collection 197 * cannot be compared to one another according to the priority 198 * queue's ordering 199 * @throws NullPointerException if the specified collection or any 200 * of its elements are null 201 */ PriorityQueue(Collection<? extends E> c)202 public PriorityQueue(Collection<? extends E> c) { 203 if (c instanceof SortedSet<?>) { 204 SortedSet<? extends E> ss = (SortedSet<? extends E>) c; 205 this.comparator = (Comparator<? super E>) ss.comparator(); 206 initElementsFromCollection(ss); 207 } 208 else if (c instanceof PriorityQueue<?>) { 209 PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c; 210 this.comparator = (Comparator<? super E>) pq.comparator(); 211 initFromPriorityQueue(pq); 212 } 213 else { 214 this.comparator = null; 215 initFromCollection(c); 216 } 217 } 218 219 /** 220 * Creates a {@code PriorityQueue} containing the elements in the 221 * specified priority queue. This priority queue will be 222 * ordered according to the same ordering as the given priority 223 * queue. 224 * 225 * @param c the priority queue whose elements are to be placed 226 * into this priority queue 227 * @throws ClassCastException if elements of {@code c} cannot be 228 * compared to one another according to {@code c}'s 229 * ordering 230 * @throws NullPointerException if the specified priority queue or any 231 * of its elements are null 232 */ PriorityQueue(PriorityQueue<? extends E> c)233 public PriorityQueue(PriorityQueue<? extends E> c) { 234 this.comparator = (Comparator<? super E>) c.comparator(); 235 initFromPriorityQueue(c); 236 } 237 238 /** 239 * Creates a {@code PriorityQueue} containing the elements in the 240 * specified sorted set. This priority queue will be ordered 241 * according to the same ordering as the given sorted set. 242 * 243 * @param c the sorted set whose elements are to be placed 244 * into this priority queue 245 * @throws ClassCastException if elements of the specified sorted 246 * set cannot be compared to one another according to the 247 * sorted set's ordering 248 * @throws NullPointerException if the specified sorted set or any 249 * of its elements are null 250 */ PriorityQueue(SortedSet<? extends E> c)251 public PriorityQueue(SortedSet<? extends E> c) { 252 this.comparator = (Comparator<? super E>) c.comparator(); 253 initElementsFromCollection(c); 254 } 255 256 /** Ensures that queue[0] exists, helping peek() and poll(). */ ensureNonEmpty(Object[] es)257 private static Object[] ensureNonEmpty(Object[] es) { 258 return (es.length > 0) ? es : new Object[1]; 259 } 260 initFromPriorityQueue(PriorityQueue<? extends E> c)261 private void initFromPriorityQueue(PriorityQueue<? extends E> c) { 262 if (c.getClass() == PriorityQueue.class) { 263 this.queue = ensureNonEmpty(c.toArray()); 264 this.size = c.size(); 265 } else { 266 initFromCollection(c); 267 } 268 } 269 initElementsFromCollection(Collection<? extends E> c)270 private void initElementsFromCollection(Collection<? extends E> c) { 271 Object[] es = c.toArray(); 272 int len = es.length; 273 if (c.getClass() != ArrayList.class) 274 es = Arrays.copyOf(es, len, Object[].class); 275 if (len == 1 || this.comparator != null) 276 for (Object e : es) 277 if (e == null) 278 throw new NullPointerException(); 279 this.queue = ensureNonEmpty(es); 280 this.size = len; 281 } 282 283 /** 284 * Initializes queue array with elements from the given Collection. 285 * 286 * @param c the collection 287 */ initFromCollection(Collection<? extends E> c)288 private void initFromCollection(Collection<? extends E> c) { 289 initElementsFromCollection(c); 290 heapify(); 291 } 292 293 /** 294 * Increases the capacity of the array. 295 * 296 * @param minCapacity the desired minimum capacity 297 */ grow(int minCapacity)298 private void grow(int minCapacity) { 299 int oldCapacity = queue.length; 300 // Double size if small; else grow by 50% 301 int newCapacity = ArraysSupport.newLength(oldCapacity, 302 minCapacity - oldCapacity, /* minimum growth */ 303 oldCapacity < 64 ? oldCapacity + 2 : oldCapacity >> 1 304 /* preferred growth */); 305 queue = Arrays.copyOf(queue, newCapacity); 306 } 307 308 /** 309 * Inserts the specified element into this priority queue. 310 * 311 * @return {@code true} (as specified by {@link Collection#add}) 312 * @throws ClassCastException if the specified element cannot be 313 * compared with elements currently in this priority queue 314 * according to the priority queue's ordering 315 * @throws NullPointerException if the specified element is null 316 */ add(E e)317 public boolean add(E e) { 318 return offer(e); 319 } 320 321 /** 322 * Inserts the specified element into this priority queue. 323 * 324 * @return {@code true} (as specified by {@link Queue#offer}) 325 * @throws ClassCastException if the specified element cannot be 326 * compared with elements currently in this priority queue 327 * according to the priority queue's ordering 328 * @throws NullPointerException if the specified element is null 329 */ offer(E e)330 public boolean offer(E e) { 331 if (e == null) 332 throw new NullPointerException(); 333 modCount++; 334 int i = size; 335 if (i >= queue.length) 336 grow(i + 1); 337 if (i == 0) { 338 // Android-changed: Keep old behavior on Android 13 or below. http://b/289878283 339 boolean usePreAndroidUBehavior = VMRuntime.getSdkVersion() < VersionCodes.UPSIDE_DOWN_CAKE 340 || !Compatibility.isChangeEnabled(PRIORITY_QUEUE_OFFER_NON_COMPARABLE_ONE_ELEMENT); 341 if (usePreAndroidUBehavior) { 342 queue[0] = e; 343 } else { 344 siftUp(i, e); 345 } 346 } else { 347 siftUp(i, e); 348 } 349 size = i + 1; 350 return true; 351 } 352 353 public E peek() { 354 return (E) queue[0]; 355 } 356 357 private int indexOf(Object o) { 358 if (o != null) { 359 final Object[] es = queue; 360 for (int i = 0, n = size; i < n; i++) 361 if (o.equals(es[i])) 362 return i; 363 } 364 return -1; 365 } 366 367 /** 368 * Removes a single instance of the specified element from this queue, 369 * if it is present. More formally, removes an element {@code e} such 370 * that {@code o.equals(e)}, if this queue contains one or more such 371 * elements. Returns {@code true} if and only if this queue contained 372 * the specified element (or equivalently, if this queue changed as a 373 * result of the call). 374 * 375 * @param o element to be removed from this queue, if present 376 * @return {@code true} if this queue changed as a result of the call 377 */ 378 public boolean remove(Object o) { 379 int i = indexOf(o); 380 if (i == -1) 381 return false; 382 else { 383 removeAt(i); 384 return true; 385 } 386 } 387 388 /** 389 * Identity-based version for use in Itr.remove. 390 * 391 * @param o element to be removed from this queue, if present 392 */ 393 void removeEq(Object o) { 394 final Object[] es = queue; 395 for (int i = 0, n = size; i < n; i++) { 396 if (o == es[i]) { 397 removeAt(i); 398 break; 399 } 400 } 401 } 402 403 /** 404 * Returns {@code true} if this queue contains the specified element. 405 * More formally, returns {@code true} if and only if this queue contains 406 * at least one element {@code e} such that {@code o.equals(e)}. 407 * 408 * @param o object to be checked for containment in this queue 409 * @return {@code true} if this queue contains the specified element 410 */ 411 public boolean contains(Object o) { 412 return indexOf(o) >= 0; 413 } 414 415 /** 416 * Returns an array containing all of the elements in this queue. 417 * The elements are in no particular order. 418 * 419 * <p>The returned array will be "safe" in that no references to it are 420 * maintained by this queue. (In other words, this method must allocate 421 * a new array). The caller is thus free to modify the returned array. 422 * 423 * <p>This method acts as bridge between array-based and collection-based 424 * APIs. 425 * 426 * @return an array containing all of the elements in this queue 427 */ 428 public Object[] toArray() { 429 return Arrays.copyOf(queue, size); 430 } 431 432 /** 433 * Returns an array containing all of the elements in this queue; the 434 * runtime type of the returned array is that of the specified array. 435 * The returned array elements are in no particular order. 436 * If the queue fits in the specified array, it is returned therein. 437 * Otherwise, a new array is allocated with the runtime type of the 438 * specified array and the size of this queue. 439 * 440 * <p>If the queue fits in the specified array with room to spare 441 * (i.e., the array has more elements than the queue), the element in 442 * the array immediately following the end of the collection is set to 443 * {@code null}. 444 * 445 * <p>Like the {@link #toArray()} method, this method acts as bridge between 446 * array-based and collection-based APIs. Further, this method allows 447 * precise control over the runtime type of the output array, and may, 448 * under certain circumstances, be used to save allocation costs. 449 * 450 * <p>Suppose {@code x} is a queue known to contain only strings. 451 * The following code can be used to dump the queue into a newly 452 * allocated array of {@code String}: 453 * 454 * <pre> {@code String[] y = x.toArray(new String[0]);}</pre> 455 * 456 * Note that {@code toArray(new Object[0])} is identical in function to 457 * {@code toArray()}. 458 * 459 * @param a the array into which the elements of the queue are to 460 * be stored, if it is big enough; otherwise, a new array of the 461 * same runtime type is allocated for this purpose. 462 * @return an array containing all of the elements in this queue 463 * @throws ArrayStoreException if the runtime type of the specified array 464 * is not a supertype of the runtime type of every element in 465 * this queue 466 * @throws NullPointerException if the specified array is null 467 */ 468 public <T> T[] toArray(T[] a) { 469 final int size = this.size; 470 if (a.length < size) 471 // Make a new array of a's runtime type, but my contents: 472 return (T[]) Arrays.copyOf(queue, size, a.getClass()); 473 System.arraycopy(queue, 0, a, 0, size); 474 if (a.length > size) 475 a[size] = null; 476 return a; 477 } 478 479 /** 480 * Returns an iterator over the elements in this queue. The iterator 481 * does not return the elements in any particular order. 482 * 483 * @return an iterator over the elements in this queue 484 */ 485 public Iterator<E> iterator() { 486 return new Itr(); 487 } 488 489 private final class Itr implements Iterator<E> { 490 /** 491 * Index (into queue array) of element to be returned by 492 * subsequent call to next. 493 */ 494 private int cursor; 495 496 /** 497 * Index of element returned by most recent call to next, 498 * unless that element came from the forgetMeNot list. 499 * Set to -1 if element is deleted by a call to remove. 500 */ 501 private int lastRet = -1; 502 503 /** 504 * A queue of elements that were moved from the unvisited portion of 505 * the heap into the visited portion as a result of "unlucky" element 506 * removals during the iteration. (Unlucky element removals are those 507 * that require a siftup instead of a siftdown.) We must visit all of 508 * the elements in this list to complete the iteration. We do this 509 * after we've completed the "normal" iteration. 510 * 511 * We expect that most iterations, even those involving removals, 512 * will not need to store elements in this field. 513 */ 514 private ArrayDeque<E> forgetMeNot; 515 516 /** 517 * Element returned by the most recent call to next iff that 518 * element was drawn from the forgetMeNot list. 519 */ 520 private E lastRetElt; 521 522 /** 523 * The modCount value that the iterator believes that the backing 524 * Queue should have. If this expectation is violated, the iterator 525 * has detected concurrent modification. 526 */ 527 private int expectedModCount = modCount; 528 529 Itr() {} // prevent access constructor creation 530 531 public boolean hasNext() { 532 return cursor < size || 533 (forgetMeNot != null && !forgetMeNot.isEmpty()); 534 } 535 536 public E next() { 537 if (expectedModCount != modCount) 538 throw new ConcurrentModificationException(); 539 if (cursor < size) 540 return (E) queue[lastRet = cursor++]; 541 if (forgetMeNot != null) { 542 lastRet = -1; 543 lastRetElt = forgetMeNot.poll(); 544 if (lastRetElt != null) 545 return lastRetElt; 546 } 547 throw new NoSuchElementException(); 548 } 549 550 public void remove() { 551 if (expectedModCount != modCount) 552 throw new ConcurrentModificationException(); 553 if (lastRet != -1) { 554 E moved = PriorityQueue.this.removeAt(lastRet); 555 lastRet = -1; 556 if (moved == null) 557 cursor--; 558 else { 559 if (forgetMeNot == null) 560 forgetMeNot = new ArrayDeque<>(); 561 forgetMeNot.add(moved); 562 } 563 } else if (lastRetElt != null) { 564 PriorityQueue.this.removeEq(lastRetElt); 565 lastRetElt = null; 566 } else { 567 throw new IllegalStateException(); 568 } 569 expectedModCount = modCount; 570 } 571 } 572 573 public int size() { 574 return size; 575 } 576 577 /** 578 * Removes all of the elements from this priority queue. 579 * The queue will be empty after this call returns. 580 */ 581 public void clear() { 582 modCount++; 583 final Object[] es = queue; 584 for (int i = 0, n = size; i < n; i++) 585 es[i] = null; 586 size = 0; 587 } 588 589 public E poll() { 590 final Object[] es; 591 final E result; 592 593 if ((result = (E) ((es = queue)[0])) != null) { 594 modCount++; 595 final int n; 596 final E x = (E) es[(n = --size)]; 597 es[n] = null; 598 if (n > 0) { 599 final Comparator<? super E> cmp; 600 if ((cmp = comparator) == null) 601 siftDownComparable(0, x, es, n); 602 else 603 siftDownUsingComparator(0, x, es, n, cmp); 604 } 605 } 606 return result; 607 } 608 609 /** 610 * Removes the ith element from queue. 611 * 612 * Normally this method leaves the elements at up to i-1, 613 * inclusive, untouched. Under these circumstances, it returns 614 * null. Occasionally, in order to maintain the heap invariant, 615 * it must swap a later element of the list with one earlier than 616 * i. Under these circumstances, this method returns the element 617 * that was previously at the end of the list and is now at some 618 * position before i. This fact is used by iterator.remove so as to 619 * avoid missing traversing elements. 620 */ 621 E removeAt(int i) { 622 // assert i >= 0 && i < size; 623 final Object[] es = queue; 624 modCount++; 625 int s = --size; 626 if (s == i) // removed last element 627 es[i] = null; 628 else { 629 E moved = (E) es[s]; 630 es[s] = null; 631 siftDown(i, moved); 632 if (es[i] == moved) { 633 siftUp(i, moved); 634 if (es[i] != moved) 635 return moved; 636 } 637 } 638 return null; 639 } 640 641 /** 642 * Inserts item x at position k, maintaining heap invariant by 643 * promoting x up the tree until it is greater than or equal to 644 * its parent, or is the root. 645 * 646 * To simplify and speed up coercions and comparisons, the 647 * Comparable and Comparator versions are separated into different 648 * methods that are otherwise identical. (Similarly for siftDown.) 649 * 650 * @param k the position to fill 651 * @param x the item to insert 652 */ 653 private void siftUp(int k, E x) { 654 if (comparator != null) 655 siftUpUsingComparator(k, x, queue, comparator); 656 else 657 siftUpComparable(k, x, queue); 658 } 659 660 private static <T> void siftUpComparable(int k, T x, Object[] es) { 661 Comparable<? super T> key = (Comparable<? super T>) x; 662 while (k > 0) { 663 int parent = (k - 1) >>> 1; 664 Object e = es[parent]; 665 if (key.compareTo((T) e) >= 0) 666 break; 667 es[k] = e; 668 k = parent; 669 } 670 es[k] = key; 671 } 672 siftUpUsingComparator( int k, T x, Object[] es, Comparator<? super T> cmp)673 private static <T> void siftUpUsingComparator( 674 int k, T x, Object[] es, Comparator<? super T> cmp) { 675 while (k > 0) { 676 int parent = (k - 1) >>> 1; 677 Object e = es[parent]; 678 if (cmp.compare(x, (T) e) >= 0) 679 break; 680 es[k] = e; 681 k = parent; 682 } 683 es[k] = x; 684 } 685 686 /** 687 * Inserts item x at position k, maintaining heap invariant by 688 * demoting x down the tree repeatedly until it is less than or 689 * equal to its children or is a leaf. 690 * 691 * @param k the position to fill 692 * @param x the item to insert 693 */ siftDown(int k, E x)694 private void siftDown(int k, E x) { 695 if (comparator != null) 696 siftDownUsingComparator(k, x, queue, size, comparator); 697 else 698 siftDownComparable(k, x, queue, size); 699 } 700 siftDownComparable(int k, T x, Object[] es, int n)701 private static <T> void siftDownComparable(int k, T x, Object[] es, int n) { 702 // assert n > 0; 703 Comparable<? super T> key = (Comparable<? super T>)x; 704 int half = n >>> 1; // loop while a non-leaf 705 while (k < half) { 706 int child = (k << 1) + 1; // assume left child is least 707 Object c = es[child]; 708 int right = child + 1; 709 if (right < n && 710 ((Comparable<? super T>) c).compareTo((T) es[right]) > 0) 711 c = es[child = right]; 712 if (key.compareTo((T) c) <= 0) 713 break; 714 es[k] = c; 715 k = child; 716 } 717 es[k] = key; 718 } 719 siftDownUsingComparator( int k, T x, Object[] es, int n, Comparator<? super T> cmp)720 private static <T> void siftDownUsingComparator( 721 int k, T x, Object[] es, int n, Comparator<? super T> cmp) { 722 // assert n > 0; 723 int half = n >>> 1; 724 while (k < half) { 725 int child = (k << 1) + 1; 726 Object c = es[child]; 727 int right = child + 1; 728 if (right < n && cmp.compare((T) c, (T) es[right]) > 0) 729 c = es[child = right]; 730 if (cmp.compare(x, (T) c) <= 0) 731 break; 732 es[k] = c; 733 k = child; 734 } 735 es[k] = x; 736 } 737 738 /** 739 * Establishes the heap invariant (described above) in the entire tree, 740 * assuming nothing about the order of the elements prior to the call. 741 * This classic algorithm due to Floyd (1964) is known to be O(size). 742 */ heapify()743 private void heapify() { 744 final Object[] es = queue; 745 int n = size, i = (n >>> 1) - 1; 746 final Comparator<? super E> cmp; 747 if ((cmp = comparator) == null) 748 for (; i >= 0; i--) 749 siftDownComparable(i, (E) es[i], es, n); 750 else 751 for (; i >= 0; i--) 752 siftDownUsingComparator(i, (E) es[i], es, n, cmp); 753 } 754 755 /** 756 * Returns the comparator used to order the elements in this 757 * queue, or {@code null} if this queue is sorted according to 758 * the {@linkplain Comparable natural ordering} of its elements. 759 * 760 * @return the comparator used to order this queue, or 761 * {@code null} if this queue is sorted according to the 762 * natural ordering of its elements 763 */ comparator()764 public Comparator<? super E> comparator() { 765 return comparator; 766 } 767 768 /** 769 * Saves this queue to a stream (that is, serializes it). 770 * 771 * @param s the stream 772 * @throws java.io.IOException if an I/O error occurs 773 * @serialData The length of the array backing the instance is 774 * emitted (int), followed by all of its elements 775 * (each an {@code Object}) in the proper order. 776 */ 777 @java.io.Serial writeObject(java.io.ObjectOutputStream s)778 private void writeObject(java.io.ObjectOutputStream s) 779 throws java.io.IOException { 780 // Write out element count, and any hidden stuff 781 s.defaultWriteObject(); 782 783 // Write out array length, for compatibility with 1.5 version 784 s.writeInt(Math.max(2, size + 1)); 785 786 // Write out all elements in the "proper order". 787 final Object[] es = queue; 788 for (int i = 0, n = size; i < n; i++) 789 s.writeObject(es[i]); 790 } 791 792 /** 793 * Reconstitutes the {@code PriorityQueue} instance from a stream 794 * (that is, deserializes it). 795 * 796 * @param s the stream 797 * @throws ClassNotFoundException if the class of a serialized object 798 * could not be found 799 * @throws java.io.IOException if an I/O error occurs 800 */ 801 @java.io.Serial readObject(java.io.ObjectInputStream s)802 private void readObject(java.io.ObjectInputStream s) 803 throws java.io.IOException, ClassNotFoundException { 804 // Read in size, and any hidden stuff 805 s.defaultReadObject(); 806 807 // Read in (and discard) array length 808 s.readInt(); 809 810 SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size); 811 final Object[] es = queue = new Object[Math.max(size, 1)]; 812 813 // Read in all elements. 814 for (int i = 0, n = size; i < n; i++) 815 es[i] = s.readObject(); 816 817 // Elements are guaranteed to be in "proper order", but the 818 // spec has never explained what that might be. 819 heapify(); 820 } 821 822 /** 823 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> 824 * and <em>fail-fast</em> {@link Spliterator} over the elements in this 825 * queue. The spliterator does not traverse elements in any particular order 826 * (the {@link Spliterator#ORDERED ORDERED} characteristic is not reported). 827 * 828 * <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, 829 * {@link Spliterator#SUBSIZED}, and {@link Spliterator#NONNULL}. 830 * Overriding implementations should document the reporting of additional 831 * characteristic values. 832 * 833 * @return a {@code Spliterator} over the elements in this queue 834 * @since 1.8 835 */ spliterator()836 public final Spliterator<E> spliterator() { 837 return new PriorityQueueSpliterator(0, -1, 0); 838 } 839 840 final class PriorityQueueSpliterator implements Spliterator<E> { 841 private int index; // current index, modified on advance/split 842 private int fence; // -1 until first use 843 private int expectedModCount; // initialized when fence set 844 845 /** Creates new spliterator covering the given range. */ PriorityQueueSpliterator(int origin, int fence, int expectedModCount)846 PriorityQueueSpliterator(int origin, int fence, int expectedModCount) { 847 this.index = origin; 848 this.fence = fence; 849 this.expectedModCount = expectedModCount; 850 } 851 getFence()852 private int getFence() { // initialize fence to size on first use 853 int hi; 854 if ((hi = fence) < 0) { 855 expectedModCount = modCount; 856 hi = fence = size; 857 } 858 return hi; 859 } 860 trySplit()861 public PriorityQueueSpliterator trySplit() { 862 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; 863 return (lo >= mid) ? null : 864 new PriorityQueueSpliterator(lo, index = mid, expectedModCount); 865 } 866 forEachRemaining(Consumer<? super E> action)867 public void forEachRemaining(Consumer<? super E> action) { 868 if (action == null) 869 throw new NullPointerException(); 870 if (fence < 0) { fence = size; expectedModCount = modCount; } 871 final Object[] es = queue; 872 int i, hi; E e; 873 for (i = index, index = hi = fence; i < hi; i++) { 874 if ((e = (E) es[i]) == null) 875 break; // must be CME 876 action.accept(e); 877 } 878 if (modCount != expectedModCount) 879 throw new ConcurrentModificationException(); 880 } 881 tryAdvance(Consumer<? super E> action)882 public boolean tryAdvance(Consumer<? super E> action) { 883 if (action == null) 884 throw new NullPointerException(); 885 if (fence < 0) { fence = size; expectedModCount = modCount; } 886 int i; 887 if ((i = index) < fence) { 888 index = i + 1; 889 E e; 890 if ((e = (E) queue[i]) == null 891 || modCount != expectedModCount) 892 throw new ConcurrentModificationException(); 893 action.accept(e); 894 return true; 895 } 896 return false; 897 } 898 estimateSize()899 public long estimateSize() { 900 return getFence() - index; 901 } 902 characteristics()903 public int characteristics() { 904 return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL; 905 } 906 } 907 908 /** 909 * @throws NullPointerException {@inheritDoc} 910 */ removeIf(Predicate<? super E> filter)911 public boolean removeIf(Predicate<? super E> filter) { 912 Objects.requireNonNull(filter); 913 return bulkRemove(filter); 914 } 915 916 /** 917 * @throws NullPointerException {@inheritDoc} 918 */ removeAll(Collection<?> c)919 public boolean removeAll(Collection<?> c) { 920 Objects.requireNonNull(c); 921 return bulkRemove(e -> c.contains(e)); 922 } 923 924 /** 925 * @throws NullPointerException {@inheritDoc} 926 */ retainAll(Collection<?> c)927 public boolean retainAll(Collection<?> c) { 928 Objects.requireNonNull(c); 929 return bulkRemove(e -> !c.contains(e)); 930 } 931 932 // A tiny bit set implementation 933 nBits(int n)934 private static long[] nBits(int n) { 935 return new long[((n - 1) >> 6) + 1]; 936 } setBit(long[] bits, int i)937 private static void setBit(long[] bits, int i) { 938 bits[i >> 6] |= 1L << i; 939 } isClear(long[] bits, int i)940 private static boolean isClear(long[] bits, int i) { 941 return (bits[i >> 6] & (1L << i)) == 0; 942 } 943 944 /** Implementation of bulk remove methods. */ bulkRemove(Predicate<? super E> filter)945 private boolean bulkRemove(Predicate<? super E> filter) { 946 final int expectedModCount = ++modCount; 947 final Object[] es = queue; 948 final int end = size; 949 int i; 950 // Optimize for initial run of survivors 951 for (i = 0; i < end && !filter.test((E) es[i]); i++) 952 ; 953 if (i >= end) { 954 if (modCount != expectedModCount) 955 throw new ConcurrentModificationException(); 956 return false; 957 } 958 // Tolerate predicates that reentrantly access the collection for 959 // read (but writers still get CME), so traverse once to find 960 // elements to delete, a second pass to physically expunge. 961 final int beg = i; 962 final long[] deathRow = nBits(end - beg); 963 deathRow[0] = 1L; // set bit 0 964 for (i = beg + 1; i < end; i++) 965 if (filter.test((E) es[i])) 966 setBit(deathRow, i - beg); 967 if (modCount != expectedModCount) 968 throw new ConcurrentModificationException(); 969 int w = beg; 970 for (i = beg; i < end; i++) 971 if (isClear(deathRow, i - beg)) 972 es[w++] = es[i]; 973 for (i = size = w; i < end; i++) 974 es[i] = null; 975 heapify(); 976 return true; 977 } 978 979 /** 980 * @throws NullPointerException {@inheritDoc} 981 */ forEach(Consumer<? super E> action)982 public void forEach(Consumer<? super E> action) { 983 Objects.requireNonNull(action); 984 final int expectedModCount = modCount; 985 final Object[] es = queue; 986 for (int i = 0, n = size; i < n; i++) 987 action.accept((E) es[i]); 988 if (expectedModCount != modCount) 989 throw new ConcurrentModificationException(); 990 } 991 992 // Android-added: Backward-compatible flag for offer() API. 993 /** 994 * Since Android 14, {@link PriorityQueue#offer(E)} requires all elements to be comparable if 995 * there was no comparator. Previously, the first element being added did not need to be 996 * comparable. 997 * 998 * This flag is enabled for apps targeting Android 14+. 999 * 1000 * @hide 1001 */ 1002 @ChangeId 1003 @EnabledSince(targetSdkVersion = VersionCodes.UPSIDE_DOWN_CAKE) 1004 public static final long PRIORITY_QUEUE_OFFER_NON_COMPARABLE_ONE_ELEMENT = 289878283L; 1005 } 1006