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