1 |
< |
package java.util; |
1 |
> |
/* |
2 |
> |
* %W% %E% |
3 |
> |
* |
4 |
> |
* Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
5 |
> |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
> |
*/ |
7 |
> |
|
8 |
> |
package java.util; |
9 |
|
|
10 |
|
/** |
11 |
|
* An unbounded priority {@linkplain Queue queue} based on a priority heap. |
14 |
|
* and {@link java.util.TreeMap}: elements are ordered either according to |
15 |
|
* their <i>natural order</i> (see {@link Comparable}), or according to a |
16 |
|
* {@link java.util.Comparator}, depending on which constructor is used. |
17 |
+ |
* |
18 |
|
* <p>The <em>head</em> of this queue is the <em>least</em> element with |
19 |
|
* respect to the specified ordering. If multiple elements are tied for least |
20 |
|
* value, the head is one of those elements. A priority queue does not permit |
21 |
< |
* <tt>null</tt> elements. |
21 |
> |
* <tt>null</tt> elements. |
22 |
|
* |
23 |
|
* <p>The {@link #remove()} and {@link #poll()} methods remove and |
24 |
|
* return the head of the queue. |
26 |
|
* <p>The {@link #element()} and {@link #peek()} methods return, but do |
27 |
|
* not delete, the head of the queue. |
28 |
|
* |
29 |
< |
* <p>A priority queue has a <i>capacity</i>. The capacity is the |
30 |
< |
* size of the array used internally to store the elements on the |
31 |
< |
* queue. |
32 |
< |
* It is always at least as large as the queue size. As |
33 |
< |
* elements are added to a priority queue, its capacity grows |
34 |
< |
* automatically. The details of the growth policy are not specified. |
29 |
> |
* <p>A priority queue is unbounded, but has a <i>capacity</i>. The |
30 |
> |
* capacity is the size of the array used internally to store the |
31 |
> |
* elements on the queue. It is always at least as large as the queue |
32 |
> |
* size. As elements are added to a priority queue, its capacity |
33 |
> |
* grows automatically. The details of the growth policy are not |
34 |
> |
* specified. |
35 |
|
* |
36 |
|
* <p>The Iterator provided in method {@link #iterator()} is <em>not</em> |
37 |
|
* guaranteed to traverse the elements of the PriorityQueue in any |
56 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
57 |
|
* Java Collections Framework</a>. |
58 |
|
* @since 1.5 |
59 |
+ |
* @version %I%, %G% |
60 |
|
* @author Josh Bloch |
61 |
|
*/ |
62 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
278 |
|
} |
279 |
|
|
280 |
|
|
272 |
– |
// Queue Methods |
273 |
– |
|
281 |
|
/** |
282 |
< |
* Add the specified element to this priority queue. |
282 |
> |
* Inserts the specified element to this priority queue. |
283 |
|
* |
284 |
|
* @return <tt>true</tt> |
285 |
|
* @throws ClassCastException if the specified element cannot be compared |
302 |
|
return true; |
303 |
|
} |
304 |
|
|
305 |
< |
public E poll() { |
305 |
> |
public E peek() { |
306 |
|
if (size == 0) |
307 |
|
return null; |
301 |
– |
return remove(); |
302 |
– |
} |
303 |
– |
|
304 |
– |
public E peek() { |
308 |
|
return (E) queue[1]; |
309 |
|
} |
310 |
|
|
315 |
|
* @return <tt>true</tt> (as per the general contract of |
316 |
|
* <tt>Collection.add</tt>). |
317 |
|
* |
318 |
< |
* @throws NullPointerException {@inheritDoc} |
318 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
319 |
|
* @throws ClassCastException if the specified element cannot be compared |
320 |
|
* with elements currently in the priority queue according |
321 |
|
* to the priority queue's ordering. |
334 |
|
* <p> |
335 |
|
* This implementation iterates over the specified collection, and adds |
336 |
|
* each object returned by the iterator to this collection, in turn. |
337 |
< |
* @throws NullPointerException {@inheritDoc} |
337 |
> |
* @param c collection whose elements are to be added to this queue |
338 |
> |
* @return <tt>true</tt> if this queue changed as a result of the |
339 |
> |
* call. |
340 |
> |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
341 |
> |
* is <tt>null</tt> |
342 |
|
* @throws ClassCastException if any element cannot be compared |
343 |
|
* with elements currently in the priority queue according |
344 |
|
* to the priority queue's ordering. |
347 |
|
return super.addAll(c); |
348 |
|
} |
349 |
|
|
343 |
– |
|
344 |
– |
/** |
345 |
– |
* Removes a single instance of the specified element from this |
346 |
– |
* queue, if it is present. More formally, |
347 |
– |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
348 |
– |
* o.equals(e))</tt>, if the queue contains one or more such |
349 |
– |
* elements. Returns <tt>true</tt> if the queue contained the |
350 |
– |
* specified element (or equivalently, if the queue changed as a |
351 |
– |
* result of the call). |
352 |
– |
* |
353 |
– |
* <p>This implementation iterates over the queue looking for the |
354 |
– |
* specified element. If it finds the element, it removes the element |
355 |
– |
* from the queue using the iterator's remove method.<p> |
356 |
– |
* |
357 |
– |
*/ |
350 |
|
public boolean remove(Object o) { |
351 |
|
if (o == null) |
352 |
|
return false; |
490 |
|
size = 0; |
491 |
|
} |
492 |
|
|
493 |
< |
/** |
502 |
< |
* Removes and returns the first element from queue. |
503 |
< |
*/ |
504 |
< |
public E remove() { |
493 |
> |
public E poll() { |
494 |
|
if (size == 0) |
495 |
< |
throw new NoSuchElementException(); |
495 |
> |
return null; |
496 |
|
modCount++; |
497 |
|
|
498 |
|
E result = (E) queue[1]; |
638 |
|
s.writeInt(queue.length); |
639 |
|
|
640 |
|
// Write out all elements in the proper order. |
641 |
< |
for (int i=0; i<size; i++) |
641 |
> |
for (int i=1; i<=size; i++) |
642 |
|
s.writeObject(queue[i]); |
643 |
|
} |
644 |
|
|
657 |
|
queue = new Object[arrayLength]; |
658 |
|
|
659 |
|
// Read in all elements in the proper order. |
660 |
< |
for (int i=0; i<size; i++) |
660 |
> |
for (int i=1; i<=size; i++) |
661 |
|
queue[i] = (E) s.readObject(); |
662 |
|
} |
663 |
|
|