1 |
|
/* |
2 |
< |
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 2003, 2013, 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. Sun designates this |
7 |
> |
* published by the Free Software Foundation. Oracle designates this |
8 |
|
* particular file as subject to the "Classpath" exception as provided |
9 |
< |
* by Sun in the LICENSE file that accompanied this code. |
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 |
75 |
|
* |
76 |
|
* @since 1.5 |
77 |
|
* @author Josh Bloch, Doug Lea |
78 |
< |
* @param <E> the type of elements held in this collection |
78 |
> |
* @param <E> the type of elements held in this queue |
79 |
|
*/ |
80 |
– |
@SuppressWarnings("unchecked") |
80 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
81 |
|
implements java.io.Serializable { |
82 |
|
|
92 |
|
* heap and each descendant d of n, n <= d. The element with the |
93 |
|
* lowest value is in queue[0], assuming the queue is nonempty. |
94 |
|
*/ |
95 |
< |
private transient Object[] queue; |
95 |
> |
transient Object[] queue; // non-private to simplify nested class access |
96 |
|
|
97 |
|
/** |
98 |
|
* The number of elements in the priority queue. |
99 |
|
*/ |
100 |
< |
private int size = 0; |
100 |
> |
int size; |
101 |
|
|
102 |
|
/** |
103 |
|
* The comparator, or null if priority queue uses elements' |
109 |
|
* The number of times this priority queue has been |
110 |
|
* <i>structurally modified</i>. See AbstractList for gory details. |
111 |
|
*/ |
112 |
< |
private transient int modCount = 0; |
112 |
> |
transient int modCount; // non-private to simplify nested class access |
113 |
|
|
114 |
|
/** |
115 |
|
* Creates a {@code PriorityQueue} with the default initial |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
+ |
* Creates a {@code PriorityQueue} with the default initial capacity and |
138 |
+ |
* whose elements are ordered according to the specified comparator. |
139 |
+ |
* |
140 |
+ |
* @param comparator the comparator that will be used to order this |
141 |
+ |
* priority queue. If {@code null}, the {@linkplain Comparable |
142 |
+ |
* natural ordering} of the elements will be used. |
143 |
+ |
* @since 1.8 |
144 |
+ |
*/ |
145 |
+ |
public PriorityQueue(Comparator<? super E> comparator) { |
146 |
+ |
this(DEFAULT_INITIAL_CAPACITY, comparator); |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
/** |
150 |
|
* Creates a {@code PriorityQueue} with the specified initial capacity |
151 |
|
* that orders its elements according to the specified comparator. |
152 |
|
* |
256 |
|
a = Arrays.copyOf(a, a.length, Object[].class); |
257 |
|
int len = a.length; |
258 |
|
if (len == 1 || this.comparator != null) |
259 |
< |
for (int i = 0; i < len; i++) |
260 |
< |
if (a[i] == null) |
259 |
> |
for (Object e : a) |
260 |
> |
if (e == null) |
261 |
|
throw new NullPointerException(); |
262 |
|
this.queue = a; |
263 |
|
this.size = a.length; |
335 |
|
int i = size; |
336 |
|
if (i >= queue.length) |
337 |
|
grow(i + 1); |
338 |
+ |
siftUp(i, e); |
339 |
|
size = i + 1; |
327 |
– |
if (i == 0) |
328 |
– |
queue[0] = e; |
329 |
– |
else |
330 |
– |
siftUp(i, e); |
340 |
|
return true; |
341 |
|
} |
342 |
|
|
343 |
+ |
@SuppressWarnings("unchecked") |
344 |
|
public E peek() { |
345 |
|
return (size == 0) ? null : (E) queue[0]; |
346 |
|
} |
401 |
|
* @return {@code true} if this queue contains the specified element |
402 |
|
*/ |
403 |
|
public boolean contains(Object o) { |
404 |
< |
return indexOf(o) != -1; |
404 |
> |
return indexOf(o) >= 0; |
405 |
|
} |
406 |
|
|
407 |
|
/** |
439 |
|
* precise control over the runtime type of the output array, and may, |
440 |
|
* under certain circumstances, be used to save allocation costs. |
441 |
|
* |
442 |
< |
* <p>Suppose <tt>x</tt> is a queue known to contain only strings. |
442 |
> |
* <p>Suppose {@code x} is a queue known to contain only strings. |
443 |
|
* The following code can be used to dump the queue into a newly |
444 |
< |
* allocated array of <tt>String</tt>: |
444 |
> |
* allocated array of {@code String}: |
445 |
|
* |
446 |
< |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
446 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
447 |
|
* |
448 |
< |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
449 |
< |
* <tt>toArray()</tt>. |
448 |
> |
* Note that {@code toArray(new Object[0])} is identical in function to |
449 |
> |
* {@code toArray()}. |
450 |
|
* |
451 |
|
* @param a the array into which the elements of the queue are to |
452 |
|
* be stored, if it is big enough; otherwise, a new array of the |
457 |
|
* this queue |
458 |
|
* @throws NullPointerException if the specified array is null |
459 |
|
*/ |
460 |
+ |
@SuppressWarnings("unchecked") |
461 |
|
public <T> T[] toArray(T[] a) { |
462 |
+ |
final int size = this.size; |
463 |
|
if (a.length < size) |
464 |
|
// Make a new array of a's runtime type, but my contents: |
465 |
|
return (T[]) Arrays.copyOf(queue, size, a.getClass()); |
484 |
|
* Index (into queue array) of element to be returned by |
485 |
|
* subsequent call to next. |
486 |
|
*/ |
487 |
< |
private int cursor = 0; |
487 |
> |
private int cursor; |
488 |
|
|
489 |
|
/** |
490 |
|
* Index of element returned by most recent call to next, |
504 |
|
* We expect that most iterations, even those involving removals, |
505 |
|
* will not need to store elements in this field. |
506 |
|
*/ |
507 |
< |
private ArrayDeque<E> forgetMeNot = null; |
507 |
> |
private ArrayDeque<E> forgetMeNot; |
508 |
|
|
509 |
|
/** |
510 |
|
* Element returned by the most recent call to next iff that |
511 |
|
* element was drawn from the forgetMeNot list. |
512 |
|
*/ |
513 |
< |
private E lastRetElt = null; |
513 |
> |
private E lastRetElt; |
514 |
|
|
515 |
|
/** |
516 |
|
* The modCount value that the iterator believes that the backing |
524 |
|
(forgetMeNot != null && !forgetMeNot.isEmpty()); |
525 |
|
} |
526 |
|
|
527 |
+ |
@SuppressWarnings("unchecked") |
528 |
|
public E next() { |
529 |
|
if (expectedModCount != modCount) |
530 |
|
throw new ConcurrentModificationException(); |
577 |
|
size = 0; |
578 |
|
} |
579 |
|
|
580 |
+ |
@SuppressWarnings("unchecked") |
581 |
|
public E poll() { |
582 |
|
if (size == 0) |
583 |
|
return null; |
603 |
|
* position before i. This fact is used by iterator.remove so as to |
604 |
|
* avoid missing traversing elements. |
605 |
|
*/ |
606 |
< |
private E removeAt(int i) { |
606 |
> |
@SuppressWarnings("unchecked") |
607 |
> |
E removeAt(int i) { |
608 |
|
// assert i >= 0 && i < size; |
609 |
|
modCount++; |
610 |
|
int s = --size; |
628 |
|
* promoting x up the tree until it is greater than or equal to |
629 |
|
* its parent, or is the root. |
630 |
|
* |
631 |
< |
* To simplify and speed up coercions and comparisons. the |
631 |
> |
* To simplify and speed up coercions and comparisons, the |
632 |
|
* Comparable and Comparator versions are separated into different |
633 |
|
* methods that are otherwise identical. (Similarly for siftDown.) |
634 |
|
* |
642 |
|
siftUpComparable(k, x); |
643 |
|
} |
644 |
|
|
645 |
+ |
@SuppressWarnings("unchecked") |
646 |
|
private void siftUpComparable(int k, E x) { |
647 |
|
Comparable<? super E> key = (Comparable<? super E>) x; |
648 |
|
while (k > 0) { |
656 |
|
queue[k] = key; |
657 |
|
} |
658 |
|
|
659 |
+ |
@SuppressWarnings("unchecked") |
660 |
|
private void siftUpUsingComparator(int k, E x) { |
661 |
|
while (k > 0) { |
662 |
|
int parent = (k - 1) >>> 1; |
684 |
|
siftDownComparable(k, x); |
685 |
|
} |
686 |
|
|
687 |
+ |
@SuppressWarnings("unchecked") |
688 |
|
private void siftDownComparable(int k, E x) { |
689 |
|
Comparable<? super E> key = (Comparable<? super E>)x; |
690 |
|
int half = size >>> 1; // loop while a non-leaf |
703 |
|
queue[k] = key; |
704 |
|
} |
705 |
|
|
706 |
+ |
@SuppressWarnings("unchecked") |
707 |
|
private void siftDownUsingComparator(int k, E x) { |
708 |
|
int half = size >>> 1; |
709 |
|
while (k < half) { |
725 |
|
* Establishes the heap invariant (described above) in the entire tree, |
726 |
|
* assuming nothing about the order of the elements prior to the call. |
727 |
|
*/ |
728 |
+ |
@SuppressWarnings("unchecked") |
729 |
|
private void heapify() { |
730 |
|
for (int i = (size >>> 1) - 1; i >= 0; i--) |
731 |
|
siftDown(i, (E) queue[i]); |
747 |
|
/** |
748 |
|
* Saves this queue to a stream (that is, serializes it). |
749 |
|
* |
750 |
+ |
* @param s the stream |
751 |
+ |
* @throws java.io.IOException if an I/O error occurs |
752 |
|
* @serialData The length of the array backing the instance is |
753 |
|
* emitted (int), followed by all of its elements |
754 |
|
* (each an {@code Object}) in the proper order. |
767 |
|
} |
768 |
|
|
769 |
|
/** |
770 |
< |
* Reconstitutes this queue from a stream (that is, deserializes it). |
770 |
> |
* Reconstitutes the {@code PriorityQueue} instance from a stream |
771 |
> |
* (that is, deserializes it). |
772 |
> |
* |
773 |
> |
* @param s the stream |
774 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
775 |
> |
* could not be found |
776 |
> |
* @throws java.io.IOException if an I/O error occurs |
777 |
|
*/ |
778 |
|
private void readObject(java.io.ObjectInputStream s) |
779 |
|
throws java.io.IOException, ClassNotFoundException { |