4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
– |
import java.util.*; // for javadoc (till 6280605 is fixed) |
7 |
|
import java.io.*; |
8 |
|
|
9 |
|
/** |
594 |
|
} |
595 |
|
|
596 |
|
public E next() { |
598 |
– |
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)) // if left-shifted, undo increment in next() |
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; |
616 |
– |
fence = tail; |
617 |
|
} |
618 |
|
} |
619 |
|
|
620 |
|
private class DescendingIterator implements Iterator<E> { |
621 |
|
/* |
622 |
|
* This class is nearly a mirror-image of DeqIterator, using |
623 |
< |
* (tail-1) instead of head for initial cursor, (head-1) |
624 |
< |
* instead of tail for fence, and elements.length instead of -1 |
625 |
< |
* for sentinel. It shares the same structure, but not many |
626 |
< |
* actual lines of code. |
623 |
> |
* tail instead of head for initial cursor, and head instead of |
624 |
> |
* tail for fence. |
625 |
|
*/ |
626 |
< |
private int cursor = (tail - 1) & (elements.length - 1); |
627 |
< |
private int fence = (head - 1) & (elements.length - 1); |
628 |
< |
private int lastRet = elements.length; |
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() { |
637 |
– |
E result; |
635 |
|
if (cursor == fence) |
636 |
|
throw new NoSuchElementException(); |
637 |
< |
if (((head - 1) & (elements.length - 1)) != fence || |
638 |
< |
(result = elements[cursor]) == null) |
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; |
644 |
– |
cursor = (cursor - 1) & (elements.length - 1); |
642 |
|
return result; |
643 |
|
} |
644 |
|
|
645 |
|
public void remove() { |
646 |
< |
if (lastRet >= elements.length) |
646 |
> |
if (lastRet < 0) |
647 |
|
throw new IllegalStateException(); |
648 |
< |
if (!delete(lastRet)) |
648 |
> |
if (!delete(lastRet)) { |
649 |
|
cursor = (cursor + 1) & (elements.length - 1); |
650 |
< |
lastRet = elements.length; |
651 |
< |
fence = (head - 1) & (elements.length - 1); |
650 |
> |
fence = head; |
651 |
> |
} |
652 |
> |
lastRet = -1; |
653 |
|
} |
654 |
|
} |
655 |
|
|
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: |
791 |
< |
result.elements = (E[]) new Object[elements.length]; |
792 |
< |
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) { |