1 |
|
/* |
2 |
|
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
< |
* as explained at http://creativecommons.org/licenses/publicdomain. |
3 |
> |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
– |
import java.io.*; |
7 |
|
|
8 |
|
/** |
9 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
14 |
|
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
15 |
|
* when used as a queue. |
16 |
|
* |
17 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
18 |
< |
* Exceptions include {@link #remove(Object) remove}, {@link |
19 |
< |
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
20 |
< |
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
21 |
< |
* iterator.remove()}, and the bulk operations, all of which run in linear |
22 |
< |
* time. |
17 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
18 |
> |
* Exceptions include |
19 |
> |
* {@link #remove(Object) remove}, |
20 |
> |
* {@link #removeFirstOccurrence removeFirstOccurrence}, |
21 |
> |
* {@link #removeLastOccurrence removeLastOccurrence}, |
22 |
> |
* {@link #contains contains}, |
23 |
> |
* {@link #iterator iterator.remove()}, |
24 |
> |
* and the bulk operations, all of which run in linear time. |
25 |
|
* |
26 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
27 |
< |
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
28 |
< |
* is created, in any way except through the iterator's own remove method, the |
29 |
< |
* iterator will generally throw a {@link ConcurrentModificationException}. |
30 |
< |
* Thus, in the face of concurrent modification, the iterator fails quickly |
31 |
< |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
32 |
< |
* an undetermined time in the future. |
26 |
> |
* <p>The iterators returned by this class's {@link #iterator() iterator} |
27 |
> |
* method are <em>fail-fast</em>: If the deque is modified at any time after |
28 |
> |
* the iterator is created, in any way except through the iterator's own |
29 |
> |
* {@code remove} method, the iterator will generally throw a {@link |
30 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
31 |
> |
* modification, the iterator fails quickly and cleanly, rather than risking |
32 |
> |
* arbitrary, non-deterministic behavior at an undetermined time in the |
33 |
> |
* future. |
34 |
|
* |
35 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
36 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
37 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
38 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
38 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
39 |
|
* Therefore, it would be wrong to write a program that depended on this |
40 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
41 |
|
* should be used only to detect bugs.</i> |
42 |
|
* |
43 |
|
* <p>This class and its iterator implement all of the |
44 |
< |
* optional methods of the {@link Collection} and {@link |
45 |
< |
* Iterator} interfaces. This class is a member of the <a |
46 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
47 |
< |
* Framework</a>. |
44 |
> |
* <em>optional</em> methods of the {@link Collection} and {@link |
45 |
> |
* Iterator} interfaces. |
46 |
> |
* |
47 |
> |
* <p>This class is a member of the |
48 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
49 |
> |
* Java Collections Framework</a>. |
50 |
|
* |
51 |
|
* @author Josh Bloch and Doug Lea |
52 |
|
* @since 1.6 |
53 |
|
* @param <E> the type of elements held in this collection |
54 |
|
*/ |
55 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
56 |
< |
implements Deque<E>, Cloneable, Serializable |
56 |
> |
implements Deque<E>, Cloneable, java.io.Serializable |
57 |
|
{ |
58 |
|
/** |
59 |
< |
* The array in which the elements of in the deque are stored. |
59 |
> |
* The array in which the elements of the deque are stored. |
60 |
|
* The capacity of the deque is the length of this array, which is |
61 |
|
* always a power of two. The array is never allowed to become |
62 |
|
* full, except transiently within an addX method where it is |
65 |
|
* other. We also guarantee that all array cells not holding |
66 |
|
* deque elements are always null. |
67 |
|
*/ |
68 |
< |
private transient E[] elements; |
68 |
> |
private transient Object[] elements; |
69 |
|
|
70 |
|
/** |
71 |
|
* The index of the element at the head of the deque (which is the |
89 |
|
// ****** Array allocation and resizing utilities ****** |
90 |
|
|
91 |
|
/** |
92 |
< |
* Allocate empty array to hold the given number of elements. |
92 |
> |
* Allocates empty array to hold the given number of elements. |
93 |
|
* |
94 |
< |
* @param numElements the number of elements to hold. |
94 |
> |
* @param numElements the number of elements to hold |
95 |
|
*/ |
96 |
< |
private void allocateElements(int numElements) { |
96 |
> |
private void allocateElements(int numElements) { |
97 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
98 |
|
// Find the best power of two to hold elements. |
99 |
|
// Tests "<=" because arrays aren't kept full. |
109 |
|
if (initialCapacity < 0) // Too many elements, must back off |
110 |
|
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements |
111 |
|
} |
112 |
< |
elements = (E[]) new Object[initialCapacity]; |
112 |
> |
elements = new Object[initialCapacity]; |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* Double the capacity of this deque. Call only when full, i.e., |
116 |
> |
* Doubles the capacity of this deque. Call only when full, i.e., |
117 |
|
* when head and tail have wrapped around to become equal. |
118 |
|
*/ |
119 |
|
private void doubleCapacity() { |
120 |
< |
assert head == tail; |
120 |
> |
assert head == tail; |
121 |
|
int p = head; |
122 |
|
int n = elements.length; |
123 |
|
int r = n - p; // number of elements to the right of p |
127 |
|
Object[] a = new Object[newCapacity]; |
128 |
|
System.arraycopy(elements, p, a, 0, r); |
129 |
|
System.arraycopy(elements, 0, a, r, p); |
130 |
< |
elements = (E[])a; |
130 |
> |
elements = a; |
131 |
|
head = 0; |
132 |
|
tail = n; |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* Copy the elements from our element array into the specified array, |
136 |
> |
* Copies the elements from our element array into the specified array, |
137 |
|
* in order (from first to last element in the deque). It is assumed |
138 |
|
* that the array is large enough to hold all elements in the deque. |
139 |
|
* |
151 |
|
} |
152 |
|
|
153 |
|
/** |
154 |
< |
* Constructs an empty array deque with the an initial capacity |
154 |
> |
* Constructs an empty array deque with an initial capacity |
155 |
|
* sufficient to hold 16 elements. |
156 |
|
*/ |
157 |
|
public ArrayDeque() { |
158 |
< |
elements = (E[]) new Object[16]; |
158 |
> |
elements = new Object[16]; |
159 |
|
} |
160 |
|
|
161 |
|
/** |
188 |
|
// terms of these. |
189 |
|
|
190 |
|
/** |
191 |
< |
* Inserts the specified element to the front this deque. |
191 |
> |
* Inserts the specified element at the front of this deque. |
192 |
|
* |
193 |
< |
* @param e the element to insert |
194 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
193 |
> |
* @param e the element to add |
194 |
> |
* @throws NullPointerException if the specified element is null |
195 |
|
*/ |
196 |
|
public void addFirst(E e) { |
197 |
|
if (e == null) |
198 |
|
throw new NullPointerException(); |
199 |
|
elements[head = (head - 1) & (elements.length - 1)] = e; |
200 |
< |
if (head == tail) |
200 |
> |
if (head == tail) |
201 |
|
doubleCapacity(); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* Inserts the specified element to the end this deque. |
206 |
< |
* This method is equivalent to {@link Collection#add} and |
207 |
< |
* {@link #push}. |
205 |
> |
* Inserts the specified element at the end of this deque. |
206 |
> |
* |
207 |
> |
* <p>This method is equivalent to {@link #add}. |
208 |
|
* |
209 |
< |
* @param e the element to insert |
210 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
209 |
> |
* @param e the element to add |
210 |
> |
* @throws NullPointerException if the specified element is null |
211 |
|
*/ |
212 |
|
public void addLast(E e) { |
213 |
|
if (e == null) |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* Retrieves and removes the first element of this deque, or |
218 |
< |
* <tt>null</tt> if this deque is empty. |
219 |
< |
* |
220 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
221 |
< |
* this deque is empty |
222 |
< |
*/ |
223 |
< |
public E pollFirst() { |
224 |
< |
int h = head; |
225 |
< |
E result = elements[h]; // Element is null if deque empty |
226 |
< |
if (result == null) |
227 |
< |
return null; |
228 |
< |
elements[h] = null; // Must null out slot |
229 |
< |
head = (h + 1) & (elements.length - 1); |
230 |
< |
return result; |
231 |
< |
} |
232 |
< |
|
233 |
< |
/** |
234 |
< |
* Retrieves and removes the last element of this deque, or |
235 |
< |
* <tt>null</tt> if this deque is empty. |
221 |
> |
* Inserts the specified element at the front of this deque. |
222 |
|
* |
223 |
< |
* @return the last element of this deque, or <tt>null</tt> if |
224 |
< |
* this deque is empty |
225 |
< |
*/ |
240 |
< |
public E pollLast() { |
241 |
< |
int t = (tail - 1) & (elements.length - 1); |
242 |
< |
E result = elements[t]; |
243 |
< |
if (result == null) |
244 |
< |
return null; |
245 |
< |
elements[t] = null; |
246 |
< |
tail = t; |
247 |
< |
return result; |
248 |
< |
} |
249 |
< |
|
250 |
< |
/** |
251 |
< |
* Inserts the specified element to the front this deque. |
252 |
< |
* |
253 |
< |
* @param e the element to insert |
254 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
255 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
223 |
> |
* @param e the element to add |
224 |
> |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
225 |
> |
* @throws NullPointerException if the specified element is null |
226 |
|
*/ |
227 |
|
public boolean offerFirst(E e) { |
228 |
|
addFirst(e); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
< |
* Inserts the specified element to the end this deque. |
233 |
> |
* Inserts the specified element at the end of this deque. |
234 |
|
* |
235 |
< |
* @param e the element to insert |
236 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
237 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
235 |
> |
* @param e the element to add |
236 |
> |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
237 |
> |
* @throws NullPointerException if the specified element is null |
238 |
|
*/ |
239 |
|
public boolean offerLast(E e) { |
240 |
|
addLast(e); |
242 |
|
} |
243 |
|
|
244 |
|
/** |
245 |
< |
* Retrieves and removes the first element of this deque. This method |
276 |
< |
* differs from the <tt>pollFirst</tt> method in that it throws an |
277 |
< |
* exception if this deque is empty. |
278 |
< |
* |
279 |
< |
* @return the first element of this deque |
280 |
< |
* @throws NoSuchElementException if this deque is empty |
245 |
> |
* @throws NoSuchElementException {@inheritDoc} |
246 |
|
*/ |
247 |
|
public E removeFirst() { |
248 |
|
E x = pollFirst(); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* Retrieves and removes the last element of this deque. This method |
291 |
< |
* differs from the <tt>pollLast</tt> method in that it throws an |
292 |
< |
* exception if this deque is empty. |
293 |
< |
* |
294 |
< |
* @return the last element of this deque |
295 |
< |
* @throws NoSuchElementException if this deque is empty |
255 |
> |
* @throws NoSuchElementException {@inheritDoc} |
256 |
|
*/ |
257 |
|
public E removeLast() { |
258 |
|
E x = pollLast(); |
261 |
|
return x; |
262 |
|
} |
263 |
|
|
264 |
< |
/** |
265 |
< |
* Retrieves, but does not remove, the first element of this deque, |
266 |
< |
* returning <tt>null</tt> if this deque is empty. |
267 |
< |
* |
268 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
269 |
< |
* this deque is empty |
270 |
< |
*/ |
271 |
< |
public E peekFirst() { |
272 |
< |
return elements[head]; // elements[head] is null if deque empty |
264 |
> |
public E pollFirst() { |
265 |
> |
int h = head; |
266 |
> |
@SuppressWarnings("unchecked") |
267 |
> |
E result = (E) elements[h]; |
268 |
> |
// Element is null if deque empty |
269 |
> |
if (result == null) |
270 |
> |
return null; |
271 |
> |
elements[h] = null; // Must null out slot |
272 |
> |
head = (h + 1) & (elements.length - 1); |
273 |
> |
return result; |
274 |
|
} |
275 |
|
|
276 |
< |
/** |
277 |
< |
* Retrieves, but does not remove, the last element of this deque, |
278 |
< |
* returning <tt>null</tt> if this deque is empty. |
279 |
< |
* |
280 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
281 |
< |
* is empty |
282 |
< |
*/ |
283 |
< |
public E peekLast() { |
284 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
276 |
> |
public E pollLast() { |
277 |
> |
int t = (tail - 1) & (elements.length - 1); |
278 |
> |
@SuppressWarnings("unchecked") |
279 |
> |
E result = (E) elements[t]; |
280 |
> |
if (result == null) |
281 |
> |
return null; |
282 |
> |
elements[t] = null; |
283 |
> |
tail = t; |
284 |
> |
return result; |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* Retrieves, but does not remove, the first element of this |
328 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
329 |
< |
* in that it throws an exception if this deque is empty. |
330 |
< |
* |
331 |
< |
* @return the first element of this deque |
332 |
< |
* @throws NoSuchElementException if this deque is empty |
288 |
> |
* @throws NoSuchElementException {@inheritDoc} |
289 |
|
*/ |
290 |
|
public E getFirst() { |
291 |
< |
E x = elements[head]; |
292 |
< |
if (x == null) |
291 |
> |
@SuppressWarnings("unchecked") |
292 |
> |
E result = (E) elements[head]; |
293 |
> |
if (result == null) |
294 |
|
throw new NoSuchElementException(); |
295 |
< |
return x; |
295 |
> |
return result; |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* Retrieves, but does not remove, the last element of this |
343 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
344 |
< |
* in that it throws an exception if this deque is empty. |
345 |
< |
* |
346 |
< |
* @return the last element of this deque |
347 |
< |
* @throws NoSuchElementException if this deque is empty |
299 |
> |
* @throws NoSuchElementException {@inheritDoc} |
300 |
|
*/ |
301 |
|
public E getLast() { |
302 |
< |
E x = elements[(tail - 1) & (elements.length - 1)]; |
303 |
< |
if (x == null) |
302 |
> |
@SuppressWarnings("unchecked") |
303 |
> |
E result = (E) elements[(tail - 1) & (elements.length - 1)]; |
304 |
> |
if (result == null) |
305 |
|
throw new NoSuchElementException(); |
306 |
< |
return x; |
306 |
> |
return result; |
307 |
> |
} |
308 |
> |
|
309 |
> |
@SuppressWarnings("unchecked") |
310 |
> |
public E peekFirst() { |
311 |
> |
// elements[head] is null if deque empty |
312 |
> |
return (E) elements[head]; |
313 |
> |
} |
314 |
> |
|
315 |
> |
@SuppressWarnings("unchecked") |
316 |
> |
public E peekLast() { |
317 |
> |
return (E) elements[(tail - 1) & (elements.length - 1)]; |
318 |
|
} |
319 |
|
|
320 |
|
/** |
321 |
|
* Removes the first occurrence of the specified element in this |
322 |
< |
* deque (when traversing the deque from head to tail). If the deque |
323 |
< |
* does not contain the element, it is unchanged. |
322 |
> |
* deque (when traversing the deque from head to tail). |
323 |
> |
* If the deque does not contain the element, it is unchanged. |
324 |
> |
* More formally, removes the first element {@code e} such that |
325 |
> |
* {@code o.equals(e)} (if such an element exists). |
326 |
> |
* Returns {@code true} if this deque contained the specified element |
327 |
> |
* (or equivalently, if this deque changed as a result of the call). |
328 |
|
* |
329 |
< |
* @param e element to be removed from this deque, if present |
330 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
329 |
> |
* @param o element to be removed from this deque, if present |
330 |
> |
* @return {@code true} if the deque contained the specified element |
331 |
|
*/ |
332 |
< |
public boolean removeFirstOccurrence(Object e) { |
333 |
< |
if (e == null) |
332 |
> |
public boolean removeFirstOccurrence(Object o) { |
333 |
> |
if (o == null) |
334 |
|
return false; |
335 |
|
int mask = elements.length - 1; |
336 |
|
int i = head; |
337 |
< |
E x; |
337 |
> |
Object x; |
338 |
|
while ( (x = elements[i]) != null) { |
339 |
< |
if (e.equals(x)) { |
339 |
> |
if (o.equals(x)) { |
340 |
|
delete(i); |
341 |
|
return true; |
342 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
|
* Removes the last occurrence of the specified element in this |
350 |
< |
* deque (when traversing the deque from head to tail). If the deque |
351 |
< |
* does not contain the element, it is unchanged. |
350 |
> |
* deque (when traversing the deque from head to tail). |
351 |
> |
* If the deque does not contain the element, it is unchanged. |
352 |
> |
* More formally, removes the last element {@code e} such that |
353 |
> |
* {@code o.equals(e)} (if such an element exists). |
354 |
> |
* Returns {@code true} if this deque contained the specified element |
355 |
> |
* (or equivalently, if this deque changed as a result of the call). |
356 |
|
* |
357 |
< |
* @param e element to be removed from this deque, if present |
358 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
357 |
> |
* @param o element to be removed from this deque, if present |
358 |
> |
* @return {@code true} if the deque contained the specified element |
359 |
|
*/ |
360 |
< |
public boolean removeLastOccurrence(Object e) { |
361 |
< |
if (e == null) |
360 |
> |
public boolean removeLastOccurrence(Object o) { |
361 |
> |
if (o == null) |
362 |
|
return false; |
363 |
|
int mask = elements.length - 1; |
364 |
|
int i = (tail - 1) & mask; |
365 |
< |
E x; |
365 |
> |
Object x; |
366 |
|
while ( (x = elements[i]) != null) { |
367 |
< |
if (e.equals(x)) { |
367 |
> |
if (o.equals(x)) { |
368 |
|
delete(i); |
369 |
|
return true; |
370 |
|
} |
376 |
|
// *** Queue methods *** |
377 |
|
|
378 |
|
/** |
379 |
< |
* Inserts the specified element to the end of this deque. |
408 |
< |
* |
409 |
< |
* <p>This method is equivalent to {@link #offerLast}. |
410 |
< |
* |
411 |
< |
* @param e the element to insert |
412 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
413 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
414 |
< |
*/ |
415 |
< |
public boolean offer(E e) { |
416 |
< |
return offerLast(e); |
417 |
< |
} |
418 |
< |
|
419 |
< |
/** |
420 |
< |
* Inserts the specified element to the end of this deque. |
379 |
> |
* Inserts the specified element at the end of this deque. |
380 |
|
* |
381 |
|
* <p>This method is equivalent to {@link #addLast}. |
382 |
|
* |
383 |
< |
* @param e the element to insert |
384 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
385 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
383 |
> |
* @param e the element to add |
384 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
385 |
> |
* @throws NullPointerException if the specified element is null |
386 |
|
*/ |
387 |
|
public boolean add(E e) { |
388 |
|
addLast(e); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
< |
* Retrieves and removes the head of the queue represented by |
435 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
436 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
437 |
< |
* if this deque is empty. |
393 |
> |
* Inserts the specified element at the end of this deque. |
394 |
|
* |
395 |
< |
* <p>This method is equivalent to {@link #pollFirst}. |
395 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
396 |
|
* |
397 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
398 |
< |
* this deque is empty |
397 |
> |
* @param e the element to add |
398 |
> |
* @return {@code true} (as specified by {@link Queue#offer}) |
399 |
> |
* @throws NullPointerException if the specified element is null |
400 |
|
*/ |
401 |
< |
public E poll() { |
402 |
< |
return pollFirst(); |
401 |
> |
public boolean offer(E e) { |
402 |
> |
return offerLast(e); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
|
* Retrieves and removes the head of the queue represented by this deque. |
407 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
407 |
> |
* |
408 |
> |
* This method differs from {@link #poll poll} only in that it throws an |
409 |
|
* exception if this deque is empty. |
410 |
|
* |
411 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
412 |
|
* |
413 |
|
* @return the head of the queue represented by this deque |
414 |
< |
* @throws NoSuchElementException if this deque is empty |
414 |
> |
* @throws NoSuchElementException {@inheritDoc} |
415 |
|
*/ |
416 |
|
public E remove() { |
417 |
|
return removeFirst(); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
422 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
421 |
> |
* Retrieves and removes the head of the queue represented by this deque |
422 |
> |
* (in other words, the first element of this deque), or returns |
423 |
> |
* {@code null} if this deque is empty. |
424 |
|
* |
425 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
425 |
> |
* <p>This method is equivalent to {@link #pollFirst}. |
426 |
|
* |
427 |
|
* @return the head of the queue represented by this deque, or |
428 |
< |
* <tt>null</tt> if this deque is empty |
428 |
> |
* {@code null} if this deque is empty |
429 |
|
*/ |
430 |
< |
public E peek() { |
431 |
< |
return peekFirst(); |
430 |
> |
public E poll() { |
431 |
> |
return pollFirst(); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
|
* Retrieves, but does not remove, the head of the queue represented by |
436 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
436 |
> |
* this deque. This method differs from {@link #peek peek} only in |
437 |
|
* that it throws an exception if this deque is empty. |
438 |
|
* |
439 |
< |
* <p>This method is equivalent to {@link #getFirst} |
439 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
440 |
|
* |
441 |
|
* @return the head of the queue represented by this deque |
442 |
< |
* @throws NoSuchElementException if this deque is empty |
442 |
> |
* @throws NoSuchElementException {@inheritDoc} |
443 |
|
*/ |
444 |
|
public E element() { |
445 |
|
return getFirst(); |
446 |
|
} |
447 |
|
|
448 |
+ |
/** |
449 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
450 |
+ |
* this deque, or returns {@code null} if this deque is empty. |
451 |
+ |
* |
452 |
+ |
* <p>This method is equivalent to {@link #peekFirst}. |
453 |
+ |
* |
454 |
+ |
* @return the head of the queue represented by this deque, or |
455 |
+ |
* {@code null} if this deque is empty |
456 |
+ |
*/ |
457 |
+ |
public E peek() { |
458 |
+ |
return peekFirst(); |
459 |
+ |
} |
460 |
+ |
|
461 |
|
// *** Stack methods *** |
462 |
|
|
463 |
|
/** |
464 |
|
* Pushes an element onto the stack represented by this deque. In other |
465 |
< |
* words, inserts the element to the front this deque. |
465 |
> |
* words, inserts the element at the front of this deque. |
466 |
|
* |
467 |
|
* <p>This method is equivalent to {@link #addFirst}. |
468 |
|
* |
469 |
|
* @param e the element to push |
470 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
470 |
> |
* @throws NullPointerException if the specified element is null |
471 |
|
*/ |
472 |
|
public void push(E e) { |
473 |
|
addFirst(e); |
480 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
481 |
|
* |
482 |
|
* @return the element at the front of this deque (which is the top |
483 |
< |
* of the stack represented by this deque) |
484 |
< |
* @throws NoSuchElementException if this deque is empty |
483 |
> |
* of the stack represented by this deque) |
484 |
> |
* @throws NoSuchElementException {@inheritDoc} |
485 |
|
*/ |
486 |
|
public E pop() { |
487 |
|
return removeFirst(); |
488 |
|
} |
489 |
|
|
490 |
+ |
private void checkInvariants() { |
491 |
+ |
assert elements[tail] == null; |
492 |
+ |
assert head == tail ? elements[head] == null : |
493 |
+ |
(elements[head] != null && |
494 |
+ |
elements[(tail - 1) & (elements.length - 1)] != null); |
495 |
+ |
assert elements[(head - 1) & (elements.length - 1)] == null; |
496 |
+ |
} |
497 |
+ |
|
498 |
|
/** |
499 |
< |
* Remove the element at the specified position in the elements array, |
500 |
< |
* adjusting head, tail, and size as necessary. This can result in |
501 |
< |
* motion of elements backwards or forwards in the array. |
502 |
< |
* |
503 |
< |
* <p>This method is called delete rather than remove to emphasize |
504 |
< |
* that its semantics differ from those of List.remove(int). |
505 |
< |
* |
499 |
> |
* Removes the element at the specified position in the elements array, |
500 |
> |
* adjusting head and tail as necessary. This can result in motion of |
501 |
> |
* elements backwards or forwards in the array. |
502 |
> |
* |
503 |
> |
* <p>This method is called delete rather than remove to emphasize |
504 |
> |
* that its semantics differ from those of {@link List#remove(int)}. |
505 |
> |
* |
506 |
|
* @return true if elements moved backwards |
507 |
|
*/ |
508 |
|
private boolean delete(int i) { |
509 |
< |
// Case 1: Deque doesn't wrap |
510 |
< |
// Case 2: Deque does wrap and removed element is in the head portion |
511 |
< |
if ((head < tail || tail == 0) || i >= head) { |
512 |
< |
System.arraycopy(elements, head, elements, head + 1, i - head); |
513 |
< |
elements[head] = null; |
514 |
< |
head = (head + 1) & (elements.length - 1); |
509 |
> |
checkInvariants(); |
510 |
> |
final Object[] elements = this.elements; |
511 |
> |
final int mask = elements.length - 1; |
512 |
> |
final int h = head; |
513 |
> |
final int t = tail; |
514 |
> |
final int front = (i - h) & mask; |
515 |
> |
final int back = (t - i) & mask; |
516 |
> |
|
517 |
> |
// Invariant: head <= i < tail mod circularity |
518 |
> |
if (front >= ((t - h) & mask)) |
519 |
> |
throw new ConcurrentModificationException(); |
520 |
> |
|
521 |
> |
// Optimize for least element motion |
522 |
> |
if (front < back) { |
523 |
> |
if (h <= i) { |
524 |
> |
System.arraycopy(elements, h, elements, h + 1, front); |
525 |
> |
} else { // Wrap around |
526 |
> |
System.arraycopy(elements, 0, elements, 1, i); |
527 |
> |
elements[0] = elements[mask]; |
528 |
> |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
529 |
> |
} |
530 |
> |
elements[h] = null; |
531 |
> |
head = (h + 1) & mask; |
532 |
|
return false; |
533 |
+ |
} else { |
534 |
+ |
if (i < t) { // Copy the null tail as well |
535 |
+ |
System.arraycopy(elements, i + 1, elements, i, back); |
536 |
+ |
tail = t - 1; |
537 |
+ |
} else { // Wrap around |
538 |
+ |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
539 |
+ |
elements[mask] = elements[0]; |
540 |
+ |
System.arraycopy(elements, 1, elements, 0, t); |
541 |
+ |
tail = (t - 1) & mask; |
542 |
+ |
} |
543 |
+ |
return true; |
544 |
|
} |
537 |
– |
|
538 |
– |
// Case 3: Deque wraps and removed element is in the tail portion |
539 |
– |
tail--; |
540 |
– |
System.arraycopy(elements, i + 1, elements, i, tail - i); |
541 |
– |
elements[tail] = null; |
542 |
– |
return true; |
545 |
|
} |
546 |
|
|
547 |
|
// *** Collection Methods *** |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
559 |
> |
* Returns {@code true} if this deque contains no elements. |
560 |
|
* |
561 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
561 |
> |
* @return {@code true} if this deque contains no elements |
562 |
|
*/ |
563 |
|
public boolean isEmpty() { |
564 |
|
return head == tail; |
569 |
|
* will be ordered from first (head) to last (tail). This is the same |
570 |
|
* order that elements would be dequeued (via successive calls to |
571 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
572 |
< |
* |
573 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
572 |
> |
* |
573 |
> |
* @return an iterator over the elements in this deque |
574 |
|
*/ |
575 |
|
public Iterator<E> iterator() { |
576 |
|
return new DeqIterator(); |
577 |
|
} |
578 |
|
|
579 |
+ |
public Iterator<E> descendingIterator() { |
580 |
+ |
return new DescendingIterator(); |
581 |
+ |
} |
582 |
+ |
|
583 |
|
private class DeqIterator implements Iterator<E> { |
584 |
|
/** |
585 |
|
* Index of element to be returned by subsequent call to next. |
603 |
|
} |
604 |
|
|
605 |
|
public E next() { |
600 |
– |
E result; |
606 |
|
if (cursor == fence) |
607 |
|
throw new NoSuchElementException(); |
608 |
+ |
@SuppressWarnings("unchecked") |
609 |
+ |
E result = (E) elements[cursor]; |
610 |
|
// This check doesn't catch all possible comodifications, |
611 |
|
// but does catch the ones that corrupt traversal |
612 |
< |
if (tail != fence || (result = elements[cursor]) == null) |
612 |
> |
if (tail != fence || result == null) |
613 |
|
throw new ConcurrentModificationException(); |
614 |
|
lastRet = cursor; |
615 |
|
cursor = (cursor + 1) & (elements.length - 1); |
619 |
|
public void remove() { |
620 |
|
if (lastRet < 0) |
621 |
|
throw new IllegalStateException(); |
622 |
< |
if (delete(lastRet)) |
623 |
< |
cursor--; |
622 |
> |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
623 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
624 |
> |
fence = tail; |
625 |
> |
} |
626 |
> |
lastRet = -1; |
627 |
> |
} |
628 |
> |
} |
629 |
> |
|
630 |
> |
private class DescendingIterator implements Iterator<E> { |
631 |
> |
/* |
632 |
> |
* This class is nearly a mirror-image of DeqIterator, using |
633 |
> |
* tail instead of head for initial cursor, and head instead of |
634 |
> |
* tail for fence. |
635 |
> |
*/ |
636 |
> |
private int cursor = tail; |
637 |
> |
private int fence = head; |
638 |
> |
private int lastRet = -1; |
639 |
> |
|
640 |
> |
public boolean hasNext() { |
641 |
> |
return cursor != fence; |
642 |
> |
} |
643 |
> |
|
644 |
> |
public E next() { |
645 |
> |
if (cursor == fence) |
646 |
> |
throw new NoSuchElementException(); |
647 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
648 |
> |
@SuppressWarnings("unchecked") |
649 |
> |
E result = (E) elements[cursor]; |
650 |
> |
if (head != fence || result == null) |
651 |
> |
throw new ConcurrentModificationException(); |
652 |
> |
lastRet = cursor; |
653 |
> |
return result; |
654 |
> |
} |
655 |
> |
|
656 |
> |
public void remove() { |
657 |
> |
if (lastRet < 0) |
658 |
> |
throw new IllegalStateException(); |
659 |
> |
if (!delete(lastRet)) { |
660 |
> |
cursor = (cursor + 1) & (elements.length - 1); |
661 |
> |
fence = head; |
662 |
> |
} |
663 |
|
lastRet = -1; |
618 |
– |
fence = tail; |
664 |
|
} |
665 |
|
} |
666 |
|
|
667 |
|
/** |
668 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
669 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
670 |
< |
* deque contains at least one element <tt>e</tt> such that |
626 |
< |
* <tt>e.equals(o)</tt>. |
668 |
> |
* Returns {@code true} if this deque contains the specified element. |
669 |
> |
* More formally, returns {@code true} if and only if this deque contains |
670 |
> |
* at least one element {@code e} such that {@code o.equals(e)}. |
671 |
|
* |
672 |
|
* @param o object to be checked for containment in this deque |
673 |
< |
* @return <tt>true</tt> if this deque contains the specified element |
673 |
> |
* @return {@code true} if this deque contains the specified element |
674 |
|
*/ |
675 |
|
public boolean contains(Object o) { |
676 |
|
if (o == null) |
677 |
|
return false; |
678 |
|
int mask = elements.length - 1; |
679 |
|
int i = head; |
680 |
< |
E x; |
680 |
> |
Object x; |
681 |
|
while ( (x = elements[i]) != null) { |
682 |
|
if (o.equals(x)) |
683 |
|
return true; |
688 |
|
|
689 |
|
/** |
690 |
|
* Removes a single instance of the specified element from this deque. |
691 |
< |
* This method is equivalent to {@link #removeFirstOccurrence}. |
691 |
> |
* If the deque does not contain the element, it is unchanged. |
692 |
> |
* More formally, removes the first element {@code e} such that |
693 |
> |
* {@code o.equals(e)} (if such an element exists). |
694 |
> |
* Returns {@code true} if this deque contained the specified element |
695 |
> |
* (or equivalently, if this deque changed as a result of the call). |
696 |
> |
* |
697 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
698 |
|
* |
699 |
< |
* @param e element to be removed from this deque, if present |
700 |
< |
* @return <tt>true</tt> if this deque contained the specified element |
699 |
> |
* @param o element to be removed from this deque, if present |
700 |
> |
* @return {@code true} if this deque contained the specified element |
701 |
|
*/ |
702 |
< |
public boolean remove(Object e) { |
703 |
< |
return removeFirstOccurrence(e); |
702 |
> |
public boolean remove(Object o) { |
703 |
> |
return removeFirstOccurrence(o); |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
|
* Removes all of the elements from this deque. |
708 |
+ |
* The deque will be empty after this call returns. |
709 |
|
*/ |
710 |
|
public void clear() { |
711 |
|
int h = head; |
717 |
|
do { |
718 |
|
elements[i] = null; |
719 |
|
i = (i + 1) & mask; |
720 |
< |
} while(i != t); |
720 |
> |
} while (i != t); |
721 |
|
} |
722 |
|
} |
723 |
|
|
724 |
|
/** |
725 |
< |
* Returns an array containing all of the elements in this list |
726 |
< |
* in the correct order. |
725 |
> |
* Returns an array containing all of the elements in this deque |
726 |
> |
* in proper sequence (from first to last element). |
727 |
|
* |
728 |
< |
* @return an array containing all of the elements in this list |
729 |
< |
* in the correct order |
728 |
> |
* <p>The returned array will be "safe" in that no references to it are |
729 |
> |
* maintained by this deque. (In other words, this method must allocate |
730 |
> |
* a new array). The caller is thus free to modify the returned array. |
731 |
> |
* |
732 |
> |
* <p>This method acts as bridge between array-based and collection-based |
733 |
> |
* APIs. |
734 |
> |
* |
735 |
> |
* @return an array containing all of the elements in this deque |
736 |
|
*/ |
737 |
|
public Object[] toArray() { |
738 |
< |
return copyElements(new Object[size()]); |
738 |
> |
return copyElements(new Object[size()]); |
739 |
|
} |
740 |
|
|
741 |
|
/** |
742 |
< |
* Returns an array containing all of the elements in this deque in the |
743 |
< |
* correct order; the runtime type of the returned array is that of the |
744 |
< |
* specified array. If the deque fits in the specified array, it is |
745 |
< |
* returned therein. Otherwise, a new array is allocated with the runtime |
746 |
< |
* type of the specified array and the size of this deque. |
742 |
> |
* Returns an array containing all of the elements in this deque in |
743 |
> |
* proper sequence (from first to last element); the runtime type of the |
744 |
> |
* returned array is that of the specified array. If the deque fits in |
745 |
> |
* the specified array, it is returned therein. Otherwise, a new array |
746 |
> |
* is allocated with the runtime type of the specified array and the |
747 |
> |
* size of this deque. |
748 |
> |
* |
749 |
> |
* <p>If this deque fits in the specified array with room to spare |
750 |
> |
* (i.e., the array has more elements than this deque), the element in |
751 |
> |
* the array immediately following the end of the deque is set to |
752 |
> |
* {@code null}. |
753 |
> |
* |
754 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
755 |
> |
* array-based and collection-based APIs. Further, this method allows |
756 |
> |
* precise control over the runtime type of the output array, and may, |
757 |
> |
* under certain circumstances, be used to save allocation costs. |
758 |
> |
* |
759 |
> |
* <p>Suppose {@code x} is a deque known to contain only strings. |
760 |
> |
* The following code can be used to dump the deque into a newly |
761 |
> |
* allocated array of {@code String}: |
762 |
|
* |
763 |
< |
* <p>If the deque fits in the specified array with room to spare (i.e., |
764 |
< |
* the array has more elements than the deque), the element in the array |
765 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
763 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
764 |
> |
* |
765 |
> |
* Note that {@code toArray(new Object[0])} is identical in function to |
766 |
> |
* {@code toArray()}. |
767 |
|
* |
768 |
|
* @param a the array into which the elements of the deque are to |
769 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
770 |
< |
* same runtime type is allocated for this purpose |
771 |
< |
* @return an array containing the elements of the deque |
772 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
773 |
< |
* of the runtime type of every element in this deque |
769 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
770 |
> |
* same runtime type is allocated for this purpose |
771 |
> |
* @return an array containing all of the elements in this deque |
772 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
773 |
> |
* is not a supertype of the runtime type of every element in |
774 |
> |
* this deque |
775 |
> |
* @throws NullPointerException if the specified array is null |
776 |
|
*/ |
777 |
+ |
@SuppressWarnings("unchecked") |
778 |
|
public <T> T[] toArray(T[] a) { |
779 |
|
int size = size(); |
780 |
|
if (a.length < size) |
781 |
|
a = (T[])java.lang.reflect.Array.newInstance( |
782 |
|
a.getClass().getComponentType(), size); |
783 |
< |
copyElements(a); |
783 |
> |
copyElements(a); |
784 |
|
if (a.length > size) |
785 |
|
a[size] = null; |
786 |
|
return a; |
794 |
|
* @return a copy of this deque |
795 |
|
*/ |
796 |
|
public ArrayDeque<E> clone() { |
797 |
< |
try { |
797 |
> |
try { |
798 |
> |
@SuppressWarnings("unchecked") |
799 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
800 |
< |
// These two lines are currently faster than cloning the array: |
724 |
< |
result.elements = (E[]) new Object[elements.length]; |
725 |
< |
System.arraycopy(elements, 0, result.elements, 0, elements.length); |
800 |
> |
result.elements = Arrays.copyOf(elements, elements.length); |
801 |
|
return result; |
802 |
< |
|
728 |
< |
} catch (CloneNotSupportedException e) { |
802 |
> |
} catch (CloneNotSupportedException e) { |
803 |
|
throw new AssertionError(); |
804 |
|
} |
805 |
|
} |
806 |
|
|
733 |
– |
/** |
734 |
– |
* Appease the serialization gods. |
735 |
– |
*/ |
807 |
|
private static final long serialVersionUID = 2340985798034038923L; |
808 |
|
|
809 |
|
/** |
810 |
< |
* Serialize this deque. |
810 |
> |
* Saves this deque to a stream (that is, serializes it). |
811 |
|
* |
812 |
< |
* @serialData The current size (<tt>int</tt>) of the deque, |
812 |
> |
* @serialData The current size ({@code int}) of the deque, |
813 |
|
* followed by all of its elements (each an object reference) in |
814 |
|
* first-to-last order. |
815 |
|
*/ |
816 |
< |
private void writeObject(ObjectOutputStream s) throws IOException { |
816 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
817 |
> |
throws java.io.IOException { |
818 |
|
s.defaultWriteObject(); |
819 |
|
|
820 |
|
// Write out size |
821 |
< |
int size = size(); |
750 |
< |
s.writeInt(size); |
821 |
> |
s.writeInt(size()); |
822 |
|
|
823 |
|
// Write out elements in order. |
753 |
– |
int i = head; |
824 |
|
int mask = elements.length - 1; |
825 |
< |
for (int j = 0; j < size; j++) { |
825 |
> |
for (int i = head; i != tail; i = (i + 1) & mask) |
826 |
|
s.writeObject(elements[i]); |
757 |
– |
i = (i + 1) & mask; |
758 |
– |
} |
827 |
|
} |
828 |
|
|
829 |
|
/** |
830 |
< |
* Deserialize this deque. |
830 |
> |
* Reconstitutes this deque from a stream (that is, deserializes it). |
831 |
|
*/ |
832 |
< |
private void readObject(ObjectInputStream s) |
833 |
< |
throws IOException, ClassNotFoundException { |
832 |
> |
private void readObject(java.io.ObjectInputStream s) |
833 |
> |
throws java.io.IOException, ClassNotFoundException { |
834 |
|
s.defaultReadObject(); |
835 |
|
|
836 |
|
// Read in size and allocate array |
841 |
|
|
842 |
|
// Read in all elements in the proper order. |
843 |
|
for (int i = 0; i < size; i++) |
844 |
< |
elements[i] = (E)s.readObject(); |
777 |
< |
|
844 |
> |
elements[i] = s.readObject(); |
845 |
|
} |
846 |
|
} |