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.15 by dholmes, Thu Jul 31 07:18:02 2003 UTC vs.
Revision 1.19 by tim, Mon Aug 4 16:14:48 2003 UTC

# Line 1 | Line 1
1   package java.util;
2  
3   /**
4 < * An unbounded priority queue based on a priority heap.  This queue orders
4 > * A priority queue based on a priority heap.  This queue orders
5   * elements according to an order specified at construction time, which is
6 < * specified in the same manner as {@link TreeSet} and {@link TreeMap}:
7 < * elements are ordered
6 > * specified in the same manner as {@link java.util.TreeSet} and
7 > * {@link java.util.TreeMap}: elements are ordered
8   * either according to their <i>natural order</i> (see {@link Comparable}), or
9 < * according to a {@link Comparator}, depending on which constructor is used.
10 < * The <em>head</em> of this queue is the least element with respect to the
11 < * specified ordering. If multiple elements are tied for least value, the
9 > * according to a {@link java.util.Comparator}, depending on which
10 > * constructor is used.
11 > * <p>The <em>head</em> of this queue is the <em>least</em> element with
12 > * respect to the specified ordering.
13 > * If multiple elements are tied for least value, the
14   * head is one of those elements. A priority queue does not permit
15   * <tt>null</tt> elements.
16   *
# Line 20 | Line 22
22   *
23   * <p>A priority queue has a <i>capacity</i>.  The capacity is the
24   * size of the array used internally to store the elements on the
25 < * queue.  It is always at least as large as the queue size.  As
25 > * queue, and is limited to <tt>Integer.MAX_VALUE-1</tt>.
26 > * It is always at least as large as the queue size.  As
27   * elements are added to a priority queue, its capacity grows
28   * automatically.  The details of the growth policy are not specified.
29   *
# Line 38 | Line 41
41   * @author Josh Bloch
42   */
43   public class PriorityQueue<E> extends AbstractQueue<E>
44 <    implements Queue<E>, java.io.Serializable {
44 >    implements Sorted, Queue<E>, java.io.Serializable {
45  
46      private static final int DEFAULT_INITIAL_CAPACITY = 11;
47  
# Line 55 | Line 58 | public class PriorityQueue<E> extends Ab
58       *
59       * queue.length must be >= 2, even if size == 0.
60       */
61 <    private transient E[] queue;
61 >    private transient Object[] queue;
62  
63      /**
64       * The number of elements in the priority queue.
# Line 66 | Line 69 | public class PriorityQueue<E> extends Ab
69       * The comparator, or null if priority queue uses elements'
70       * natural ordering.
71       */
72 <    private final Comparator<E> comparator;
72 >    private final Comparator<? super E> comparator;
73  
74      /**
75       * The number of times this priority queue has been
# Line 105 | Line 108 | public class PriorityQueue<E> extends Ab
108       * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
109       * than 1
110       */
111 <    public PriorityQueue(int initialCapacity, Comparator<E> comparator) {
111 >    public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) {
112          if (initialCapacity < 1)
113              throw new IllegalArgumentException();
114 <        queue = (E[]) new Object[initialCapacity + 1];
114 >        this.queue = new Object[initialCapacity + 1];
115          this.comparator = comparator;
116      }
117  
118      /**
119       * Create a <tt>PriorityQueue</tt> containing the elements in the specified
120       * collection.  The priority queue has an initial capacity of 110% of the
121 <     * size of the specified collection; or 1 if the collection is empty.
121 >     * size of the specified collection (bounded by
122 >     * <tt>Integer.MAX_VALUE-1</tt>); or 1 if the collection is empty.
123       * If the specified collection
124       * implements the {@link Sorted} interface, the priority queue will be
125       * sorted according to the same comparator, or according to its elements'
# Line 132 | Line 136 | public class PriorityQueue<E> extends Ab
136       * @throws NullPointerException if <tt>c</tt> or any element within it
137       * is <tt>null</tt>
138       */
139 <    public PriorityQueue(Collection<E> c) {
139 >    public PriorityQueue(Collection<? extends E> c) {
140          int sz = c.size();
141          int initialCapacity = (int)Math.min((sz * 110L) / 100,
142                                              Integer.MAX_VALUE - 1);
143          if (initialCapacity < 1)
144              initialCapacity = 1;
145  
146 <        queue = (E[]) new Object[initialCapacity + 1];
146 >        this.queue = new Object[initialCapacity + 1];
147 >
148 >        // FIXME: if c is larger than Integer.MAX_VALUE we'll
149 >        // overflow the array
150  
151          if (c instanceof Sorted) {
152 <            // FIXME: this code assumes too much
146 <            comparator = ((Sorted)c).comparator();
147 <            for (Iterator<E> i = c.iterator(); i.hasNext(); )
148 <                queue[++size] = i.next();
152 >            comparator = (Comparator<? super E>)((Sorted)c).comparator();
153          } else {
154              comparator = null;
151            for (Iterator<E> i = c.iterator(); i.hasNext(); )
152                add(i.next());
155          }
156 +
157 +        for (Iterator<? extends E> i = c.iterator(); i.hasNext(); )
158 +            add(i.next());
159      }
160  
161      // Queue Methods
# Line 158 | Line 163 | public class PriorityQueue<E> extends Ab
163      /**
164       * Add the specified element to this priority queue.
165       *
161     * @param element the element to add.
166       * @return <tt>true</tt>
167       * @throws ClassCastException if the specified element cannot be compared
168       * with elements currently in the priority queue according
169       * to the priority queue's ordering.
170 <     * @throws NullPointerException if the specified element is null.
170 >     * @throws NullPointerException if the specified element is <tt>null</tt>.
171       */
172 <    public boolean offer(E element) {
173 <        if (element == null)
172 >    public boolean offer(E o) {
173 >        if (o == null)
174              throw new NullPointerException();
175          modCount++;
176          ++size;
177  
178          // Grow backing store if necessary
179 +        // FIXME: watch for overflow
180 +        // FIXME: what if we're full?
181          while (size >= queue.length) {
182 <            E[] newQueue = (E[]) new Object[2 * queue.length];
182 >            Object[] newQueue = new Object[2 * queue.length];
183              System.arraycopy(queue, 0, newQueue, 0, queue.length);
184              queue = newQueue;
185          }
186  
187 <        queue[size] = element;
187 >        queue[size] = o;
188          fixUp(size);
189          return true;
190      }
# Line 186 | Line 192 | public class PriorityQueue<E> extends Ab
192      public E poll() {
193          if (size == 0)
194              return null;
195 <        return remove(1);
195 >        return (E) remove(1);
196      }
197  
198      public E peek() {
199 <        return queue[1];
199 >        return (E) queue[1];
200      }
201  
202      // Collection Methods
# Line 203 | Line 209 | public class PriorityQueue<E> extends Ab
209       * with elements currently in the priority queue according
210       * to the priority queue's ordering.
211       */
212 <    public boolean add(E element) {
213 <        return super.add(element);
212 >    public boolean add(E o) {
213 >        return super.add(o);
214      }
215  
216      /**
211     * @throws NullPointerException if any element is <tt>null</tt>.
217       * @throws ClassCastException if any element cannot be compared
218       * with elements currently in the priority queue according
219       * to the priority queue's ordering.
220 +     * @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt>
221 +     * is <tt>null</tt>
222       */
223      public boolean addAll(Collection<? extends E> c) {
224          return super.addAll(c);
# Line 223 | Line 230 | public class PriorityQueue<E> extends Ab
230  
231          if (comparator == null) {
232              for (int i = 1; i <= size; i++) {
233 <                if (((Comparable)queue[i]).compareTo(o) == 0) {
233 >                if (((Comparable<E>)queue[i]).compareTo((E)o) == 0) {
234                      remove(i);
235                      return true;
236                  }
237              }
238          } else {
239              for (int i = 1; i <= size; i++) {
240 <                if (comparator.compare(queue[i], (E)o) == 0) {
240 >                if (comparator.compare((E)queue[i], (E)o) == 0) {
241                      remove(i);
242                      return true;
243                  }
# Line 272 | Line 279 | public class PriorityQueue<E> extends Ab
279              checkForComodification();
280              if (cursor > size)
281                  throw new NoSuchElementException();
282 <            E result = queue[cursor];
282 >            E result = (E) queue[cursor];
283              lastRet = cursor++;
284              return result;
285          }
# Line 328 | Line 335 | public class PriorityQueue<E> extends Ab
335          assert i <= size;
336          modCount++;
337  
338 <        E result = queue[i];
338 >        E result = (E) queue[i];
339          queue[i] = queue[size];
340          queue[size--] = null;  // Drop extra ref to prevent memory leak
341          if (i <= size)
# Line 349 | Line 356 | public class PriorityQueue<E> extends Ab
356          if (comparator == null) {
357              while (k > 1) {
358                  int j = k >> 1;
359 <                if (((Comparable)queue[j]).compareTo(queue[k]) <= 0)
359 >                if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0)
360                      break;
361 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
361 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
362                  k = j;
363              }
364          } else {
365              while (k > 1) {
366                  int j = k >> 1;
367 <                if (comparator.compare(queue[j], queue[k]) <= 0)
367 >                if (comparator.compare((E)queue[j], (E)queue[k]) <= 0)
368                      break;
369 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
369 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
370                  k = j;
371              }
372          }
# Line 378 | Line 385 | public class PriorityQueue<E> extends Ab
385          int j;
386          if (comparator == null) {
387              while ((j = k << 1) <= size) {
388 <                if (j<size && ((Comparable)queue[j]).compareTo(queue[j+1]) > 0)
388 >                if (j<size && ((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0)
389                      j++; // j indexes smallest kid
390 <                if (((Comparable)queue[k]).compareTo(queue[j]) <= 0)
390 >                if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0)
391                      break;
392 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
392 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
393                  k = j;
394              }
395          } else {
396              while ((j = k << 1) <= size) {
397 <                if (j < size && comparator.compare(queue[j], queue[j+1]) > 0)
397 >                if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0)
398                      j++; // j indexes smallest kid
399 <                if (comparator.compare(queue[k], queue[j]) <= 0)
399 >                if (comparator.compare((E)queue[k], (E)queue[j]) <= 0)
400                      break;
401 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
401 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
402                  k = j;
403              }
404          }
405      }
406  
407 <    public Comparator comparator() {
407 >    public Comparator<? super E> comparator() {
408          return comparator;
409      }
410  
# Line 435 | Line 442 | public class PriorityQueue<E> extends Ab
442  
443          // Read in array length and allocate array
444          int arrayLength = s.readInt();
445 <        queue = (E[]) new Object[arrayLength];
445 >        queue = new Object[arrayLength];
446  
447          // Read in all elements in the proper order.
448          for (int i=0; i<size; i++)
449 <            queue[i] = (E)s.readObject();
449 >            queue[i] = s.readObject();
450      }
451  
452   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines