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. |
12 |
< |
* This queue orders elements according to an order specified at construction |
13 |
< |
* time, which is specified in the same manner as {@link java.util.TreeSet} |
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. |
10 |
< |
* <p>The <em>head</em> of this queue is the <em>least</em> element with |
11 |
< |
* respect to the specified ordering. If multiple elements are tied for least |
12 |
< |
* value, the head is one of those elements. A priority queue does not permit |
13 |
< |
* <tt>null</tt> elements. |
11 |
> |
* An unbounded priority {@linkplain Queue queue} based on a priority |
12 |
> |
* heap. This queue orders elements according to an order specified |
13 |
> |
* at construction time, which is specified either according to their |
14 |
> |
* <i>natural order</i> (see {@link Comparable}), or according to a |
15 |
> |
* {@link java.util.Comparator}, depending on which constructor is |
16 |
> |
* used. A priority queue does not permit <tt>null</tt> elements. |
17 |
|
* |
18 |
< |
* <p>The {@link #remove()} and {@link #poll()} methods remove and |
19 |
< |
* return the head of the queue. |
18 |
> |
* <p>The <em>head</em> of this queue is the <em>least</em> element |
19 |
> |
* with respect to the specified ordering. If multiple elements are |
20 |
> |
* tied for least value, the head is one of those elements -- ties are |
21 |
> |
* broken arbitrarily. The {@link #remove()} and {@link #poll()} |
22 |
> |
* methods remove and return the head of the queue, and the {@link |
23 |
> |
* #element()} and {@link #peek()} methods return, but do not delete, |
24 |
> |
* the head of the queue. |
25 |
|
* |
26 |
< |
* <p>The {@link #element()} and {@link #peek()} methods return, but do |
27 |
< |
* not delete, the head of the queue. |
26 |
> |
* <p>A priority queue is unbounded, but has an internal |
27 |
> |
* <i>capacity</i> governing the size of an array used to store the |
28 |
> |
* elements on the queue. It is always at least as large as the queue |
29 |
> |
* size. As elements are added to a priority queue, its capacity |
30 |
> |
* grows automatically. The details of the growth policy are not |
31 |
> |
* specified. |
32 |
|
* |
33 |
< |
* <p>A priority queue has a <i>capacity</i>. The capacity is the |
34 |
< |
* size of the array used internally to store the elements on the |
35 |
< |
* queue. |
24 |
< |
* It is always at least as large as the queue size. As |
25 |
< |
* elements are added to a priority queue, its capacity grows |
26 |
< |
* automatically. The details of the growth policy are not specified. |
27 |
< |
* |
28 |
< |
* <p>The Iterator provided in method {@link #iterator()} is <em>not</em> |
33 |
> |
* <p>This class implements all of the <em>optional</em> methods of |
34 |
> |
* the {@link Collection} and {@link Iterator} interfaces. The |
35 |
> |
* Iterator provided in method {@link #iterator()} is <em>not</em> |
36 |
|
* guaranteed to traverse the elements of the PriorityQueue in any |
37 |
|
* particular order. If you need ordered traversal, consider using |
38 |
|
* <tt>Arrays.sort(pq.toArray())</tt>. |
55 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
56 |
|
* Java Collections Framework</a>. |
57 |
|
* @since 1.5 |
58 |
+ |
* @version %I%, %G% |
59 |
|
* @author Josh Bloch |
60 |
|
*/ |
61 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
277 |
|
} |
278 |
|
|
279 |
|
|
272 |
– |
// Queue Methods |
273 |
– |
|
280 |
|
/** |
281 |
< |
* Add the specified element to this priority queue. |
281 |
> |
* Inserts the specified element to this priority queue. |
282 |
|
* |
283 |
|
* @return <tt>true</tt> |
284 |
|
* @throws ClassCastException if the specified element cannot be compared |
301 |
|
return true; |
302 |
|
} |
303 |
|
|
304 |
< |
public E poll() { |
304 |
> |
public E peek() { |
305 |
|
if (size == 0) |
306 |
|
return null; |
301 |
– |
return remove(); |
302 |
– |
} |
303 |
– |
|
304 |
– |
public E peek() { |
307 |
|
return (E) queue[1]; |
308 |
|
} |
309 |
|
|
314 |
|
* @return <tt>true</tt> (as per the general contract of |
315 |
|
* <tt>Collection.add</tt>). |
316 |
|
* |
317 |
< |
* @throws NullPointerException {@inheritDoc} |
317 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
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. |
321 |
|
*/ |
322 |
|
public boolean add(E o) { |
323 |
< |
return super.add(o); |
323 |
> |
return offer(o); |
324 |
|
} |
325 |
|
|
326 |
|
|
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} |
336 |
> |
* @param c collection whose elements are to be added to this queue |
337 |
> |
* @return <tt>true</tt> if this queue changed as a result of the |
338 |
> |
* call. |
339 |
> |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
340 |
> |
* is <tt>null</tt> |
341 |
|
* @throws ClassCastException if any element cannot be compared |
342 |
|
* with elements currently in the priority queue according |
343 |
|
* to the priority queue's ordering. |
346 |
|
return super.addAll(c); |
347 |
|
} |
348 |
|
|
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 |
– |
*/ |
349 |
|
public boolean remove(Object o) { |
350 |
|
if (o == null) |
351 |
|
return false; |
453 |
|
cursor--; |
454 |
|
} else { |
455 |
|
if (forgetMeNot == null) |
456 |
< |
forgetMeNot = new ArrayList(); |
456 |
> |
forgetMeNot = new ArrayList<E>(); |
457 |
|
forgetMeNot.add(moved); |
458 |
|
} |
459 |
|
} else if (lastRetElt != null) { |
489 |
|
size = 0; |
490 |
|
} |
491 |
|
|
492 |
< |
/** |
502 |
< |
* Removes and returns the first element from queue. |
503 |
< |
*/ |
504 |
< |
public E remove() { |
492 |
> |
public E poll() { |
493 |
|
if (size == 0) |
494 |
< |
throw new NoSuchElementException(); |
494 |
> |
return null; |
495 |
|
modCount++; |
496 |
|
|
497 |
|
E result = (E) queue[1]; |
637 |
|
s.writeInt(queue.length); |
638 |
|
|
639 |
|
// Write out all elements in the proper order. |
640 |
< |
for (int i=0; i<size; i++) |
640 |
> |
for (int i=1; i<=size; i++) |
641 |
|
s.writeObject(queue[i]); |
642 |
|
} |
643 |
|
|
656 |
|
queue = new Object[arrayLength]; |
657 |
|
|
658 |
|
// Read in all elements in the proper order. |
659 |
< |
for (int i=0; i<size; i++) |
660 |
< |
queue[i] = s.readObject(); |
659 |
> |
for (int i=1; i<=size; i++) |
660 |
> |
queue[i] = (E) s.readObject(); |
661 |
|
} |
662 |
|
|
663 |
|
} |