93 |
|
private void grow(int needed) { |
94 |
|
// overflow-conscious code |
95 |
|
// checkInvariants(); |
96 |
< |
int oldCapacity = elements.length; |
96 |
> |
final int oldCapacity = elements.length; |
97 |
|
int newCapacity; |
98 |
|
// Double size if small; else grow by 50% |
99 |
|
int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1); |
115 |
|
|
116 |
|
/** Capacity calculation for edge conditions, especially overflow. */ |
117 |
|
private int newCapacity(int needed, int jump) { |
118 |
< |
int oldCapacity = elements.length; |
119 |
< |
int minCapacity; |
118 |
> |
final int oldCapacity = elements.length, minCapacity; |
119 |
|
if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) { |
120 |
|
if (minCapacity < 0) |
121 |
|
throw new IllegalStateException("Sorry, deque too big"); |
367 |
|
public E pollFirst() { |
368 |
|
// checkInvariants(); |
369 |
|
int s, h; |
370 |
< |
if ((s = size) == 0) |
370 |
> |
if ((s = size) <= 0) |
371 |
|
return null; |
372 |
|
final Object[] elements = this.elements; |
373 |
|
@SuppressWarnings("unchecked") E e = (E) elements[h = head]; |
381 |
|
public E pollLast() { |
382 |
|
// checkInvariants(); |
383 |
|
final int s, tail; |
384 |
< |
if ((s = size) == 0) |
384 |
> |
if ((s = size) <= 0) |
385 |
|
return null; |
386 |
|
final Object[] elements = this.elements; |
387 |
|
@SuppressWarnings("unchecked") |
396 |
|
*/ |
397 |
|
public E getFirst() { |
398 |
|
// checkInvariants(); |
399 |
< |
if (size == 0) throw new NoSuchElementException(); |
399 |
> |
if (size <= 0) throw new NoSuchElementException(); |
400 |
|
return elementAt(head); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
|
* @throws NoSuchElementException {@inheritDoc} |
405 |
|
*/ |
406 |
+ |
@SuppressWarnings("unchecked") |
407 |
|
public E getLast() { |
408 |
|
// checkInvariants(); |
409 |
< |
if (size == 0) throw new NoSuchElementException(); |
410 |
< |
return elementAt(tail()); |
409 |
> |
final int s; |
410 |
> |
if ((s = size) <= 0) throw new NoSuchElementException(); |
411 |
> |
final Object[] elements = this.elements; |
412 |
> |
return (E) elements[add(head, s - 1, elements.length)]; |
413 |
|
} |
414 |
|
|
415 |
|
public E peekFirst() { |
416 |
|
// checkInvariants(); |
417 |
< |
return (size == 0) ? null : elementAt(head); |
417 |
> |
return (size <= 0) ? null : elementAt(head); |
418 |
|
} |
419 |
|
|
420 |
+ |
@SuppressWarnings("unchecked") |
421 |
|
public E peekLast() { |
422 |
|
// checkInvariants(); |
423 |
< |
return (size == 0) ? null : elementAt(tail()); |
423 |
> |
final int s; |
424 |
> |
if ((s = size) <= 0) return null; |
425 |
> |
final Object[] elements = this.elements; |
426 |
> |
return (E) elements[add(head, s - 1, elements.length)]; |
427 |
|
} |
428 |
|
|
429 |
|
/** |
707 |
|
} |
708 |
|
|
709 |
|
public E next() { |
710 |
< |
if (remaining == 0) |
710 |
> |
if (remaining <= 0) |
711 |
|
throw new NoSuchElementException(); |
712 |
|
final Object[] elements = ArrayDeque.this.elements; |
713 |
|
E e = checkedElementAt(elements, cursor); |
730 |
|
} |
731 |
|
|
732 |
|
public void forEachRemaining(Consumer<? super E> action) { |
733 |
< |
int k; |
733 |
> |
final int k; |
734 |
|
if ((k = remaining) > 0) { |
735 |
|
remaining = 0; |
736 |
|
ArrayDeque.forEachRemaining(action, elements, cursor, k); |
744 |
|
DescendingIterator() { cursor = tail(); } |
745 |
|
|
746 |
|
public final E next() { |
747 |
< |
if (remaining == 0) |
747 |
> |
if (remaining <= 0) |
748 |
|
throw new NoSuchElementException(); |
749 |
|
final Object[] elements = ArrayDeque.this.elements; |
750 |
|
E e = checkedElementAt(elements, cursor); |
760 |
|
} |
761 |
|
|
762 |
|
public final void forEachRemaining(Consumer<? super E> action) { |
763 |
< |
int k; |
763 |
> |
final int k; |
764 |
|
if ((k = remaining) > 0) { |
765 |
|
remaining = 0; |
766 |
|
forEachRemainingDescending(action, elements, cursor, k); |
823 |
|
} |
824 |
|
|
825 |
|
public void forEachRemaining(Consumer<? super E> action) { |
826 |
< |
int k = remaining(); // side effect! |
826 |
> |
final int k = remaining(); // side effect! |
827 |
|
remaining = 0; |
828 |
|
ArrayDeque.forEachRemaining(action, elements, cursor, k); |
829 |
|
} |
830 |
|
|
831 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
832 |
|
Objects.requireNonNull(action); |
833 |
< |
if (remaining() == 0) |
833 |
> |
final int k; |
834 |
> |
if ((k = remaining()) <= 0) |
835 |
|
return false; |
836 |
|
action.accept(checkedElementAt(elements, cursor)); |
837 |
|
if (++cursor >= elements.length) cursor = 0; |
838 |
< |
remaining--; |
838 |
> |
remaining = k - 1; |
839 |
|
return true; |
840 |
|
} |
841 |
|
|