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 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 |
12 |
< |
* head is one of those elements. A priority queue does not permit |
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 |
< |
* |
16 |
> |
* |
17 |
|
* <p>The {@link #remove()} and {@link #poll()} methods remove and |
18 |
|
* return the head of the queue. |
19 |
|
* |
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 |
|
|
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. |
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 |
78 |
|
private transient int modCount = 0; |
79 |
|
|
80 |
|
/** |
81 |
< |
* Create a <tt>PriorityQueue</tt> with the default initial capacity |
81 |
> |
* Creates a <tt>PriorityQueue</tt> with the default initial capacity |
82 |
|
* (11) that orders its elements according to their natural |
83 |
|
* ordering (using <tt>Comparable</tt>.) |
84 |
|
*/ |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
< |
* Create a <tt>PriorityQueue</tt> with the specified initial capacity |
90 |
> |
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
91 |
|
* that orders its elements according to their natural ordering |
92 |
|
* (using <tt>Comparable</tt>.) |
93 |
|
* |
98 |
|
} |
99 |
|
|
100 |
|
/** |
101 |
< |
* Create a <tt>PriorityQueue</tt> with the specified initial capacity |
101 |
> |
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
102 |
|
* that orders its elements according to the specified comparator. |
103 |
|
* |
104 |
|
* @param initialCapacity the initial capacity for this priority queue. |
105 |
|
* @param comparator the comparator used to order this priority queue. |
106 |
|
* If <tt>null</tt> then the order depends on the elements' natural |
107 |
|
* ordering. |
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 |
< |
initialCapacity = 1; |
114 |
< |
queue = (E[]) new Object[initialCapacity + 1]; |
113 |
> |
throw new IllegalArgumentException(); |
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. If the specified collection |
119 |
> |
* Creates a <tt>PriorityQueue</tt> containing the elements in the |
120 |
> |
* specified collection. |
121 |
> |
* The priority queue has an initial capacity of 110% of the |
122 |
> |
* size of the specified collection 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' |
126 |
|
* natural order if the collection is sorted according to its elements' |
128 |
|
* <tt>Sorted</tt>, the priority queue is ordered according to |
129 |
|
* its elements' natural order. |
130 |
|
* |
131 |
< |
* @param initialElements the collection whose elements are to be placed |
131 |
> |
* @param c the collection whose elements are to be placed |
132 |
|
* into this priority queue. |
133 |
|
* @throws ClassCastException if elements of the specified collection |
134 |
|
* cannot be compared to one another according to the priority |
135 |
|
* queue's ordering. |
136 |
< |
* @throws NullPointerException if the specified collection or an |
137 |
< |
* element of the specified collection is <tt>null</tt>. |
136 |
> |
* @throws NullPointerException if <tt>c</tt> or any element within it |
137 |
> |
* is <tt>null</tt> |
138 |
|
*/ |
139 |
< |
public PriorityQueue(Collection<E> initialElements) { |
140 |
< |
int sz = initialElements.size(); |
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; |
138 |
– |
queue = (E[]) new Object[initialCapacity + 1]; |
145 |
|
|
146 |
< |
if (initialElements instanceof Sorted) { |
147 |
< |
comparator = ((Sorted)initialElements).comparator(); |
148 |
< |
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
149 |
< |
queue[++size] = i.next(); |
146 |
> |
this.queue = new Object[initialCapacity + 1]; |
147 |
> |
|
148 |
> |
if (c instanceof Sorted) { |
149 |
> |
comparator = (Comparator<? super E>)((Sorted)c).comparator(); |
150 |
|
} else { |
151 |
|
comparator = null; |
146 |
– |
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
147 |
– |
add(i.next()); |
152 |
|
} |
153 |
+ |
|
154 |
+ |
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); ) |
155 |
+ |
add(i.next()); |
156 |
|
} |
157 |
|
|
158 |
|
// Queue Methods |
160 |
|
/** |
161 |
|
* Add the specified element to this priority queue. |
162 |
|
* |
156 |
– |
* @param element the element to add. |
163 |
|
* @return <tt>true</tt> |
164 |
|
* @throws ClassCastException if the specified element cannot be compared |
165 |
|
* with elements currently in the priority queue according |
166 |
|
* to the priority queue's ordering. |
167 |
< |
* @throws NullPointerException if the specified element is null. |
167 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
168 |
|
*/ |
169 |
< |
public boolean offer(E element) { |
170 |
< |
if (element == null) |
169 |
> |
public boolean offer(E o) { |
170 |
> |
if (o == null) |
171 |
|
throw new NullPointerException(); |
172 |
|
modCount++; |
173 |
|
++size; |
174 |
|
|
175 |
|
// Grow backing store if necessary |
176 |
|
while (size >= queue.length) { |
177 |
< |
E[] newQueue = (E[]) new Object[2 * queue.length]; |
177 |
> |
Object[] newQueue = new Object[2 * queue.length]; |
178 |
|
System.arraycopy(queue, 0, newQueue, 0, queue.length); |
179 |
|
queue = newQueue; |
180 |
|
} |
181 |
|
|
182 |
< |
queue[size] = element; |
182 |
> |
queue[size] = o; |
183 |
|
fixUp(size); |
184 |
|
return true; |
185 |
|
} |
187 |
|
public E poll() { |
188 |
|
if (size == 0) |
189 |
|
return null; |
190 |
< |
return remove(1); |
190 |
> |
return (E) remove(1); |
191 |
|
} |
192 |
|
|
193 |
|
public E peek() { |
194 |
< |
return queue[1]; |
194 |
> |
return (E) queue[1]; |
195 |
|
} |
196 |
|
|
197 |
|
// Collection Methods |
200 |
|
|
201 |
|
/** |
202 |
|
* @throws NullPointerException if the specified element is <tt>null</tt>. |
203 |
+ |
* @throws ClassCastException if the specified element cannot be compared |
204 |
+ |
* with elements currently in the priority queue according |
205 |
+ |
* to the priority queue's ordering. |
206 |
|
*/ |
207 |
< |
public boolean add(E element) { |
208 |
< |
return super.add(element); |
207 |
> |
public boolean add(E o) { |
208 |
> |
return super.add(o); |
209 |
|
} |
210 |
|
|
202 |
– |
// /** |
203 |
– |
// * @throws NullPointerException if any element is <tt>null</tt>. |
204 |
– |
// */ |
205 |
– |
// public boolean addAll(Collection c) { |
206 |
– |
// return super.addAll(c); |
207 |
– |
// } |
208 |
– |
|
211 |
|
/** |
212 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
212 |
> |
* @throws ClassCastException if any element cannot be compared |
213 |
> |
* with elements currently in the priority queue according |
214 |
> |
* to the priority queue's ordering. |
215 |
> |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
216 |
> |
* is <tt>null</tt> |
217 |
|
*/ |
218 |
+ |
public boolean addAll(Collection<? extends E> c) { |
219 |
+ |
return super.addAll(c); |
220 |
+ |
} |
221 |
+ |
|
222 |
|
public boolean remove(Object o) { |
223 |
|
if (o == null) |
224 |
< |
throw new NullPointerException(); |
224 |
> |
return false; |
225 |
|
|
226 |
|
if (comparator == null) { |
227 |
|
for (int i = 1; i <= size; i++) { |
228 |
< |
if (((Comparable)queue[i]).compareTo(o) == 0) { |
228 |
> |
if (((Comparable<E>)queue[i]).compareTo((E)o) == 0) { |
229 |
|
remove(i); |
230 |
|
return true; |
231 |
|
} |
232 |
|
} |
233 |
|
} else { |
234 |
|
for (int i = 1; i <= size; i++) { |
235 |
< |
if (comparator.compare(queue[i], (E)o) == 0) { |
235 |
> |
if (comparator.compare((E)queue[i], (E)o) == 0) { |
236 |
|
remove(i); |
237 |
|
return true; |
238 |
|
} |
241 |
|
return false; |
242 |
|
} |
243 |
|
|
234 |
– |
/** |
235 |
– |
* Returns an iterator over the elements in this priority queue. The |
236 |
– |
* elements of the priority queue will be returned by this iterator in the |
237 |
– |
* order specified by the queue, which is to say the order they would be |
238 |
– |
* returned by repeated calls to <tt>poll</tt>. |
239 |
– |
* |
240 |
– |
* @return an <tt>Iterator</tt> over the elements in this priority queue. |
241 |
– |
*/ |
244 |
|
public Iterator<E> iterator() { |
245 |
|
return new Itr(); |
246 |
|
} |
274 |
|
checkForComodification(); |
275 |
|
if (cursor > size) |
276 |
|
throw new NoSuchElementException(); |
277 |
< |
E result = queue[cursor]; |
277 |
> |
E result = (E) queue[cursor]; |
278 |
|
lastRet = cursor++; |
279 |
|
return result; |
280 |
|
} |
330 |
|
assert i <= size; |
331 |
|
modCount++; |
332 |
|
|
333 |
< |
E result = queue[i]; |
333 |
> |
E result = (E) queue[i]; |
334 |
|
queue[i] = queue[size]; |
335 |
|
queue[size--] = null; // Drop extra ref to prevent memory leak |
336 |
|
if (i <= size) |
351 |
|
if (comparator == null) { |
352 |
|
while (k > 1) { |
353 |
|
int j = k >> 1; |
354 |
< |
if (((Comparable)queue[j]).compareTo(queue[k]) <= 0) |
354 |
> |
if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0) |
355 |
|
break; |
356 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
356 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
357 |
|
k = j; |
358 |
|
} |
359 |
|
} else { |
360 |
|
while (k > 1) { |
361 |
|
int j = k >> 1; |
362 |
< |
if (comparator.compare(queue[j], queue[k]) <= 0) |
362 |
> |
if (comparator.compare((E)queue[j], (E)queue[k]) <= 0) |
363 |
|
break; |
364 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
364 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
365 |
|
k = j; |
366 |
|
} |
367 |
|
} |
380 |
|
int j; |
381 |
|
if (comparator == null) { |
382 |
|
while ((j = k << 1) <= size) { |
383 |
< |
if (j<size && ((Comparable)queue[j]).compareTo(queue[j+1]) > 0) |
383 |
> |
if (j<size && ((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0) |
384 |
|
j++; // j indexes smallest kid |
385 |
< |
if (((Comparable)queue[k]).compareTo(queue[j]) <= 0) |
385 |
> |
if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0) |
386 |
|
break; |
387 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
387 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
388 |
|
k = j; |
389 |
|
} |
390 |
|
} else { |
391 |
|
while ((j = k << 1) <= size) { |
392 |
< |
if (j < size && comparator.compare(queue[j], queue[j+1]) > 0) |
392 |
> |
if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0) |
393 |
|
j++; // j indexes smallest kid |
394 |
< |
if (comparator.compare(queue[k], queue[j]) <= 0) |
394 |
> |
if (comparator.compare((E)queue[k], (E)queue[j]) <= 0) |
395 |
|
break; |
396 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
396 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
397 |
|
k = j; |
398 |
|
} |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
< |
public Comparator comparator() { |
402 |
> |
public Comparator<? super E> comparator() { |
403 |
|
return comparator; |
404 |
|
} |
405 |
|
|
437 |
|
|
438 |
|
// Read in array length and allocate array |
439 |
|
int arrayLength = s.readInt(); |
440 |
< |
queue = (E[]) new Object[arrayLength]; |
440 |
> |
queue = new Object[arrayLength]; |
441 |
|
|
442 |
|
// Read in all elements in the proper order. |
443 |
|
for (int i=0; i<size; i++) |
444 |
< |
queue[i] = (E)s.readObject(); |
444 |
> |
queue[i] = s.readObject(); |
445 |
|
} |
446 |
|
|
447 |
|
} |