--- jsr166/src/main/java/util/PriorityQueue.java 2003/08/06 01:57:53 1.23
+++ jsr166/src/main/java/util/PriorityQueue.java 2003/08/27 10:27:07 1.35
@@ -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.PriorityBlockingQueue} 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;
/**
@@ -81,7 +95,7 @@ public class PriorityQueue extends Ab
/**
* Creates a PriorityQueue with the default initial capacity
* (11) that orders its elements according to their natural
- * ordering (using Comparable.)
+ * ordering (using Comparable).
*/
public PriorityQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
@@ -90,7 +104,7 @@ public class PriorityQueue extends Ab
/**
* Creates a PriorityQueue with the specified initial capacity
* that orders its elements according to their natural ordering
- * (using Comparable.)
+ * (using Comparable).
*
* @param initialCapacity the initial capacity for this priority queue.
* @throws IllegalArgumentException if initialCapacity is less
@@ -159,7 +173,7 @@ public class PriorityQueue extends Ab
* specified collection. The priority queue has an initial
* capacity of 110% of the size of the specified collection or 1
* if the collection is empty. If the specified collection is an
- * instance of a {@link SortedSet} or is another
+ * instance of a {@link java.util.SortedSet} or is another
* PriorityQueue, the priority queue will be sorted
* according to the same comparator, or according to its elements'
* natural order if the collection is sorted according to its
@@ -176,17 +190,16 @@ 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);
- }
- else {
+ } else {
comparator = null;
fillFromUnsorted(c);
}
@@ -379,6 +392,7 @@ public class PriorityQueue extends Ab
}
private class Itr implements Iterator {
+
/**
* Index (into queue array) of element to be returned by
* subsequent call to next.
@@ -399,15 +413,20 @@ public class PriorityQueue extends Ab
*/
private int expectedModCount = modCount;
+ // Workarounds until version that better handles remove() installed.
+ // These are used to copy-on-write the array upon first remove
+ private Object[] q = queue;
+ private int qsize = size;
+
public boolean hasNext() {
- return cursor <= size;
+ return cursor <= qsize;
}
public E next() {
checkForComodification();
- if (cursor > size)
+ if (cursor > qsize)
throw new NoSuchElementException();
- E result = (E) queue[cursor];
+ E result = (E) q[cursor];
lastRet = cursor++;
return result;
}
@@ -417,9 +436,13 @@ public class PriorityQueue extends Ab
throw new IllegalStateException();
checkForComodification();
- PriorityQueue.this.remove(lastRet);
- if (lastRet < cursor)
- cursor--;
+ // Copy on first remove
+ if (q == queue) {
+ q = new Object[queue.length];
+ System.arraycopy(queue, 0, q, 0, queue.length);
+ }
+ PriorityQueue.this.remove(q[lastRet]);
+
lastRet = 0;
expectedModCount = modCount;
}
@@ -451,8 +474,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;
@@ -461,8 +482,11 @@ public class PriorityQueue extends Ab
E result = (E) queue[i];
queue[i] = queue[size];
queue[size--] = null; // Drop extra ref to prevent memory leak
- if (i <= size)
+ if (i <= size) {
fixDown(i);
+ fixUp(i);
+ }
+
return result;
}
@@ -486,7 +510,7 @@ public class PriorityQueue extends Ab
}
} else {
while (k > 1) {
- int j = k >> 1;
+ int j = k >>> 1;
if (comparator.compare((E)queue[j], (E)queue[k]) <= 0)
break;
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
@@ -507,17 +531,20 @@ public class PriorityQueue extends Ab
private void fixDown(int k) {
int j;
if (comparator == null) {
- while ((j = k << 1) <= size) {
- if (j)queue[j]).compareTo((E)queue[j+1]) > 0)
+ 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)
break;
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
k = j;
}
} else {
- while ((j = k << 1) <= size) {
- if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0)
+ while ((j = k << 1) <= size && (j > 0)) {
+ if (j 0)
j++; // j indexes smallest kid
if (comparator.compare((E)queue[k], (E)queue[j]) <= 0)
break;
@@ -525,13 +552,14 @@ public class PriorityQueue extends Ab
k = j;
}
}
+
}
/**
* Returns the comparator used to order this collection, or null
* if this collection is sorted according to its elements natural ordering
- * (using Comparable.)
+ * (using Comparable).
*
* @return the comparator used to order this collection, or null
* if this collection is sorted according to its elements natural ordering.