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 |
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 |
20 |
> |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
21 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
22 |
|
* time. |
23 |
|
* |
24 |
|
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
25 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
26 |
< |
* is created, in any way except through the iterator's own remove method, the |
27 |
< |
* iterator will generally throw a {@link ConcurrentModificationException}. |
28 |
< |
* Thus, in the face of concurrent modification, the iterator fails quickly |
29 |
< |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
30 |
< |
* an undetermined time in the future. |
26 |
> |
* is created, in any way except through the iterator's own <tt>remove</tt> |
27 |
> |
* method, the iterator will generally throw a {@link |
28 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
29 |
> |
* modification, the iterator fails quickly and cleanly, rather than risking |
30 |
> |
* arbitrary, non-deterministic behavior at an undetermined time in the |
31 |
> |
* future. |
32 |
|
* |
33 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
34 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
35 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
36 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
36 |
> |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
37 |
|
* Therefore, it would be wrong to write a program that depended on this |
38 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
39 |
|
* should be used only to detect bugs.</i> |
40 |
|
* |
41 |
|
* <p>This class and its iterator implement all of the |
42 |
< |
* optional methods of the {@link Collection} and {@link |
43 |
< |
* Iterator} interfaces. This class is a member of the <a |
44 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
45 |
< |
* Framework</a>. |
42 |
> |
* <em>optional</em> methods of the {@link Collection} and {@link |
43 |
> |
* Iterator} interfaces. |
44 |
> |
* |
45 |
> |
* <p>This class is a member of the |
46 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
47 |
> |
* Java Collections Framework</a>. |
48 |
|
* |
49 |
|
* @author Josh Bloch and Doug Lea |
50 |
|
* @since 1.6 |
51 |
|
* @param <E> the type of elements held in this collection |
52 |
|
*/ |
53 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
54 |
< |
implements Deque<E>, Cloneable, Serializable |
54 |
> |
implements Deque<E>, Cloneable, java.io.Serializable |
55 |
|
{ |
56 |
|
/** |
57 |
< |
* The array in which the elements of in the deque are stored. |
57 |
> |
* The array in which the elements of the deque are stored. |
58 |
|
* The capacity of the deque is the length of this array, which is |
59 |
|
* always a power of two. The array is never allowed to become |
60 |
|
* full, except transiently within an addX method where it is |
89 |
|
/** |
90 |
|
* Allocate empty array to hold the given number of elements. |
91 |
|
* |
92 |
< |
* @param numElements the number of elements to hold. |
92 |
> |
* @param numElements the number of elements to hold |
93 |
|
*/ |
94 |
< |
private void allocateElements(int numElements) { |
94 |
> |
private void allocateElements(int numElements) { |
95 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
96 |
|
// Find the best power of two to hold elements. |
97 |
|
// Tests "<=" because arrays aren't kept full. |
115 |
|
* when head and tail have wrapped around to become equal. |
116 |
|
*/ |
117 |
|
private void doubleCapacity() { |
118 |
< |
assert head == tail; |
118 |
> |
assert head == tail; |
119 |
|
int p = head; |
120 |
|
int n = elements.length; |
121 |
|
int r = n - p; // number of elements to the right of p |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* Copy the elements from our element array into the specified array, |
134 |
> |
* Copies the elements from our element array into the specified array, |
135 |
|
* in order (from first to last element in the deque). It is assumed |
136 |
|
* that the array is large enough to hold all elements in the deque. |
137 |
|
* |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
< |
* Constructs an empty array deque with the an initial capacity |
152 |
> |
* Constructs an empty array deque with an initial capacity |
153 |
|
* sufficient to hold 16 elements. |
154 |
|
*/ |
155 |
|
public ArrayDeque() { |
186 |
|
// terms of these. |
187 |
|
|
188 |
|
/** |
189 |
< |
* Inserts the specified element to the front this deque. |
189 |
> |
* Inserts the specified element at the front of this deque. |
190 |
|
* |
191 |
< |
* @param e the element to insert |
192 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
191 |
> |
* @param e the element to add |
192 |
> |
* @throws NullPointerException if the specified element is null |
193 |
|
*/ |
194 |
|
public void addFirst(E e) { |
195 |
|
if (e == null) |
196 |
|
throw new NullPointerException(); |
197 |
|
elements[head = (head - 1) & (elements.length - 1)] = e; |
198 |
< |
if (head == tail) |
198 |
> |
if (head == tail) |
199 |
|
doubleCapacity(); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
< |
* Inserts the specified element to the end this deque. |
204 |
< |
* This method is equivalent to {@link Collection#add} and |
205 |
< |
* {@link #push}. |
203 |
> |
* Inserts the specified element at the end of this deque. |
204 |
> |
* |
205 |
> |
* <p>This method is equivalent to {@link #add}. |
206 |
|
* |
207 |
< |
* @param e the element to insert |
208 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
207 |
> |
* @param e the element to add |
208 |
> |
* @throws NullPointerException if the specified element is null |
209 |
|
*/ |
210 |
|
public void addLast(E e) { |
211 |
|
if (e == null) |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* 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. |
219 |
> |
* Inserts the specified element at the front of this deque. |
220 |
|
* |
221 |
< |
* @return the last element of this deque, or <tt>null</tt> if |
222 |
< |
* this deque is empty |
223 |
< |
*/ |
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 |
221 |
> |
* @param e the element to add |
222 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
223 |
> |
* @throws NullPointerException if the specified element is null |
224 |
|
*/ |
225 |
|
public boolean offerFirst(E e) { |
226 |
|
addFirst(e); |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* Inserts the specified element to the end this deque. |
231 |
> |
* Inserts the specified element at the end of this deque. |
232 |
|
* |
233 |
< |
* @param e the element to insert |
234 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
235 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
233 |
> |
* @param e the element to add |
234 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerLast}) |
235 |
> |
* @throws NullPointerException if the specified element is null |
236 |
|
*/ |
237 |
|
public boolean offerLast(E e) { |
238 |
|
addLast(e); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
< |
* 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 |
243 |
> |
* @throws NoSuchElementException {@inheritDoc} |
244 |
|
*/ |
245 |
|
public E removeFirst() { |
246 |
|
E x = pollFirst(); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* 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 |
253 |
> |
* @throws NoSuchElementException {@inheritDoc} |
254 |
|
*/ |
255 |
|
public E removeLast() { |
256 |
|
E x = pollLast(); |
259 |
|
return x; |
260 |
|
} |
261 |
|
|
262 |
< |
/** |
263 |
< |
* Retrieves, but does not remove, the first element of this deque, |
264 |
< |
* returning <tt>null</tt> if this deque is empty. |
265 |
< |
* |
266 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
267 |
< |
* this deque is empty |
268 |
< |
*/ |
269 |
< |
public E peekFirst() { |
312 |
< |
return elements[head]; // elements[head] is null if deque empty |
262 |
> |
public E pollFirst() { |
263 |
> |
int h = head; |
264 |
> |
E result = elements[h]; // Element is null if deque empty |
265 |
> |
if (result == null) |
266 |
> |
return null; |
267 |
> |
elements[h] = null; // Must null out slot |
268 |
> |
head = (h + 1) & (elements.length - 1); |
269 |
> |
return result; |
270 |
|
} |
271 |
|
|
272 |
< |
/** |
273 |
< |
* Retrieves, but does not remove, the last element of this deque, |
274 |
< |
* returning <tt>null</tt> if this deque is empty. |
275 |
< |
* |
276 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
277 |
< |
* is empty |
278 |
< |
*/ |
279 |
< |
public E peekLast() { |
323 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
272 |
> |
public E pollLast() { |
273 |
> |
int t = (tail - 1) & (elements.length - 1); |
274 |
> |
E result = elements[t]; |
275 |
> |
if (result == null) |
276 |
> |
return null; |
277 |
> |
elements[t] = null; |
278 |
> |
tail = t; |
279 |
> |
return result; |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
< |
* 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 |
283 |
> |
* @throws NoSuchElementException {@inheritDoc} |
284 |
|
*/ |
285 |
|
public E getFirst() { |
286 |
|
E x = elements[head]; |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
< |
* 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 |
293 |
> |
* @throws NoSuchElementException {@inheritDoc} |
294 |
|
*/ |
295 |
|
public E getLast() { |
296 |
|
E x = elements[(tail - 1) & (elements.length - 1)]; |
299 |
|
return x; |
300 |
|
} |
301 |
|
|
302 |
+ |
public E peekFirst() { |
303 |
+ |
return elements[head]; // elements[head] is null if deque empty |
304 |
+ |
} |
305 |
+ |
|
306 |
+ |
public E peekLast() { |
307 |
+ |
return elements[(tail - 1) & (elements.length - 1)]; |
308 |
+ |
} |
309 |
+ |
|
310 |
|
/** |
311 |
|
* Removes the first occurrence of the specified element in this |
312 |
< |
* deque (when traversing the deque from head to tail). If the deque |
313 |
< |
* does not contain the element, it is unchanged. |
312 |
> |
* deque (when traversing the deque from head to tail). |
313 |
> |
* If the deque does not contain the element, it is unchanged. |
314 |
> |
* More formally, removes the first element <tt>e</tt> such that |
315 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
316 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
317 |
> |
* (or equivalently, if this deque changed as a result of the call). |
318 |
|
* |
319 |
< |
* @param e element to be removed from this deque, if present |
319 |
> |
* @param o element to be removed from this deque, if present |
320 |
|
* @return <tt>true</tt> if the deque contained the specified element |
321 |
|
*/ |
322 |
< |
public boolean removeFirstOccurrence(Object e) { |
323 |
< |
if (e == null) |
322 |
> |
public boolean removeFirstOccurrence(Object o) { |
323 |
> |
if (o == null) |
324 |
|
return false; |
325 |
|
int mask = elements.length - 1; |
326 |
|
int i = head; |
327 |
|
E x; |
328 |
|
while ( (x = elements[i]) != null) { |
329 |
< |
if (e.equals(x)) { |
329 |
> |
if (o.equals(x)) { |
330 |
|
delete(i); |
331 |
|
return true; |
332 |
|
} |
337 |
|
|
338 |
|
/** |
339 |
|
* Removes the last occurrence of the specified element in this |
340 |
< |
* deque (when traversing the deque from head to tail). If the deque |
341 |
< |
* does not contain the element, it is unchanged. |
340 |
> |
* deque (when traversing the deque from head to tail). |
341 |
> |
* If the deque does not contain the element, it is unchanged. |
342 |
> |
* More formally, removes the last element <tt>e</tt> such that |
343 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
344 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
345 |
> |
* (or equivalently, if this deque changed as a result of the call). |
346 |
|
* |
347 |
< |
* @param e element to be removed from this deque, if present |
347 |
> |
* @param o element to be removed from this deque, if present |
348 |
|
* @return <tt>true</tt> if the deque contained the specified element |
349 |
|
*/ |
350 |
< |
public boolean removeLastOccurrence(Object e) { |
351 |
< |
if (e == null) |
350 |
> |
public boolean removeLastOccurrence(Object o) { |
351 |
> |
if (o == null) |
352 |
|
return false; |
353 |
|
int mask = elements.length - 1; |
354 |
|
int i = (tail - 1) & mask; |
355 |
|
E x; |
356 |
|
while ( (x = elements[i]) != null) { |
357 |
< |
if (e.equals(x)) { |
357 |
> |
if (o.equals(x)) { |
358 |
|
delete(i); |
359 |
|
return true; |
360 |
|
} |
366 |
|
// *** Queue methods *** |
367 |
|
|
368 |
|
/** |
369 |
< |
* 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. |
369 |
> |
* Inserts the specified element at the end of this deque. |
370 |
|
* |
371 |
|
* <p>This method is equivalent to {@link #addLast}. |
372 |
|
* |
373 |
< |
* @param e the element to insert |
374 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
375 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
373 |
> |
* @param e the element to add |
374 |
> |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
375 |
> |
* @throws NullPointerException if the specified element is null |
376 |
|
*/ |
377 |
|
public boolean add(E e) { |
378 |
|
addLast(e); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
< |
* 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. |
383 |
> |
* Inserts the specified element at the end of this deque. |
384 |
|
* |
385 |
< |
* <p>This method is equivalent to {@link #pollFirst}. |
385 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
386 |
|
* |
387 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
388 |
< |
* this deque is empty |
387 |
> |
* @param e the element to add |
388 |
> |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
389 |
> |
* @throws NullPointerException if the specified element is null |
390 |
|
*/ |
391 |
< |
public E poll() { |
392 |
< |
return pollFirst(); |
391 |
> |
public boolean offer(E e) { |
392 |
> |
return offerLast(e); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* Retrieves and removes the head of the queue represented by this deque. |
397 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
397 |
> |
* |
398 |
> |
* This method differs from {@link #poll poll} only in that it throws an |
399 |
|
* exception if this deque is empty. |
400 |
|
* |
401 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
402 |
|
* |
403 |
|
* @return the head of the queue represented by this deque |
404 |
< |
* @throws NoSuchElementException if this deque is empty |
404 |
> |
* @throws NoSuchElementException {@inheritDoc} |
405 |
|
*/ |
406 |
|
public E remove() { |
407 |
|
return removeFirst(); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
412 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
411 |
> |
* Retrieves and removes the head of the queue represented by this deque |
412 |
> |
* (in other words, the first element of this deque), or returns |
413 |
> |
* <tt>null</tt> if this deque is empty. |
414 |
|
* |
415 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
415 |
> |
* <p>This method is equivalent to {@link #pollFirst}. |
416 |
|
* |
417 |
|
* @return the head of the queue represented by this deque, or |
418 |
< |
* <tt>null</tt> if this deque is empty |
418 |
> |
* <tt>null</tt> if this deque is empty |
419 |
|
*/ |
420 |
< |
public E peek() { |
421 |
< |
return peekFirst(); |
420 |
> |
public E poll() { |
421 |
> |
return pollFirst(); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
|
* Retrieves, but does not remove, the head of the queue represented by |
426 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
426 |
> |
* this deque. This method differs from {@link #peek peek} only in |
427 |
|
* that it throws an exception if this deque is empty. |
428 |
|
* |
429 |
< |
* <p>This method is equivalent to {@link #getFirst} |
429 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
430 |
|
* |
431 |
|
* @return the head of the queue represented by this deque |
432 |
< |
* @throws NoSuchElementException if this deque is empty |
432 |
> |
* @throws NoSuchElementException {@inheritDoc} |
433 |
|
*/ |
434 |
|
public E element() { |
435 |
|
return getFirst(); |
436 |
|
} |
437 |
|
|
438 |
+ |
/** |
439 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
440 |
+ |
* this deque, or returns <tt>null</tt> if this deque is empty. |
441 |
+ |
* |
442 |
+ |
* <p>This method is equivalent to {@link #peekFirst}. |
443 |
+ |
* |
444 |
+ |
* @return the head of the queue represented by this deque, or |
445 |
+ |
* <tt>null</tt> if this deque is empty |
446 |
+ |
*/ |
447 |
+ |
public E peek() { |
448 |
+ |
return peekFirst(); |
449 |
+ |
} |
450 |
+ |
|
451 |
|
// *** Stack methods *** |
452 |
|
|
453 |
|
/** |
454 |
|
* Pushes an element onto the stack represented by this deque. In other |
455 |
< |
* words, inserts the element to the front this deque. |
455 |
> |
* words, inserts the element at the front of this deque. |
456 |
|
* |
457 |
|
* <p>This method is equivalent to {@link #addFirst}. |
458 |
|
* |
459 |
|
* @param e the element to push |
460 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
460 |
> |
* @throws NullPointerException if the specified element is null |
461 |
|
*/ |
462 |
|
public void push(E e) { |
463 |
|
addFirst(e); |
470 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
471 |
|
* |
472 |
|
* @return the element at the front of this deque (which is the top |
473 |
< |
* of the stack represented by this deque) |
474 |
< |
* @throws NoSuchElementException if this deque is empty |
473 |
> |
* of the stack represented by this deque) |
474 |
> |
* @throws NoSuchElementException {@inheritDoc} |
475 |
|
*/ |
476 |
|
public E pop() { |
477 |
|
return removeFirst(); |
478 |
|
} |
479 |
|
|
480 |
+ |
private void checkInvariants() { |
481 |
+ |
assert elements[tail] == null; |
482 |
+ |
assert head == tail ? elements[head] == null : |
483 |
+ |
(elements[head] != null && |
484 |
+ |
elements[(tail - 1) & (elements.length - 1)] != null); |
485 |
+ |
assert elements[(head - 1) & (elements.length - 1)] == null; |
486 |
+ |
} |
487 |
+ |
|
488 |
|
/** |
489 |
< |
* Remove the element at the specified position in the elements array, |
490 |
< |
* adjusting head, tail, and size as necessary. This can result in |
491 |
< |
* motion of elements backwards or forwards in the array. |
492 |
< |
* |
493 |
< |
* <p>This method is called delete rather than remove to emphasize |
494 |
< |
* that its semantics differ from those of List.remove(int). |
495 |
< |
* |
489 |
> |
* Removes the element at the specified position in the elements array, |
490 |
> |
* adjusting head and tail as necessary. This can result in motion of |
491 |
> |
* elements backwards or forwards in the array. |
492 |
> |
* |
493 |
> |
* <p>This method is called delete rather than remove to emphasize |
494 |
> |
* that its semantics differ from those of {@link List#remove(int)}. |
495 |
> |
* |
496 |
|
* @return true if elements moved backwards |
497 |
|
*/ |
498 |
|
private boolean delete(int i) { |
499 |
< |
// Case 1: Deque doesn't wrap |
500 |
< |
// Case 2: Deque does wrap and removed element is in the head portion |
501 |
< |
if ((head < tail || tail == 0) || i >= head) { |
502 |
< |
System.arraycopy(elements, head, elements, head + 1, i - head); |
503 |
< |
elements[head] = null; |
504 |
< |
head = (head + 1) & (elements.length - 1); |
499 |
> |
checkInvariants(); |
500 |
> |
final E[] elements = this.elements; |
501 |
> |
final int mask = elements.length - 1; |
502 |
> |
final int h = head; |
503 |
> |
final int t = tail; |
504 |
> |
final int front = (i - h) & mask; |
505 |
> |
final int back = (t - i) & mask; |
506 |
> |
|
507 |
> |
// Invariant: head <= i < tail mod circularity |
508 |
> |
if (front >= ((t - h) & mask)) |
509 |
> |
throw new ConcurrentModificationException(); |
510 |
> |
|
511 |
> |
// Optimize for least element motion |
512 |
> |
if (front < back) { |
513 |
> |
if (h <= i) { |
514 |
> |
System.arraycopy(elements, h, elements, h + 1, front); |
515 |
> |
} else { // Wrap around |
516 |
> |
System.arraycopy(elements, 0, elements, 1, i); |
517 |
> |
elements[0] = elements[mask]; |
518 |
> |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
519 |
> |
} |
520 |
> |
elements[h] = null; |
521 |
> |
head = (h + 1) & mask; |
522 |
|
return false; |
523 |
+ |
} else { |
524 |
+ |
if (i < t) { // Copy the null tail as well |
525 |
+ |
System.arraycopy(elements, i + 1, elements, i, back); |
526 |
+ |
tail = t - 1; |
527 |
+ |
} else { // Wrap around |
528 |
+ |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
529 |
+ |
elements[mask] = elements[0]; |
530 |
+ |
System.arraycopy(elements, 1, elements, 0, t); |
531 |
+ |
tail = (t - 1) & mask; |
532 |
+ |
} |
533 |
+ |
return true; |
534 |
|
} |
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; |
535 |
|
} |
536 |
|
|
537 |
|
// *** Collection Methods *** |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
549 |
> |
* Returns <tt>true</tt> if this deque contains no elements. |
550 |
|
* |
551 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
551 |
> |
* @return <tt>true</tt> if this deque contains no elements |
552 |
|
*/ |
553 |
|
public boolean isEmpty() { |
554 |
|
return head == tail; |
559 |
|
* will be ordered from first (head) to last (tail). This is the same |
560 |
|
* order that elements would be dequeued (via successive calls to |
561 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
562 |
< |
* |
563 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
562 |
> |
* |
563 |
> |
* @return an iterator over the elements in this deque |
564 |
|
*/ |
565 |
|
public Iterator<E> iterator() { |
566 |
|
return new DeqIterator(); |
567 |
|
} |
568 |
|
|
569 |
+ |
public Iterator<E> descendingIterator() { |
570 |
+ |
return new DescendingIterator(); |
571 |
+ |
} |
572 |
+ |
|
573 |
|
private class DeqIterator implements Iterator<E> { |
574 |
|
/** |
575 |
|
* Index of element to be returned by subsequent call to next. |
593 |
|
} |
594 |
|
|
595 |
|
public E next() { |
600 |
– |
E result; |
596 |
|
if (cursor == fence) |
597 |
|
throw new NoSuchElementException(); |
598 |
+ |
E result = elements[cursor]; |
599 |
|
// This check doesn't catch all possible comodifications, |
600 |
|
// but does catch the ones that corrupt traversal |
601 |
< |
if (tail != fence || (result = elements[cursor]) == null) |
601 |
> |
if (tail != fence || result == null) |
602 |
|
throw new ConcurrentModificationException(); |
603 |
|
lastRet = cursor; |
604 |
|
cursor = (cursor + 1) & (elements.length - 1); |
608 |
|
public void remove() { |
609 |
|
if (lastRet < 0) |
610 |
|
throw new IllegalStateException(); |
611 |
< |
if (delete(lastRet)) |
612 |
< |
cursor--; |
611 |
> |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
612 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
613 |
> |
fence = tail; |
614 |
> |
} |
615 |
> |
lastRet = -1; |
616 |
> |
} |
617 |
> |
} |
618 |
> |
|
619 |
> |
private class DescendingIterator implements Iterator<E> { |
620 |
> |
/* |
621 |
> |
* This class is nearly a mirror-image of DeqIterator, using |
622 |
> |
* tail instead of head for initial cursor, and head instead of |
623 |
> |
* tail for fence. |
624 |
> |
*/ |
625 |
> |
private int cursor = tail; |
626 |
> |
private int fence = head; |
627 |
> |
private int lastRet = -1; |
628 |
> |
|
629 |
> |
public boolean hasNext() { |
630 |
> |
return cursor != fence; |
631 |
> |
} |
632 |
> |
|
633 |
> |
public E next() { |
634 |
> |
if (cursor == fence) |
635 |
> |
throw new NoSuchElementException(); |
636 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
637 |
> |
E result = elements[cursor]; |
638 |
> |
if (head != fence || result == null) |
639 |
> |
throw new ConcurrentModificationException(); |
640 |
> |
lastRet = cursor; |
641 |
> |
return result; |
642 |
> |
} |
643 |
> |
|
644 |
> |
public void remove() { |
645 |
> |
if (lastRet < 0) |
646 |
> |
throw new IllegalStateException(); |
647 |
> |
if (!delete(lastRet)) { |
648 |
> |
cursor = (cursor + 1) & (elements.length - 1); |
649 |
> |
fence = head; |
650 |
> |
} |
651 |
|
lastRet = -1; |
618 |
– |
fence = tail; |
652 |
|
} |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
657 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
658 |
< |
* deque contains at least one element <tt>e</tt> such that |
626 |
< |
* <tt>e.equals(o)</tt>. |
656 |
> |
* Returns <tt>true</tt> if this deque contains the specified element. |
657 |
> |
* More formally, returns <tt>true</tt> if and only if this deque contains |
658 |
> |
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
659 |
|
* |
660 |
|
* @param o object to be checked for containment in this deque |
661 |
|
* @return <tt>true</tt> if this deque contains the specified element |
676 |
|
|
677 |
|
/** |
678 |
|
* Removes a single instance of the specified element from this deque. |
679 |
< |
* This method is equivalent to {@link #removeFirstOccurrence}. |
679 |
> |
* If the deque does not contain the element, it is unchanged. |
680 |
> |
* More formally, removes the first element <tt>e</tt> such that |
681 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
682 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
683 |
> |
* (or equivalently, if this deque changed as a result of the call). |
684 |
> |
* |
685 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
686 |
|
* |
687 |
< |
* @param e element to be removed from this deque, if present |
687 |
> |
* @param o element to be removed from this deque, if present |
688 |
|
* @return <tt>true</tt> if this deque contained the specified element |
689 |
|
*/ |
690 |
< |
public boolean remove(Object e) { |
691 |
< |
return removeFirstOccurrence(e); |
690 |
> |
public boolean remove(Object o) { |
691 |
> |
return removeFirstOccurrence(o); |
692 |
|
} |
693 |
|
|
694 |
|
/** |
695 |
|
* Removes all of the elements from this deque. |
696 |
+ |
* The deque will be empty after this call returns. |
697 |
|
*/ |
698 |
|
public void clear() { |
699 |
|
int h = head; |
705 |
|
do { |
706 |
|
elements[i] = null; |
707 |
|
i = (i + 1) & mask; |
708 |
< |
} while(i != t); |
708 |
> |
} while (i != t); |
709 |
|
} |
710 |
|
} |
711 |
|
|
712 |
|
/** |
713 |
< |
* Returns an array containing all of the elements in this list |
714 |
< |
* in the correct order. |
713 |
> |
* Returns an array containing all of the elements in this deque |
714 |
> |
* in proper sequence (from first to last element). |
715 |
> |
* |
716 |
> |
* <p>The returned array will be "safe" in that no references to it are |
717 |
> |
* maintained by this deque. (In other words, this method must allocate |
718 |
> |
* a new array). The caller is thus free to modify the returned array. |
719 |
> |
* |
720 |
> |
* <p>This method acts as bridge between array-based and collection-based |
721 |
> |
* APIs. |
722 |
|
* |
723 |
< |
* @return an array containing all of the elements in this list |
678 |
< |
* in the correct order |
723 |
> |
* @return an array containing all of the elements in this deque |
724 |
|
*/ |
725 |
|
public Object[] toArray() { |
726 |
< |
return copyElements(new Object[size()]); |
726 |
> |
return copyElements(new Object[size()]); |
727 |
|
} |
728 |
|
|
729 |
|
/** |
730 |
< |
* Returns an array containing all of the elements in this deque in the |
731 |
< |
* correct order; the runtime type of the returned array is that of the |
732 |
< |
* specified array. If the deque fits in the specified array, it is |
733 |
< |
* returned therein. Otherwise, a new array is allocated with the runtime |
734 |
< |
* type of the specified array and the size of this deque. |
730 |
> |
* Returns an array containing all of the elements in this deque in |
731 |
> |
* proper sequence (from first to last element); the runtime type of the |
732 |
> |
* returned array is that of the specified array. If the deque fits in |
733 |
> |
* the specified array, it is returned therein. Otherwise, a new array |
734 |
> |
* is allocated with the runtime type of the specified array and the |
735 |
> |
* size of this deque. |
736 |
|
* |
737 |
< |
* <p>If the deque fits in the specified array with room to spare (i.e., |
738 |
< |
* the array has more elements than the deque), the element in the array |
739 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
737 |
> |
* <p>If this deque fits in the specified array with room to spare |
738 |
> |
* (i.e., the array has more elements than this deque), the element in |
739 |
> |
* the array immediately following the end of the deque is set to |
740 |
> |
* <tt>null</tt>. |
741 |
> |
* |
742 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
743 |
> |
* array-based and collection-based APIs. Further, this method allows |
744 |
> |
* precise control over the runtime type of the output array, and may, |
745 |
> |
* under certain circumstances, be used to save allocation costs. |
746 |
> |
* |
747 |
> |
* <p>Suppose <tt>x</tt> is a deque known to contain only strings. |
748 |
> |
* The following code can be used to dump the deque into a newly |
749 |
> |
* allocated array of <tt>String</tt>: |
750 |
> |
* |
751 |
> |
* <pre> |
752 |
> |
* String[] y = x.toArray(new String[0]);</pre> |
753 |
> |
* |
754 |
> |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
755 |
> |
* <tt>toArray()</tt>. |
756 |
|
* |
757 |
|
* @param a the array into which the elements of the deque are to |
758 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
759 |
< |
* same runtime type is allocated for this purpose |
760 |
< |
* @return an array containing the elements of the deque |
761 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
762 |
< |
* of the runtime type of every element in this deque |
758 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
759 |
> |
* same runtime type is allocated for this purpose |
760 |
> |
* @return an array containing all of the elements in this deque |
761 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
762 |
> |
* is not a supertype of the runtime type of every element in |
763 |
> |
* this deque |
764 |
> |
* @throws NullPointerException if the specified array is null |
765 |
|
*/ |
766 |
|
public <T> T[] toArray(T[] a) { |
767 |
|
int size = size(); |
768 |
|
if (a.length < size) |
769 |
|
a = (T[])java.lang.reflect.Array.newInstance( |
770 |
|
a.getClass().getComponentType(), size); |
771 |
< |
copyElements(a); |
771 |
> |
copyElements(a); |
772 |
|
if (a.length > size) |
773 |
|
a[size] = null; |
774 |
|
return a; |
782 |
|
* @return a copy of this deque |
783 |
|
*/ |
784 |
|
public ArrayDeque<E> clone() { |
785 |
< |
try { |
785 |
> |
try { |
786 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
787 |
< |
// 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); |
787 |
> |
result.elements = Arrays.copyOf(elements, elements.length); |
788 |
|
return result; |
789 |
|
|
790 |
< |
} catch (CloneNotSupportedException e) { |
790 |
> |
} catch (CloneNotSupportedException e) { |
791 |
|
throw new AssertionError(); |
792 |
|
} |
793 |
|
} |
804 |
|
* followed by all of its elements (each an object reference) in |
805 |
|
* first-to-last order. |
806 |
|
*/ |
807 |
< |
private void writeObject(ObjectOutputStream s) throws IOException { |
807 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
808 |
> |
throws java.io.IOException { |
809 |
|
s.defaultWriteObject(); |
810 |
|
|
811 |
|
// Write out size |
812 |
< |
int size = size(); |
750 |
< |
s.writeInt(size); |
812 |
> |
s.writeInt(size()); |
813 |
|
|
814 |
|
// Write out elements in order. |
753 |
– |
int i = head; |
815 |
|
int mask = elements.length - 1; |
816 |
< |
for (int j = 0; j < size; j++) { |
816 |
> |
for (int i = head; i != tail; i = (i + 1) & mask) |
817 |
|
s.writeObject(elements[i]); |
757 |
– |
i = (i + 1) & mask; |
758 |
– |
} |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
|
* Deserialize this deque. |
822 |
|
*/ |
823 |
< |
private void readObject(ObjectInputStream s) |
824 |
< |
throws IOException, ClassNotFoundException { |
823 |
> |
private void readObject(java.io.ObjectInputStream s) |
824 |
> |
throws java.io.IOException, ClassNotFoundException { |
825 |
|
s.defaultReadObject(); |
826 |
|
|
827 |
|
// Read in size and allocate array |
833 |
|
// Read in all elements in the proper order. |
834 |
|
for (int i = 0; i < size; i++) |
835 |
|
elements[i] = (E)s.readObject(); |
777 |
– |
|
836 |
|
} |
837 |
|
} |