1 |
|
package java.util; |
2 |
|
|
3 |
|
/** |
4 |
< |
* An unbounded priority queue based on a priority heap. This queue orders |
4 |
> |
* An unbounded priority {@linkplain Queue queue} based on a priority heap. |
5 |
> |
* This queue orders |
6 |
|
* elements according to an order specified at construction time, which is |
7 |
< |
* specified in the same manner as {@link TreeSet} and {@link TreeMap}: elements are ordered |
7 |
> |
* specified in the same manner as {@link java.util.TreeSet} and |
8 |
> |
* {@link java.util.TreeMap}: elements are ordered |
9 |
|
* either according to their <i>natural order</i> (see {@link Comparable}), or |
10 |
< |
* according to a {@link Comparator}, depending on which constructor is used. |
11 |
< |
* The {@link #peek}, {@link #poll}, and {@link #remove} methods return the |
12 |
< |
* minimal element with respect to the specified ordering. If multiple |
13 |
< |
* elements are tied for least value, no guarantees are made as to |
14 |
< |
* which of these elements is returned. |
10 |
> |
* according to a {@link java.util.Comparator}, depending on which |
11 |
> |
* constructor is used. |
12 |
> |
* <p>The <em>head</em> of this queue is the <em>least</em> element with |
13 |
> |
* respect to the specified ordering. |
14 |
> |
* If multiple elements are tied for least value, the |
15 |
> |
* head is one of those elements. A priority queue does not permit |
16 |
> |
* <tt>null</tt> elements. |
17 |
> |
* |
18 |
> |
* <p>The {@link #remove()} and {@link #poll()} methods remove and |
19 |
> |
* return the head of the queue. |
20 |
> |
* |
21 |
> |
* <p>The {@link #element()} and {@link #peek()} methods return, but do |
22 |
> |
* not delete, the head of the queue. |
23 |
|
* |
24 |
|
* <p>A priority queue has a <i>capacity</i>. The capacity is the |
25 |
|
* size of the array used internally to store the elements on the |
26 |
< |
* queue. It is always at least as large as the queue size. As |
26 |
> |
* queue. |
27 |
> |
* It is always at least as large as the queue size. As |
28 |
|
* elements are added to a priority queue, its capacity grows |
29 |
|
* automatically. The details of the growth policy are not specified. |
30 |
|
* |
31 |
< |
*<p>Implementation note: this implementation provides O(log(n)) time |
32 |
< |
*for the insertion methods (<tt>offer</tt>, <tt>poll</tt>, |
33 |
< |
*<tt>remove()</tt> and <tt>add</tt>) methods; linear time for the |
34 |
< |
*<tt>remove(Object)</tt> and <tt>contains(Object)</tt> methods; and |
35 |
< |
*constant time for the retrieval methods (<tt>peek</tt>, |
36 |
< |
*<tt>element</tt>, and <tt>size</tt>). |
31 |
> |
* <p>The Iterator provided in method {@link #iterator()} is <em>not</em> |
32 |
> |
* guaranteed to traverse the elements of the PriorityQueue in any |
33 |
> |
* particular order. If you need ordered traversal, consider using |
34 |
> |
* <tt>Arrays.sort(pq.toArray())</tt>. |
35 |
> |
* |
36 |
> |
* <p> <strong>Note that this implementation is not synchronized.</strong> |
37 |
> |
* Multiple threads should not access a <tt>PriorityQueue</tt> |
38 |
> |
* instance concurrently if any of the threads modifies the list |
39 |
> |
* structurally. Instead, use the thread-safe {@link |
40 |
> |
* java.util.concurrent.BlockingPriorityQueue} class. |
41 |
> |
* |
42 |
> |
* |
43 |
> |
* <p>Implementation note: this implementation provides O(log(n)) time |
44 |
> |
* for the insertion methods (<tt>offer</tt>, <tt>poll</tt>, |
45 |
> |
* <tt>remove()</tt> and <tt>add</tt>) methods; linear time for the |
46 |
> |
* <tt>remove(Object)</tt> and <tt>contains(Object)</tt> methods; and |
47 |
> |
* constant time for the retrieval methods (<tt>peek</tt>, |
48 |
> |
* <tt>element</tt>, and <tt>size</tt>). |
49 |
|
* |
50 |
|
* <p>This class is a member of the |
51 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
54 |
|
* @author Josh Bloch |
55 |
|
*/ |
56 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
57 |
< |
implements Queue<E>, |
58 |
< |
java.io.Serializable { |
57 |
> |
implements Queue<E>, java.io.Serializable { |
58 |
> |
|
59 |
> |
private static final long serialVersionUID = -7720805057305804111L; |
60 |
> |
|
61 |
|
private static final int DEFAULT_INITIAL_CAPACITY = 11; |
62 |
|
|
63 |
|
/** |
73 |
|
* |
74 |
|
* queue.length must be >= 2, even if size == 0. |
75 |
|
*/ |
76 |
< |
private transient E[] queue; |
76 |
> |
private transient Object[] queue; |
77 |
|
|
78 |
|
/** |
79 |
|
* The number of elements in the priority queue. |
84 |
|
* The comparator, or null if priority queue uses elements' |
85 |
|
* natural ordering. |
86 |
|
*/ |
87 |
< |
private final Comparator<E> comparator; |
87 |
> |
private final Comparator<? super E> comparator; |
88 |
|
|
89 |
|
/** |
90 |
|
* The number of times this priority queue has been |
93 |
|
private transient int modCount = 0; |
94 |
|
|
95 |
|
/** |
96 |
< |
* Create a new priority queue with the default initial capacity |
96 |
> |
* Creates a <tt>PriorityQueue</tt> with the default initial capacity |
97 |
|
* (11) that orders its elements according to their natural |
98 |
< |
* ordering (using <tt>Comparable</tt>.) |
98 |
> |
* ordering (using <tt>Comparable</tt>). |
99 |
|
*/ |
100 |
|
public PriorityQueue() { |
101 |
< |
this(DEFAULT_INITIAL_CAPACITY); |
101 |
> |
this(DEFAULT_INITIAL_CAPACITY, null); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* Create a new priority queue with the specified initial capacity |
105 |
> |
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
106 |
|
* that orders its elements according to their natural ordering |
107 |
< |
* (using <tt>Comparable</tt>.) |
107 |
> |
* (using <tt>Comparable</tt>). |
108 |
|
* |
109 |
|
* @param initialCapacity the initial capacity for this priority queue. |
110 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
111 |
+ |
* than 1 |
112 |
|
*/ |
113 |
|
public PriorityQueue(int initialCapacity) { |
114 |
|
this(initialCapacity, null); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* Create a new priority queue with the specified initial capacity (11) |
118 |
> |
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
119 |
|
* that orders its elements according to the specified comparator. |
120 |
|
* |
121 |
|
* @param initialCapacity the initial capacity for this priority queue. |
122 |
|
* @param comparator the comparator used to order this priority queue. |
123 |
+ |
* If <tt>null</tt> then the order depends on the elements' natural |
124 |
+ |
* ordering. |
125 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
126 |
+ |
* than 1 |
127 |
|
*/ |
128 |
< |
public PriorityQueue(int initialCapacity, Comparator<E> comparator) { |
128 |
> |
public PriorityQueue(int initialCapacity, |
129 |
> |
Comparator<? super E> comparator) { |
130 |
|
if (initialCapacity < 1) |
131 |
< |
initialCapacity = 1; |
132 |
< |
queue = new E[initialCapacity + 1]; |
131 |
> |
throw new IllegalArgumentException(); |
132 |
> |
this.queue = new Object[initialCapacity + 1]; |
133 |
|
this.comparator = comparator; |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* Create a new priority queue containing the elements in the specified |
138 |
< |
* collection. The priority queue has an initial capacity of 110% of the |
107 |
< |
* size of the specified collection. If the specified collection |
108 |
< |
* implements the {@link Sorted} interface, the priority queue will be |
109 |
< |
* sorted according to the same comparator, or according to its elements' |
110 |
< |
* natural order if the collection is sorted according to its elements' |
111 |
< |
* natural order. If the specified collection does not implement |
112 |
< |
* <tt>Sorted</tt>, the priority queue is ordered according to |
113 |
< |
* its elements' natural order. |
114 |
< |
* |
115 |
< |
* @param initialElements the collection whose elements are to be placed |
116 |
< |
* into this priority queue. |
117 |
< |
* @throws ClassCastException if elements of the specified collection |
118 |
< |
* cannot be compared to one another according to the priority |
119 |
< |
* queue's ordering. |
120 |
< |
* @throws NullPointerException if the specified collection or an |
121 |
< |
* element of the specified collection is <tt>null</tt>. |
137 |
> |
* Common code to initialize underlying queue array across |
138 |
> |
* constructors below. |
139 |
|
*/ |
140 |
< |
public PriorityQueue(Collection<E> initialElements) { |
141 |
< |
int sz = initialElements.size(); |
140 |
> |
private void initializeArray(Collection<? extends E> c) { |
141 |
> |
int sz = c.size(); |
142 |
|
int initialCapacity = (int)Math.min((sz * 110L) / 100, |
143 |
|
Integer.MAX_VALUE - 1); |
144 |
|
if (initialCapacity < 1) |
145 |
|
initialCapacity = 1; |
146 |
< |
queue = new E[initialCapacity + 1]; |
146 |
> |
|
147 |
> |
this.queue = new Object[initialCapacity + 1]; |
148 |
> |
} |
149 |
> |
|
150 |
> |
/** |
151 |
> |
* Initially fill elements of the queue array under the |
152 |
> |
* knowledge that it is sorted or is another PQ, in which |
153 |
> |
* case we can just place the elements without fixups. |
154 |
> |
*/ |
155 |
> |
private void fillFromSorted(Collection<? extends E> c) { |
156 |
> |
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); ) |
157 |
> |
queue[++size] = i.next(); |
158 |
> |
} |
159 |
|
|
160 |
|
|
161 |
< |
if (initialElements instanceof Sorted) { |
162 |
< |
comparator = ((Sorted)initialElements).comparator(); |
163 |
< |
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
164 |
< |
queue[++size] = i.next(); |
161 |
> |
/** |
162 |
> |
* Initially fill elements of the queue array that is |
163 |
> |
* not to our knowledge sorted, so we must add them |
164 |
> |
* one by one. |
165 |
> |
*/ |
166 |
> |
private void fillFromUnsorted(Collection<? extends E> c) { |
167 |
> |
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); ) |
168 |
> |
add(i.next()); |
169 |
> |
} |
170 |
> |
|
171 |
> |
/** |
172 |
> |
* Creates a <tt>PriorityQueue</tt> containing the elements in the |
173 |
> |
* specified collection. The priority queue has an initial |
174 |
> |
* capacity of 110% of the size of the specified collection or 1 |
175 |
> |
* if the collection is empty. If the specified collection is an |
176 |
> |
* instance of a {@link java.util.SortedSet} or is another |
177 |
> |
* <tt>PriorityQueue</tt>, the priority queue will be sorted |
178 |
> |
* according to the same comparator, or according to its elements' |
179 |
> |
* natural order if the collection is sorted according to its |
180 |
> |
* elements' natural order. Otherwise, the priority queue is |
181 |
> |
* ordered according to its elements' natural order. |
182 |
> |
* |
183 |
> |
* @param c the collection whose elements are to be placed |
184 |
> |
* into this priority queue. |
185 |
> |
* @throws ClassCastException if elements of the specified collection |
186 |
> |
* cannot be compared to one another according to the priority |
187 |
> |
* queue's ordering. |
188 |
> |
* @throws NullPointerException if <tt>c</tt> or any element within it |
189 |
> |
* is <tt>null</tt> |
190 |
> |
*/ |
191 |
> |
public PriorityQueue(Collection<? extends E> c) { |
192 |
> |
initializeArray(c); |
193 |
> |
if (c instanceof SortedSet) { |
194 |
> |
// @fixme double-cast workaround for compiler |
195 |
> |
SortedSet<? extends E> s = (SortedSet<? extends E>) (SortedSet)c; |
196 |
> |
comparator = (Comparator<? super E>)s.comparator(); |
197 |
> |
fillFromSorted(s); |
198 |
> |
} else if (c instanceof PriorityQueue) { |
199 |
> |
PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c; |
200 |
> |
comparator = (Comparator<? super E>)s.comparator(); |
201 |
> |
fillFromSorted(s); |
202 |
|
} else { |
203 |
|
comparator = null; |
204 |
< |
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
139 |
< |
add(i.next()); |
204 |
> |
fillFromUnsorted(c); |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
+ |
/** |
209 |
+ |
* Creates a <tt>PriorityQueue</tt> containing the elements in the |
210 |
+ |
* specified collection. The priority queue has an initial |
211 |
+ |
* capacity of 110% of the size of the specified collection or 1 |
212 |
+ |
* if the collection is empty. This priority queue will be sorted |
213 |
+ |
* according to the same comparator as the given collection, or |
214 |
+ |
* according to its elements' natural order if the collection is |
215 |
+ |
* sorted according to its elements' natural order. |
216 |
+ |
* |
217 |
+ |
* @param c the collection whose elements are to be placed |
218 |
+ |
* into this priority queue. |
219 |
+ |
* @throws ClassCastException if elements of the specified collection |
220 |
+ |
* cannot be compared to one another according to the priority |
221 |
+ |
* queue's ordering. |
222 |
+ |
* @throws NullPointerException if <tt>c</tt> or any element within it |
223 |
+ |
* is <tt>null</tt> |
224 |
+ |
*/ |
225 |
+ |
public PriorityQueue(PriorityQueue<? extends E> c) { |
226 |
+ |
initializeArray(c); |
227 |
+ |
comparator = (Comparator<? super E>)c.comparator(); |
228 |
+ |
fillFromSorted(c); |
229 |
+ |
} |
230 |
+ |
|
231 |
+ |
/** |
232 |
+ |
* Creates a <tt>PriorityQueue</tt> containing the elements in the |
233 |
+ |
* specified collection. The priority queue has an initial |
234 |
+ |
* capacity of 110% of the size of the specified collection or 1 |
235 |
+ |
* if the collection is empty. This priority queue will be sorted |
236 |
+ |
* according to the same comparator as the given collection, or |
237 |
+ |
* according to its elements' natural order if the collection is |
238 |
+ |
* sorted according to its elements' natural order. |
239 |
+ |
* |
240 |
+ |
* @param c the collection whose elements are to be placed |
241 |
+ |
* into this priority queue. |
242 |
+ |
* @throws ClassCastException if elements of the specified collection |
243 |
+ |
* cannot be compared to one another according to the priority |
244 |
+ |
* queue's ordering. |
245 |
+ |
* @throws NullPointerException if <tt>c</tt> or any element within it |
246 |
+ |
* is <tt>null</tt> |
247 |
+ |
*/ |
248 |
+ |
public PriorityQueue(SortedSet<? extends E> c) { |
249 |
+ |
initializeArray(c); |
250 |
+ |
comparator = (Comparator<? super E>)c.comparator(); |
251 |
+ |
fillFromSorted(c); |
252 |
+ |
} |
253 |
+ |
|
254 |
+ |
/** |
255 |
+ |
* Resize array, if necessary, to be able to hold given index |
256 |
+ |
*/ |
257 |
+ |
private void grow(int index) { |
258 |
+ |
int newlen = queue.length; |
259 |
+ |
if (index < newlen) // don't need to grow |
260 |
+ |
return; |
261 |
+ |
if (index == Integer.MAX_VALUE) |
262 |
+ |
throw new OutOfMemoryError(); |
263 |
+ |
while (newlen <= index) { |
264 |
+ |
if (newlen >= Integer.MAX_VALUE / 2) // avoid overflow |
265 |
+ |
newlen = Integer.MAX_VALUE; |
266 |
+ |
else |
267 |
+ |
newlen <<= 2; |
268 |
+ |
} |
269 |
+ |
Object[] newQueue = new Object[newlen]; |
270 |
+ |
System.arraycopy(queue, 0, newQueue, 0, queue.length); |
271 |
+ |
queue = newQueue; |
272 |
+ |
} |
273 |
+ |
|
274 |
|
// Queue Methods |
275 |
|
|
276 |
+ |
|
277 |
+ |
|
278 |
|
/** |
279 |
< |
* Remove and return the minimal element from this priority queue |
147 |
< |
* if it contains one or more elements, otherwise return |
148 |
< |
* <tt>null</tt>. The term <i>minimal</i> is defined according to |
149 |
< |
* this priority queue's order. |
279 |
> |
* Add the specified element to this priority queue. |
280 |
|
* |
281 |
< |
* @return the minimal element from this priority queue if it contains |
282 |
< |
* one or more elements, otherwise <tt>null</tt>. |
281 |
> |
* @return <tt>true</tt> |
282 |
> |
* @throws ClassCastException if the specified element cannot be compared |
283 |
> |
* with elements currently in the priority queue according |
284 |
> |
* to the priority queue's ordering. |
285 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
286 |
|
*/ |
287 |
+ |
public boolean offer(E o) { |
288 |
+ |
if (o == null) |
289 |
+ |
throw new NullPointerException(); |
290 |
+ |
modCount++; |
291 |
+ |
++size; |
292 |
+ |
|
293 |
+ |
// Grow backing store if necessary |
294 |
+ |
if (size >= queue.length) |
295 |
+ |
grow(size); |
296 |
+ |
|
297 |
+ |
queue[size] = o; |
298 |
+ |
fixUp(size); |
299 |
+ |
return true; |
300 |
+ |
} |
301 |
+ |
|
302 |
|
public E poll() { |
303 |
|
if (size == 0) |
304 |
|
return null; |
305 |
< |
return remove(1); |
305 |
> |
return (E) remove(1); |
306 |
> |
} |
307 |
> |
|
308 |
> |
public E peek() { |
309 |
> |
return (E) queue[1]; |
310 |
|
} |
311 |
|
|
312 |
+ |
// Collection Methods - the first two override to update docs |
313 |
+ |
|
314 |
|
/** |
315 |
< |
* Return, but do not remove, the minimal element from the |
316 |
< |
* priority queue, or return <tt>null</tt> if the queue is empty. |
317 |
< |
* The term <i>minimal</i> is defined according to this priority |
164 |
< |
* queue's order. This method returns the same object reference |
165 |
< |
* that would be returned by by the <tt>poll</tt> method. The two |
166 |
< |
* methods differ in that this method does not remove the element |
167 |
< |
* from the priority queue. |
315 |
> |
* Adds the specified element to this queue. |
316 |
> |
* @return <tt>true</tt> (as per the general contract of |
317 |
> |
* <tt>Collection.add</tt>). |
318 |
|
* |
319 |
< |
* @return the minimal element from this priority queue if it contains |
320 |
< |
* one or more elements, otherwise <tt>null</tt>. |
319 |
> |
* @throws NullPointerException {@inheritDoc} |
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 E peek() { |
325 |
< |
return queue[1]; |
324 |
> |
public boolean add(E o) { |
325 |
> |
return super.add(o); |
326 |
|
} |
327 |
|
|
328 |
< |
// Collection Methods |
177 |
< |
|
328 |
> |
|
329 |
|
/** |
330 |
< |
* Removes a single instance of the specified element from this priority |
331 |
< |
* queue, if it is present. Returns true if this collection contained the |
332 |
< |
* specified element (or equivalently, if this collection changed as a |
330 |
> |
* Adds all of the elements in the specified collection to this queue. |
331 |
> |
* The behavior of this operation is undefined if |
332 |
> |
* the specified collection is modified while the operation is in |
333 |
> |
* progress. (This implies that the behavior of this call is undefined if |
334 |
> |
* the specified collection is this queue, and this queue is nonempty.) |
335 |
> |
* <p> |
336 |
> |
* This implementation iterates over the specified collection, and adds |
337 |
> |
* each object returned by the iterator to this collection, in turn. |
338 |
> |
* @throws NullPointerException {@inheritDoc} |
339 |
> |
* @throws ClassCastException if any element cannot be compared |
340 |
> |
* with elements currently in the priority queue according |
341 |
> |
* to the priority queue's ordering. |
342 |
> |
*/ |
343 |
> |
public boolean addAll(Collection<? extends E> c) { |
344 |
> |
return super.addAll(c); |
345 |
> |
} |
346 |
> |
|
347 |
> |
|
348 |
> |
/** |
349 |
> |
* Removes a single instance of the specified element from this |
350 |
> |
* queue, if it is present. More formally, |
351 |
> |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
352 |
> |
* o.equals(e))</tt>, if the queue contains one or more such |
353 |
> |
* elements. Returns <tt>true</tt> if the queue contained the |
354 |
> |
* specified element (or equivalently, if the queue changed as a |
355 |
|
* result of the call). |
356 |
|
* |
357 |
< |
* @param element the element to be removed from this collection, |
358 |
< |
* if present. |
359 |
< |
* @return <tt>true</tt> if this collection changed as a result of the |
360 |
< |
* call |
188 |
< |
* @throws ClassCastException if the specified element cannot be compared |
189 |
< |
* with elements currently in the priority queue according |
190 |
< |
* to the priority queue's ordering. |
191 |
< |
* @throws NullPointerException if the specified element is null. |
357 |
> |
* <p>This implementation iterates over the queue looking for the |
358 |
> |
* specified element. If it finds the element, it removes the element |
359 |
> |
* from the queue using the iterator's remove method.<p> |
360 |
> |
* |
361 |
|
*/ |
362 |
< |
public boolean remove(Object element) { |
363 |
< |
if (element == null) |
364 |
< |
throw new NullPointerException(); |
362 |
> |
public boolean remove(Object o) { |
363 |
> |
if (o == null) |
364 |
> |
return false; |
365 |
|
|
366 |
|
if (comparator == null) { |
367 |
|
for (int i = 1; i <= size; i++) { |
368 |
< |
if (((Comparable)queue[i]).compareTo(element) == 0) { |
368 |
> |
if (((Comparable<E>)queue[i]).compareTo((E)o) == 0) { |
369 |
|
remove(i); |
370 |
|
return true; |
371 |
|
} |
372 |
|
} |
373 |
|
} else { |
374 |
|
for (int i = 1; i <= size; i++) { |
375 |
< |
if (comparator.compare(queue[i], (E) element) == 0) { |
375 |
> |
if (comparator.compare((E)queue[i], (E)o) == 0) { |
376 |
|
remove(i); |
377 |
|
return true; |
378 |
|
} |
382 |
|
} |
383 |
|
|
384 |
|
/** |
385 |
< |
* Returns an iterator over the elements in this priority queue. The |
386 |
< |
* elements of the priority queue will be returned by this iterator in the |
387 |
< |
* order specified by the queue, which is to say the order they would be |
388 |
< |
* returned by repeated calls to <tt>poll</tt>. |
220 |
< |
* |
221 |
< |
* @return an <tt>Iterator</tt> over the elements in this priority queue. |
385 |
> |
* Returns an iterator over the elements in this queue. The iterator |
386 |
> |
* does not return the elements in any particular order. |
387 |
> |
* |
388 |
> |
* @return an iterator over the elements in this queue. |
389 |
|
*/ |
390 |
|
public Iterator<E> iterator() { |
391 |
|
return new Itr(); |
420 |
|
checkForComodification(); |
421 |
|
if (cursor > size) |
422 |
|
throw new NoSuchElementException(); |
423 |
< |
E result = queue[cursor]; |
423 |
> |
E result = (E) queue[cursor]; |
424 |
|
lastRet = cursor++; |
425 |
|
return result; |
426 |
|
} |
443 |
|
} |
444 |
|
} |
445 |
|
|
279 |
– |
/** |
280 |
– |
* Returns the number of elements in this priority queue. |
281 |
– |
* |
282 |
– |
* @return the number of elements in this priority queue. |
283 |
– |
*/ |
446 |
|
public int size() { |
447 |
|
return size; |
448 |
|
} |
449 |
|
|
450 |
|
/** |
289 |
– |
* Add the specified element to this priority queue. |
290 |
– |
* |
291 |
– |
* @param element the element to add. |
292 |
– |
* @return true |
293 |
– |
* @throws ClassCastException if the specified element cannot be compared |
294 |
– |
* with elements currently in the priority queue according |
295 |
– |
* to the priority queue's ordering. |
296 |
– |
* @throws NullPointerException if the specified element is null. |
297 |
– |
*/ |
298 |
– |
public boolean offer(E element) { |
299 |
– |
if (element == null) |
300 |
– |
throw new NullPointerException(); |
301 |
– |
modCount++; |
302 |
– |
|
303 |
– |
// Grow backing store if necessary |
304 |
– |
if (++size == queue.length) { |
305 |
– |
E[] newQueue = new E[2 * queue.length]; |
306 |
– |
System.arraycopy(queue, 0, newQueue, 0, size); |
307 |
– |
queue = newQueue; |
308 |
– |
} |
309 |
– |
|
310 |
– |
queue[size] = element; |
311 |
– |
fixUp(size); |
312 |
– |
return true; |
313 |
– |
} |
314 |
– |
|
315 |
– |
/** |
451 |
|
* Remove all elements from the priority queue. |
452 |
|
*/ |
453 |
|
public void clear() { |
471 |
|
assert i <= size; |
472 |
|
modCount++; |
473 |
|
|
474 |
< |
E result = queue[i]; |
474 |
> |
E result = (E) queue[i]; |
475 |
|
queue[i] = queue[size]; |
476 |
|
queue[size--] = null; // Drop extra ref to prevent memory leak |
477 |
|
if (i <= size) |
492 |
|
if (comparator == null) { |
493 |
|
while (k > 1) { |
494 |
|
int j = k >> 1; |
495 |
< |
if (((Comparable)queue[j]).compareTo(queue[k]) <= 0) |
495 |
> |
if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0) |
496 |
|
break; |
497 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
497 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
498 |
|
k = j; |
499 |
|
} |
500 |
|
} else { |
501 |
|
while (k > 1) { |
502 |
|
int j = k >> 1; |
503 |
< |
if (comparator.compare(queue[j], queue[k]) <= 0) |
503 |
> |
if (comparator.compare((E)queue[j], (E)queue[k]) <= 0) |
504 |
|
break; |
505 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
505 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
506 |
|
k = j; |
507 |
|
} |
508 |
|
} |
521 |
|
int j; |
522 |
|
if (comparator == null) { |
523 |
|
while ((j = k << 1) <= size) { |
524 |
< |
if (j<size && ((Comparable)queue[j]).compareTo(queue[j+1]) > 0) |
524 |
> |
if (j<size && ((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0) |
525 |
|
j++; // j indexes smallest kid |
526 |
< |
if (((Comparable)queue[k]).compareTo(queue[j]) <= 0) |
526 |
> |
if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0) |
527 |
|
break; |
528 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
528 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
529 |
|
k = j; |
530 |
|
} |
531 |
|
} else { |
532 |
|
while ((j = k << 1) <= size) { |
533 |
< |
if (j < size && comparator.compare(queue[j], queue[j+1]) > 0) |
533 |
> |
if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0) |
534 |
|
j++; // j indexes smallest kid |
535 |
< |
if (comparator.compare(queue[k], queue[j]) <= 0) |
535 |
> |
if (comparator.compare((E)queue[k], (E)queue[j]) <= 0) |
536 |
|
break; |
537 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
537 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
538 |
|
k = j; |
539 |
|
} |
540 |
|
} |
541 |
|
} |
542 |
|
|
543 |
+ |
|
544 |
|
/** |
545 |
< |
* Returns the comparator associated with this priority queue, or |
546 |
< |
* <tt>null</tt> if it uses its elements' natural ordering. |
545 |
> |
* Returns the comparator used to order this collection, or <tt>null</tt> |
546 |
> |
* if this collection is sorted according to its elements natural ordering |
547 |
> |
* (using <tt>Comparable</tt>). |
548 |
|
* |
549 |
< |
* @return the comparator associated with this priority queue, or |
550 |
< |
* <tt>null</tt> if it uses its elements' natural ordering. |
549 |
> |
* @return the comparator used to order this collection, or <tt>null</tt> |
550 |
> |
* if this collection is sorted according to its elements natural ordering. |
551 |
|
*/ |
552 |
< |
public Comparator comparator() { |
552 |
> |
public Comparator<? super E> comparator() { |
553 |
|
return comparator; |
554 |
|
} |
555 |
|
|
562 |
|
* <tt>Object</tt>) in the proper order. |
563 |
|
* @param s the stream |
564 |
|
*/ |
565 |
< |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
565 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
566 |
|
throws java.io.IOException{ |
567 |
|
// Write out element count, and any hidden stuff |
568 |
|
s.defaultWriteObject(); |
580 |
|
* deserialize it). |
581 |
|
* @param s the stream |
582 |
|
*/ |
583 |
< |
private synchronized void readObject(java.io.ObjectInputStream s) |
583 |
> |
private void readObject(java.io.ObjectInputStream s) |
584 |
|
throws java.io.IOException, ClassNotFoundException { |
585 |
|
// Read in size, and any hidden stuff |
586 |
|
s.defaultReadObject(); |
587 |
|
|
588 |
|
// Read in array length and allocate array |
589 |
|
int arrayLength = s.readInt(); |
590 |
< |
queue = new E[arrayLength]; |
590 |
> |
queue = new Object[arrayLength]; |
591 |
|
|
592 |
|
// Read in all elements in the proper order. |
593 |
|
for (int i=0; i<size; i++) |
594 |
< |
queue[i] = (E)s.readObject(); |
594 |
> |
queue[i] = s.readObject(); |
595 |
|
} |
596 |
|
|
597 |
|
} |
598 |
+ |
|