3 |
|
/** |
4 |
|
* An unbounded 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 java.util.TreeSet} and {@link java.util.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 java.util.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 |
|
* |
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. |
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 |
|
* |
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 |
|
|
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 or 1 if the collection is empty. |
122 |
|
* If the specified collection |
123 |
|
* implements the {@link Sorted} interface, the priority queue will be |
124 |
|
* sorted according to the same comparator, or according to its elements' |
145 |
|
this.queue = new Object[initialCapacity + 1]; |
146 |
|
|
147 |
|
if (c instanceof Sorted) { |
148 |
< |
// FIXME: this code assumes too much |
146 |
< |
this.comparator = (Comparator<? super E>) ((Sorted)c).comparator(); |
147 |
< |
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); ) |
148 |
< |
queue[++size] = i.next(); |
148 |
> |
comparator = (Comparator<? super E>)((Sorted)c).comparator(); |
149 |
|
} else { |
150 |
|
comparator = null; |
151 |
– |
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); ) |
152 |
– |
add(i.next()); |
151 |
|
} |
152 |
+ |
|
153 |
+ |
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); ) |
154 |
+ |
add(i.next()); |
155 |
|
} |
156 |
|
|
157 |
|
// Queue Methods |
159 |
|
/** |
160 |
|
* Add the specified element to this priority queue. |
161 |
|
* |
161 |
– |
* @param element the element to add. |
162 |
|
* @return <tt>true</tt> |
163 |
|
* @throws ClassCastException if the specified element cannot be compared |
164 |
|
* with elements currently in the priority queue according |
165 |
|
* to the priority queue's ordering. |
166 |
< |
* @throws NullPointerException if the specified element is null. |
166 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
167 |
|
*/ |
168 |
< |
public boolean offer(E element) { |
169 |
< |
if (element == null) |
168 |
> |
public boolean offer(E o) { |
169 |
> |
if (o == null) |
170 |
|
throw new NullPointerException(); |
171 |
|
modCount++; |
172 |
|
++size; |
178 |
|
queue = newQueue; |
179 |
|
} |
180 |
|
|
181 |
< |
queue[size] = element; |
181 |
> |
queue[size] = o; |
182 |
|
fixUp(size); |
183 |
|
return true; |
184 |
|
} |
203 |
|
* with elements currently in the priority queue according |
204 |
|
* to the priority queue's ordering. |
205 |
|
*/ |
206 |
< |
public boolean add(E element) { |
207 |
< |
return super.add(element); |
206 |
> |
public boolean add(E o) { |
207 |
> |
return super.add(o); |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
– |
* @throws NullPointerException if any element is <tt>null</tt>. |
211 |
|
* @throws ClassCastException if any element cannot be compared |
212 |
|
* with elements currently in the priority queue according |
213 |
|
* to the priority queue's ordering. |
214 |
+ |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
215 |
+ |
* is <tt>null</tt> |
216 |
|
*/ |
217 |
|
public boolean addAll(Collection<? extends E> c) { |
218 |
|
return super.addAll(c); |