7 |
|
|
8 |
|
import java.io.Serializable; |
9 |
|
import java.util.function.Consumer; |
10 |
< |
import java.util.stream.Stream; |
10 |
> |
import java.util.function.Predicate; |
11 |
> |
import java.util.function.UnaryOperator; |
12 |
|
|
13 |
|
/** |
14 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
54 |
|
* Java Collections Framework</a>. |
55 |
|
* |
56 |
|
* @author Josh Bloch and Doug Lea |
56 |
– |
* @since 1.6 |
57 |
|
* @param <E> the type of elements held in this deque |
58 |
+ |
* @since 1.6 |
59 |
|
*/ |
60 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
61 |
|
implements Deque<E>, Cloneable, Serializable |
62 |
|
{ |
63 |
|
/** |
64 |
|
* The array in which the elements of the deque are stored. |
65 |
< |
* The capacity of the deque is the length of this array, which is |
66 |
< |
* always a power of two. The array is never allowed to become |
66 |
< |
* full, except transiently within an addX method where it is |
67 |
< |
* resized (see doubleCapacity) immediately upon becoming full, |
68 |
< |
* thus avoiding head and tail wrapping around to equal each |
69 |
< |
* other. We also guarantee that all array cells not holding |
70 |
< |
* deque elements are always null. |
65 |
> |
* We guarantee that all array cells not holding deque elements |
66 |
> |
* are always null. |
67 |
|
*/ |
68 |
< |
transient Object[] elements; // non-private to simplify nested class access |
68 |
> |
transient Object[] elements; |
69 |
|
|
70 |
|
/** |
71 |
|
* The index of the element at the head of the deque (which is the |
72 |
|
* element that would be removed by remove() or pop()); or an |
73 |
< |
* arbitrary number equal to tail if the deque is empty. |
73 |
> |
* arbitrary number 0 <= head < elements.length if the deque is empty. |
74 |
|
*/ |
75 |
|
transient int head; |
76 |
|
|
77 |
+ |
/** Number of elements in this collection. */ |
78 |
+ |
transient int size; |
79 |
+ |
|
80 |
|
/** |
81 |
< |
* The index at which the next element would be added to the tail |
82 |
< |
* of the deque (via addLast(E), add(E), or push(E)). |
81 |
> |
* The maximum size of array to allocate. |
82 |
> |
* Some VMs reserve some header words in an array. |
83 |
> |
* Attempts to allocate larger arrays may result in |
84 |
> |
* OutOfMemoryError: Requested array size exceeds VM limit |
85 |
|
*/ |
86 |
< |
transient int tail; |
86 |
> |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
87 |
|
|
88 |
|
/** |
89 |
< |
* The minimum capacity that we'll use for a newly created deque. |
90 |
< |
* Must be a power of 2. |
89 |
> |
* Increases the capacity of this deque by at least the given amount. |
90 |
> |
* |
91 |
> |
* @param needed the required minimum extra capacity; must be positive |
92 |
|
*/ |
93 |
< |
private static final int MIN_INITIAL_CAPACITY = 8; |
93 |
> |
private void grow(int needed) { |
94 |
> |
// overflow-conscious code |
95 |
> |
// checkInvariants(); |
96 |
> |
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); |
100 |
> |
if (jump < needed |
101 |
> |
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0) |
102 |
> |
newCapacity = newCapacity(needed, jump); |
103 |
> |
elements = Arrays.copyOf(elements, newCapacity); |
104 |
> |
if (oldCapacity - head < size) { |
105 |
> |
// wrap around; slide first leg forward to end of array |
106 |
> |
int newSpace = newCapacity - oldCapacity; |
107 |
> |
System.arraycopy(elements, head, |
108 |
> |
elements, head + newSpace, |
109 |
> |
oldCapacity - head); |
110 |
> |
Arrays.fill(elements, head, head + newSpace, null); |
111 |
> |
head += newSpace; |
112 |
> |
} |
113 |
> |
// checkInvariants(); |
114 |
> |
} |
115 |
|
|
116 |
< |
// ****** Array allocation and resizing utilities ****** |
116 |
> |
/** Capacity calculation for edge conditions, especially overflow. */ |
117 |
> |
private int newCapacity(int needed, int jump) { |
118 |
> |
int oldCapacity = elements.length; |
119 |
> |
int minCapacity; |
120 |
> |
if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) { |
121 |
> |
if (minCapacity < 0) |
122 |
> |
throw new IllegalStateException("Sorry, deque too big"); |
123 |
> |
return Integer.MAX_VALUE; |
124 |
> |
} |
125 |
> |
if (needed > jump) |
126 |
> |
return minCapacity; |
127 |
> |
return (oldCapacity + jump - MAX_ARRAY_SIZE < 0) |
128 |
> |
? oldCapacity + jump |
129 |
> |
: MAX_ARRAY_SIZE; |
130 |
> |
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* Allocates empty array to hold the given number of elements. |
133 |
> |
* Increases the internal storage of this collection, if necessary, |
134 |
> |
* to ensure that it can hold at least the given number of elements. |
135 |
|
* |
136 |
< |
* @param numElements the number of elements to hold |
136 |
> |
* @param minCapacity the desired minimum capacity |
137 |
> |
* @since 9 |
138 |
|
*/ |
139 |
< |
private void allocateElements(int numElements) { |
140 |
< |
int initialCapacity = MIN_INITIAL_CAPACITY; |
141 |
< |
// Find the best power of two to hold elements. |
142 |
< |
// Tests "<=" because arrays aren't kept full. |
104 |
< |
if (numElements >= initialCapacity) { |
105 |
< |
initialCapacity = numElements; |
106 |
< |
initialCapacity |= (initialCapacity >>> 1); |
107 |
< |
initialCapacity |= (initialCapacity >>> 2); |
108 |
< |
initialCapacity |= (initialCapacity >>> 4); |
109 |
< |
initialCapacity |= (initialCapacity >>> 8); |
110 |
< |
initialCapacity |= (initialCapacity >>> 16); |
111 |
< |
initialCapacity++; |
112 |
< |
|
113 |
< |
if (initialCapacity < 0) // Too many elements, must back off |
114 |
< |
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements |
115 |
< |
} |
116 |
< |
elements = new Object[initialCapacity]; |
139 |
> |
public void ensureCapacity(int minCapacity) { |
140 |
> |
if (minCapacity > elements.length) |
141 |
> |
grow(minCapacity - elements.length); |
142 |
> |
// checkInvariants(); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
< |
* Doubles the capacity of this deque. Call only when full, i.e., |
147 |
< |
* when head and tail have wrapped around to become equal. |
146 |
> |
* Minimizes the internal storage of this collection. |
147 |
> |
* |
148 |
> |
* @since 9 |
149 |
|
*/ |
150 |
< |
private void doubleCapacity() { |
151 |
< |
assert head == tail; |
152 |
< |
int p = head; |
153 |
< |
int n = elements.length; |
154 |
< |
int r = n - p; // number of elements to the right of p |
155 |
< |
int newCapacity = n << 1; |
129 |
< |
if (newCapacity < 0) |
130 |
< |
throw new IllegalStateException("Sorry, deque too big"); |
131 |
< |
Object[] a = new Object[newCapacity]; |
132 |
< |
System.arraycopy(elements, p, a, 0, r); |
133 |
< |
System.arraycopy(elements, 0, a, r, p); |
134 |
< |
elements = a; |
135 |
< |
head = 0; |
136 |
< |
tail = n; |
150 |
> |
public void trimToSize() { |
151 |
> |
if (size < elements.length) { |
152 |
> |
elements = toArray(); |
153 |
> |
head = 0; |
154 |
> |
} |
155 |
> |
// checkInvariants(); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
167 |
|
* Constructs an empty array deque with an initial capacity |
168 |
|
* sufficient to hold the specified number of elements. |
169 |
|
* |
170 |
< |
* @param numElements lower bound on initial capacity of the deque |
170 |
> |
* @param numElements lower bound on initial capacity of the deque |
171 |
|
*/ |
172 |
|
public ArrayDeque(int numElements) { |
173 |
< |
allocateElements(numElements); |
173 |
> |
elements = new Object[numElements]; |
174 |
|
} |
175 |
|
|
176 |
|
/** |
184 |
|
* @throws NullPointerException if the specified collection is null |
185 |
|
*/ |
186 |
|
public ArrayDeque(Collection<? extends E> c) { |
187 |
< |
allocateElements(c.size()); |
188 |
< |
addAll(c); |
187 |
> |
Object[] elements = c.toArray(); |
188 |
> |
// defend against c.toArray (incorrectly) not returning Object[] |
189 |
> |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
190 |
> |
if (elements.getClass() != Object[].class) |
191 |
> |
elements = Arrays.copyOf(elements, size, Object[].class); |
192 |
> |
for (Object obj : elements) |
193 |
> |
Objects.requireNonNull(obj); |
194 |
> |
size = elements.length; |
195 |
> |
this.elements = elements; |
196 |
> |
} |
197 |
> |
|
198 |
> |
/** |
199 |
> |
* Returns the array index of the last element. |
200 |
> |
* May return invalid index -1 if there are no elements. |
201 |
> |
*/ |
202 |
> |
final int tail() { |
203 |
> |
return add(head, size - 1, elements.length); |
204 |
> |
} |
205 |
> |
|
206 |
> |
/** |
207 |
> |
* Adds i and j, mod modulus. |
208 |
> |
* Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus. |
209 |
> |
*/ |
210 |
> |
static final int add(int i, int j, int modulus) { |
211 |
> |
if ((i += j) - modulus >= 0) i -= modulus; |
212 |
> |
return i; |
213 |
> |
} |
214 |
> |
|
215 |
> |
/** |
216 |
> |
* Increments i, mod modulus. |
217 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
218 |
> |
*/ |
219 |
> |
static final int inc(int i, int modulus) { |
220 |
> |
if (++i == modulus) i = 0; |
221 |
> |
return i; |
222 |
> |
} |
223 |
> |
|
224 |
> |
/** |
225 |
> |
* Decrements i, mod modulus. |
226 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
227 |
> |
*/ |
228 |
> |
static final int dec(int i, int modulus) { |
229 |
> |
if (--i < 0) i += modulus; |
230 |
> |
return i; |
231 |
> |
} |
232 |
> |
|
233 |
> |
/** |
234 |
> |
* Returns element at array index i. |
235 |
> |
*/ |
236 |
> |
@SuppressWarnings("unchecked") |
237 |
> |
final E elementAt(int i) { |
238 |
> |
return (E) elements[i]; |
239 |
> |
} |
240 |
> |
|
241 |
> |
/** |
242 |
> |
* A version of elementAt that checks for null elements. |
243 |
> |
* This check doesn't catch all possible comodifications, |
244 |
> |
* but does catch ones that corrupt traversal. |
245 |
> |
*/ |
246 |
> |
E checkedElementAt(Object[] elements, int i) { |
247 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
248 |
> |
if (e == null) |
249 |
> |
throw new ConcurrentModificationException(); |
250 |
> |
return e; |
251 |
|
} |
252 |
|
|
253 |
|
// The main insertion and extraction methods are addFirst, |
261 |
|
* @throws NullPointerException if the specified element is null |
262 |
|
*/ |
263 |
|
public void addFirst(E e) { |
264 |
< |
if (e == null) |
265 |
< |
throw new NullPointerException(); |
266 |
< |
elements[head = (head - 1) & (elements.length - 1)] = e; |
267 |
< |
if (head == tail) |
268 |
< |
doubleCapacity(); |
264 |
> |
// checkInvariants(); |
265 |
> |
Objects.requireNonNull(e); |
266 |
> |
Object[] elements; |
267 |
> |
int capacity, s = size; |
268 |
> |
while (s == (capacity = (elements = this.elements).length)) |
269 |
> |
grow(1); |
270 |
> |
elements[head = dec(head, capacity)] = e; |
271 |
> |
size = s + 1; |
272 |
|
} |
273 |
|
|
274 |
|
/** |
280 |
|
* @throws NullPointerException if the specified element is null |
281 |
|
*/ |
282 |
|
public void addLast(E e) { |
283 |
< |
if (e == null) |
284 |
< |
throw new NullPointerException(); |
285 |
< |
elements[tail] = e; |
286 |
< |
if ( (tail = (tail + 1) & (elements.length - 1)) == head) |
287 |
< |
doubleCapacity(); |
283 |
> |
// checkInvariants(); |
284 |
> |
Objects.requireNonNull(e); |
285 |
> |
Object[] elements; |
286 |
> |
int capacity, s = size; |
287 |
> |
while (s == (capacity = (elements = this.elements).length)) |
288 |
> |
grow(1); |
289 |
> |
elements[add(head, s, capacity)] = e; |
290 |
> |
size = s + 1; |
291 |
> |
} |
292 |
> |
|
293 |
> |
/** |
294 |
> |
* Adds all of the elements in the specified collection at the end |
295 |
> |
* of this deque, as if by calling {@link #addLast} on each one, |
296 |
> |
* in the order that they are returned by the collection's |
297 |
> |
* iterator. |
298 |
> |
* |
299 |
> |
* @param c the elements to be inserted into this deque |
300 |
> |
* @return {@code true} if this deque changed as a result of the call |
301 |
> |
* @throws NullPointerException if the specified collection or any |
302 |
> |
* of its elements are null |
303 |
> |
*/ |
304 |
> |
@Override |
305 |
> |
public boolean addAll(Collection<? extends E> c) { |
306 |
> |
// checkInvariants(); |
307 |
> |
Object[] a, elements; |
308 |
> |
int len, capacity, s = size; |
309 |
> |
if ((len = (a = c.toArray()).length) == 0) |
310 |
> |
return false; |
311 |
> |
while ((capacity = (elements = this.elements).length) - s < len) |
312 |
> |
grow(len - (capacity - s)); |
313 |
> |
int i = add(head, s, capacity); |
314 |
> |
for (Object x : a) { |
315 |
> |
Objects.requireNonNull(x); |
316 |
> |
elements[i] = x; |
317 |
> |
i = inc(i, capacity); |
318 |
> |
size++; |
319 |
> |
} |
320 |
> |
return true; |
321 |
|
} |
322 |
|
|
323 |
|
/** |
348 |
|
* @throws NoSuchElementException {@inheritDoc} |
349 |
|
*/ |
350 |
|
public E removeFirst() { |
351 |
+ |
// checkInvariants(); |
352 |
|
E x = pollFirst(); |
353 |
|
if (x == null) |
354 |
|
throw new NoSuchElementException(); |
359 |
|
* @throws NoSuchElementException {@inheritDoc} |
360 |
|
*/ |
361 |
|
public E removeLast() { |
362 |
+ |
// checkInvariants(); |
363 |
|
E x = pollLast(); |
364 |
|
if (x == null) |
365 |
|
throw new NoSuchElementException(); |
367 |
|
} |
368 |
|
|
369 |
|
public E pollFirst() { |
370 |
< |
int h = head; |
371 |
< |
@SuppressWarnings("unchecked") |
372 |
< |
E result = (E) elements[h]; |
254 |
< |
// Element is null if deque empty |
255 |
< |
if (result == null) |
370 |
> |
// checkInvariants(); |
371 |
> |
final int s, h; |
372 |
> |
if ((s = size) == 0) |
373 |
|
return null; |
374 |
< |
elements[h] = null; // Must null out slot |
375 |
< |
head = (h + 1) & (elements.length - 1); |
376 |
< |
return result; |
374 |
> |
final Object[] elements = this.elements; |
375 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[h = head]; |
376 |
> |
elements[h] = null; |
377 |
> |
head = inc(h, elements.length); |
378 |
> |
size = s - 1; |
379 |
> |
return e; |
380 |
|
} |
381 |
|
|
382 |
|
public E pollLast() { |
383 |
< |
int t = (tail - 1) & (elements.length - 1); |
384 |
< |
@SuppressWarnings("unchecked") |
385 |
< |
E result = (E) elements[t]; |
266 |
< |
if (result == null) |
383 |
> |
// checkInvariants(); |
384 |
> |
final int s, tail; |
385 |
> |
if ((s = size) == 0) |
386 |
|
return null; |
387 |
< |
elements[t] = null; |
388 |
< |
tail = t; |
389 |
< |
return result; |
387 |
> |
final Object[] elements = this.elements; |
388 |
> |
@SuppressWarnings("unchecked") |
389 |
> |
E e = (E) elements[tail = add(head, s - 1, elements.length)]; |
390 |
> |
elements[tail] = null; |
391 |
> |
size = s - 1; |
392 |
> |
return e; |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* @throws NoSuchElementException {@inheritDoc} |
397 |
|
*/ |
398 |
|
public E getFirst() { |
399 |
< |
@SuppressWarnings("unchecked") |
400 |
< |
E result = (E) elements[head]; |
401 |
< |
if (result == null) |
280 |
< |
throw new NoSuchElementException(); |
281 |
< |
return result; |
399 |
> |
// checkInvariants(); |
400 |
> |
if (size == 0) throw new NoSuchElementException(); |
401 |
> |
return elementAt(head); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* @throws NoSuchElementException {@inheritDoc} |
406 |
|
*/ |
407 |
|
public E getLast() { |
408 |
< |
@SuppressWarnings("unchecked") |
409 |
< |
E result = (E) elements[(tail - 1) & (elements.length - 1)]; |
410 |
< |
if (result == null) |
291 |
< |
throw new NoSuchElementException(); |
292 |
< |
return result; |
408 |
> |
// checkInvariants(); |
409 |
> |
if (size == 0) throw new NoSuchElementException(); |
410 |
> |
return elementAt(tail()); |
411 |
|
} |
412 |
|
|
295 |
– |
@SuppressWarnings("unchecked") |
413 |
|
public E peekFirst() { |
414 |
< |
// elements[head] is null if deque empty |
415 |
< |
return (E) elements[head]; |
414 |
> |
// checkInvariants(); |
415 |
> |
return (size == 0) ? null : elementAt(head); |
416 |
|
} |
417 |
|
|
301 |
– |
@SuppressWarnings("unchecked") |
418 |
|
public E peekLast() { |
419 |
< |
return (E) elements[(tail - 1) & (elements.length - 1)]; |
419 |
> |
// checkInvariants(); |
420 |
> |
return (size == 0) ? null : elementAt(tail()); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
433 |
|
* @return {@code true} if the deque contained the specified element |
434 |
|
*/ |
435 |
|
public boolean removeFirstOccurrence(Object o) { |
436 |
+ |
// checkInvariants(); |
437 |
|
if (o != null) { |
438 |
< |
int mask = elements.length - 1; |
439 |
< |
int i = head; |
440 |
< |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
441 |
< |
if (o.equals(x)) { |
438 |
> |
final Object[] elements = this.elements; |
439 |
> |
final int capacity = elements.length; |
440 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) { |
441 |
> |
if (o.equals(elements[i])) { |
442 |
|
delete(i); |
443 |
|
return true; |
444 |
|
} |
461 |
|
*/ |
462 |
|
public boolean removeLastOccurrence(Object o) { |
463 |
|
if (o != null) { |
464 |
< |
int mask = elements.length - 1; |
465 |
< |
int i = (tail - 1) & mask; |
466 |
< |
for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) { |
467 |
< |
if (o.equals(x)) { |
464 |
> |
final Object[] elements = this.elements; |
465 |
> |
final int capacity = elements.length; |
466 |
> |
for (int k = size, i = add(head, k - 1, capacity); |
467 |
> |
--k >= 0; i = dec(i, capacity)) { |
468 |
> |
if (o.equals(elements[i])) { |
469 |
|
delete(i); |
470 |
|
return true; |
471 |
|
} |
588 |
|
return removeFirst(); |
589 |
|
} |
590 |
|
|
472 |
– |
private void checkInvariants() { |
473 |
– |
assert elements[tail] == null; |
474 |
– |
assert head == tail ? elements[head] == null : |
475 |
– |
(elements[head] != null && |
476 |
– |
elements[(tail - 1) & (elements.length - 1)] != null); |
477 |
– |
assert elements[(head - 1) & (elements.length - 1)] == null; |
478 |
– |
} |
479 |
– |
|
591 |
|
/** |
592 |
< |
* Removes the element at the specified position in the elements array, |
593 |
< |
* adjusting head and tail as necessary. This can result in motion of |
594 |
< |
* elements backwards or forwards in the array. |
592 |
> |
* Removes the element at the specified position in the elements array. |
593 |
> |
* This can result in forward or backwards motion of array elements. |
594 |
> |
* We optimize for least element motion. |
595 |
|
* |
596 |
|
* <p>This method is called delete rather than remove to emphasize |
597 |
|
* that its semantics differ from those of {@link List#remove(int)}. |
598 |
|
* |
599 |
|
* @return true if elements moved backwards |
600 |
|
*/ |
601 |
< |
private boolean delete(int i) { |
602 |
< |
checkInvariants(); |
601 |
> |
boolean delete(int i) { |
602 |
> |
// checkInvariants(); |
603 |
|
final Object[] elements = this.elements; |
604 |
< |
final int mask = elements.length - 1; |
604 |
> |
final int capacity = elements.length; |
605 |
|
final int h = head; |
606 |
< |
final int t = tail; |
607 |
< |
final int front = (i - h) & mask; |
608 |
< |
final int back = (t - i) & mask; |
498 |
< |
|
499 |
< |
// Invariant: head <= i < tail mod circularity |
500 |
< |
if (front >= ((t - h) & mask)) |
501 |
< |
throw new ConcurrentModificationException(); |
502 |
< |
|
503 |
< |
// Optimize for least element motion |
606 |
> |
int front; // number of elements before to-be-deleted elt |
607 |
> |
if ((front = i - h) < 0) front += capacity; |
608 |
> |
final int back = size - front - 1; // number of elements after |
609 |
|
if (front < back) { |
610 |
+ |
// move front elements forwards |
611 |
|
if (h <= i) { |
612 |
|
System.arraycopy(elements, h, elements, h + 1, front); |
613 |
|
} else { // Wrap around |
614 |
|
System.arraycopy(elements, 0, elements, 1, i); |
615 |
< |
elements[0] = elements[mask]; |
616 |
< |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
615 |
> |
elements[0] = elements[capacity - 1]; |
616 |
> |
System.arraycopy(elements, h, elements, h + 1, front - (i + 1)); |
617 |
|
} |
618 |
|
elements[h] = null; |
619 |
< |
head = (h + 1) & mask; |
619 |
> |
head = inc(h, capacity); |
620 |
> |
size--; |
621 |
> |
// checkInvariants(); |
622 |
|
return false; |
623 |
|
} else { |
624 |
< |
if (i < t) { // Copy the null tail as well |
624 |
> |
// move back elements backwards |
625 |
> |
int tail = tail(); |
626 |
> |
if (i <= tail) { |
627 |
|
System.arraycopy(elements, i + 1, elements, i, back); |
518 |
– |
tail = t - 1; |
628 |
|
} else { // Wrap around |
629 |
< |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
630 |
< |
elements[mask] = elements[0]; |
631 |
< |
System.arraycopy(elements, 1, elements, 0, t); |
632 |
< |
tail = (t - 1) & mask; |
629 |
> |
int firstLeg = capacity - (i + 1); |
630 |
> |
System.arraycopy(elements, i + 1, elements, i, firstLeg); |
631 |
> |
elements[capacity - 1] = elements[0]; |
632 |
> |
System.arraycopy(elements, 1, elements, 0, back - firstLeg - 1); |
633 |
|
} |
634 |
+ |
elements[tail] = null; |
635 |
+ |
size--; |
636 |
+ |
// checkInvariants(); |
637 |
|
return true; |
638 |
|
} |
639 |
|
} |
646 |
|
* @return the number of elements in this deque |
647 |
|
*/ |
648 |
|
public int size() { |
649 |
< |
return (tail - head) & (elements.length - 1); |
649 |
> |
return size; |
650 |
|
} |
651 |
|
|
652 |
|
/** |
655 |
|
* @return {@code true} if this deque contains no elements |
656 |
|
*/ |
657 |
|
public boolean isEmpty() { |
658 |
< |
return head == tail; |
658 |
> |
return size == 0; |
659 |
|
} |
660 |
|
|
661 |
|
/** |
675 |
|
} |
676 |
|
|
677 |
|
private class DeqIterator implements Iterator<E> { |
678 |
< |
/** |
679 |
< |
* Index of element to be returned by subsequent call to next. |
568 |
< |
*/ |
569 |
< |
private int cursor = head; |
678 |
> |
/** Index of element to be returned by subsequent call to next. */ |
679 |
> |
int cursor; |
680 |
|
|
681 |
< |
/** |
682 |
< |
* Tail recorded at construction (also in remove), to stop |
573 |
< |
* iterator and also to check for comodification. |
574 |
< |
*/ |
575 |
< |
private int fence = tail; |
681 |
> |
/** Number of elements yet to be returned. */ |
682 |
> |
int remaining = size; |
683 |
|
|
684 |
|
/** |
685 |
|
* Index of element returned by most recent call to next. |
686 |
|
* Reset to -1 if element is deleted by a call to remove. |
687 |
|
*/ |
688 |
< |
private int lastRet = -1; |
688 |
> |
int lastRet = -1; |
689 |
> |
|
690 |
> |
DeqIterator() { cursor = head; } |
691 |
> |
|
692 |
> |
void doAdvance() { |
693 |
> |
cursor = inc(cursor, elements.length); |
694 |
> |
} |
695 |
> |
|
696 |
> |
void doRemove() { |
697 |
> |
if (delete(lastRet)) |
698 |
> |
// if left-shifted, undo advance in next() |
699 |
> |
cursor = dec(cursor, elements.length); |
700 |
> |
} |
701 |
|
|
702 |
< |
public boolean hasNext() { |
703 |
< |
return cursor != fence; |
702 |
> |
public final boolean hasNext() { |
703 |
> |
return remaining > 0; |
704 |
|
} |
705 |
|
|
706 |
< |
public E next() { |
707 |
< |
if (cursor == fence) |
706 |
> |
public final E next() { |
707 |
> |
if (remaining == 0) |
708 |
|
throw new NoSuchElementException(); |
709 |
< |
@SuppressWarnings("unchecked") |
591 |
< |
E result = (E) elements[cursor]; |
592 |
< |
// This check doesn't catch all possible comodifications, |
593 |
< |
// but does catch the ones that corrupt traversal |
594 |
< |
if (tail != fence || result == null) |
595 |
< |
throw new ConcurrentModificationException(); |
709 |
> |
E e = checkedElementAt(elements, cursor); |
710 |
|
lastRet = cursor; |
711 |
< |
cursor = (cursor + 1) & (elements.length - 1); |
712 |
< |
return result; |
711 |
> |
doAdvance(); |
712 |
> |
remaining--; |
713 |
> |
return e; |
714 |
|
} |
715 |
|
|
716 |
< |
public void remove() { |
716 |
> |
public final void remove() { |
717 |
|
if (lastRet < 0) |
718 |
|
throw new IllegalStateException(); |
719 |
< |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
605 |
< |
cursor = (cursor - 1) & (elements.length - 1); |
606 |
< |
fence = tail; |
607 |
< |
} |
719 |
> |
doRemove(); |
720 |
|
lastRet = -1; |
721 |
|
} |
722 |
+ |
|
723 |
+ |
public final void forEachRemaining(Consumer<? super E> action) { |
724 |
+ |
Objects.requireNonNull(action); |
725 |
+ |
final Object[] elements = ArrayDeque.this.elements; |
726 |
+ |
final int capacity = elements.length; |
727 |
+ |
for (; remaining > 0; remaining--) { |
728 |
+ |
action.accept(checkedElementAt(elements, cursor)); |
729 |
+ |
doAdvance(); |
730 |
+ |
} |
731 |
+ |
} |
732 |
+ |
} |
733 |
+ |
|
734 |
+ |
private class DescendingIterator extends DeqIterator { |
735 |
+ |
DescendingIterator() { cursor = tail(); } |
736 |
+ |
|
737 |
+ |
@Override void doAdvance() { |
738 |
+ |
cursor = dec(cursor, elements.length); |
739 |
+ |
} |
740 |
+ |
|
741 |
+ |
@Override void doRemove() { |
742 |
+ |
if (!delete(lastRet)) |
743 |
+ |
// if right-shifted, undo advance in next |
744 |
+ |
cursor = inc(cursor, elements.length); |
745 |
+ |
} |
746 |
|
} |
747 |
|
|
748 |
|
/** |
749 |
< |
* This class is nearly a mirror-image of DeqIterator, using tail |
750 |
< |
* instead of head for initial cursor, and head instead of tail |
751 |
< |
* for fence. |
749 |
> |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
750 |
> |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
751 |
> |
* deque. |
752 |
> |
* |
753 |
> |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
754 |
> |
* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and |
755 |
> |
* {@link Spliterator#NONNULL}. Overriding implementations should document |
756 |
> |
* the reporting of additional characteristic values. |
757 |
> |
* |
758 |
> |
* @return a {@code Spliterator} over the elements in this deque |
759 |
> |
* @since 1.8 |
760 |
|
*/ |
761 |
< |
private class DescendingIterator implements Iterator<E> { |
762 |
< |
private int cursor = tail; |
763 |
< |
private int fence = head; |
764 |
< |
private int lastRet = -1; |
761 |
> |
public Spliterator<E> spliterator() { |
762 |
> |
return new ArrayDequeSpliterator(head, size); |
763 |
> |
} |
764 |
> |
|
765 |
> |
final class ArrayDequeSpliterator implements Spliterator<E> { |
766 |
> |
private int cursor; |
767 |
> |
private int remaining; |
768 |
> |
|
769 |
> |
/** Creates new spliterator covering the given array slice. */ |
770 |
> |
ArrayDequeSpliterator(int cursor, int count) { |
771 |
> |
this.cursor = cursor; |
772 |
> |
this.remaining = count; |
773 |
> |
} |
774 |
> |
|
775 |
> |
public ArrayDequeSpliterator trySplit() { |
776 |
> |
if (remaining > 1) { |
777 |
> |
int mid = remaining >> 1; |
778 |
> |
int oldCursor = cursor; |
779 |
> |
cursor += mid; |
780 |
> |
if (cursor >= elements.length) cursor -= elements.length; |
781 |
> |
remaining -= mid; |
782 |
> |
return new ArrayDequeSpliterator(oldCursor, mid); |
783 |
> |
} |
784 |
> |
return null; |
785 |
> |
} |
786 |
|
|
787 |
< |
public boolean hasNext() { |
788 |
< |
return cursor != fence; |
787 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
788 |
> |
Objects.requireNonNull(action); |
789 |
> |
final Object[] elements = ArrayDeque.this.elements; |
790 |
> |
final int capacity = elements.length; |
791 |
> |
for (; remaining > 0; cursor = inc(cursor, capacity), remaining--) |
792 |
> |
action.accept(checkedElementAt(elements, cursor)); |
793 |
|
} |
794 |
|
|
795 |
< |
public E next() { |
796 |
< |
if (cursor == fence) |
797 |
< |
throw new NoSuchElementException(); |
798 |
< |
cursor = (cursor - 1) & (elements.length - 1); |
799 |
< |
@SuppressWarnings("unchecked") |
800 |
< |
E result = (E) elements[cursor]; |
801 |
< |
if (head != fence || result == null) |
802 |
< |
throw new ConcurrentModificationException(); |
634 |
< |
lastRet = cursor; |
635 |
< |
return result; |
795 |
> |
public boolean tryAdvance(Consumer<? super E> action) { |
796 |
> |
Objects.requireNonNull(action); |
797 |
> |
if (remaining <= 0) |
798 |
> |
return false; |
799 |
> |
action.accept(checkedElementAt(elements, cursor)); |
800 |
> |
cursor = inc(cursor, elements.length); |
801 |
> |
remaining--; |
802 |
> |
return true; |
803 |
|
} |
804 |
|
|
805 |
< |
public void remove() { |
806 |
< |
if (lastRet < 0) |
807 |
< |
throw new IllegalStateException(); |
808 |
< |
if (!delete(lastRet)) { |
809 |
< |
cursor = (cursor + 1) & (elements.length - 1); |
810 |
< |
fence = head; |
805 |
> |
public long estimateSize() { |
806 |
> |
return remaining; |
807 |
> |
} |
808 |
> |
|
809 |
> |
public int characteristics() { |
810 |
> |
return Spliterator.NONNULL |
811 |
> |
| Spliterator.ORDERED |
812 |
> |
| Spliterator.SIZED |
813 |
> |
| Spliterator.SUBSIZED; |
814 |
> |
} |
815 |
> |
} |
816 |
> |
|
817 |
> |
@Override |
818 |
> |
public void forEach(Consumer<? super E> action) { |
819 |
> |
// checkInvariants(); |
820 |
> |
Objects.requireNonNull(action); |
821 |
> |
final Object[] elements = this.elements; |
822 |
> |
final int capacity = elements.length; |
823 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
824 |
> |
action.accept(elementAt(i)); |
825 |
> |
// checkInvariants(); |
826 |
> |
} |
827 |
> |
|
828 |
> |
/** |
829 |
> |
* Replaces each element of this deque with the result of applying the |
830 |
> |
* operator to that element, as specified by {@link List#replaceAll}. |
831 |
> |
* |
832 |
> |
* @param operator the operator to apply to each element |
833 |
> |
* @since 9 |
834 |
> |
*/ |
835 |
> |
public void replaceAll(UnaryOperator<E> operator) { |
836 |
> |
Objects.requireNonNull(operator); |
837 |
> |
final Object[] elements = this.elements; |
838 |
> |
final int capacity = elements.length; |
839 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
840 |
> |
elements[i] = operator.apply(elementAt(i)); |
841 |
> |
// checkInvariants(); |
842 |
> |
} |
843 |
> |
|
844 |
> |
/** |
845 |
> |
* @throws NullPointerException {@inheritDoc} |
846 |
> |
*/ |
847 |
> |
@Override |
848 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
849 |
> |
Objects.requireNonNull(filter); |
850 |
> |
return bulkRemove(filter); |
851 |
> |
} |
852 |
> |
|
853 |
> |
/** |
854 |
> |
* @throws NullPointerException {@inheritDoc} |
855 |
> |
*/ |
856 |
> |
@Override |
857 |
> |
public boolean removeAll(Collection<?> c) { |
858 |
> |
Objects.requireNonNull(c); |
859 |
> |
return bulkRemove(e -> c.contains(e)); |
860 |
> |
} |
861 |
> |
|
862 |
> |
/** |
863 |
> |
* @throws NullPointerException {@inheritDoc} |
864 |
> |
*/ |
865 |
> |
@Override |
866 |
> |
public boolean retainAll(Collection<?> c) { |
867 |
> |
Objects.requireNonNull(c); |
868 |
> |
return bulkRemove(e -> !c.contains(e)); |
869 |
> |
} |
870 |
> |
|
871 |
> |
/** Implementation of bulk remove methods. */ |
872 |
> |
private boolean bulkRemove(Predicate<? super E> filter) { |
873 |
> |
// checkInvariants(); |
874 |
> |
final Object[] elements = this.elements; |
875 |
> |
final int capacity = elements.length; |
876 |
> |
int i = head, j = i, remaining = size, deleted = 0; |
877 |
> |
try { |
878 |
> |
for (; remaining > 0; remaining--, i = inc(i, capacity)) { |
879 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
880 |
> |
if (filter.test(e)) |
881 |
> |
deleted++; |
882 |
> |
else { |
883 |
> |
if (j != i) |
884 |
> |
elements[j] = e; |
885 |
> |
j = inc(j, capacity); |
886 |
> |
} |
887 |
|
} |
888 |
< |
lastRet = -1; |
888 |
> |
return deleted > 0; |
889 |
> |
} catch (Throwable ex) { |
890 |
> |
for (; remaining > 0; |
891 |
> |
remaining--, i = inc(i, capacity), j = inc(j, capacity)) |
892 |
> |
elements[j] = elements[i]; |
893 |
> |
throw ex; |
894 |
> |
} finally { |
895 |
> |
size -= deleted; |
896 |
> |
for (; --deleted >= 0; j = inc(j, capacity)) |
897 |
> |
elements[j] = null; |
898 |
> |
// checkInvariants(); |
899 |
|
} |
900 |
|
} |
901 |
|
|
909 |
|
*/ |
910 |
|
public boolean contains(Object o) { |
911 |
|
if (o != null) { |
912 |
< |
int mask = elements.length - 1; |
913 |
< |
int i = head; |
914 |
< |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
915 |
< |
if (o.equals(x)) |
912 |
> |
final Object[] elements = this.elements; |
913 |
> |
final int capacity = elements.length; |
914 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
915 |
> |
if (o.equals(elements[i])) |
916 |
|
return true; |
664 |
– |
} |
917 |
|
} |
918 |
|
return false; |
919 |
|
} |
940 |
|
* The deque will be empty after this call returns. |
941 |
|
*/ |
942 |
|
public void clear() { |
943 |
< |
int h = head; |
944 |
< |
int t = tail; |
945 |
< |
if (h != t) { // clear all cells |
946 |
< |
head = tail = 0; |
947 |
< |
int i = h; |
948 |
< |
int mask = elements.length - 1; |
949 |
< |
do { |
950 |
< |
elements[i] = null; |
951 |
< |
i = (i + 1) & mask; |
700 |
< |
} while (i != t); |
943 |
> |
final Object[] elements = this.elements; |
944 |
> |
final int capacity = elements.length; |
945 |
> |
final int h = this.head; |
946 |
> |
final int s = size; |
947 |
> |
if (capacity - h >= s) |
948 |
> |
Arrays.fill(elements, h, h + s, null); |
949 |
> |
else { |
950 |
> |
Arrays.fill(elements, h, capacity, null); |
951 |
> |
Arrays.fill(elements, 0, s - capacity + h, null); |
952 |
|
} |
953 |
+ |
size = head = 0; |
954 |
+ |
// checkInvariants(); |
955 |
|
} |
956 |
|
|
957 |
|
/** |
969 |
|
*/ |
970 |
|
public Object[] toArray() { |
971 |
|
final int head = this.head; |
972 |
< |
final int tail = this.tail; |
973 |
< |
boolean wrap = (tail < head); |
974 |
< |
int end = wrap ? tail + elements.length : tail; |
975 |
< |
Object[] a = Arrays.copyOfRange(elements, head, end); |
723 |
< |
if (wrap) |
724 |
< |
System.arraycopy(elements, 0, a, elements.length - head, tail); |
972 |
> |
final int firstLeg; |
973 |
> |
Object[] a = Arrays.copyOfRange(elements, head, head + size); |
974 |
> |
if ((firstLeg = elements.length - head) < size) |
975 |
> |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
976 |
|
return a; |
977 |
|
} |
978 |
|
|
998 |
|
* The following code can be used to dump the deque into a newly |
999 |
|
* allocated array of {@code String}: |
1000 |
|
* |
1001 |
< |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1001 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1002 |
|
* |
1003 |
|
* Note that {@code toArray(new Object[0])} is identical in function to |
1004 |
|
* {@code toArray()}. |
1014 |
|
*/ |
1015 |
|
@SuppressWarnings("unchecked") |
1016 |
|
public <T> T[] toArray(T[] a) { |
1017 |
+ |
final Object[] elements = this.elements; |
1018 |
|
final int head = this.head; |
1019 |
< |
final int tail = this.tail; |
1020 |
< |
boolean wrap = (tail < head); |
1021 |
< |
int size = (tail - head) + (wrap ? elements.length : 0); |
770 |
< |
int firstLeg = size - (wrap ? tail : 0); |
771 |
< |
int len = a.length; |
772 |
< |
if (size > len) { |
1019 |
> |
final int firstLeg; |
1020 |
> |
boolean wrap = (firstLeg = elements.length - head) < size; |
1021 |
> |
if (size > a.length) { |
1022 |
|
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
1023 |
|
a.getClass()); |
1024 |
|
} else { |
1025 |
< |
System.arraycopy(elements, head, a, 0, firstLeg); |
1026 |
< |
if (size < len) |
1025 |
> |
System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size); |
1026 |
> |
if (size < a.length) |
1027 |
|
a[size] = null; |
1028 |
|
} |
1029 |
|
if (wrap) |
1030 |
< |
System.arraycopy(elements, 0, a, firstLeg, tail); |
1030 |
> |
System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); |
1031 |
|
return a; |
1032 |
|
} |
1033 |
|
|
1065 |
|
s.defaultWriteObject(); |
1066 |
|
|
1067 |
|
// Write out size |
1068 |
< |
s.writeInt(size()); |
1068 |
> |
s.writeInt(size); |
1069 |
|
|
1070 |
|
// Write out elements in order. |
1071 |
< |
int mask = elements.length - 1; |
1072 |
< |
for (int i = head; i != tail; i = (i + 1) & mask) |
1071 |
> |
final Object[] elements = this.elements; |
1072 |
> |
final int capacity = elements.length; |
1073 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
1074 |
|
s.writeObject(elements[i]); |
1075 |
|
} |
1076 |
|
|
1086 |
|
s.defaultReadObject(); |
1087 |
|
|
1088 |
|
// Read in size and allocate array |
1089 |
< |
int size = s.readInt(); |
840 |
< |
allocateElements(size); |
841 |
< |
head = 0; |
842 |
< |
tail = size; |
1089 |
> |
elements = new Object[size = s.readInt()]; |
1090 |
|
|
1091 |
|
// Read in all elements in the proper order. |
1092 |
|
for (int i = 0; i < size; i++) |
1093 |
|
elements[i] = s.readObject(); |
1094 |
|
} |
1095 |
|
|
1096 |
< |
public Spliterator<E> spliterator() { |
1097 |
< |
return new DeqSpliterator<E>(this, -1, -1); |
1098 |
< |
} |
1099 |
< |
|
1100 |
< |
static final class DeqSpliterator<E> implements Spliterator<E> { |
1101 |
< |
private final ArrayDeque<E> deq; |
1102 |
< |
private int fence; // -1 until first use |
1103 |
< |
private int index; // current index, modified on traverse/split |
1104 |
< |
|
1105 |
< |
/** Creates new spliterator covering the given array and range */ |
1106 |
< |
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
1107 |
< |
this.deq = deq; |
1108 |
< |
this.index = origin; |
1109 |
< |
this.fence = fence; |
1110 |
< |
} |
1111 |
< |
|
1112 |
< |
private int getFence() { // force initialization |
1113 |
< |
int t; |
867 |
< |
if ((t = fence) < 0) { |
868 |
< |
t = fence = deq.tail; |
869 |
< |
index = deq.head; |
870 |
< |
} |
871 |
< |
return t; |
872 |
< |
} |
873 |
< |
|
874 |
< |
public Spliterator<E> trySplit() { |
875 |
< |
int t = getFence(), h = index, n = deq.elements.length; |
876 |
< |
if (h != t && ((h + 1) & (n - 1)) != t) { |
877 |
< |
if (h > t) |
878 |
< |
t += n; |
879 |
< |
int m = ((h + t) >>> 1) & (n - 1); |
880 |
< |
return new DeqSpliterator<>(deq, h, index = m); |
881 |
< |
} |
882 |
< |
return null; |
883 |
< |
} |
884 |
< |
|
885 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
886 |
< |
if (consumer == null) |
887 |
< |
throw new NullPointerException(); |
888 |
< |
Object[] a = deq.elements; |
889 |
< |
int m = a.length - 1, f = getFence(), i = index; |
890 |
< |
index = f; |
891 |
< |
while (i != f) { |
892 |
< |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
893 |
< |
i = (i + 1) & m; |
894 |
< |
if (e == null) |
895 |
< |
throw new ConcurrentModificationException(); |
896 |
< |
consumer.accept(e); |
897 |
< |
} |
898 |
< |
} |
899 |
< |
|
900 |
< |
public boolean tryAdvance(Consumer<? super E> consumer) { |
901 |
< |
if (consumer == null) |
902 |
< |
throw new NullPointerException(); |
903 |
< |
Object[] a = deq.elements; |
904 |
< |
int m = a.length - 1, f = getFence(), i = index; |
905 |
< |
if (i != fence) { |
906 |
< |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
907 |
< |
index = (i + 1) & m; |
908 |
< |
if (e == null) |
909 |
< |
throw new ConcurrentModificationException(); |
910 |
< |
consumer.accept(e); |
911 |
< |
return true; |
912 |
< |
} |
913 |
< |
return false; |
914 |
< |
} |
915 |
< |
|
916 |
< |
public long estimateSize() { |
917 |
< |
int n = getFence() - index; |
918 |
< |
if (n < 0) |
919 |
< |
n += deq.elements.length; |
920 |
< |
return (long) n; |
921 |
< |
} |
922 |
< |
|
923 |
< |
@Override |
924 |
< |
public int characteristics() { |
925 |
< |
return Spliterator.ORDERED | Spliterator.SIZED | |
926 |
< |
Spliterator.NONNULL | Spliterator.SUBSIZED; |
1096 |
> |
/** debugging */ |
1097 |
> |
private void checkInvariants() { |
1098 |
> |
try { |
1099 |
> |
int capacity = elements.length; |
1100 |
> |
assert size >= 0 && size <= capacity; |
1101 |
> |
assert head >= 0 && ((capacity == 0 && head == 0 && size == 0) |
1102 |
> |
|| head < capacity); |
1103 |
> |
assert size == 0 |
1104 |
> |
|| (elements[head] != null && elements[tail()] != null); |
1105 |
> |
assert size == capacity |
1106 |
> |
|| (elements[dec(head, capacity)] == null |
1107 |
> |
&& elements[inc(tail(), capacity)] == null); |
1108 |
> |
} catch (Throwable t) { |
1109 |
> |
System.err.printf("head=%d size=%d capacity=%d%n", |
1110 |
> |
head, size, elements.length); |
1111 |
> |
System.err.printf("elements=%s%n", |
1112 |
> |
Arrays.toString(elements)); |
1113 |
> |
throw t; |
1114 |
|
} |
1115 |
|
} |
1116 |
|
|