--- jsr166/src/main/java/util/PriorityQueue.java 2003/08/08 20:05:07 1.26
+++ jsr166/src/main/java/util/PriorityQueue.java 2003/08/25 23:47:01 1.32
@@ -28,6 +28,18 @@
* elements are added to a priority queue, its capacity grows
* automatically. The details of the growth policy are not specified.
*
+ *
The Iterator provided in method {@link #iterator()} is not
+ * guaranteed to traverse the elements of the PriorityQueue in any
+ * particular order. If you need ordered traversal, consider using
+ * Arrays.sort(pq.toArray()).
+ *
+ *
Note that this implementation is not synchronized.
+ * Multiple threads should not access a PriorityQueue
+ * instance concurrently if any of the threads modifies the list
+ * structurally. Instead, use the thread-safe {@link
+ * java.util.concurrent.BlockingPriorityQueue} class.
+ *
+ *
*
Implementation note: this implementation provides O(log(n)) time
* for the insertion methods (offer, poll,
* remove() and add) methods; linear time for the
@@ -44,6 +56,8 @@
public class PriorityQueue extends AbstractQueue
implements Queue, java.io.Serializable {
+ private static final long serialVersionUID = -7720805057305804111L;
+
private static final int DEFAULT_INITIAL_CAPACITY = 11;
/**
@@ -176,11 +190,12 @@ public class PriorityQueue extends Ab
*/
public PriorityQueue(Collection extends E> c) {
initializeArray(c);
- if (c instanceof SortedSet extends E>) {
- SortedSet extends E> s = (SortedSet extends E>) c;
+ if (c instanceof SortedSet) {
+ // @fixme double-cast workaround for compiler
+ SortedSet extends E> s = (SortedSet extends E>) (SortedSet)c;
comparator = (Comparator super E>)s.comparator();
fillFromSorted(s);
- } else if (c instanceof PriorityQueue extends E>) {
+ } else if (c instanceof PriorityQueue) {
PriorityQueue extends E> s = (PriorityQueue extends E>) c;
comparator = (Comparator super E>)s.comparator();
fillFromSorted(s);
@@ -416,8 +431,7 @@ public class PriorityQueue extends Ab
checkForComodification();
PriorityQueue.this.remove(lastRet);
- if (lastRet < cursor)
- cursor--;
+ cursor--;
lastRet = 0;
expectedModCount = modCount;
}
@@ -449,8 +463,6 @@ public class PriorityQueue extends Ab
* Removes and returns the ith element from queue. Recall
* that queue is one-based, so 1 <= i <= size.
*
- * XXX: Could further special-case i==size, but is it worth it?
- * XXX: Could special-case i==0, but is it worth it?
*/
private E remove(int i) {
assert i <= size;
@@ -505,7 +517,7 @@ public class PriorityQueue extends Ab
private void fixDown(int k) {
int j;
if (comparator == null) {
- while ((j = k << 1) <= size) {
+ while ((j = k << 1) <= size && j > 0) {
if (j)queue[j]).compareTo((E)queue[j+1]) > 0)
j++; // j indexes smallest kid
if (((Comparable)queue[k]).compareTo((E)queue[j]) <= 0)
@@ -514,7 +526,7 @@ public class PriorityQueue extends Ab
k = j;
}
} else {
- while ((j = k << 1) <= size) {
+ while ((j = k << 1) <= size && j > 0) {
if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0)
j++; // j indexes smallest kid
if (comparator.compare((E)queue[k], (E)queue[j]) <= 0)