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; |
18 |
|
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
19 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
20 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
21 |
< |
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
21 |
> |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
22 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
23 |
|
* time. |
24 |
|
* |
40 |
|
* should be used only to detect bugs.</i> |
41 |
|
* |
42 |
|
* <p>This class and its iterator implement all of the |
43 |
< |
* optional methods of the {@link Collection} and {@link |
44 |
< |
* Iterator} interfaces. This class is a member of the <a |
45 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
46 |
< |
* Framework</a>. |
43 |
> |
* <em>optional</em> methods of the {@link Collection} and {@link |
44 |
> |
* Iterator} interfaces. |
45 |
> |
* |
46 |
> |
* <p>This class is a member of the |
47 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
48 |
> |
* Java Collections Framework</a>. |
49 |
|
* |
50 |
|
* @author Josh Bloch and Doug Lea |
51 |
|
* @since 1.6 |
90 |
|
/** |
91 |
|
* Allocate empty array to hold the given number of elements. |
92 |
|
* |
93 |
< |
* @param numElements the number of elements to hold. |
93 |
> |
* @param numElements the number of elements to hold |
94 |
|
*/ |
95 |
|
private void allocateElements(int numElements) { |
96 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
189 |
|
/** |
190 |
|
* Inserts the specified element at the front of this deque. |
191 |
|
* |
192 |
< |
* @param e the element to insert |
193 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
192 |
> |
* @param e the element to add |
193 |
> |
* @throws NullPointerException if the specified element is null |
194 |
|
*/ |
195 |
|
public void addFirst(E e) { |
196 |
|
if (e == null) |
202 |
|
|
203 |
|
/** |
204 |
|
* Inserts the specified element at the end of this deque. |
203 |
– |
* This method is equivalent to {@link Collection#add} and |
204 |
– |
* {@link #push}. |
205 |
|
* |
206 |
< |
* @param e the element to insert |
207 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
206 |
> |
* <p>This method is equivalent to {@link #add}. |
207 |
> |
* |
208 |
> |
* @param e the element to add |
209 |
> |
* @throws NullPointerException if the specified element is null |
210 |
|
*/ |
211 |
|
public void addLast(E e) { |
212 |
|
if (e == null) |
217 |
|
} |
218 |
|
|
219 |
|
/** |
218 |
– |
* Retrieves and removes the first element of this deque, or |
219 |
– |
* <tt>null</tt> if this deque is empty. |
220 |
– |
* |
221 |
– |
* @return the first element of this deque, or <tt>null</tt> if |
222 |
– |
* this deque is empty |
223 |
– |
*/ |
224 |
– |
public E pollFirst() { |
225 |
– |
int h = head; |
226 |
– |
E result = elements[h]; // Element is null if deque empty |
227 |
– |
if (result == null) |
228 |
– |
return null; |
229 |
– |
elements[h] = null; // Must null out slot |
230 |
– |
head = (h + 1) & (elements.length - 1); |
231 |
– |
return result; |
232 |
– |
} |
233 |
– |
|
234 |
– |
/** |
235 |
– |
* Retrieves and removes the last element of this deque, or |
236 |
– |
* <tt>null</tt> if this deque is empty. |
237 |
– |
* |
238 |
– |
* @return the last element of this deque, or <tt>null</tt> if |
239 |
– |
* this deque is empty |
240 |
– |
*/ |
241 |
– |
public E pollLast() { |
242 |
– |
int t = (tail - 1) & (elements.length - 1); |
243 |
– |
E result = elements[t]; |
244 |
– |
if (result == null) |
245 |
– |
return null; |
246 |
– |
elements[t] = null; |
247 |
– |
tail = t; |
248 |
– |
return result; |
249 |
– |
} |
250 |
– |
|
251 |
– |
/** |
220 |
|
* Inserts the specified element at the front of this deque. |
221 |
|
* |
222 |
< |
* @param e the element to insert |
223 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
224 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
222 |
> |
* @param e the element to add |
223 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
224 |
> |
* @throws NullPointerException if the specified element is null |
225 |
|
*/ |
226 |
|
public boolean offerFirst(E e) { |
227 |
|
addFirst(e); |
231 |
|
/** |
232 |
|
* Inserts the specified element at the end of this deque. |
233 |
|
* |
234 |
< |
* @param e the element to insert |
235 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
236 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
234 |
> |
* @param e the element to add |
235 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerLast}) |
236 |
> |
* @throws NullPointerException if the specified element is null |
237 |
|
*/ |
238 |
|
public boolean offerLast(E e) { |
239 |
|
addLast(e); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
< |
* Retrieves and removes the first element of this deque. This method |
277 |
< |
* differs from the {@link #pollFirst} method only in that it throws an |
278 |
< |
* exception if this deque is empty. |
279 |
< |
* |
280 |
< |
* @return the first element of this deque |
281 |
< |
* @throws NoSuchElementException if this deque is empty |
244 |
> |
* @throws NoSuchElementException {@inheritDoc} |
245 |
|
*/ |
246 |
|
public E removeFirst() { |
247 |
|
E x = pollFirst(); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
< |
* Retrieves and removes the last element of this deque. This method |
292 |
< |
* differs from the {@link #pollLast} method only in that it throws an |
293 |
< |
* exception if this deque is empty. |
294 |
< |
* |
295 |
< |
* @return the last element of this deque |
296 |
< |
* @throws NoSuchElementException if this deque is empty |
254 |
> |
* @throws NoSuchElementException {@inheritDoc} |
255 |
|
*/ |
256 |
|
public E removeLast() { |
257 |
|
E x = pollLast(); |
260 |
|
return x; |
261 |
|
} |
262 |
|
|
263 |
< |
/** |
264 |
< |
* Retrieves, but does not remove, the first element of this deque, |
265 |
< |
* returning <tt>null</tt> if this deque is empty. |
266 |
< |
* |
267 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
268 |
< |
* this deque is empty |
269 |
< |
*/ |
270 |
< |
public E peekFirst() { |
313 |
< |
return elements[head]; // elements[head] is null if deque empty |
263 |
> |
public E pollFirst() { |
264 |
> |
int h = head; |
265 |
> |
E result = elements[h]; // Element is null if deque empty |
266 |
> |
if (result == null) |
267 |
> |
return null; |
268 |
> |
elements[h] = null; // Must null out slot |
269 |
> |
head = (h + 1) & (elements.length - 1); |
270 |
> |
return result; |
271 |
|
} |
272 |
|
|
273 |
< |
/** |
274 |
< |
* Retrieves, but does not remove, the last element of this deque, |
275 |
< |
* returning <tt>null</tt> if this deque is empty. |
276 |
< |
* |
277 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
278 |
< |
* is empty |
279 |
< |
*/ |
280 |
< |
public E peekLast() { |
324 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
273 |
> |
public E pollLast() { |
274 |
> |
int t = (tail - 1) & (elements.length - 1); |
275 |
> |
E result = elements[t]; |
276 |
> |
if (result == null) |
277 |
> |
return null; |
278 |
> |
elements[t] = null; |
279 |
> |
tail = t; |
280 |
> |
return result; |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
< |
* Retrieves, but does not remove, the first element of this |
329 |
< |
* deque. This method differs from the {@link #peekFirst} method only |
330 |
< |
* in that it throws an exception if this deque is empty. |
331 |
< |
* |
332 |
< |
* @return the first element of this deque |
333 |
< |
* @throws NoSuchElementException if this deque is empty |
284 |
> |
* @throws NoSuchElementException {@inheritDoc} |
285 |
|
*/ |
286 |
|
public E getFirst() { |
287 |
|
E x = elements[head]; |
291 |
|
} |
292 |
|
|
293 |
|
/** |
294 |
< |
* Retrieves, but does not remove, the last element of this |
344 |
< |
* deque. This method differs from the {@link #peekLast} method only |
345 |
< |
* in that it throws an exception if this deque is empty. |
346 |
< |
* |
347 |
< |
* @return the last element of this deque |
348 |
< |
* @throws NoSuchElementException if this deque is empty |
294 |
> |
* @throws NoSuchElementException {@inheritDoc} |
295 |
|
*/ |
296 |
|
public E getLast() { |
297 |
|
E x = elements[(tail - 1) & (elements.length - 1)]; |
300 |
|
return x; |
301 |
|
} |
302 |
|
|
303 |
+ |
public E peekFirst() { |
304 |
+ |
return elements[head]; // elements[head] is null if deque empty |
305 |
+ |
} |
306 |
+ |
|
307 |
+ |
public E peekLast() { |
308 |
+ |
return elements[(tail - 1) & (elements.length - 1)]; |
309 |
+ |
} |
310 |
+ |
|
311 |
|
/** |
312 |
|
* Removes the first occurrence of the specified element in this |
313 |
< |
* deque (when traversing the deque from head to tail). More |
314 |
< |
* formally, removes the first element e such that (o==null ? |
315 |
< |
* e==null : o.equals(e)). If the deque does not contain the |
316 |
< |
* element, it is unchanged. |
313 |
> |
* deque (when traversing the deque from head to tail). |
314 |
> |
* If the deque does not contain the element, it is unchanged. |
315 |
> |
* More formally, removes the first element <tt>e</tt> such that |
316 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
317 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
318 |
> |
* (or equivalently, if this deque changed as a result of the call). |
319 |
|
* |
320 |
|
* @param o element to be removed from this deque, if present |
321 |
|
* @return <tt>true</tt> if the deque contained the specified element |
338 |
|
|
339 |
|
/** |
340 |
|
* Removes the last occurrence of the specified element in this |
341 |
< |
* deque (when traversing the deque from head to tail). More |
342 |
< |
* formally, removes the last element e such that (o==null ? |
343 |
< |
* e==null : o.equals(e)). If the deque |
344 |
< |
* does not contain the element, it is unchanged. |
341 |
> |
* deque (when traversing the deque from head to tail). |
342 |
> |
* If the deque does not contain the element, it is unchanged. |
343 |
> |
* More formally, removes the last element <tt>e</tt> such that |
344 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
345 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
346 |
> |
* (or equivalently, if this deque changed as a result of the call). |
347 |
|
* |
348 |
|
* @param o element to be removed from this deque, if present |
349 |
|
* @return <tt>true</tt> if the deque contained the specified element |
369 |
|
/** |
370 |
|
* Inserts the specified element at the end of this deque. |
371 |
|
* |
414 |
– |
* <p>This method is equivalent to {@link #offerLast}. |
415 |
– |
* |
416 |
– |
* @param e the element to insert |
417 |
– |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
418 |
– |
* @throws NullPointerException if <tt>e</tt> is null |
419 |
– |
*/ |
420 |
– |
public boolean offer(E e) { |
421 |
– |
return offerLast(e); |
422 |
– |
} |
423 |
– |
|
424 |
– |
/** |
425 |
– |
* Inserts the specified element at the end of this deque. |
426 |
– |
* |
372 |
|
* <p>This method is equivalent to {@link #addLast}. |
373 |
|
* |
374 |
< |
* @param e the element to insert |
375 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
376 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
374 |
> |
* @param e the element to add |
375 |
> |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
376 |
> |
* @throws NullPointerException if the specified element is null |
377 |
|
*/ |
378 |
|
public boolean add(E e) { |
379 |
|
addLast(e); |
381 |
|
} |
382 |
|
|
383 |
|
/** |
384 |
< |
* Retrieves and removes the head of the queue represented by |
440 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
441 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
442 |
< |
* if this deque is empty. |
384 |
> |
* Inserts the specified element at the end of this deque. |
385 |
|
* |
386 |
< |
* <p>This method is equivalent to {@link #pollFirst}. |
386 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
387 |
|
* |
388 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
389 |
< |
* this deque is empty |
388 |
> |
* @param e the element to add |
389 |
> |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
390 |
> |
* @throws NullPointerException if the specified element is null |
391 |
|
*/ |
392 |
< |
public E poll() { |
393 |
< |
return pollFirst(); |
392 |
> |
public boolean offer(E e) { |
393 |
> |
return offerLast(e); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
|
* Retrieves and removes the head of the queue represented by this deque. |
398 |
< |
* This method differs from the {@link #poll} method only in that it |
399 |
< |
* throws an exception if this deque is empty. |
398 |
> |
* |
399 |
> |
* This method differs from {@link #poll poll} only in that it throws an |
400 |
> |
* exception if this deque is empty. |
401 |
|
* |
402 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
403 |
|
* |
404 |
|
* @return the head of the queue represented by this deque |
405 |
< |
* @throws NoSuchElementException if this deque is empty |
405 |
> |
* @throws NoSuchElementException {@inheritDoc} |
406 |
|
*/ |
407 |
|
public E remove() { |
408 |
|
return removeFirst(); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
413 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
412 |
> |
* Retrieves and removes the head of the queue represented by this deque |
413 |
> |
* (in other words, the first element of this deque), or returns |
414 |
> |
* <tt>null</tt> if this deque is empty. |
415 |
|
* |
416 |
< |
* <p>This method is equivalent to {@link #peekFirst}. |
416 |
> |
* <p>This method is equivalent to {@link #pollFirst}. |
417 |
|
* |
418 |
|
* @return the head of the queue represented by this deque, or |
419 |
< |
* <tt>null</tt> if this deque is empty |
419 |
> |
* <tt>null</tt> if this deque is empty |
420 |
|
*/ |
421 |
< |
public E peek() { |
422 |
< |
return peekFirst(); |
421 |
> |
public E poll() { |
422 |
> |
return pollFirst(); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* Retrieves, but does not remove, the head of the queue represented by |
427 |
< |
* this deque. This method differs from the {@link #peek} method only in |
427 |
> |
* this deque. This method differs from {@link #peek peek} only in |
428 |
|
* that it throws an exception if this deque is empty. |
429 |
|
* |
430 |
|
* <p>This method is equivalent to {@link #getFirst}. |
431 |
|
* |
432 |
|
* @return the head of the queue represented by this deque |
433 |
< |
* @throws NoSuchElementException if this deque is empty |
433 |
> |
* @throws NoSuchElementException {@inheritDoc} |
434 |
|
*/ |
435 |
|
public E element() { |
436 |
|
return getFirst(); |
437 |
|
} |
438 |
|
|
439 |
+ |
/** |
440 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
441 |
+ |
* this deque, or returns <tt>null</tt> if this deque is empty. |
442 |
+ |
* |
443 |
+ |
* <p>This method is equivalent to {@link #peekFirst}. |
444 |
+ |
* |
445 |
+ |
* @return the head of the queue represented by this deque, or |
446 |
+ |
* <tt>null</tt> if this deque is empty |
447 |
+ |
*/ |
448 |
+ |
public E peek() { |
449 |
+ |
return peekFirst(); |
450 |
+ |
} |
451 |
+ |
|
452 |
|
// *** Stack methods *** |
453 |
|
|
454 |
|
/** |
458 |
|
* <p>This method is equivalent to {@link #addFirst}. |
459 |
|
* |
460 |
|
* @param e the element to push |
461 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
461 |
> |
* @throws NullPointerException if the specified element is null |
462 |
|
*/ |
463 |
|
public void push(E e) { |
464 |
|
addFirst(e); |
471 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
472 |
|
* |
473 |
|
* @return the element at the front of this deque (which is the top |
474 |
< |
* of the stack represented by this deque) |
475 |
< |
* @throws NoSuchElementException if this deque is empty |
474 |
> |
* of the stack represented by this deque) |
475 |
> |
* @throws NoSuchElementException {@inheritDoc} |
476 |
|
*/ |
477 |
|
public E pop() { |
478 |
|
return removeFirst(); |
479 |
|
} |
480 |
|
|
481 |
+ |
private void checkInvariants() { |
482 |
+ |
assert elements[tail] == null; |
483 |
+ |
assert head == tail ? elements[head] == null : |
484 |
+ |
(elements[head] != null && |
485 |
+ |
elements[(tail - 1) & (elements.length - 1)] != null); |
486 |
+ |
assert elements[(head - 1) & (elements.length - 1)] == null; |
487 |
+ |
} |
488 |
+ |
|
489 |
|
/** |
490 |
|
* Removes the element at the specified position in the elements array, |
491 |
< |
* adjusting head, tail, and size as necessary. This can result in |
492 |
< |
* motion of elements backwards or forwards in the array. |
491 |
> |
* adjusting head and tail as necessary. This can result in motion of |
492 |
> |
* elements backwards or forwards in the array. |
493 |
|
* |
494 |
|
* <p>This method is called delete rather than remove to emphasize |
495 |
< |
* that its semantics differ from those of List.remove(int). |
495 |
> |
* that its semantics differ from those of {@link List#remove(int)}. |
496 |
|
* |
497 |
|
* @return true if elements moved backwards |
498 |
|
*/ |
499 |
|
private boolean delete(int i) { |
500 |
< |
// Case 1: Deque doesn't wrap |
501 |
< |
// Case 2: Deque does wrap and removed element is in the head portion |
502 |
< |
if ((head < tail || tail == 0) || i >= head) { |
503 |
< |
System.arraycopy(elements, head, elements, head + 1, i - head); |
504 |
< |
elements[head] = null; |
505 |
< |
head = (head + 1) & (elements.length - 1); |
500 |
> |
checkInvariants(); |
501 |
> |
final E[] elements = this.elements; |
502 |
> |
final int mask = elements.length - 1; |
503 |
> |
final int h = head; |
504 |
> |
final int t = tail; |
505 |
> |
final int front = (i - h) & mask; |
506 |
> |
final int back = (t - i) & mask; |
507 |
> |
|
508 |
> |
// Invariant: head <= i < tail mod circularity |
509 |
> |
if (front >= ((t - h) & mask)) |
510 |
> |
throw new ConcurrentModificationException(); |
511 |
> |
|
512 |
> |
// Optimize for least element motion |
513 |
> |
if (front < back) { |
514 |
> |
if (h <= i) { |
515 |
> |
System.arraycopy(elements, h, elements, h + 1, front); |
516 |
> |
} else { // Wrap around |
517 |
> |
System.arraycopy(elements, 0, elements, 1, i); |
518 |
> |
elements[0] = elements[mask]; |
519 |
> |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
520 |
> |
} |
521 |
> |
elements[h] = null; |
522 |
> |
head = (h + 1) & mask; |
523 |
|
return false; |
524 |
+ |
} else { |
525 |
+ |
if (i < t) { // Copy the null tail as well |
526 |
+ |
System.arraycopy(elements, i + 1, elements, i, back); |
527 |
+ |
tail = t - 1; |
528 |
+ |
} else { // Wrap around |
529 |
+ |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
530 |
+ |
elements[mask] = elements[0]; |
531 |
+ |
System.arraycopy(elements, 1, elements, 0, t); |
532 |
+ |
tail = (t - 1) & mask; |
533 |
+ |
} |
534 |
+ |
return true; |
535 |
|
} |
542 |
– |
|
543 |
– |
// Case 3: Deque wraps and removed element is in the tail portion |
544 |
– |
tail--; |
545 |
– |
System.arraycopy(elements, i + 1, elements, i, tail - i); |
546 |
– |
elements[tail] = null; |
547 |
– |
return true; |
536 |
|
} |
537 |
|
|
538 |
|
// *** Collection Methods *** |
547 |
|
} |
548 |
|
|
549 |
|
/** |
550 |
< |
* Returns <tt>true</tt> if this deque contains no elements.<p> |
550 |
> |
* Returns <tt>true</tt> if this deque contains no elements. |
551 |
|
* |
552 |
< |
* @return <tt>true</tt> if this deque contains no elements. |
552 |
> |
* @return <tt>true</tt> if this deque contains no elements |
553 |
|
*/ |
554 |
|
public boolean isEmpty() { |
555 |
|
return head == tail; |
561 |
|
* order that elements would be dequeued (via successive calls to |
562 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
563 |
|
* |
564 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
564 |
> |
* @return an iterator over the elements in this deque |
565 |
|
*/ |
566 |
|
public Iterator<E> iterator() { |
567 |
|
return new DeqIterator(); |
568 |
|
} |
569 |
|
|
570 |
+ |
public Iterator<E> descendingIterator() { |
571 |
+ |
return new DescendingIterator(); |
572 |
+ |
} |
573 |
+ |
|
574 |
|
private class DeqIterator implements Iterator<E> { |
575 |
|
/** |
576 |
|
* Index of element to be returned by subsequent call to next. |
594 |
|
} |
595 |
|
|
596 |
|
public E next() { |
605 |
– |
E result; |
597 |
|
if (cursor == fence) |
598 |
|
throw new NoSuchElementException(); |
599 |
+ |
E result = elements[cursor]; |
600 |
|
// This check doesn't catch all possible comodifications, |
601 |
|
// but does catch the ones that corrupt traversal |
602 |
< |
if (tail != fence || (result = elements[cursor]) == null) |
602 |
> |
if (tail != fence || result == null) |
603 |
|
throw new ConcurrentModificationException(); |
604 |
|
lastRet = cursor; |
605 |
|
cursor = (cursor + 1) & (elements.length - 1); |
609 |
|
public void remove() { |
610 |
|
if (lastRet < 0) |
611 |
|
throw new IllegalStateException(); |
612 |
< |
if (delete(lastRet)) |
613 |
< |
cursor--; |
612 |
> |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
613 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
614 |
> |
fence = tail; |
615 |
> |
} |
616 |
> |
lastRet = -1; |
617 |
> |
} |
618 |
> |
} |
619 |
> |
|
620 |
> |
private class DescendingIterator implements Iterator<E> { |
621 |
> |
/* |
622 |
> |
* This class is nearly a mirror-image of DeqIterator, using |
623 |
> |
* tail instead of head for initial cursor, and head instead of |
624 |
> |
* tail for fence. |
625 |
> |
*/ |
626 |
> |
private int cursor = tail; |
627 |
> |
private int fence = head; |
628 |
> |
private int lastRet = -1; |
629 |
> |
|
630 |
> |
public boolean hasNext() { |
631 |
> |
return cursor != fence; |
632 |
> |
} |
633 |
> |
|
634 |
> |
public E next() { |
635 |
> |
if (cursor == fence) |
636 |
> |
throw new NoSuchElementException(); |
637 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
638 |
> |
E result = elements[cursor]; |
639 |
> |
if (head != fence || result == null) |
640 |
> |
throw new ConcurrentModificationException(); |
641 |
> |
lastRet = cursor; |
642 |
> |
return result; |
643 |
> |
} |
644 |
> |
|
645 |
> |
public void remove() { |
646 |
> |
if (lastRet < 0) |
647 |
> |
throw new IllegalStateException(); |
648 |
> |
if (!delete(lastRet)) { |
649 |
> |
cursor = (cursor + 1) & (elements.length - 1); |
650 |
> |
fence = head; |
651 |
> |
} |
652 |
|
lastRet = -1; |
623 |
– |
fence = tail; |
653 |
|
} |
654 |
|
} |
655 |
|
|
656 |
|
/** |
657 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
658 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
659 |
< |
* deque contains at least one element <tt>e</tt> such that |
631 |
< |
* <tt>e.equals(o)</tt>. |
657 |
> |
* Returns <tt>true</tt> if this deque contains the specified element. |
658 |
> |
* More formally, returns <tt>true</tt> if and only if this deque contains |
659 |
> |
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
660 |
|
* |
661 |
|
* @param o object to be checked for containment in this deque |
662 |
|
* @return <tt>true</tt> if this deque contains the specified element |
677 |
|
|
678 |
|
/** |
679 |
|
* Removes a single instance of the specified element from this deque. |
680 |
< |
* This method is equivalent to {@link #removeFirstOccurrence}. |
680 |
> |
* If the deque does not contain the element, it is unchanged. |
681 |
> |
* More formally, removes the first element <tt>e</tt> such that |
682 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
683 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
684 |
> |
* (or equivalently, if this deque changed as a result of the call). |
685 |
|
* |
686 |
< |
* @param e element to be removed from this deque, if present |
686 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
687 |
> |
* |
688 |
> |
* @param o element to be removed from this deque, if present |
689 |
|
* @return <tt>true</tt> if this deque contained the specified element |
690 |
|
*/ |
691 |
< |
public boolean remove(Object e) { |
692 |
< |
return removeFirstOccurrence(e); |
691 |
> |
public boolean remove(Object o) { |
692 |
> |
return removeFirstOccurrence(o); |
693 |
|
} |
694 |
|
|
695 |
|
/** |
706 |
|
do { |
707 |
|
elements[i] = null; |
708 |
|
i = (i + 1) & mask; |
709 |
< |
} while(i != t); |
709 |
> |
} while (i != t); |
710 |
|
} |
711 |
|
} |
712 |
|
|
713 |
|
/** |
714 |
|
* Returns an array containing all of the elements in this deque |
715 |
< |
* in the correct order. |
715 |
> |
* in proper sequence (from first to last element). |
716 |
> |
* |
717 |
> |
* <p>The returned array will be "safe" in that no references to it are |
718 |
> |
* maintained by this deque. (In other words, this method must allocate |
719 |
> |
* a new array). The caller is thus free to modify the returned array. |
720 |
> |
* |
721 |
> |
* <p>This method acts as bridge between array-based and collection-based |
722 |
> |
* APIs. |
723 |
|
* |
724 |
|
* @return an array containing all of the elements in this deque |
684 |
– |
* in the correct order |
725 |
|
*/ |
726 |
|
public Object[] toArray() { |
727 |
< |
return copyElements(new Object[size()]); |
727 |
> |
return copyElements(new Object[size()]); |
728 |
|
} |
729 |
|
|
730 |
|
/** |
731 |
< |
* Returns an array containing all of the elements in this deque in the |
732 |
< |
* correct order; the runtime type of the returned array is that of the |
733 |
< |
* specified array. If the deque fits in the specified array, it is |
734 |
< |
* returned therein. Otherwise, a new array is allocated with the runtime |
735 |
< |
* type of the specified array and the size of this deque. |
731 |
> |
* Returns an array containing all of the elements in this deque in |
732 |
> |
* proper sequence (from first to last element); the runtime type of the |
733 |
> |
* returned array is that of the specified array. If the deque fits in |
734 |
> |
* the specified array, it is returned therein. Otherwise, a new array |
735 |
> |
* is allocated with the runtime type of the specified array and the |
736 |
> |
* size of this deque. |
737 |
> |
* |
738 |
> |
* <p>If this deque fits in the specified array with room to spare |
739 |
> |
* (i.e., the array has more elements than this deque), the element in |
740 |
> |
* the array immediately following the end of the deque is set to |
741 |
> |
* <tt>null</tt>. |
742 |
|
* |
743 |
< |
* <p>If the deque fits in the specified array with room to spare (i.e., |
744 |
< |
* the array has more elements than the deque), the element in the array |
745 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
743 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
744 |
> |
* array-based and collection-based APIs. Further, this method allows |
745 |
> |
* precise control over the runtime type of the output array, and may, |
746 |
> |
* under certain circumstances, be used to save allocation costs. |
747 |
> |
* |
748 |
> |
* <p>Suppose <tt>x</tt> is a deque known to contain only strings. |
749 |
> |
* The following code can be used to dump the deque into a newly |
750 |
> |
* allocated array of <tt>String</tt>: |
751 |
> |
* |
752 |
> |
* <pre> |
753 |
> |
* String[] y = x.toArray(new String[0]);</pre> |
754 |
> |
* |
755 |
> |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
756 |
> |
* <tt>toArray()</tt>. |
757 |
|
* |
758 |
|
* @param a the array into which the elements of the deque are to |
759 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
760 |
< |
* same runtime type is allocated for this purpose |
761 |
< |
* @return an array containing the elements of the deque |
762 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
763 |
< |
* of the runtime type of every element in this deque |
759 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
760 |
> |
* same runtime type is allocated for this purpose |
761 |
> |
* @return an array containing all of the elements in this deque |
762 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
763 |
> |
* is not a supertype of the runtime type of every element in |
764 |
> |
* this deque |
765 |
> |
* @throws NullPointerException if the specified array is null |
766 |
|
*/ |
767 |
|
public <T> T[] toArray(T[] a) { |
768 |
|
int size = size(); |
769 |
|
if (a.length < size) |
770 |
|
a = (T[])java.lang.reflect.Array.newInstance( |
771 |
|
a.getClass().getComponentType(), size); |
772 |
< |
copyElements(a); |
772 |
> |
copyElements(a); |
773 |
|
if (a.length > size) |
774 |
|
a[size] = null; |
775 |
|
return a; |
785 |
|
public ArrayDeque<E> clone() { |
786 |
|
try { |
787 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
788 |
< |
// These two lines are currently faster than cloning the array: |
730 |
< |
result.elements = (E[]) new Object[elements.length]; |
731 |
< |
System.arraycopy(elements, 0, result.elements, 0, elements.length); |
788 |
> |
result.elements = Arrays.copyOf(elements, elements.length); |
789 |
|
return result; |
790 |
|
|
791 |
|
} catch (CloneNotSupportedException e) { |
809 |
|
s.defaultWriteObject(); |
810 |
|
|
811 |
|
// Write out size |
812 |
< |
int size = size(); |
756 |
< |
s.writeInt(size); |
812 |
> |
s.writeInt(size()); |
813 |
|
|
814 |
|
// Write out elements in order. |
759 |
– |
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]); |
763 |
– |
i = (i + 1) & mask; |
764 |
– |
} |
818 |
|
} |
819 |
|
|
820 |
|
/** |
833 |
|
// Read in all elements in the proper order. |
834 |
|
for (int i = 0; i < size; i++) |
835 |
|
elements[i] = (E)s.readObject(); |
783 |
– |
|
836 |
|
} |
837 |
|
} |