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.BlockingPriorityQueue} 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 |
93 |
|
/** |
94 |
|
* Creates a <tt>PriorityQueue</tt> with the default initial capacity |
95 |
|
* (11) that orders its elements according to their natural |
96 |
< |
* ordering (using <tt>Comparable</tt>.) |
96 |
> |
* ordering (using <tt>Comparable</tt>). |
97 |
|
*/ |
98 |
|
public PriorityQueue() { |
99 |
|
this(DEFAULT_INITIAL_CAPACITY, null); |
102 |
|
/** |
103 |
|
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
104 |
|
* that orders its elements according to their natural ordering |
105 |
< |
* (using <tt>Comparable</tt>.) |
105 |
> |
* (using <tt>Comparable</tt>). |
106 |
|
* |
107 |
|
* @param initialCapacity the initial capacity for this priority queue. |
108 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
109 |
+ |
* than 1 |
110 |
|
*/ |
111 |
|
public PriorityQueue(int initialCapacity) { |
112 |
|
this(initialCapacity, null); |
123 |
|
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
124 |
|
* than 1 |
125 |
|
*/ |
126 |
< |
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { |
126 |
> |
public PriorityQueue(int initialCapacity, |
127 |
> |
Comparator<? super E> comparator) { |
128 |
|
if (initialCapacity < 1) |
129 |
|
throw new IllegalArgumentException(); |
130 |
|
this.queue = new Object[initialCapacity + 1]; |
171 |
|
* specified collection. The priority queue has an initial |
172 |
|
* capacity of 110% of the size of the specified collection or 1 |
173 |
|
* if the collection is empty. If the specified collection is an |
174 |
< |
* instance of a {@link SortedSet} or is another |
174 |
> |
* instance of a {@link java.util.SortedSet} or is another |
175 |
|
* <tt>PriorityQueue</tt>, the priority queue will be sorted |
176 |
|
* according to the same comparator, or according to its elements' |
177 |
|
* natural order if the collection is sorted according to its |
188 |
|
*/ |
189 |
|
public PriorityQueue(Collection<? extends E> c) { |
190 |
|
initializeArray(c); |
191 |
< |
if (c instanceof SortedSet<? extends E>) { |
192 |
< |
SortedSet<? extends E> s = (SortedSet<? extends E>) c; |
191 |
> |
if (c instanceof SortedSet) { |
192 |
> |
// @fixme double-cast workaround for compiler |
193 |
> |
SortedSet<? extends E> s = (SortedSet<? extends E>) (SortedSet)c; |
194 |
|
comparator = (Comparator<? super E>)s.comparator(); |
195 |
|
fillFromSorted(s); |
196 |
< |
} |
180 |
< |
else if (c instanceof PriorityQueue<? extends E>) { |
196 |
> |
} else if (c instanceof PriorityQueue) { |
197 |
|
PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c; |
198 |
|
comparator = (Comparator<? super E>)s.comparator(); |
199 |
|
fillFromSorted(s); |
200 |
< |
} |
185 |
< |
else { |
200 |
> |
} else { |
201 |
|
comparator = null; |
202 |
|
fillFromUnsorted(c); |
203 |
|
} |
271 |
|
|
272 |
|
// Queue Methods |
273 |
|
|
274 |
+ |
|
275 |
+ |
|
276 |
|
/** |
277 |
|
* Add the specified element to this priority queue. |
278 |
|
* |
307 |
|
return (E) queue[1]; |
308 |
|
} |
309 |
|
|
310 |
< |
// Collection Methods |
294 |
< |
|
295 |
< |
// these first two override just to get the throws docs |
310 |
> |
// Collection Methods - the first two override to update docs |
311 |
|
|
312 |
|
/** |
313 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
313 |
> |
* Adds the specified element to this queue. |
314 |
> |
* @return <tt>true</tt> (as per the general contract of |
315 |
> |
* <tt>Collection.add</tt>). |
316 |
> |
* |
317 |
> |
* @throws NullPointerException {@inheritDoc} |
318 |
|
* @throws ClassCastException if the specified element cannot be compared |
319 |
|
* with elements currently in the priority queue according |
320 |
|
* to the priority queue's ordering. |
323 |
|
return super.add(o); |
324 |
|
} |
325 |
|
|
326 |
+ |
|
327 |
|
/** |
328 |
+ |
* Adds all of the elements in the specified collection to this queue. |
329 |
+ |
* The behavior of this operation is undefined if |
330 |
+ |
* the specified collection is modified while the operation is in |
331 |
+ |
* progress. (This implies that the behavior of this call is undefined if |
332 |
+ |
* the specified collection is this queue, and this queue is nonempty.) |
333 |
+ |
* <p> |
334 |
+ |
* This implementation iterates over the specified collection, and adds |
335 |
+ |
* each object returned by the iterator to this collection, in turn. |
336 |
+ |
* @throws NullPointerException {@inheritDoc} |
337 |
|
* @throws ClassCastException if any element cannot be compared |
338 |
|
* with elements currently in the priority queue according |
339 |
|
* 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> |
340 |
|
*/ |
341 |
|
public boolean addAll(Collection<? extends E> c) { |
342 |
|
return super.addAll(c); |
343 |
|
} |
344 |
|
|
345 |
+ |
|
346 |
+ |
/** |
347 |
+ |
* Removes a single instance of the specified element from this |
348 |
+ |
* queue, if it is present. More formally, |
349 |
+ |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
350 |
+ |
* o.equals(e))</tt>, if the queue contains one or more such |
351 |
+ |
* elements. Returns <tt>true</tt> if the queue contained the |
352 |
+ |
* specified element (or equivalently, if the queue changed as a |
353 |
+ |
* result of the call). |
354 |
+ |
* |
355 |
+ |
* <p>This implementation iterates over the queue looking for the |
356 |
+ |
* specified element. If it finds the element, it removes the element |
357 |
+ |
* from the queue using the iterator's remove method.<p> |
358 |
+ |
* |
359 |
+ |
*/ |
360 |
|
public boolean remove(Object o) { |
361 |
|
if (o == null) |
362 |
|
return false; |
379 |
|
return false; |
380 |
|
} |
381 |
|
|
382 |
+ |
/** |
383 |
+ |
* Returns an iterator over the elements in this queue. The iterator |
384 |
+ |
* does not return the elements in any particular order. |
385 |
+ |
* |
386 |
+ |
* @return an iterator over the elements in this queue. |
387 |
+ |
*/ |
388 |
|
public Iterator<E> iterator() { |
389 |
|
return new Itr(); |
390 |
|
} |
441 |
|
} |
442 |
|
} |
443 |
|
|
396 |
– |
/** |
397 |
– |
* Returns the number of elements in this priority queue. |
398 |
– |
* |
399 |
– |
* @return the number of elements in this priority queue. |
400 |
– |
*/ |
444 |
|
public int size() { |
445 |
|
return size; |
446 |
|
} |
538 |
|
} |
539 |
|
} |
540 |
|
|
541 |
+ |
|
542 |
+ |
/** |
543 |
+ |
* Returns the comparator used to order this collection, or <tt>null</tt> |
544 |
+ |
* if this collection is sorted according to its elements natural ordering |
545 |
+ |
* (using <tt>Comparable</tt>). |
546 |
+ |
* |
547 |
+ |
* @return the comparator used to order this collection, or <tt>null</tt> |
548 |
+ |
* if this collection is sorted according to its elements natural ordering. |
549 |
+ |
*/ |
550 |
|
public Comparator<? super E> comparator() { |
551 |
|
return comparator; |
552 |
|
} |