1 |
|
package java.util; |
2 |
|
|
3 |
|
/** |
4 |
< |
* An unbounded priority queue based on a priority heap. This queue orders |
4 |
> |
* An unbounded priority {@linkplain Queue queue} based on a priority heap. |
5 |
> |
* This queue orders |
6 |
|
* elements according to an order specified at construction time, which is |
7 |
|
* specified in the same manner as {@link java.util.TreeSet} and |
8 |
|
* {@link java.util.TreeMap}: elements are ordered |
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 |
|
/** |
95 |
|
/** |
96 |
|
* Creates a <tt>PriorityQueue</tt> with the default initial capacity |
97 |
|
* (11) that orders its elements according to their natural |
98 |
< |
* ordering (using <tt>Comparable</tt>.) |
98 |
> |
* ordering (using <tt>Comparable</tt>). |
99 |
|
*/ |
100 |
|
public PriorityQueue() { |
101 |
|
this(DEFAULT_INITIAL_CAPACITY, null); |
104 |
|
/** |
105 |
|
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
106 |
|
* that orders its elements according to their natural ordering |
107 |
< |
* (using <tt>Comparable</tt>.) |
107 |
> |
* (using <tt>Comparable</tt>). |
108 |
|
* |
109 |
|
* @param initialCapacity the initial capacity for this priority queue. |
110 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
111 |
+ |
* than 1 |
112 |
|
*/ |
113 |
|
public PriorityQueue(int initialCapacity) { |
114 |
|
this(initialCapacity, null); |
125 |
|
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
126 |
|
* than 1 |
127 |
|
*/ |
128 |
< |
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { |
128 |
> |
public PriorityQueue(int initialCapacity, |
129 |
> |
Comparator<? super E> comparator) { |
130 |
|
if (initialCapacity < 1) |
131 |
|
throw new IllegalArgumentException(); |
132 |
|
this.queue = new Object[initialCapacity + 1]; |
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 |
< |
} |
180 |
< |
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 |
< |
} |
185 |
< |
else { |
202 |
> |
} else { |
203 |
|
comparator = null; |
204 |
|
fillFromUnsorted(c); |
205 |
|
} |
273 |
|
|
274 |
|
// Queue Methods |
275 |
|
|
276 |
+ |
|
277 |
+ |
|
278 |
|
/** |
279 |
|
* Add the specified element to this priority queue. |
280 |
|
* |
309 |
|
return (E) queue[1]; |
310 |
|
} |
311 |
|
|
312 |
< |
// Collection Methods |
294 |
< |
|
295 |
< |
// these first two override just to get the throws docs |
312 |
> |
// Collection Methods - the first two override to update docs |
313 |
|
|
314 |
|
/** |
315 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
315 |
> |
* Adds the specified element to this queue. |
316 |
> |
* @return <tt>true</tt> (as per the general contract of |
317 |
> |
* <tt>Collection.add</tt>). |
318 |
> |
* |
319 |
> |
* @throws NullPointerException {@inheritDoc} |
320 |
|
* @throws ClassCastException if the specified element cannot be compared |
321 |
|
* with elements currently in the priority queue according |
322 |
|
* to the priority queue's ordering. |
325 |
|
return super.add(o); |
326 |
|
} |
327 |
|
|
328 |
+ |
|
329 |
|
/** |
330 |
+ |
* Adds all of the elements in the specified collection to this queue. |
331 |
+ |
* The behavior of this operation is undefined if |
332 |
+ |
* the specified collection is modified while the operation is in |
333 |
+ |
* progress. (This implies that the behavior of this call is undefined if |
334 |
+ |
* the specified collection is this queue, and this queue is nonempty.) |
335 |
+ |
* <p> |
336 |
+ |
* This implementation iterates over the specified collection, and adds |
337 |
+ |
* each object returned by the iterator to this collection, in turn. |
338 |
+ |
* @throws NullPointerException {@inheritDoc} |
339 |
|
* @throws ClassCastException if any element cannot be compared |
340 |
|
* with elements currently in the priority queue according |
341 |
|
* to the priority queue's ordering. |
311 |
– |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
312 |
– |
* is <tt>null</tt> |
342 |
|
*/ |
343 |
|
public boolean addAll(Collection<? extends E> c) { |
344 |
|
return super.addAll(c); |
345 |
|
} |
346 |
|
|
347 |
+ |
|
348 |
+ |
/** |
349 |
+ |
* Removes a single instance of the specified element from this |
350 |
+ |
* queue, if it is present. More formally, |
351 |
+ |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
352 |
+ |
* o.equals(e))</tt>, if the queue contains one or more such |
353 |
+ |
* elements. Returns <tt>true</tt> if the queue contained the |
354 |
+ |
* specified element (or equivalently, if the queue changed as a |
355 |
+ |
* result of the call). |
356 |
+ |
* |
357 |
+ |
* <p>This implementation iterates over the queue looking for the |
358 |
+ |
* specified element. If it finds the element, it removes the element |
359 |
+ |
* from the queue using the iterator's remove method.<p> |
360 |
+ |
* |
361 |
+ |
*/ |
362 |
|
public boolean remove(Object o) { |
363 |
|
if (o == null) |
364 |
|
return false; |
381 |
|
return false; |
382 |
|
} |
383 |
|
|
384 |
+ |
/** |
385 |
+ |
* Returns an iterator over the elements in this queue. The iterator |
386 |
+ |
* does not return the elements in any particular order. |
387 |
+ |
* |
388 |
+ |
* @return an iterator over the elements in this queue. |
389 |
+ |
*/ |
390 |
|
public Iterator<E> iterator() { |
391 |
|
return new Itr(); |
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 |
|
} |
453 |
|
} |
454 |
|
} |
455 |
|
|
396 |
– |
/** |
397 |
– |
* Returns the number of elements in this priority queue. |
398 |
– |
* |
399 |
– |
* @return the number of elements in this priority queue. |
400 |
– |
*/ |
456 |
|
public int size() { |
457 |
|
return size; |
458 |
|
} |
474 |
|
* Removes and returns the ith element from queue. Recall |
475 |
|
* that queue is one-based, so 1 <= i <= size. |
476 |
|
* |
422 |
– |
* XXX: Could further special-case i==size, but is it worth it? |
423 |
– |
* 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 |
+ |
|
559 |
+ |
/** |
560 |
+ |
* Returns the comparator used to order this collection, or <tt>null</tt> |
561 |
+ |
* if this collection is sorted according to its elements natural ordering |
562 |
+ |
* (using <tt>Comparable</tt>). |
563 |
+ |
* |
564 |
+ |
* @return the comparator used to order this collection, or <tt>null</tt> |
565 |
+ |
* if this collection is sorted according to its elements natural ordering. |
566 |
+ |
*/ |
567 |
|
public Comparator<? super E> comparator() { |
568 |
|
return comparator; |
569 |
|
} |