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 |
81 |
|
/** |
82 |
|
* Creates a <tt>PriorityQueue</tt> with the default initial capacity |
83 |
|
* (11) that orders its elements according to their natural |
84 |
< |
* ordering (using <tt>Comparable</tt>.) |
84 |
> |
* ordering (using <tt>Comparable</tt>). |
85 |
|
*/ |
86 |
|
public PriorityQueue() { |
87 |
|
this(DEFAULT_INITIAL_CAPACITY, null); |
90 |
|
/** |
91 |
|
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
92 |
|
* that orders its elements according to their natural ordering |
93 |
< |
* (using <tt>Comparable</tt>.) |
93 |
> |
* (using <tt>Comparable</tt>). |
94 |
|
* |
95 |
|
* @param initialCapacity the initial capacity for this priority queue. |
96 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
97 |
+ |
* than 1 |
98 |
|
*/ |
99 |
|
public PriorityQueue(int initialCapacity) { |
100 |
|
this(initialCapacity, null); |
111 |
|
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
112 |
|
* than 1 |
113 |
|
*/ |
114 |
< |
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { |
114 |
> |
public PriorityQueue(int initialCapacity, |
115 |
> |
Comparator<? super E> comparator) { |
116 |
|
if (initialCapacity < 1) |
117 |
|
throw new IllegalArgumentException(); |
118 |
|
this.queue = new Object[initialCapacity + 1]; |
159 |
|
* specified collection. The priority queue has an initial |
160 |
|
* capacity of 110% of the size of the specified collection or 1 |
161 |
|
* if the collection is empty. If the specified collection is an |
162 |
< |
* instance of a {@link SortedSet} or is another |
162 |
> |
* instance of a {@link java.util.SortedSet} or is another |
163 |
|
* <tt>PriorityQueue</tt>, the priority queue will be sorted |
164 |
|
* according to the same comparator, or according to its elements' |
165 |
|
* natural order if the collection is sorted according to its |
180 |
|
SortedSet<? extends E> s = (SortedSet<? extends E>) c; |
181 |
|
comparator = (Comparator<? super E>)s.comparator(); |
182 |
|
fillFromSorted(s); |
183 |
< |
} |
180 |
< |
else if (c instanceof PriorityQueue<? extends E>) { |
183 |
> |
} else if (c instanceof PriorityQueue<? extends E>) { |
184 |
|
PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c; |
185 |
|
comparator = (Comparator<? super E>)s.comparator(); |
186 |
|
fillFromSorted(s); |
187 |
< |
} |
185 |
< |
else { |
187 |
> |
} else { |
188 |
|
comparator = null; |
189 |
|
fillFromUnsorted(c); |
190 |
|
} |
258 |
|
|
259 |
|
// Queue Methods |
260 |
|
|
261 |
+ |
|
262 |
+ |
|
263 |
|
/** |
264 |
|
* Add the specified element to this priority queue. |
265 |
|
* |
294 |
|
return (E) queue[1]; |
295 |
|
} |
296 |
|
|
297 |
< |
// Collection Methods |
294 |
< |
|
295 |
< |
// these first two override just to get the throws docs |
297 |
> |
// Collection Methods - the first two override to update docs |
298 |
|
|
299 |
|
/** |
300 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
300 |
> |
* Adds the specified element to this queue. |
301 |
> |
* @return <tt>true</tt> (as per the general contract of |
302 |
> |
* <tt>Collection.add</tt>). |
303 |
> |
* |
304 |
> |
* @throws NullPointerException {@inheritDoc} |
305 |
|
* @throws ClassCastException if the specified element cannot be compared |
306 |
|
* with elements currently in the priority queue according |
307 |
|
* to the priority queue's ordering. |
310 |
|
return super.add(o); |
311 |
|
} |
312 |
|
|
313 |
+ |
|
314 |
|
/** |
315 |
+ |
* Adds all of the elements in the specified collection to this queue. |
316 |
+ |
* The behavior of this operation is undefined if |
317 |
+ |
* the specified collection is modified while the operation is in |
318 |
+ |
* progress. (This implies that the behavior of this call is undefined if |
319 |
+ |
* the specified collection is this queue, and this queue is nonempty.) |
320 |
+ |
* <p> |
321 |
+ |
* This implementation iterates over the specified collection, and adds |
322 |
+ |
* each object returned by the iterator to this collection, in turn. |
323 |
+ |
* @throws NullPointerException {@inheritDoc} |
324 |
|
* @throws ClassCastException if any element cannot be compared |
325 |
|
* with elements currently in the priority queue according |
326 |
|
* 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> |
327 |
|
*/ |
328 |
|
public boolean addAll(Collection<? extends E> c) { |
329 |
|
return super.addAll(c); |
330 |
|
} |
331 |
|
|
332 |
+ |
|
333 |
+ |
/** |
334 |
+ |
* Removes a single instance of the specified element from this |
335 |
+ |
* queue, if it is present. More formally, |
336 |
+ |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
337 |
+ |
* o.equals(e))</tt>, if the queue contains one or more such |
338 |
+ |
* elements. Returns <tt>true</tt> if the queue contained the |
339 |
+ |
* specified element (or equivalently, if the queue changed as a |
340 |
+ |
* result of the call). |
341 |
+ |
* |
342 |
+ |
* <p>This implementation iterates over the queue looking for the |
343 |
+ |
* specified element. If it finds the element, it removes the element |
344 |
+ |
* from the queue using the iterator's remove method.<p> |
345 |
+ |
* |
346 |
+ |
*/ |
347 |
|
public boolean remove(Object o) { |
348 |
|
if (o == null) |
349 |
|
return false; |
366 |
|
return false; |
367 |
|
} |
368 |
|
|
369 |
+ |
/** |
370 |
+ |
* Returns an iterator over the elements in this queue. The iterator |
371 |
+ |
* does not return the elements in any particular order. |
372 |
+ |
* |
373 |
+ |
* @return an iterator over the elements in this queue. |
374 |
+ |
*/ |
375 |
|
public Iterator<E> iterator() { |
376 |
|
return new Itr(); |
377 |
|
} |
428 |
|
} |
429 |
|
} |
430 |
|
|
396 |
– |
/** |
397 |
– |
* Returns the number of elements in this priority queue. |
398 |
– |
* |
399 |
– |
* @return the number of elements in this priority queue. |
400 |
– |
*/ |
431 |
|
public int size() { |
432 |
|
return size; |
433 |
|
} |
525 |
|
} |
526 |
|
} |
527 |
|
|
528 |
+ |
|
529 |
+ |
/** |
530 |
+ |
* Returns the comparator used to order this collection, or <tt>null</tt> |
531 |
+ |
* if this collection is sorted according to its elements natural ordering |
532 |
+ |
* (using <tt>Comparable</tt>). |
533 |
+ |
* |
534 |
+ |
* @return the comparator used to order this collection, or <tt>null</tt> |
535 |
+ |
* if this collection is sorted according to its elements natural ordering. |
536 |
+ |
*/ |
537 |
|
public Comparator<? super E> comparator() { |
538 |
|
return comparator; |
539 |
|
} |