50 |
|
* Iterator} interfaces. |
51 |
|
* |
52 |
|
* <p>This class is a member of the |
53 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
53 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
54 |
|
* Java Collections Framework</a>. |
55 |
|
* |
56 |
|
* @author Josh Bloch and Doug Lea |
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 |
|
|
313 |
|
/** |
314 |
|
* Adds all of the elements in the specified collection at the end |
315 |
|
* of this deque, as if by calling {@link #addLast} on each one, |
316 |
< |
* in the order that they are returned by the collection's |
317 |
< |
* iterator. |
316 |
> |
* in the order that they are returned by the collection's iterator. |
317 |
|
* |
318 |
|
* @param c the elements to be inserted into this deque |
319 |
|
* @return {@code true} if this deque changed as a result of the call |
519 |
|
/** |
520 |
|
* Retrieves and removes the head of the queue represented by this deque. |
521 |
|
* |
522 |
< |
* This method differs from {@link #poll poll} only in that it throws an |
523 |
< |
* exception if this deque is empty. |
522 |
> |
* This method differs from {@link #poll() poll()} only in that it |
523 |
> |
* throws an exception if this deque is empty. |
524 |
|
* |
525 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
526 |
|
* |
709 |
|
throw new NoSuchElementException(); |
710 |
|
final Object[] es = elements; |
711 |
|
E e = nonNullElementAt(es, cursor); |
712 |
< |
lastRet = cursor; |
714 |
< |
cursor = inc(cursor, es.length); |
712 |
> |
cursor = inc(lastRet = cursor, es.length); |
713 |
|
remaining--; |
714 |
|
return e; |
715 |
|
} |
757 |
|
throw new NoSuchElementException(); |
758 |
|
final Object[] es = elements; |
759 |
|
E e = nonNullElementAt(es, cursor); |
760 |
< |
lastRet = cursor; |
763 |
< |
cursor = dec(cursor, es.length); |
760 |
> |
cursor = dec(lastRet = cursor, es.length); |
761 |
|
remaining--; |
762 |
|
return e; |
763 |
|
} |
819 |
|
|
820 |
|
/** Constructs spliterator over the given range. */ |
821 |
|
DeqSpliterator(int origin, int fence) { |
822 |
+ |
// assert 0 <= origin && origin < elements.length; |
823 |
+ |
// assert 0 <= fence && fence < elements.length; |
824 |
|
this.cursor = origin; |
825 |
|
this.fence = fence; |
826 |
|
} |
863 |
|
} |
864 |
|
|
865 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
866 |
< |
if (action == null) |
868 |
< |
throw new NullPointerException(); |
869 |
< |
final int t, i; |
870 |
< |
if ((t = getFence()) == (i = cursor)) |
871 |
< |
return false; |
866 |
> |
Objects.requireNonNull(action); |
867 |
|
final Object[] es = elements; |
868 |
+ |
if (fence < 0) { fence = tail; cursor = head; } // late-binding |
869 |
+ |
final int i; |
870 |
+ |
if ((i = cursor) == fence) |
871 |
+ |
return false; |
872 |
+ |
E e = nonNullElementAt(es, i); |
873 |
|
cursor = inc(i, es.length); |
874 |
< |
action.accept(nonNullElementAt(es, i)); |
874 |
> |
action.accept(e); |
875 |
|
return true; |
876 |
|
} |
877 |
|
|
887 |
|
} |
888 |
|
} |
889 |
|
|
890 |
+ |
/** |
891 |
+ |
* @throws NullPointerException {@inheritDoc} |
892 |
+ |
*/ |
893 |
|
public void forEach(Consumer<? super E> action) { |
894 |
|
Objects.requireNonNull(action); |
895 |
|
final Object[] es = elements; |
1078 |
|
|
1079 |
|
/** |
1080 |
|
* Nulls out slots starting at array index i, upto index end. |
1081 |
+ |
* Condition i == end means "empty" - nothing to do. |
1082 |
|
*/ |
1083 |
|
private static void circularClear(Object[] es, int i, int end) { |
1084 |
+ |
// assert 0 <= i && i < es.length; |
1085 |
+ |
// assert 0 <= end && end < es.length; |
1086 |
|
for (int to = (i <= end) ? end : es.length; |
1087 |
|
; i = 0, to = end) { |
1088 |
< |
Arrays.fill(es, i, to, null); |
1088 |
> |
for (; i < to; i++) es[i] = null; |
1089 |
|
if (to == end) break; |
1090 |
|
} |
1091 |
|
} |
1110 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1111 |
|
final Object[] es = elements; |
1112 |
|
final T[] a; |
1113 |
< |
final int size = size(), head = this.head, end; |
1114 |
< |
final int len = Math.min(size, es.length - head); |
1115 |
< |
if ((end = head + size) >= 0) { |
1113 |
> |
final int head = this.head, tail = this.tail, end; |
1114 |
> |
if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) { |
1115 |
> |
// Uses null extension feature of copyOfRange |
1116 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1117 |
|
} else { |
1118 |
|
// integer overflow! |
1119 |
< |
a = Arrays.copyOfRange(es, 0, size, klazz); |
1120 |
< |
System.arraycopy(es, head, a, 0, len); |
1119 |
> |
a = Arrays.copyOfRange(es, 0, end - head, klazz); |
1120 |
> |
System.arraycopy(es, head, a, 0, es.length - head); |
1121 |
|
} |
1122 |
< |
if (tail < head) |
1123 |
< |
System.arraycopy(es, 0, a, len, tail); |
1122 |
> |
if (end != tail) |
1123 |
> |
System.arraycopy(es, 0, a, es.length - head, tail); |
1124 |
|
return a; |
1125 |
|
} |
1126 |
|
|
1249 |
|
// head == tail disambiguates to "empty". |
1250 |
|
try { |
1251 |
|
int capacity = elements.length; |
1252 |
< |
// assert head >= 0 && head < capacity; |
1253 |
< |
// assert tail >= 0 && tail < capacity; |
1252 |
> |
// assert 0 <= head && head < capacity; |
1253 |
> |
// assert 0 <= tail && tail < capacity; |
1254 |
|
// assert capacity > 0; |
1255 |
|
// assert size() < capacity; |
1256 |
|
// assert head == tail || elements[head] != null; |