595 |
|
} |
596 |
|
|
597 |
|
public E next() { |
598 |
– |
E result; |
598 |
|
if (cursor == fence) |
599 |
|
throw new NoSuchElementException(); |
600 |
+ |
E result = elements[cursor]; |
601 |
|
// This check doesn't catch all possible comodifications, |
602 |
|
// but does catch the ones that corrupt traversal |
603 |
< |
if (tail != fence || (result = elements[cursor]) == null) |
603 |
> |
if (tail != fence || result == null) |
604 |
|
throw new ConcurrentModificationException(); |
605 |
|
lastRet = cursor; |
606 |
|
cursor = (cursor + 1) & (elements.length - 1); |
610 |
|
public void remove() { |
611 |
|
if (lastRet < 0) |
612 |
|
throw new IllegalStateException(); |
613 |
< |
if (delete(lastRet)) // if left-shifted, undo increment in next() |
613 |
> |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
614 |
|
cursor = (cursor - 1) & (elements.length - 1); |
615 |
+ |
fence = tail; |
616 |
+ |
} |
617 |
|
lastRet = -1; |
616 |
– |
fence = tail; |
618 |
|
} |
619 |
|
} |
620 |
|
|
621 |
|
private class DescendingIterator implements Iterator<E> { |
622 |
|
/* |
623 |
|
* This class is nearly a mirror-image of DeqIterator, using |
624 |
< |
* (tail-1) instead of head for initial cursor, (head-1) |
625 |
< |
* 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. |
624 |
> |
* tail instead of head for initial cursor, and head instead of |
625 |
> |
* tail for fence. |
626 |
|
*/ |
627 |
< |
private int cursor = (tail - 1) & (elements.length - 1); |
628 |
< |
private int fence = (head - 1) & (elements.length - 1); |
629 |
< |
private int lastRet = elements.length; |
627 |
> |
private int cursor = tail; |
628 |
> |
private int fence = head; |
629 |
> |
private int lastRet = -1; |
630 |
|
|
631 |
|
public boolean hasNext() { |
632 |
|
return cursor != fence; |
633 |
|
} |
634 |
|
|
635 |
|
public E next() { |
637 |
– |
E result; |
636 |
|
if (cursor == fence) |
637 |
|
throw new NoSuchElementException(); |
638 |
< |
if (((head - 1) & (elements.length - 1)) != fence || |
639 |
< |
(result = elements[cursor]) == null) |
638 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
639 |
> |
E result = elements[cursor]; |
640 |
> |
if (head != fence || result == null) |
641 |
|
throw new ConcurrentModificationException(); |
642 |
|
lastRet = cursor; |
644 |
– |
cursor = (cursor - 1) & (elements.length - 1); |
643 |
|
return result; |
644 |
|
} |
645 |
|
|
646 |
|
public void remove() { |
647 |
< |
if (lastRet >= elements.length) |
647 |
> |
if (lastRet < 0) |
648 |
|
throw new IllegalStateException(); |
649 |
< |
if (!delete(lastRet)) |
649 |
> |
if (!delete(lastRet)) { |
650 |
|
cursor = (cursor + 1) & (elements.length - 1); |
651 |
< |
lastRet = elements.length; |
652 |
< |
fence = (head - 1) & (elements.length - 1); |
651 |
> |
fence = head; |
652 |
> |
} |
653 |
> |
lastRet = -1; |
654 |
|
} |
655 |
|
} |
656 |
|
|