689 |
|
|
690 |
|
DeqIterator() { cursor = head; } |
691 |
|
|
692 |
– |
int advance(int i, int modulus) { |
693 |
– |
return inc(i, modulus); |
694 |
– |
} |
695 |
– |
|
696 |
– |
void doRemove() { |
697 |
– |
if (delete(lastRet)) |
698 |
– |
// if left-shifted, undo advance in next() |
699 |
– |
cursor = dec(cursor, elements.length); |
700 |
– |
} |
701 |
– |
|
692 |
|
public final boolean hasNext() { |
693 |
|
return remaining > 0; |
694 |
|
} |
695 |
|
|
696 |
< |
public final E next() { |
696 |
> |
public E next() { |
697 |
|
if (remaining == 0) |
698 |
|
throw new NoSuchElementException(); |
699 |
|
E e = checkedElementAt(elements, cursor); |
700 |
|
lastRet = cursor; |
701 |
< |
cursor = advance(cursor, elements.length); |
701 |
> |
cursor = inc(cursor, elements.length); |
702 |
|
remaining--; |
703 |
|
return e; |
704 |
|
} |
705 |
|
|
706 |
+ |
void postDelete(boolean leftShifted) { |
707 |
+ |
if (leftShifted) |
708 |
+ |
cursor = dec(cursor, elements.length); // undo inc in next |
709 |
+ |
} |
710 |
+ |
|
711 |
|
public final void remove() { |
712 |
|
if (lastRet < 0) |
713 |
|
throw new IllegalStateException(); |
714 |
< |
doRemove(); |
714 |
> |
postDelete(delete(lastRet)); |
715 |
|
lastRet = -1; |
716 |
|
} |
717 |
|
|
718 |
< |
public final void forEachRemaining(Consumer<? super E> action) { |
718 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
719 |
|
Objects.requireNonNull(action); |
720 |
|
final Object[] elements = ArrayDeque.this.elements; |
721 |
|
final int capacity = elements.length; |
722 |
|
int k = remaining; |
723 |
|
remaining = 0; |
724 |
< |
for (int i = cursor; --k >= 0; i = advance(i, capacity)) |
724 |
> |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
725 |
|
action.accept(checkedElementAt(elements, i)); |
726 |
|
} |
727 |
|
} |
729 |
|
private class DescendingIterator extends DeqIterator { |
730 |
|
DescendingIterator() { cursor = tail(); } |
731 |
|
|
732 |
< |
@Override int advance(int i, int modulus) { |
733 |
< |
return dec(i, modulus); |
732 |
> |
public final E next() { |
733 |
> |
if (remaining == 0) |
734 |
> |
throw new NoSuchElementException(); |
735 |
> |
E e = checkedElementAt(elements, cursor); |
736 |
> |
lastRet = cursor; |
737 |
> |
cursor = dec(cursor, elements.length); |
738 |
> |
remaining--; |
739 |
> |
return e; |
740 |
> |
} |
741 |
> |
|
742 |
> |
void postDelete(boolean leftShifted) { |
743 |
> |
if (!leftShifted) |
744 |
> |
cursor = inc(cursor, elements.length); // undo dec in next |
745 |
|
} |
746 |
|
|
747 |
< |
@Override void doRemove() { |
748 |
< |
if (!delete(lastRet)) |
749 |
< |
// if right-shifted, undo advance in next |
750 |
< |
cursor = inc(cursor, elements.length); |
747 |
> |
public final void forEachRemaining(Consumer<? super E> action) { |
748 |
> |
Objects.requireNonNull(action); |
749 |
> |
final Object[] elements = ArrayDeque.this.elements; |
750 |
> |
final int capacity = elements.length; |
751 |
> |
int k = remaining; |
752 |
> |
remaining = 0; |
753 |
> |
for (int i = cursor; --k >= 0; i = dec(i, capacity)) |
754 |
> |
action.accept(checkedElementAt(elements, i)); |
755 |
|
} |
756 |
|
} |
757 |
|
|