263 |
|
public void addFirst(E e) { |
264 |
|
// checkInvariants(); |
265 |
|
Objects.requireNonNull(e); |
266 |
< |
Object[] elements; |
267 |
< |
int capacity, s = size; |
268 |
< |
while (s == (capacity = (elements = this.elements).length)) |
269 |
< |
grow(1); |
270 |
< |
elements[head = dec(head, capacity)] = e; |
266 |
> |
final Object[] elements; |
267 |
> |
final int capacity, s; |
268 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) |
269 |
> |
addFirstSlowPath(e); |
270 |
> |
else |
271 |
> |
elements[head = dec(head, capacity)] = e; |
272 |
|
size = s + 1; |
273 |
+ |
// checkInvariants(); |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
private void addFirstSlowPath(E e) { |
277 |
+ |
grow(1); |
278 |
+ |
final Object[] elements = this.elements; |
279 |
+ |
elements[head = dec(head, elements.length)] = e; |
280 |
|
} |
281 |
|
|
282 |
|
/** |
290 |
|
public void addLast(E e) { |
291 |
|
// checkInvariants(); |
292 |
|
Objects.requireNonNull(e); |
293 |
< |
Object[] elements; |
294 |
< |
int capacity, s = size; |
295 |
< |
while (s == (capacity = (elements = this.elements).length)) |
296 |
< |
grow(1); |
297 |
< |
elements[add(head, s, capacity)] = e; |
293 |
> |
final Object[] elements; |
294 |
> |
final int capacity, s; |
295 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) |
296 |
> |
addLastSlowPath(e); |
297 |
> |
else |
298 |
> |
elements[add(head, s, capacity)] = e; |
299 |
|
size = s + 1; |
300 |
+ |
// checkInvariants(); |
301 |
+ |
} |
302 |
+ |
|
303 |
+ |
private void addLastSlowPath(E e) { |
304 |
+ |
grow(1); |
305 |
+ |
final Object[] elements = this.elements; |
306 |
+ |
elements[add(head, size, elements.length)] = e; |
307 |
|
} |
308 |
|
|
309 |
|
/** |
705 |
|
|
706 |
|
DeqIterator() { cursor = head; } |
707 |
|
|
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 |
– |
|
708 |
|
public final boolean hasNext() { |
709 |
|
return remaining > 0; |
710 |
|
} |
711 |
|
|
712 |
< |
public final E next() { |
712 |
> |
public E next() { |
713 |
|
if (remaining == 0) |
714 |
|
throw new NoSuchElementException(); |
715 |
|
E e = checkedElementAt(elements, cursor); |
716 |
|
lastRet = cursor; |
717 |
< |
cursor = advance(cursor, elements.length); |
717 |
> |
cursor = inc(cursor, elements.length); |
718 |
|
remaining--; |
719 |
|
return e; |
720 |
|
} |
721 |
|
|
722 |
+ |
void postDelete(boolean leftShifted) { |
723 |
+ |
if (leftShifted) |
724 |
+ |
cursor = dec(cursor, elements.length); // undo inc in next |
725 |
+ |
} |
726 |
+ |
|
727 |
|
public final void remove() { |
728 |
|
if (lastRet < 0) |
729 |
|
throw new IllegalStateException(); |
730 |
< |
doRemove(); |
730 |
> |
postDelete(delete(lastRet)); |
731 |
|
lastRet = -1; |
732 |
|
} |
733 |
|
|
734 |
< |
public final void forEachRemaining(Consumer<? super E> action) { |
734 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
735 |
|
Objects.requireNonNull(action); |
736 |
|
final Object[] elements = ArrayDeque.this.elements; |
737 |
|
final int capacity = elements.length; |
738 |
|
int k = remaining; |
739 |
|
remaining = 0; |
740 |
< |
for (int i = cursor; --k >= 0; i = advance(i, capacity)) |
740 |
> |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
741 |
|
action.accept(checkedElementAt(elements, i)); |
742 |
|
} |
743 |
|
} |
745 |
|
private class DescendingIterator extends DeqIterator { |
746 |
|
DescendingIterator() { cursor = tail(); } |
747 |
|
|
748 |
< |
@Override int advance(int i, int modulus) { |
749 |
< |
return dec(i, modulus); |
748 |
> |
public final E next() { |
749 |
> |
if (remaining == 0) |
750 |
> |
throw new NoSuchElementException(); |
751 |
> |
E e = checkedElementAt(elements, cursor); |
752 |
> |
lastRet = cursor; |
753 |
> |
cursor = dec(cursor, elements.length); |
754 |
> |
remaining--; |
755 |
> |
return e; |
756 |
> |
} |
757 |
> |
|
758 |
> |
void postDelete(boolean leftShifted) { |
759 |
> |
if (!leftShifted) |
760 |
> |
cursor = inc(cursor, elements.length); // undo dec in next |
761 |
|
} |
762 |
|
|
763 |
< |
@Override void doRemove() { |
764 |
< |
if (!delete(lastRet)) |
765 |
< |
// if right-shifted, undo advance in next |
766 |
< |
cursor = inc(cursor, elements.length); |
763 |
> |
public final void forEachRemaining(Consumer<? super E> action) { |
764 |
> |
Objects.requireNonNull(action); |
765 |
> |
final Object[] elements = ArrayDeque.this.elements; |
766 |
> |
final int capacity = elements.length; |
767 |
> |
int k = remaining; |
768 |
> |
remaining = 0; |
769 |
> |
for (int i = cursor; --k >= 0; i = dec(i, capacity)) |
770 |
> |
action.accept(checkedElementAt(elements, i)); |
771 |
|
} |
772 |
|
} |
773 |
|
|