28 |
|
* elements are added to a priority queue, its capacity grows |
29 |
|
* automatically. The details of the growth policy are not specified. |
30 |
|
* |
31 |
+ |
* <p>The Iterator provided in method {@link #iterator()} is <em>not</em> |
32 |
+ |
* guaranteed to traverse the elements of the PriorityQueue in any |
33 |
+ |
* particular order. If you need ordered traversal, consider using |
34 |
+ |
* <tt>Arrays.sort(pq.toArray())</tt>. |
35 |
+ |
* |
36 |
+ |
* <p> <strong>Note that this implementation is not synchronized.</strong> |
37 |
+ |
* Multiple threads should not access a <tt>PriorityQueue</tt> |
38 |
+ |
* instance concurrently if any of the threads modifies the list |
39 |
+ |
* structurally. Instead, use the thread-safe {@link |
40 |
+ |
* java.util.concurrent.PriorityBlockingQueue} class. |
41 |
+ |
* |
42 |
+ |
* |
43 |
|
* <p>Implementation note: this implementation provides O(log(n)) time |
44 |
|
* for the insertion methods (<tt>offer</tt>, <tt>poll</tt>, |
45 |
|
* <tt>remove()</tt> and <tt>add</tt>) methods; linear time for the |
56 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
57 |
|
implements Queue<E>, java.io.Serializable { |
58 |
|
|
59 |
+ |
private static final long serialVersionUID = -7720805057305804111L; |
60 |
+ |
|
61 |
|
private static final int DEFAULT_INITIAL_CAPACITY = 11; |
62 |
|
|
63 |
|
/** |
173 |
|
* specified collection. The priority queue has an initial |
174 |
|
* capacity of 110% of the size of the specified collection or 1 |
175 |
|
* if the collection is empty. If the specified collection is an |
176 |
< |
* instance of a {@link SortedSet} or is another |
176 |
> |
* instance of a {@link java.util.SortedSet} or is another |
177 |
|
* <tt>PriorityQueue</tt>, the priority queue will be sorted |
178 |
|
* according to the same comparator, or according to its elements' |
179 |
|
* natural order if the collection is sorted according to its |
190 |
|
*/ |
191 |
|
public PriorityQueue(Collection<? extends E> c) { |
192 |
|
initializeArray(c); |
193 |
< |
if (c instanceof SortedSet<? extends E>) { |
194 |
< |
SortedSet<? extends E> s = (SortedSet<? extends E>) c; |
193 |
> |
if (c instanceof SortedSet) { |
194 |
> |
// @fixme double-cast workaround for compiler |
195 |
> |
SortedSet<? extends E> s = (SortedSet<? extends E>) (SortedSet)c; |
196 |
|
comparator = (Comparator<? super E>)s.comparator(); |
197 |
|
fillFromSorted(s); |
198 |
< |
} |
184 |
< |
else if (c instanceof PriorityQueue<? extends E>) { |
198 |
> |
} else if (c instanceof PriorityQueue) { |
199 |
|
PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c; |
200 |
|
comparator = (Comparator<? super E>)s.comparator(); |
201 |
|
fillFromSorted(s); |
202 |
< |
} |
189 |
< |
else { |
202 |
> |
} else { |
203 |
|
comparator = null; |
204 |
|
fillFromUnsorted(c); |
205 |
|
} |
392 |
|
} |
393 |
|
|
394 |
|
private class Itr implements Iterator<E> { |
395 |
+ |
|
396 |
|
/** |
397 |
|
* Index (into queue array) of element to be returned by |
398 |
|
* subsequent call to next. |
413 |
|
*/ |
414 |
|
private int expectedModCount = modCount; |
415 |
|
|
416 |
+ |
// Workarounds until version that better handles remove() installed. |
417 |
+ |
// These are used to copy-on-write the array upon first remove |
418 |
+ |
private Object[] q = queue; |
419 |
+ |
private int qsize = size; |
420 |
+ |
|
421 |
|
public boolean hasNext() { |
422 |
< |
return cursor <= size; |
422 |
> |
return cursor <= qsize; |
423 |
|
} |
424 |
|
|
425 |
|
public E next() { |
426 |
|
checkForComodification(); |
427 |
< |
if (cursor > size) |
427 |
> |
if (cursor > qsize) |
428 |
|
throw new NoSuchElementException(); |
429 |
< |
E result = (E) queue[cursor]; |
429 |
> |
E result = (E) q[cursor]; |
430 |
|
lastRet = cursor++; |
431 |
|
return result; |
432 |
|
} |
436 |
|
throw new IllegalStateException(); |
437 |
|
checkForComodification(); |
438 |
|
|
439 |
< |
PriorityQueue.this.remove(lastRet); |
440 |
< |
if (lastRet < cursor) |
441 |
< |
cursor--; |
439 |
> |
// Copy on first remove |
440 |
> |
if (q == queue) { |
441 |
> |
q = new Object[queue.length]; |
442 |
> |
System.arraycopy(queue, 0, q, 0, queue.length); |
443 |
> |
} |
444 |
> |
PriorityQueue.this.remove(q[lastRet]); |
445 |
> |
|
446 |
|
lastRet = 0; |
447 |
|
expectedModCount = modCount; |
448 |
|
} |
474 |
|
* Removes and returns the ith element from queue. Recall |
475 |
|
* that queue is one-based, so 1 <= i <= size. |
476 |
|
* |
454 |
– |
* XXX: Could further special-case i==size, but is it worth it? |
455 |
– |
* XXX: Could special-case i==0, but is it worth it? |
477 |
|
*/ |
478 |
|
private E remove(int i) { |
479 |
|
assert i <= size; |
482 |
|
E result = (E) queue[i]; |
483 |
|
queue[i] = queue[size]; |
484 |
|
queue[size--] = null; // Drop extra ref to prevent memory leak |
485 |
< |
if (i <= size) |
485 |
> |
if (i <= size) { |
486 |
|
fixDown(i); |
487 |
+ |
fixUp(i); |
488 |
+ |
} |
489 |
+ |
|
490 |
|
return result; |
491 |
|
} |
492 |
|
|
510 |
|
} |
511 |
|
} else { |
512 |
|
while (k > 1) { |
513 |
< |
int j = k >> 1; |
513 |
> |
int j = k >>> 1; |
514 |
|
if (comparator.compare((E)queue[j], (E)queue[k]) <= 0) |
515 |
|
break; |
516 |
|
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
531 |
|
private void fixDown(int k) { |
532 |
|
int j; |
533 |
|
if (comparator == null) { |
534 |
< |
while ((j = k << 1) <= size) { |
535 |
< |
if (j<size && ((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0) |
534 |
> |
while ((j = k << 1) <= size && (j > 0)) { |
535 |
> |
if (j<size && |
536 |
> |
((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0) |
537 |
|
j++; // j indexes smallest kid |
538 |
+ |
|
539 |
|
if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0) |
540 |
|
break; |
541 |
|
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
542 |
|
k = j; |
543 |
|
} |
544 |
|
} else { |
545 |
< |
while ((j = k << 1) <= size) { |
546 |
< |
if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0) |
545 |
> |
while ((j = k << 1) <= size && (j > 0)) { |
546 |
> |
if (j<size && |
547 |
> |
comparator.compare((E)queue[j], (E)queue[j+1]) > 0) |
548 |
|
j++; // j indexes smallest kid |
549 |
|
if (comparator.compare((E)queue[k], (E)queue[j]) <= 0) |
550 |
|
break; |
552 |
|
k = j; |
553 |
|
} |
554 |
|
} |
555 |
+ |
|
556 |
|
} |
557 |
|
|
558 |
|
|