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 |
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; |
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 |
|
* |
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) { |
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 |
|
|