ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/PriorityQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/PriorityQueue.java (file contents):
Revision 1.39 by dl, Sun Sep 7 15:06:19 2003 UTC vs.
Revision 1.43 by dl, Sun Oct 5 22:59:21 2003 UTC

# Line 8 | Line 8
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.
17 < * <p>The <em>head</em> of this queue is the <em>least</em> element with
18 < * respect to the specified ordering.  If multiple elements are tied for least
19 < * value, the head is one of those elements. A priority queue does not permit
20 < * <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 > * A priority queue relying on natural ordering also does not
18 > * permit insertion of non-comparable objects (doing so may result
19 > * in <tt>ClassCastException</tt>).
20   *
21 < * <p>The {@link #remove()} and {@link #poll()} methods remove and
22 < * return the head of the queue.
21 > * <p>The <em>head</em> of this queue is the <em>least</em> element
22 > * with respect to the specified ordering.  If multiple elements are
23 > * tied for least value, the head is one of those elements -- ties are
24 > * broken arbitrarily.  The queue retrieval operations <tt>poll</tt>,
25 > * <tt>remove</tt>, <tt>peek</tt>, and <tt>element</tt> access the
26 > * element at the head of the queue.
27   *
28 < * <p>The {@link #element()} and {@link #peek()} methods return, but do
29 < * not delete, the head of the queue.
28 > * <p>A priority queue is unbounded, but has an internal
29 > * <i>capacity</i> governing the size of an array used to store the
30 > * elements on the queue.  It is always at least as large as the queue
31 > * size.  As elements are added to a priority queue, its capacity
32 > * grows automatically.  The details of the growth policy are not
33 > * specified.
34   *
35 < * <p>A priority queue has a <i>capacity</i>.  The capacity is the
36 < * size of the array used internally to store the elements on the
37 < * queue.
31 < * It is always at least as large as the queue size.  As
32 < * elements are added to a priority queue, its capacity grows
33 < * automatically.  The details of the growth policy are not specified.
34 < *
35 < * <p>The Iterator provided in method {@link #iterator()} is <em>not</em>
35 > * <p>This class implements all of the <em>optional</em> methods of
36 > * the {@link Collection} and {@link Iterator} interfaces.  The
37 > * Iterator provided in method {@link #iterator()} is <em>not</em>
38   * guaranteed to traverse the elements of the PriorityQueue in any
39   * particular order. If you need ordered traversal, consider using
40   * <tt>Arrays.sort(pq.toArray())</tt>.
# Line 277 | Line 279 | public class PriorityQueue<E> extends Ab
279      }
280              
281  
280    // Queue Methods
281
282      /**
283 <     * Add the specified element to this priority queue.
283 >     * Inserts the specified element into this priority queue.
284       *
285       * @return <tt>true</tt>
286       * @throws ClassCastException if the specified element cannot be compared
# Line 303 | Line 303 | public class PriorityQueue<E> extends Ab
303          return true;
304      }
305  
306 <    public E poll() {
306 >    public E peek() {
307          if (size == 0)
308              return null;
309        return remove();
310    }
311
312    public E peek() {
309          return (E) queue[1];
310      }
311  
# Line 320 | Line 316 | public class PriorityQueue<E> extends Ab
316       * @return <tt>true</tt> (as per the general contract of
317       * <tt>Collection.add</tt>).
318       *
319 <     * @throws NullPointerException {@inheritDoc}
319 >     * @throws NullPointerException if the specified element is <tt>null</tt>.
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.
323       */
324      public boolean add(E o) {
325 <        return super.add(o);
325 >        return offer(o);
326      }
327  
332  
333    /**
334     * Adds all of the elements in the specified collection to this queue.
335     * The behavior of this operation is undefined if
336     * the specified collection is modified while the operation is in
337     * progress.  (This implies that the behavior of this call is undefined if
338     * the specified collection is this queue, and this queue is nonempty.)
339     * <p>
340     * This implementation iterates over the specified collection, and adds
341     * each object returned by the iterator to this collection, in turn.
342     * @throws NullPointerException {@inheritDoc}
343     * @throws ClassCastException if any element cannot be compared
344     * with elements currently in the priority queue according
345     * to the priority queue's ordering.
346     */
347    public boolean addAll(Collection<? extends E> c) {
348        return super.addAll(c);
349    }
350
351
352    /**
353     * Removes a single instance of the specified element from this
354     * queue, if it is present.  More formally,
355     * removes an element <tt>e</tt> such that <tt>(o==null ? e==null :
356     * o.equals(e))</tt>, if the queue contains one or more such
357     * elements.  Returns <tt>true</tt> if the queue contained the
358     * specified element (or equivalently, if the queue changed as a
359     * result of the call).
360     *
361     * <p>This implementation iterates over the queue looking for the
362     * specified element.  If it finds the element, it removes the element
363     * from the queue using the iterator's remove method.<p>
364     *
365     */
328      public boolean remove(Object o) {
329          if (o == null)
330              return false;
# Line 506 | Line 468 | public class PriorityQueue<E> extends Ab
468          size = 0;
469      }
470  
471 <    /**
510 <     * Removes and returns the first element from queue.
511 <     */
512 <    public E remove() {
471 >    public E poll() {
472          if (size == 0)
473 <            throw new NoSuchElementException();
473 >            return null;
474          modCount++;
475  
476          E result = (E) queue[1];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines