117 |
|
if (jump < needed |
118 |
|
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0) |
119 |
|
newCapacity = newCapacity(needed, jump); |
120 |
< |
elements = Arrays.copyOf(elements, newCapacity); |
120 |
> |
final Object[] es = elements = Arrays.copyOf(elements, newCapacity); |
121 |
|
// Exceptionally, here tail == head needs to be disambiguated |
122 |
< |
if (tail < head || (tail == head && elements[head] != null)) { |
122 |
> |
if (tail < head || (tail == head && es[head] != null)) { |
123 |
|
// wrap around; slide first leg forward to end of array |
124 |
|
int newSpace = newCapacity - oldCapacity; |
125 |
< |
System.arraycopy(elements, head, |
126 |
< |
elements, head + newSpace, |
125 |
> |
System.arraycopy(es, head, |
126 |
> |
es, head + newSpace, |
127 |
|
oldCapacity - head); |
128 |
< |
Arrays.fill(elements, head, head + newSpace, null); |
129 |
< |
head += newSpace; |
128 |
> |
for (int i = head, to = (head += newSpace); i < to; i++) |
129 |
> |
es[i] = null; |
130 |
|
} |
131 |
|
// checkInvariants(); |
132 |
|
} |
235 |
|
* @return index 0 <= i < modulus |
236 |
|
*/ |
237 |
|
static final int add(int i, int distance, int modulus) { |
238 |
< |
if ((i += distance) - modulus >= 0) distance -= modulus; |
238 |
> |
if ((i += distance) - modulus >= 0) i -= modulus; |
239 |
|
return i; |
240 |
|
} |
241 |
|
|
710 |
|
throw new NoSuchElementException(); |
711 |
|
final Object[] es = elements; |
712 |
|
E e = nonNullElementAt(es, cursor); |
713 |
< |
lastRet = cursor; |
714 |
< |
cursor = inc(cursor, es.length); |
713 |
> |
cursor = inc(lastRet = cursor, es.length); |
714 |
|
remaining--; |
715 |
|
return e; |
716 |
|
} |
758 |
|
throw new NoSuchElementException(); |
759 |
|
final Object[] es = elements; |
760 |
|
E e = nonNullElementAt(es, cursor); |
761 |
< |
lastRet = cursor; |
763 |
< |
cursor = dec(cursor, es.length); |
761 |
> |
cursor = dec(lastRet = cursor, es.length); |
762 |
|
remaining--; |
763 |
|
return e; |
764 |
|
} |
820 |
|
|
821 |
|
/** Constructs spliterator over the given range. */ |
822 |
|
DeqSpliterator(int origin, int fence) { |
823 |
+ |
// assert 0 <= origin && origin < elements.length; |
824 |
+ |
// assert 0 <= fence && fence < elements.length; |
825 |
|
this.cursor = origin; |
826 |
|
this.fence = fence; |
827 |
|
} |
864 |
|
} |
865 |
|
|
866 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
867 |
< |
if (action == null) |
868 |
< |
throw new NullPointerException(); |
869 |
< |
final int t, i; |
870 |
< |
if ((t = getFence()) == (i = cursor)) |
871 |
< |
return false; |
867 |
> |
Objects.requireNonNull(action); |
868 |
|
final Object[] es = elements; |
869 |
+ |
if (fence < 0) { fence = tail; cursor = head; } // late-binding |
870 |
+ |
final int i; |
871 |
+ |
if ((i = cursor) == fence) |
872 |
+ |
return false; |
873 |
+ |
E e = nonNullElementAt(es, i); |
874 |
|
cursor = inc(i, es.length); |
875 |
< |
action.accept(nonNullElementAt(es, i)); |
875 |
> |
action.accept(e); |
876 |
|
return true; |
877 |
|
} |
878 |
|
|
1076 |
|
|
1077 |
|
/** |
1078 |
|
* Nulls out slots starting at array index i, upto index end. |
1079 |
+ |
* Condition i == end means "empty" - nothing to do. |
1080 |
|
*/ |
1081 |
|
private static void circularClear(Object[] es, int i, int end) { |
1082 |
+ |
// assert 0 <= i && i < es.length; |
1083 |
+ |
// assert 0 <= end && end < es.length; |
1084 |
|
for (int to = (i <= end) ? end : es.length; |
1085 |
|
; i = 0, to = end) { |
1086 |
< |
Arrays.fill(es, i, to, null); |
1086 |
> |
for (; i < to; i++) es[i] = null; |
1087 |
|
if (to == end) break; |
1088 |
|
} |
1089 |
|
} |
1110 |
|
final T[] a; |
1111 |
|
final int head = this.head, tail = this.tail, end; |
1112 |
|
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1113 |
+ |
// Uses null extension feature of copyOfRange |
1114 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1115 |
|
} else { |
1116 |
|
// integer overflow! |
1247 |
|
// head == tail disambiguates to "empty". |
1248 |
|
try { |
1249 |
|
int capacity = elements.length; |
1250 |
< |
// assert head >= 0 && head < capacity; |
1251 |
< |
// assert tail >= 0 && tail < capacity; |
1250 |
> |
// assert 0 <= head && head < capacity; |
1251 |
> |
// assert 0 <= tail && tail < capacity; |
1252 |
|
// assert capacity > 0; |
1253 |
|
// assert size() < capacity; |
1254 |
|
// assert head == tail || elements[head] != null; |