9 |
|
import java.util.function.Consumer; |
10 |
|
import java.util.function.Predicate; |
11 |
|
import java.util.function.UnaryOperator; |
12 |
+ |
import jdk.internal.misc.SharedSecrets; |
13 |
|
|
14 |
|
/** |
15 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
51 |
|
* Iterator} interfaces. |
52 |
|
* |
53 |
|
* <p>This class is a member of the |
54 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
54 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
55 |
|
* Java Collections Framework</a>. |
56 |
|
* |
57 |
|
* @author Josh Bloch and Doug Lea |
118 |
|
if (jump < needed |
119 |
|
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0) |
120 |
|
newCapacity = newCapacity(needed, jump); |
121 |
< |
elements = Arrays.copyOf(elements, newCapacity); |
121 |
> |
final Object[] es = elements = Arrays.copyOf(elements, newCapacity); |
122 |
|
// Exceptionally, here tail == head needs to be disambiguated |
123 |
< |
if (tail < head || (tail == head && elements[head] != null)) { |
123 |
> |
if (tail < head || (tail == head && es[head] != null)) { |
124 |
|
// wrap around; slide first leg forward to end of array |
125 |
|
int newSpace = newCapacity - oldCapacity; |
126 |
< |
System.arraycopy(elements, head, |
127 |
< |
elements, head + newSpace, |
126 |
> |
System.arraycopy(es, head, |
127 |
> |
es, head + newSpace, |
128 |
|
oldCapacity - head); |
129 |
< |
Arrays.fill(elements, head, head + newSpace, null); |
130 |
< |
head += newSpace; |
129 |
> |
for (int i = head, to = (head += newSpace); i < to; i++) |
130 |
> |
es[i] = null; |
131 |
|
} |
132 |
|
// checkInvariants(); |
133 |
|
} |
181 |
|
* sufficient to hold 16 elements. |
182 |
|
*/ |
183 |
|
public ArrayDeque() { |
184 |
< |
elements = new Object[16]; |
184 |
> |
elements = new Object[16 + 1]; |
185 |
|
} |
186 |
|
|
187 |
|
/** |
209 |
|
*/ |
210 |
|
public ArrayDeque(Collection<? extends E> c) { |
211 |
|
this(c.size()); |
212 |
< |
addAll(c); |
212 |
> |
copyElements(c); |
213 |
|
} |
214 |
|
|
215 |
|
/** |
216 |
< |
* Increments i, mod modulus. |
216 |
> |
* Circularly increments i, mod modulus. |
217 |
|
* Precondition and postcondition: 0 <= i < modulus. |
218 |
|
*/ |
219 |
|
static final int inc(int i, int modulus) { |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* Decrements i, mod modulus. |
225 |
> |
* Circularly decrements i, mod modulus. |
226 |
|
* Precondition and postcondition: 0 <= i < modulus. |
227 |
|
*/ |
228 |
|
static final int dec(int i, int modulus) { |
235 |
|
* Precondition: 0 <= i < modulus, 0 <= distance <= modulus. |
236 |
|
* @return index 0 <= i < modulus |
237 |
|
*/ |
238 |
< |
static final int add(int i, int distance, int modulus) { |
238 |
> |
static final int inc(int i, int distance, int modulus) { |
239 |
|
if ((i += distance) - modulus >= 0) i -= modulus; |
240 |
|
return i; |
241 |
|
} |
245 |
|
* Index i must be logically ahead of index j. |
246 |
|
* Precondition: 0 <= i < modulus, 0 <= j < modulus. |
247 |
|
* @return the "circular distance" from j to i; corner case i == j |
248 |
< |
* is diambiguated to "empty", returning 0. |
248 |
> |
* is disambiguated to "empty", returning 0. |
249 |
|
*/ |
250 |
|
static final int sub(int i, int j, int modulus) { |
251 |
|
if ((i -= j) < 0) i += modulus; |
314 |
|
/** |
315 |
|
* Adds all of the elements in the specified collection at the end |
316 |
|
* of this deque, as if by calling {@link #addLast} on each one, |
317 |
< |
* in the order that they are returned by the collection's |
317 |
< |
* iterator. |
317 |
> |
* in the order that they are returned by the collection's iterator. |
318 |
|
* |
319 |
|
* @param c the elements to be inserted into this deque |
320 |
|
* @return {@code true} if this deque changed as a result of the call |
325 |
|
final int s, needed; |
326 |
|
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0) |
327 |
|
grow(needed); |
328 |
< |
c.forEach(this::addLast); |
328 |
> |
copyElements(c); |
329 |
|
// checkInvariants(); |
330 |
|
return size() > s; |
331 |
|
} |
332 |
|
|
333 |
+ |
private void copyElements(Collection<? extends E> c) { |
334 |
+ |
c.forEach(this::addLast); |
335 |
+ |
} |
336 |
+ |
|
337 |
|
/** |
338 |
|
* Inserts the specified element at the front of this deque. |
339 |
|
* |
524 |
|
/** |
525 |
|
* Retrieves and removes the head of the queue represented by this deque. |
526 |
|
* |
527 |
< |
* This method differs from {@link #poll poll} only in that it throws an |
528 |
< |
* exception if this deque is empty. |
527 |
> |
* This method differs from {@link #poll() poll()} only in that it |
528 |
> |
* throws an exception if this deque is empty. |
529 |
|
* |
530 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
531 |
|
* |
845 |
|
final int i, n; |
846 |
|
return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0) |
847 |
|
? null |
848 |
< |
: new DeqSpliterator(i, cursor = add(i, n, es.length)); |
848 |
> |
: new DeqSpliterator(i, cursor = inc(i, n, es.length)); |
849 |
|
} |
850 |
|
|
851 |
|
public void forEachRemaining(Consumer<? super E> action) { |
892 |
|
} |
893 |
|
} |
894 |
|
|
895 |
+ |
/** |
896 |
+ |
* @throws NullPointerException {@inheritDoc} |
897 |
+ |
*/ |
898 |
|
public void forEach(Consumer<? super E> action) { |
899 |
|
Objects.requireNonNull(action); |
900 |
|
final Object[] es = elements; |
1083 |
|
|
1084 |
|
/** |
1085 |
|
* Nulls out slots starting at array index i, upto index end. |
1086 |
+ |
* Condition i == end means "empty" - nothing to do. |
1087 |
|
*/ |
1088 |
|
private static void circularClear(Object[] es, int i, int end) { |
1089 |
+ |
// assert 0 <= i && i < es.length; |
1090 |
+ |
// assert 0 <= end && end < es.length; |
1091 |
|
for (int to = (i <= end) ? end : es.length; |
1092 |
|
; i = 0, to = end) { |
1093 |
< |
Arrays.fill(es, i, to, null); |
1093 |
> |
for (; i < to; i++) es[i] = null; |
1094 |
|
if (to == end) break; |
1095 |
|
} |
1096 |
|
} |
1240 |
|
|
1241 |
|
// Read in size and allocate array |
1242 |
|
int size = s.readInt(); |
1243 |
+ |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1); |
1244 |
|
elements = new Object[size + 1]; |
1245 |
|
this.tail = size; |
1246 |
|
|