1 |
dl |
1.38 |
/* |
2 |
jsr166 |
1.57 |
* %W% %E% |
3 |
dl |
1.38 |
* |
4 |
jsr166 |
1.60 |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
5 |
dl |
1.38 |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
package java.util; |
9 |
tim |
1.1 |
|
10 |
|
|
/** |
11 |
dl |
1.41 |
* An unbounded priority {@linkplain Queue queue} based on a priority |
12 |
dl |
1.52 |
* heap. The elements of the priority queue are ordered according to |
13 |
|
|
* their {@linkplain Comparable natural ordering}, or by a {@link |
14 |
|
|
* Comparator} provided at queue construction time, depending on which |
15 |
|
|
* constructor is used. A priority queue does not permit |
16 |
|
|
* <tt>null</tt> elements. A priority queue relying on natural |
17 |
|
|
* ordering also does not permit insertion of non-comparable objects |
18 |
|
|
* (doing so may result in <tt>ClassCastException</tt>). |
19 |
dl |
1.40 |
* |
20 |
dl |
1.41 |
* <p>The <em>head</em> of this queue is the <em>least</em> element |
21 |
|
|
* with respect to the specified ordering. If multiple elements are |
22 |
|
|
* tied for least value, the head is one of those elements -- ties are |
23 |
dl |
1.42 |
* broken arbitrarily. The queue retrieval operations <tt>poll</tt>, |
24 |
|
|
* <tt>remove</tt>, <tt>peek</tt>, and <tt>element</tt> access the |
25 |
|
|
* element at the head of the queue. |
26 |
tim |
1.14 |
* |
27 |
dl |
1.41 |
* <p>A priority queue is unbounded, but has an internal |
28 |
|
|
* <i>capacity</i> governing the size of an array used to store the |
29 |
dl |
1.40 |
* elements on the queue. It is always at least as large as the queue |
30 |
|
|
* size. As elements are added to a priority queue, its capacity |
31 |
|
|
* grows automatically. The details of the growth policy are not |
32 |
|
|
* specified. |
33 |
tim |
1.2 |
* |
34 |
dl |
1.50 |
* <p>This class and its iterator implement all of the |
35 |
|
|
* <em>optional</em> methods of the {@link Collection} and {@link |
36 |
dl |
1.52 |
* Iterator} interfaces. The Iterator provided in method {@link |
37 |
|
|
* #iterator()} is <em>not</em> guaranteed to traverse the elements of |
38 |
|
|
* the priority queue in any particular order. If you need ordered |
39 |
|
|
* traversal, consider using <tt>Arrays.sort(pq.toArray())</tt>. |
40 |
dl |
1.29 |
* |
41 |
|
|
* <p> <strong>Note that this implementation is not synchronized.</strong> |
42 |
|
|
* Multiple threads should not access a <tt>PriorityQueue</tt> |
43 |
|
|
* instance concurrently if any of the threads modifies the list |
44 |
|
|
* structurally. Instead, use the thread-safe {@link |
45 |
dholmes |
1.34 |
* java.util.concurrent.PriorityBlockingQueue} class. |
46 |
dl |
1.29 |
* |
47 |
dholmes |
1.11 |
* <p>Implementation note: this implementation provides O(log(n)) time |
48 |
|
|
* for the insertion methods (<tt>offer</tt>, <tt>poll</tt>, |
49 |
|
|
* <tt>remove()</tt> and <tt>add</tt>) methods; linear time for the |
50 |
|
|
* <tt>remove(Object)</tt> and <tt>contains(Object)</tt> methods; and |
51 |
|
|
* constant time for the retrieval methods (<tt>peek</tt>, |
52 |
|
|
* <tt>element</tt>, and <tt>size</tt>). |
53 |
tim |
1.2 |
* |
54 |
|
|
* <p>This class is a member of the |
55 |
|
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
56 |
|
|
* Java Collections Framework</a>. |
57 |
dl |
1.7 |
* @since 1.5 |
58 |
jsr166 |
1.62 |
* @version %I%, %G% |
59 |
dl |
1.7 |
* @author Josh Bloch |
60 |
dl |
1.45 |
* @param <E> the type of elements held in this collection |
61 |
tim |
1.2 |
*/ |
62 |
|
|
public class PriorityQueue<E> extends AbstractQueue<E> |
63 |
dl |
1.47 |
implements java.io.Serializable { |
64 |
dholmes |
1.11 |
|
65 |
dl |
1.31 |
private static final long serialVersionUID = -7720805057305804111L; |
66 |
dl |
1.30 |
|
67 |
tim |
1.2 |
private static final int DEFAULT_INITIAL_CAPACITY = 11; |
68 |
tim |
1.1 |
|
69 |
tim |
1.2 |
/** |
70 |
dl |
1.55 |
* Priority queue represented as a balanced binary heap: the two |
71 |
|
|
* children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The |
72 |
|
|
* priority queue is ordered by comparator, or by the elements' |
73 |
|
|
* natural ordering, if comparator is null: For each node n in the |
74 |
|
|
* heap and each descendant d of n, n <= d. The element with the |
75 |
|
|
* lowest value is in queue[0], assuming the queue is nonempty. |
76 |
tim |
1.2 |
*/ |
77 |
tim |
1.16 |
private transient Object[] queue; |
78 |
tim |
1.1 |
|
79 |
tim |
1.2 |
/** |
80 |
|
|
* The number of elements in the priority queue. |
81 |
|
|
*/ |
82 |
|
|
private int size = 0; |
83 |
tim |
1.1 |
|
84 |
tim |
1.2 |
/** |
85 |
|
|
* The comparator, or null if priority queue uses elements' |
86 |
|
|
* natural ordering. |
87 |
|
|
*/ |
88 |
tim |
1.16 |
private final Comparator<? super E> comparator; |
89 |
tim |
1.2 |
|
90 |
|
|
/** |
91 |
|
|
* The number of times this priority queue has been |
92 |
|
|
* <i>structurally modified</i>. See AbstractList for gory details. |
93 |
|
|
*/ |
94 |
dl |
1.5 |
private transient int modCount = 0; |
95 |
tim |
1.2 |
|
96 |
|
|
/** |
97 |
dl |
1.52 |
* Creates a <tt>PriorityQueue</tt> with the default initial |
98 |
|
|
* capacity (11) that orders its elements according to their |
99 |
|
|
* {@linkplain Comparable natural ordering}. |
100 |
tim |
1.2 |
*/ |
101 |
|
|
public PriorityQueue() { |
102 |
dholmes |
1.11 |
this(DEFAULT_INITIAL_CAPACITY, null); |
103 |
tim |
1.1 |
} |
104 |
tim |
1.2 |
|
105 |
|
|
/** |
106 |
dl |
1.52 |
* Creates a <tt>PriorityQueue</tt> with the specified initial |
107 |
|
|
* capacity that orders its elements according to their |
108 |
|
|
* {@linkplain Comparable natural ordering}. |
109 |
tim |
1.2 |
* |
110 |
dl |
1.52 |
* @param initialCapacity the initial capacity for this priority queue |
111 |
dholmes |
1.23 |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
112 |
|
|
* than 1 |
113 |
tim |
1.2 |
*/ |
114 |
|
|
public PriorityQueue(int initialCapacity) { |
115 |
|
|
this(initialCapacity, null); |
116 |
tim |
1.1 |
} |
117 |
tim |
1.2 |
|
118 |
|
|
/** |
119 |
dholmes |
1.21 |
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
120 |
tim |
1.2 |
* that orders its elements according to the specified comparator. |
121 |
|
|
* |
122 |
dl |
1.52 |
* @param initialCapacity the initial capacity for this priority queue |
123 |
|
|
* @param comparator the comparator that will be used to order |
124 |
|
|
* this priority queue. If <tt>null</tt>, the <i>natural |
125 |
|
|
* ordering</i> of the elements will be used. |
126 |
|
|
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is |
127 |
|
|
* less than 1 |
128 |
tim |
1.2 |
*/ |
129 |
dl |
1.52 |
public PriorityQueue(int initialCapacity, |
130 |
dholmes |
1.23 |
Comparator<? super E> comparator) { |
131 |
dl |
1.55 |
// Note: This restriction of at least one is not actually needed, |
132 |
|
|
// but continues for 1.5 compatibility |
133 |
tim |
1.2 |
if (initialCapacity < 1) |
134 |
dholmes |
1.15 |
throw new IllegalArgumentException(); |
135 |
dl |
1.55 |
this.queue = new Object[initialCapacity]; |
136 |
tim |
1.2 |
this.comparator = comparator; |
137 |
tim |
1.1 |
} |
138 |
jsr166 |
1.56 |
|
139 |
dl |
1.22 |
/** |
140 |
|
|
* Creates a <tt>PriorityQueue</tt> containing the elements in the |
141 |
dl |
1.55 |
* specified collection. If the specified collection is an |
142 |
tim |
1.25 |
* instance of a {@link java.util.SortedSet} or is another |
143 |
dl |
1.52 |
* <tt>PriorityQueue</tt>, the priority queue will be ordered |
144 |
|
|
* according to the same ordering. Otherwise, this priority queue |
145 |
|
|
* will be ordered according to the natural ordering of its elements. |
146 |
tim |
1.2 |
* |
147 |
dl |
1.52 |
* @param c the collection whose elements are to be placed |
148 |
|
|
* into this priority queue |
149 |
tim |
1.2 |
* @throws ClassCastException if elements of the specified collection |
150 |
|
|
* cannot be compared to one another according to the priority |
151 |
dl |
1.52 |
* queue's ordering |
152 |
|
|
* @throws NullPointerException if the specified collection or any |
153 |
|
|
* of its elements are null |
154 |
tim |
1.2 |
*/ |
155 |
tim |
1.16 |
public PriorityQueue(Collection<? extends E> c) { |
156 |
dl |
1.55 |
initFromCollection(c); |
157 |
jsr166 |
1.56 |
if (c instanceof SortedSet) |
158 |
dl |
1.55 |
comparator = (Comparator<? super E>) |
159 |
|
|
((SortedSet<? extends E>)c).comparator(); |
160 |
jsr166 |
1.56 |
else if (c instanceof PriorityQueue) |
161 |
dl |
1.55 |
comparator = (Comparator<? super E>) |
162 |
|
|
((PriorityQueue<? extends E>)c).comparator(); |
163 |
|
|
else { |
164 |
tim |
1.2 |
comparator = null; |
165 |
dl |
1.55 |
heapify(); |
166 |
tim |
1.2 |
} |
167 |
dl |
1.22 |
} |
168 |
|
|
|
169 |
|
|
/** |
170 |
|
|
* Creates a <tt>PriorityQueue</tt> containing the elements in the |
171 |
dl |
1.55 |
* specified priority queue. This priority queue will be |
172 |
dl |
1.52 |
* ordered according to the same ordering as the given priority |
173 |
|
|
* queue. |
174 |
|
|
* |
175 |
|
|
* @param c the priority queue whose elements are to be placed |
176 |
|
|
* into this priority queue |
177 |
|
|
* @throws ClassCastException if elements of <tt>c</tt> cannot be |
178 |
|
|
* compared to one another according to <tt>c</tt>'s |
179 |
|
|
* ordering |
180 |
|
|
* @throws NullPointerException if the specified priority queue or any |
181 |
|
|
* of its elements are null |
182 |
dl |
1.22 |
*/ |
183 |
|
|
public PriorityQueue(PriorityQueue<? extends E> c) { |
184 |
|
|
comparator = (Comparator<? super E>)c.comparator(); |
185 |
dl |
1.55 |
initFromCollection(c); |
186 |
dl |
1.22 |
} |
187 |
dholmes |
1.18 |
|
188 |
dl |
1.22 |
/** |
189 |
|
|
* Creates a <tt>PriorityQueue</tt> containing the elements in the |
190 |
dl |
1.55 |
* specified sorted set. This priority queue will be ordered |
191 |
dl |
1.52 |
* according to the same ordering as the given sorted set. |
192 |
|
|
* |
193 |
|
|
* @param c the sorted set whose elements are to be placed |
194 |
|
|
* into this priority queue. |
195 |
|
|
* @throws ClassCastException if elements of the specified sorted |
196 |
|
|
* set cannot be compared to one another according to the |
197 |
|
|
* sorted set's ordering |
198 |
|
|
* @throws NullPointerException if the specified sorted set or any |
199 |
|
|
* of its elements are null |
200 |
dl |
1.22 |
*/ |
201 |
|
|
public PriorityQueue(SortedSet<? extends E> c) { |
202 |
|
|
comparator = (Comparator<? super E>)c.comparator(); |
203 |
dl |
1.55 |
initFromCollection(c); |
204 |
tim |
1.1 |
} |
205 |
|
|
|
206 |
dl |
1.22 |
/** |
207 |
dl |
1.55 |
* Initialize queue array with elements from the given Collection. |
208 |
|
|
* @param c the collection |
209 |
dl |
1.22 |
*/ |
210 |
dl |
1.55 |
private void initFromCollection(Collection<? extends E> c) { |
211 |
|
|
Object[] a = c.toArray(); |
212 |
|
|
// If c.toArray incorrectly doesn't return Object[], copy it. |
213 |
|
|
if (a.getClass() != Object[].class) |
214 |
|
|
a = Arrays.copyOf(a, a.length, Object[].class); |
215 |
|
|
queue = a; |
216 |
|
|
size = a.length; |
217 |
jsr166 |
1.56 |
} |
218 |
dl |
1.55 |
|
219 |
|
|
/** |
220 |
|
|
* Increases the capacity of the array. |
221 |
|
|
* |
222 |
|
|
* @param minCapacity the desired minimum capacity |
223 |
|
|
*/ |
224 |
|
|
private void grow(int minCapacity) { |
225 |
|
|
if (minCapacity < 0) // overflow |
226 |
dl |
1.22 |
throw new OutOfMemoryError(); |
227 |
dl |
1.55 |
int oldCapacity = queue.length; |
228 |
|
|
// Double size if small; else grow by 50% |
229 |
|
|
int newCapacity = ((oldCapacity < 64)? |
230 |
|
|
((oldCapacity + 1) * 2): |
231 |
dl |
1.58 |
((oldCapacity / 2) * 3)); |
232 |
|
|
if (newCapacity < 0) // overflow |
233 |
|
|
newCapacity = Integer.MAX_VALUE; |
234 |
dl |
1.55 |
if (newCapacity < minCapacity) |
235 |
|
|
newCapacity = minCapacity; |
236 |
|
|
queue = Arrays.copyOf(queue, newCapacity); |
237 |
dl |
1.22 |
} |
238 |
dl |
1.36 |
|
239 |
tim |
1.2 |
/** |
240 |
dl |
1.42 |
* Inserts the specified element into this priority queue. |
241 |
tim |
1.2 |
* |
242 |
dl |
1.52 |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
243 |
|
|
* @throws ClassCastException if the specified element cannot be |
244 |
|
|
* compared with elements currently in this priority queue |
245 |
|
|
* according to the priority queue's ordering |
246 |
|
|
* @throws NullPointerException if the specified element is null |
247 |
tim |
1.2 |
*/ |
248 |
dl |
1.52 |
public boolean add(E e) { |
249 |
|
|
return offer(e); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
/** |
253 |
|
|
* Inserts the specified element into this priority queue. |
254 |
|
|
* |
255 |
|
|
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
256 |
|
|
* @throws ClassCastException if the specified element cannot be |
257 |
|
|
* compared with elements currently in this priority queue |
258 |
|
|
* according to the priority queue's ordering |
259 |
|
|
* @throws NullPointerException if the specified element is null |
260 |
|
|
*/ |
261 |
|
|
public boolean offer(E e) { |
262 |
|
|
if (e == null) |
263 |
dholmes |
1.11 |
throw new NullPointerException(); |
264 |
|
|
modCount++; |
265 |
dl |
1.55 |
int i = size; |
266 |
|
|
if (i >= queue.length) |
267 |
|
|
grow(i + 1); |
268 |
|
|
size = i + 1; |
269 |
|
|
if (i == 0) |
270 |
|
|
queue[0] = e; |
271 |
|
|
else |
272 |
|
|
siftUp(i, e); |
273 |
dholmes |
1.11 |
return true; |
274 |
|
|
} |
275 |
|
|
|
276 |
dl |
1.40 |
public E peek() { |
277 |
tim |
1.2 |
if (size == 0) |
278 |
|
|
return null; |
279 |
dl |
1.55 |
return (E) queue[0]; |
280 |
tim |
1.1 |
} |
281 |
|
|
|
282 |
dl |
1.52 |
private int indexOf(Object o) { |
283 |
dl |
1.55 |
if (o != null) { |
284 |
|
|
for (int i = 0; i < size; i++) |
285 |
|
|
if (o.equals(queue[i])) |
286 |
|
|
return i; |
287 |
|
|
} |
288 |
dl |
1.52 |
return -1; |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
/** |
292 |
|
|
* Removes a single instance of the specified element from this queue, |
293 |
|
|
* if it is present. More formally, removes an element <tt>e</tt> such |
294 |
|
|
* that <tt>o.equals(e)</tt>, if this queue contains one or more such |
295 |
|
|
* elements. Returns true if this queue contained the specified element |
296 |
|
|
* (or equivalently, if this queue changed as a result of the call). |
297 |
|
|
* |
298 |
|
|
* @param o element to be removed from this queue, if present |
299 |
|
|
* @return <tt>true</tt> if this queue changed as a result of the call |
300 |
|
|
*/ |
301 |
|
|
public boolean remove(Object o) { |
302 |
|
|
int i = indexOf(o); |
303 |
|
|
if (i == -1) |
304 |
|
|
return false; |
305 |
|
|
else { |
306 |
|
|
removeAt(i); |
307 |
|
|
return true; |
308 |
|
|
} |
309 |
|
|
} |
310 |
dholmes |
1.11 |
|
311 |
jsr166 |
1.56 |
/** |
312 |
dl |
1.55 |
* Version of remove using reference equality, not equals. |
313 |
jsr166 |
1.59 |
* Needed by iterator.remove. |
314 |
jsr166 |
1.56 |
* |
315 |
dl |
1.55 |
* @param o element to be removed from this queue, if present |
316 |
jsr166 |
1.59 |
* @return <tt>true</tt> if removed |
317 |
dl |
1.55 |
*/ |
318 |
|
|
boolean removeEq(Object o) { |
319 |
|
|
for (int i = 0; i < size; i++) { |
320 |
|
|
if (o == queue[i]) { |
321 |
|
|
removeAt(i); |
322 |
|
|
return true; |
323 |
|
|
} |
324 |
|
|
} |
325 |
|
|
return false; |
326 |
|
|
} |
327 |
|
|
|
328 |
dholmes |
1.11 |
/** |
329 |
dl |
1.52 |
* Returns <tt>true</tt> if this queue contains the specified element. |
330 |
|
|
* More formally, returns <tt>true</tt> if and only if this queue contains |
331 |
|
|
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
332 |
dholmes |
1.23 |
* |
333 |
dl |
1.52 |
* @param o object to be checked for containment in this queue |
334 |
|
|
* @return <tt>true</tt> if this queue contains the specified element |
335 |
dholmes |
1.11 |
*/ |
336 |
dl |
1.52 |
public boolean contains(Object o) { |
337 |
|
|
return indexOf(o) != -1; |
338 |
tim |
1.14 |
} |
339 |
dholmes |
1.11 |
|
340 |
dl |
1.49 |
/** |
341 |
dl |
1.52 |
* Returns an array containing all of the elements in this queue, |
342 |
|
|
* The elements are in no particular order. |
343 |
|
|
* |
344 |
|
|
* <p>The returned array will be "safe" in that no references to it are |
345 |
|
|
* maintained by this list. (In other words, this method must allocate |
346 |
|
|
* a new array). The caller is thus free to modify the returned array. |
347 |
|
|
* |
348 |
jsr166 |
1.59 |
* @return an array containing all of the elements in this queue |
349 |
dl |
1.49 |
*/ |
350 |
dl |
1.52 |
public Object[] toArray() { |
351 |
dl |
1.55 |
return Arrays.copyOf(queue, size); |
352 |
dl |
1.52 |
} |
353 |
tim |
1.2 |
|
354 |
dl |
1.52 |
/** |
355 |
|
|
* Returns an array containing all of the elements in this queue. |
356 |
|
|
* The elements are in no particular order. The runtime type of |
357 |
|
|
* the returned array is that of the specified array. If the queue |
358 |
|
|
* fits in the specified array, it is returned therein. |
359 |
|
|
* Otherwise, a new array is allocated with the runtime type of |
360 |
|
|
* the specified array and the size of this queue. |
361 |
|
|
* |
362 |
|
|
* <p>If the queue fits in the specified array with room to spare |
363 |
|
|
* (i.e., the array has more elements than the queue), the element in |
364 |
|
|
* the array immediately following the end of the collection is set to |
365 |
|
|
* <tt>null</tt>. (This is useful in determining the length of the |
366 |
|
|
* queue <i>only</i> if the caller knows that the queue does not contain |
367 |
|
|
* any null elements.) |
368 |
|
|
* |
369 |
|
|
* @param a the array into which the elements of the queue are to |
370 |
|
|
* be stored, if it is big enough; otherwise, a new array of the |
371 |
|
|
* same runtime type is allocated for this purpose. |
372 |
|
|
* @return an array containing the elements of the queue |
373 |
|
|
* @throws ArrayStoreException if the runtime type of the specified array |
374 |
|
|
* is not a supertype of the runtime type of every element in |
375 |
|
|
* this queue |
376 |
|
|
* @throws NullPointerException if the specified array is null |
377 |
|
|
*/ |
378 |
|
|
public <T> T[] toArray(T[] a) { |
379 |
|
|
if (a.length < size) |
380 |
|
|
// Make a new array of a's runtime type, but my contents: |
381 |
dl |
1.55 |
return (T[]) Arrays.copyOf(queue, size, a.getClass()); |
382 |
|
|
System.arraycopy(queue, 0, a, 0, size); |
383 |
dl |
1.52 |
if (a.length > size) |
384 |
|
|
a[size] = null; |
385 |
|
|
return a; |
386 |
tim |
1.1 |
} |
387 |
tim |
1.2 |
|
388 |
dholmes |
1.23 |
/** |
389 |
|
|
* Returns an iterator over the elements in this queue. The iterator |
390 |
|
|
* does not return the elements in any particular order. |
391 |
|
|
* |
392 |
dl |
1.52 |
* @return an iterator over the elements in this queue |
393 |
dholmes |
1.23 |
*/ |
394 |
tim |
1.2 |
public Iterator<E> iterator() { |
395 |
dl |
1.7 |
return new Itr(); |
396 |
tim |
1.2 |
} |
397 |
|
|
|
398 |
dl |
1.55 |
private final class Itr implements Iterator<E> { |
399 |
dl |
1.7 |
/** |
400 |
|
|
* Index (into queue array) of element to be returned by |
401 |
tim |
1.2 |
* subsequent call to next. |
402 |
dl |
1.7 |
*/ |
403 |
dl |
1.55 |
private int cursor = 0; |
404 |
tim |
1.2 |
|
405 |
dl |
1.7 |
/** |
406 |
dl |
1.36 |
* Index of element returned by most recent call to next, |
407 |
|
|
* unless that element came from the forgetMeNot list. |
408 |
dl |
1.55 |
* Set to -1 if element is deleted by a call to remove. |
409 |
dl |
1.7 |
*/ |
410 |
dl |
1.55 |
private int lastRet = -1; |
411 |
dl |
1.7 |
|
412 |
|
|
/** |
413 |
dl |
1.55 |
* A queue of elements that were moved from the unvisited portion of |
414 |
dl |
1.36 |
* the heap into the visited portion as a result of "unlucky" element |
415 |
|
|
* removals during the iteration. (Unlucky element removals are those |
416 |
dl |
1.55 |
* that require a siftup instead of a siftdown.) We must visit all of |
417 |
dl |
1.36 |
* the elements in this list to complete the iteration. We do this |
418 |
|
|
* after we've completed the "normal" iteration. |
419 |
|
|
* |
420 |
|
|
* We expect that most iterations, even those involving removals, |
421 |
|
|
* will not use need to store elements in this field. |
422 |
|
|
*/ |
423 |
dl |
1.55 |
private ArrayDeque<E> forgetMeNot = null; |
424 |
dl |
1.36 |
|
425 |
|
|
/** |
426 |
|
|
* Element returned by the most recent call to next iff that |
427 |
|
|
* element was drawn from the forgetMeNot list. |
428 |
|
|
*/ |
429 |
dl |
1.55 |
private E lastRetElt = null; |
430 |
|
|
|
431 |
|
|
/** |
432 |
|
|
* The modCount value that the iterator believes that the backing |
433 |
|
|
* List should have. If this expectation is violated, the iterator |
434 |
|
|
* has detected concurrent modification. |
435 |
|
|
*/ |
436 |
|
|
private int expectedModCount = modCount; |
437 |
dl |
1.35 |
|
438 |
dl |
1.7 |
public boolean hasNext() { |
439 |
jsr166 |
1.56 |
return cursor < size || |
440 |
dl |
1.55 |
(forgetMeNot != null && !forgetMeNot.isEmpty()); |
441 |
dl |
1.7 |
} |
442 |
|
|
|
443 |
|
|
public E next() { |
444 |
dl |
1.55 |
if (expectedModCount != modCount) |
445 |
|
|
throw new ConcurrentModificationException(); |
446 |
jsr166 |
1.56 |
if (cursor < size) |
447 |
dl |
1.55 |
return (E) queue[lastRet = cursor++]; |
448 |
|
|
if (forgetMeNot != null) { |
449 |
|
|
lastRet = -1; |
450 |
|
|
lastRetElt = forgetMeNot.poll(); |
451 |
jsr166 |
1.56 |
if (lastRetElt != null) |
452 |
dl |
1.55 |
return lastRetElt; |
453 |
dl |
1.36 |
} |
454 |
dl |
1.55 |
throw new NoSuchElementException(); |
455 |
dl |
1.7 |
} |
456 |
tim |
1.2 |
|
457 |
dl |
1.7 |
public void remove() { |
458 |
dl |
1.55 |
if (expectedModCount != modCount) |
459 |
|
|
throw new ConcurrentModificationException(); |
460 |
|
|
if (lastRet == -1 && lastRetElt == null) |
461 |
|
|
throw new IllegalStateException(); |
462 |
|
|
if (lastRet != -1) { |
463 |
dl |
1.36 |
E moved = PriorityQueue.this.removeAt(lastRet); |
464 |
dl |
1.55 |
lastRet = -1; |
465 |
jsr166 |
1.56 |
if (moved == null) |
466 |
dl |
1.36 |
cursor--; |
467 |
dl |
1.55 |
else { |
468 |
dl |
1.36 |
if (forgetMeNot == null) |
469 |
dl |
1.55 |
forgetMeNot = new ArrayDeque<E>(); |
470 |
dl |
1.36 |
forgetMeNot.add(moved); |
471 |
jsr166 |
1.56 |
} |
472 |
dl |
1.55 |
} else { |
473 |
|
|
PriorityQueue.this.removeEq(lastRetElt); |
474 |
dl |
1.36 |
lastRetElt = null; |
475 |
jsr166 |
1.56 |
} |
476 |
tim |
1.2 |
expectedModCount = modCount; |
477 |
dl |
1.7 |
} |
478 |
tim |
1.2 |
|
479 |
|
|
} |
480 |
|
|
|
481 |
tim |
1.1 |
public int size() { |
482 |
tim |
1.2 |
return size; |
483 |
tim |
1.1 |
} |
484 |
tim |
1.2 |
|
485 |
|
|
/** |
486 |
dl |
1.52 |
* Removes all of the elements from this priority queue. |
487 |
dl |
1.49 |
* The queue will be empty after this call returns. |
488 |
tim |
1.2 |
*/ |
489 |
|
|
public void clear() { |
490 |
|
|
modCount++; |
491 |
dl |
1.55 |
for (int i = 0; i < size; i++) |
492 |
tim |
1.2 |
queue[i] = null; |
493 |
|
|
size = 0; |
494 |
|
|
} |
495 |
|
|
|
496 |
dl |
1.40 |
public E poll() { |
497 |
dl |
1.36 |
if (size == 0) |
498 |
dl |
1.40 |
return null; |
499 |
dl |
1.55 |
int s = --size; |
500 |
dl |
1.36 |
modCount++; |
501 |
dl |
1.55 |
E result = (E)queue[0]; |
502 |
|
|
E x = (E)queue[s]; |
503 |
|
|
queue[s] = null; |
504 |
|
|
if (s != 0) |
505 |
|
|
siftDown(0, x); |
506 |
dl |
1.36 |
return result; |
507 |
|
|
} |
508 |
|
|
|
509 |
|
|
/** |
510 |
dl |
1.55 |
* Removes the ith element from queue. |
511 |
tim |
1.2 |
* |
512 |
dl |
1.55 |
* Normally this method leaves the elements at up to i-1, |
513 |
|
|
* inclusive, untouched. Under these circumstances, it returns |
514 |
|
|
* null. Occasionally, in order to maintain the heap invariant, |
515 |
|
|
* it must swap a later element of the list with one earlier than |
516 |
|
|
* i. Under these circumstances, this method returns the element |
517 |
|
|
* that was previously at the end of the list and is now at some |
518 |
|
|
* position before i. This fact is used by iterator.remove so as to |
519 |
|
|
* avoid missing traverseing elements. |
520 |
tim |
1.2 |
*/ |
521 |
dl |
1.52 |
private E removeAt(int i) { |
522 |
dl |
1.55 |
assert i >= 0 && i < size; |
523 |
tim |
1.2 |
modCount++; |
524 |
dl |
1.55 |
int s = --size; |
525 |
|
|
if (s == i) // removed last element |
526 |
|
|
queue[i] = null; |
527 |
|
|
else { |
528 |
|
|
E moved = (E) queue[s]; |
529 |
jsr166 |
1.56 |
queue[s] = null; |
530 |
dl |
1.55 |
siftDown(i, moved); |
531 |
dl |
1.36 |
if (queue[i] == moved) { |
532 |
dl |
1.55 |
siftUp(i, moved); |
533 |
dl |
1.36 |
if (queue[i] != moved) |
534 |
|
|
return moved; |
535 |
|
|
} |
536 |
dl |
1.35 |
} |
537 |
dl |
1.36 |
return null; |
538 |
tim |
1.1 |
} |
539 |
|
|
|
540 |
tim |
1.2 |
/** |
541 |
dl |
1.55 |
* Inserts item x at position k, maintaining heap invariant by |
542 |
|
|
* promoting x up the tree until it is greater than or equal to |
543 |
|
|
* its parent, or is the root. |
544 |
|
|
* |
545 |
|
|
* To simplify and speed up coercions and comparisons. the |
546 |
|
|
* Comparable and Comparator versions are separated into different |
547 |
|
|
* methods that are otherwise identical. (Similarly for siftDown.) |
548 |
jsr166 |
1.56 |
* |
549 |
dl |
1.55 |
* @param k the position to fill |
550 |
|
|
* @param x the item to insert |
551 |
|
|
*/ |
552 |
|
|
private void siftUp(int k, E x) { |
553 |
jsr166 |
1.56 |
if (comparator != null) |
554 |
dl |
1.55 |
siftUpUsingComparator(k, x); |
555 |
|
|
else |
556 |
|
|
siftUpComparable(k, x); |
557 |
|
|
} |
558 |
|
|
|
559 |
|
|
private void siftUpComparable(int k, E x) { |
560 |
|
|
Comparable<? super E> key = (Comparable<? super E>) x; |
561 |
|
|
while (k > 0) { |
562 |
|
|
int parent = (k - 1) >>> 1; |
563 |
|
|
Object e = queue[parent]; |
564 |
jsr166 |
1.56 |
if (key.compareTo((E)e) >= 0) |
565 |
dl |
1.55 |
break; |
566 |
|
|
queue[k] = e; |
567 |
|
|
k = parent; |
568 |
|
|
} |
569 |
|
|
queue[k] = key; |
570 |
|
|
} |
571 |
|
|
|
572 |
|
|
private void siftUpUsingComparator(int k, E x) { |
573 |
|
|
while (k > 0) { |
574 |
|
|
int parent = (k - 1) >>> 1; |
575 |
|
|
Object e = queue[parent]; |
576 |
jsr166 |
1.56 |
if (comparator.compare(x, (E)e) >= 0) |
577 |
dl |
1.55 |
break; |
578 |
|
|
queue[k] = e; |
579 |
|
|
k = parent; |
580 |
|
|
} |
581 |
|
|
queue[k] = x; |
582 |
|
|
} |
583 |
|
|
|
584 |
|
|
/** |
585 |
|
|
* Inserts item x at position k, maintaining heap invariant by |
586 |
|
|
* demoting x down the tree repeatedly until it is less than or |
587 |
|
|
* equal to its children or is a leaf. |
588 |
|
|
* |
589 |
|
|
* @param k the position to fill |
590 |
|
|
* @param x the item to insert |
591 |
|
|
*/ |
592 |
|
|
private void siftDown(int k, E x) { |
593 |
jsr166 |
1.56 |
if (comparator != null) |
594 |
dl |
1.55 |
siftDownUsingComparator(k, x); |
595 |
|
|
else |
596 |
|
|
siftDownComparable(k, x); |
597 |
|
|
} |
598 |
|
|
|
599 |
|
|
private void siftDownComparable(int k, E x) { |
600 |
|
|
Comparable<? super E> key = (Comparable<? super E>)x; |
601 |
|
|
int half = size >>> 1; // loop while a non-leaf |
602 |
|
|
while (k < half) { |
603 |
|
|
int child = (k << 1) + 1; // assume left child is least |
604 |
|
|
Object c = queue[child]; |
605 |
|
|
int right = child + 1; |
606 |
|
|
if (right < size && |
607 |
|
|
((Comparable<? super E>)c).compareTo((E)queue[right]) > 0) |
608 |
|
|
c = queue[child = right]; |
609 |
|
|
if (key.compareTo((E)c) <= 0) |
610 |
|
|
break; |
611 |
|
|
queue[k] = c; |
612 |
|
|
k = child; |
613 |
|
|
} |
614 |
|
|
queue[k] = key; |
615 |
|
|
} |
616 |
|
|
|
617 |
|
|
private void siftDownUsingComparator(int k, E x) { |
618 |
|
|
int half = size >>> 1; |
619 |
|
|
while (k < half) { |
620 |
|
|
int child = (k << 1) + 1; |
621 |
|
|
Object c = queue[child]; |
622 |
|
|
int right = child + 1; |
623 |
|
|
if (right < size && |
624 |
|
|
comparator.compare((E)c, (E)queue[right]) > 0) |
625 |
jsr166 |
1.56 |
c = queue[child = right]; |
626 |
|
|
if (comparator.compare(x, (E)c) <= 0) |
627 |
dl |
1.55 |
break; |
628 |
|
|
queue[k] = c; |
629 |
|
|
k = child; |
630 |
tim |
1.2 |
} |
631 |
dl |
1.55 |
queue[k] = x; |
632 |
dl |
1.36 |
} |
633 |
dl |
1.35 |
|
634 |
dl |
1.36 |
/** |
635 |
|
|
* Establishes the heap invariant (described above) in the entire tree, |
636 |
|
|
* assuming nothing about the order of the elements prior to the call. |
637 |
|
|
*/ |
638 |
|
|
private void heapify() { |
639 |
jsr166 |
1.56 |
for (int i = (size >>> 1) - 1; i >= 0; i--) |
640 |
dl |
1.55 |
siftDown(i, (E)queue[i]); |
641 |
tim |
1.2 |
} |
642 |
|
|
|
643 |
dholmes |
1.23 |
/** |
644 |
dl |
1.52 |
* Returns the comparator used to order the elements in this |
645 |
|
|
* queue, or <tt>null</tt> if this queue is sorted according to |
646 |
|
|
* the {@linkplain Comparable natural ordering} of its elements. |
647 |
|
|
* |
648 |
|
|
* @return the comparator used to order this queue, or |
649 |
|
|
* <tt>null</tt> if this queue is sorted according to the |
650 |
|
|
* natural ordering of its elements. |
651 |
dholmes |
1.23 |
*/ |
652 |
tim |
1.16 |
public Comparator<? super E> comparator() { |
653 |
tim |
1.2 |
return comparator; |
654 |
|
|
} |
655 |
dl |
1.5 |
|
656 |
|
|
/** |
657 |
|
|
* Save the state of the instance to a stream (that |
658 |
|
|
* is, serialize it). |
659 |
|
|
* |
660 |
|
|
* @serialData The length of the array backing the instance is |
661 |
|
|
* emitted (int), followed by all of its elements (each an |
662 |
|
|
* <tt>Object</tt>) in the proper order. |
663 |
dl |
1.7 |
* @param s the stream |
664 |
dl |
1.5 |
*/ |
665 |
dl |
1.22 |
private void writeObject(java.io.ObjectOutputStream s) |
666 |
dl |
1.5 |
throws java.io.IOException{ |
667 |
dl |
1.7 |
// Write out element count, and any hidden stuff |
668 |
|
|
s.defaultWriteObject(); |
669 |
dl |
1.5 |
|
670 |
|
|
// Write out array length |
671 |
dl |
1.55 |
// For compatibility with 1.5 version, must be at least 2. |
672 |
|
|
s.writeInt(Math.max(2, queue.length)); |
673 |
dl |
1.5 |
|
674 |
dl |
1.7 |
// Write out all elements in the proper order. |
675 |
dl |
1.55 |
for (int i=0; i<size; i++) |
676 |
dl |
1.5 |
s.writeObject(queue[i]); |
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
/** |
680 |
dl |
1.52 |
* Reconstitute the <tt>PriorityQueue</tt> instance from a stream |
681 |
|
|
* (that is, deserialize it). |
682 |
dl |
1.7 |
* @param s the stream |
683 |
dl |
1.5 |
*/ |
684 |
dl |
1.22 |
private void readObject(java.io.ObjectInputStream s) |
685 |
dl |
1.5 |
throws java.io.IOException, ClassNotFoundException { |
686 |
dl |
1.7 |
// Read in size, and any hidden stuff |
687 |
|
|
s.defaultReadObject(); |
688 |
dl |
1.5 |
|
689 |
|
|
// Read in array length and allocate array |
690 |
|
|
int arrayLength = s.readInt(); |
691 |
tim |
1.16 |
queue = new Object[arrayLength]; |
692 |
dl |
1.5 |
|
693 |
dl |
1.7 |
// Read in all elements in the proper order. |
694 |
dl |
1.55 |
for (int i=0; i<size; i++) |
695 |
dl |
1.37 |
queue[i] = (E) s.readObject(); |
696 |
dl |
1.5 |
} |
697 |
|
|
|
698 |
tim |
1.1 |
} |