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 |
|
/** |
442 |
|
if (o != null) { |
443 |
|
final Object[] elements = this.elements; |
444 |
|
final int capacity = elements.length; |
445 |
< |
int from, end, to, leftover; |
446 |
< |
leftover = (end = (from = head) + size) |
445 |
> |
int from, end, to, todo; |
446 |
> |
todo = (end = (from = head) + size) |
447 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
448 |
< |
for (;; from = 0, to = leftover, leftover = 0) { |
448 |
> |
for (;; from = 0, to = todo, todo = 0) { |
449 |
|
for (int i = from; i < to; i++) |
450 |
|
if (o.equals(elements[i])) { |
451 |
|
delete(i); |
452 |
|
return true; |
453 |
|
} |
454 |
< |
if (leftover == 0) break; |
454 |
> |
if (todo == 0) break; |
455 |
|
} |
456 |
|
} |
457 |
|
return false; |
473 |
|
if (o != null) { |
474 |
|
final Object[] elements = this.elements; |
475 |
|
final int capacity = elements.length; |
476 |
< |
int from, to, end, leftover; |
477 |
< |
leftover = (to = ((end = (from = tail()) - size) >= -1) ? end : -1) - end; |
478 |
< |
for (;; from = capacity - 1, to = capacity - 1 - leftover, leftover = 0) { |
476 |
> |
int from, to, end, todo; |
477 |
> |
todo = (to = ((end = (from = tail()) - size) >= -1) ? end : -1) - end; |
478 |
> |
for (;; from = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
479 |
|
for (int i = from; i > to; i--) |
480 |
|
if (o.equals(elements[i])) { |
481 |
|
delete(i); |
482 |
|
return true; |
483 |
|
} |
484 |
< |
if (leftover == 0) break; |
484 |
> |
if (todo == 0) break; |
485 |
|
} |
486 |
|
} |
487 |
|
return false; |
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 |
|
|
856 |
|
Objects.requireNonNull(action); |
857 |
|
final Object[] elements = this.elements; |
858 |
|
final int capacity = elements.length; |
859 |
< |
int from, end, to, leftover; |
860 |
< |
leftover = (end = (from = head) + size) |
859 |
> |
int from, end, to, todo; |
860 |
> |
todo = (end = (from = head) + size) |
861 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
862 |
< |
for (;; from = 0, to = leftover, leftover = 0) { |
862 |
> |
for (;; from = 0, to = todo, todo = 0) { |
863 |
|
for (int i = from; i < to; i++) |
864 |
|
action.accept((E) elements[i]); |
865 |
< |
if (leftover == 0) break; |
865 |
> |
if (todo == 0) break; |
866 |
|
} |
867 |
|
// checkInvariants(); |
868 |
|
} |
872 |
|
* modification, for use in iterators. |
873 |
|
*/ |
874 |
|
static <E> void forEachRemaining( |
875 |
< |
Consumer<? super E> action, Object[] elements, int from, int size) { |
875 |
> |
Consumer<? super E> action, Object[] elements, int from, int remaining) { |
876 |
|
Objects.requireNonNull(action); |
877 |
|
final int capacity = elements.length; |
878 |
< |
int end, to, leftover; |
879 |
< |
leftover = (end = from + size) |
878 |
> |
int end, to, todo; |
879 |
> |
todo = (end = from + remaining) |
880 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
881 |
< |
for (;; from = 0, to = leftover, leftover = 0) { |
881 |
> |
for (;; from = 0, to = todo, todo = 0) { |
882 |
|
for (int i = from; i < to; i++) { |
883 |
|
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
884 |
|
if (e == null) |
885 |
|
throw new ConcurrentModificationException(); |
886 |
|
action.accept(e); |
887 |
|
} |
888 |
< |
if (leftover == 0) break; |
888 |
> |
if (todo == 0) break; |
889 |
|
} |
890 |
|
} |
891 |
|
|
892 |
|
static <E> void forEachRemainingDescending( |
893 |
< |
Consumer<? super E> action, Object[] elements, int from, int size) { |
893 |
> |
Consumer<? super E> action, Object[] elements, int from, int remaining) { |
894 |
|
Objects.requireNonNull(action); |
895 |
|
final int capacity = elements.length; |
896 |
< |
int end, to, leftover; |
897 |
< |
leftover = (to = ((end = from - size) >= -1) ? end : -1) - end; |
898 |
< |
for (;; from = capacity - 1, to = capacity - 1 - leftover, leftover = 0) { |
896 |
> |
int end, to, todo; |
897 |
> |
todo = (to = ((end = from - remaining) >= -1) ? end : -1) - end; |
898 |
> |
for (;; from = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
899 |
|
for (int i = from; i > to; i--) { |
900 |
|
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
901 |
|
if (e == null) |
902 |
|
throw new ConcurrentModificationException(); |
903 |
|
action.accept(e); |
904 |
|
} |
905 |
< |
if (leftover == 0) break; |
905 |
> |
if (todo == 0) break; |
906 |
|
} |
907 |
|
} |
908 |
|
|
917 |
|
Objects.requireNonNull(operator); |
918 |
|
final Object[] elements = this.elements; |
919 |
|
final int capacity = elements.length; |
920 |
< |
int from, end, to, leftover; |
921 |
< |
leftover = (end = (from = head) + size) |
920 |
> |
int from, end, to, todo; |
921 |
> |
todo = (end = (from = head) + size) |
922 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
923 |
< |
for (;; from = 0, to = leftover, leftover = 0) { |
923 |
> |
for (;; from = 0, to = todo, todo = 0) { |
924 |
|
for (int i = from; i < to; i++) |
925 |
|
elements[i] = operator.apply(elementAt(i)); |
926 |
< |
if (leftover == 0) break; |
926 |
> |
if (todo == 0) break; |
927 |
|
} |
928 |
|
// checkInvariants(); |
929 |
|
} |
998 |
|
if (o != null) { |
999 |
|
final Object[] elements = this.elements; |
1000 |
|
final int capacity = elements.length; |
1001 |
< |
int from, end, to, leftover; |
1002 |
< |
leftover = (end = (from = head) + size) |
1001 |
> |
int from, end, to, todo; |
1002 |
> |
todo = (end = (from = head) + size) |
1003 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
1004 |
< |
for (;; from = 0, to = leftover, leftover = 0) { |
1004 |
> |
for (;; from = 0, to = todo, todo = 0) { |
1005 |
|
for (int i = from; i < to; i++) |
1006 |
|
if (o.equals(elements[i])) |
1007 |
|
return true; |
1008 |
< |
if (leftover == 0) break; |
1008 |
> |
if (todo == 0) break; |
1009 |
|
} |
1010 |
|
} |
1011 |
|
return false; |
1039 |
|
} |
1040 |
|
|
1041 |
|
/** |
1042 |
< |
* Nulls out size elements, starting at head. |
1042 |
> |
* Nulls out count elements, starting at array index from. |
1043 |
|
*/ |
1044 |
< |
private static void clearSlice(Object[] elements, int head, int size) { |
1045 |
< |
final int capacity = elements.length, end = head + size; |
1044 |
> |
private static void clearSlice(Object[] elements, int from, int count) { |
1045 |
> |
final int capacity = elements.length, end = from + count; |
1046 |
|
final int leg = (capacity - end >= 0) ? end : capacity; |
1047 |
< |
Arrays.fill(elements, head, leg, null); |
1047 |
> |
Arrays.fill(elements, from, leg, null); |
1048 |
|
if (leg != end) |
1049 |
|
Arrays.fill(elements, 0, end - capacity, null); |
1050 |
|
} |
1175 |
|
// Write out elements in order. |
1176 |
|
final Object[] elements = this.elements; |
1177 |
|
final int capacity = elements.length; |
1178 |
< |
int from, end, to, leftover; |
1179 |
< |
leftover = (end = (from = head) + size) |
1178 |
> |
int from, end, to, todo; |
1179 |
> |
todo = (end = (from = head) + size) |
1180 |
|
- (to = (capacity - end >= 0) ? end : capacity); |
1181 |
< |
for (;; from = 0, to = leftover, leftover = 0) { |
1181 |
> |
for (;; from = 0, to = todo, todo = 0) { |
1182 |
|
for (int i = from; i < to; i++) |
1183 |
|
s.writeObject(elements[i]); |
1184 |
< |
if (leftover == 0) break; |
1184 |
> |
if (todo == 0) break; |
1185 |
|
} |
1186 |
|
} |
1187 |
|
|