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 TBD |
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 TBD |
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 |
> |
size = elements.length; |
191 |
> |
if (elements.getClass() != Object[].class) |
192 |
> |
elements = Arrays.copyOf(elements, size, Object[].class); |
193 |
> |
for (Object obj : elements) |
194 |
> |
Objects.requireNonNull(obj); |
195 |
> |
this.elements = elements; |
196 |
> |
} |
197 |
> |
|
198 |
> |
/** |
199 |
> |
* Increments i, mod modulus. |
200 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
201 |
> |
*/ |
202 |
> |
static final int inc(int i, int modulus) { |
203 |
> |
if (++i == modulus) i = 0; |
204 |
> |
return i; |
205 |
> |
} |
206 |
> |
|
207 |
> |
/** |
208 |
> |
* Decrements i, mod modulus. |
209 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
210 |
> |
*/ |
211 |
> |
static final int dec(int i, int modulus) { |
212 |
> |
if (--i < 0) i += modulus; |
213 |
> |
return i; |
214 |
> |
} |
215 |
> |
|
216 |
> |
/** |
217 |
> |
* Adds i and j, mod modulus. |
218 |
> |
* Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus. |
219 |
> |
*/ |
220 |
> |
static final int add(int i, int j, int modulus) { |
221 |
> |
if ((i += j) - modulus >= 0) i -= modulus; |
222 |
> |
return i; |
223 |
> |
} |
224 |
> |
|
225 |
> |
/** |
226 |
> |
* Returns the array index of the last element. |
227 |
> |
* May return invalid index -1 if there are no elements. |
228 |
> |
*/ |
229 |
> |
final int tail() { |
230 |
> |
return add(head, size - 1, elements.length); |
231 |
> |
} |
232 |
> |
|
233 |
> |
/** |
234 |
> |
* Returns element at array index i. |
235 |
> |
*/ |
236 |
> |
@SuppressWarnings("unchecked") |
237 |
> |
private 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 |
> |
final Object[] elements; |
267 |
> |
final int capacity, s; |
268 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) |
269 |
> |
addFirstSlowPath(e); |
270 |
> |
else |
271 |
> |
elements[head = dec(head, capacity)] = e; |
272 |
> |
size = s + 1; |
273 |
> |
// checkInvariants(); |
274 |
> |
} |
275 |
> |
|
276 |
> |
private void addFirstSlowPath(E e) { |
277 |
> |
grow(1); |
278 |
> |
final Object[] elements = this.elements; |
279 |
> |
elements[head = dec(head, elements.length)] = e; |
280 |
|
} |
281 |
|
|
282 |
|
/** |
288 |
|
* @throws NullPointerException if the specified element is null |
289 |
|
*/ |
290 |
|
public void addLast(E e) { |
291 |
< |
if (e == null) |
292 |
< |
throw new NullPointerException(); |
293 |
< |
elements[tail] = e; |
294 |
< |
if ( (tail = (tail + 1) & (elements.length - 1)) == head) |
295 |
< |
doubleCapacity(); |
291 |
> |
// checkInvariants(); |
292 |
> |
Objects.requireNonNull(e); |
293 |
> |
final Object[] elements; |
294 |
> |
final int capacity, s; |
295 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) |
296 |
> |
addLastSlowPath(e); |
297 |
> |
else |
298 |
> |
elements[add(head, s, capacity)] = e; |
299 |
> |
size = s + 1; |
300 |
> |
// checkInvariants(); |
301 |
> |
} |
302 |
> |
|
303 |
> |
private void addLastSlowPath(E e) { |
304 |
> |
grow(1); |
305 |
> |
final Object[] elements = this.elements; |
306 |
> |
elements[add(head, size, elements.length)] = e; |
307 |
> |
} |
308 |
> |
|
309 |
> |
/** |
310 |
> |
* Adds all of the elements in the specified collection at the end |
311 |
> |
* of this deque, as if by calling {@link #addLast} on each one, |
312 |
> |
* in the order that they are returned by the collection's |
313 |
> |
* iterator. |
314 |
> |
* |
315 |
> |
* @param c the elements to be inserted into this deque |
316 |
> |
* @return {@code true} if this deque changed as a result of the call |
317 |
> |
* @throws NullPointerException if the specified collection or any |
318 |
> |
* of its elements are null |
319 |
> |
*/ |
320 |
> |
public boolean addAll(Collection<? extends E> c) { |
321 |
> |
final int s = size, needed = c.size() - (elements.length - s); |
322 |
> |
if (needed > 0) |
323 |
> |
grow(needed); |
324 |
> |
c.forEach((e) -> addLast(e)); |
325 |
> |
// checkInvariants(); |
326 |
> |
return size > s; |
327 |
|
} |
328 |
|
|
329 |
|
/** |
354 |
|
* @throws NoSuchElementException {@inheritDoc} |
355 |
|
*/ |
356 |
|
public E removeFirst() { |
357 |
< |
E x = pollFirst(); |
358 |
< |
if (x == null) |
357 |
> |
// checkInvariants(); |
358 |
> |
E e = pollFirst(); |
359 |
> |
if (e == null) |
360 |
|
throw new NoSuchElementException(); |
361 |
< |
return x; |
361 |
> |
return e; |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
|
* @throws NoSuchElementException {@inheritDoc} |
366 |
|
*/ |
367 |
|
public E removeLast() { |
368 |
< |
E x = pollLast(); |
369 |
< |
if (x == null) |
368 |
> |
// checkInvariants(); |
369 |
> |
E e = pollLast(); |
370 |
> |
if (e == null) |
371 |
|
throw new NoSuchElementException(); |
372 |
< |
return x; |
372 |
> |
return e; |
373 |
|
} |
374 |
|
|
375 |
|
public E pollFirst() { |
376 |
< |
int h = head; |
377 |
< |
@SuppressWarnings("unchecked") |
378 |
< |
E result = (E) elements[h]; |
254 |
< |
// Element is null if deque empty |
255 |
< |
if (result == null) |
376 |
> |
// checkInvariants(); |
377 |
> |
final int s, h; |
378 |
> |
if ((s = size) == 0) |
379 |
|
return null; |
380 |
< |
elements[h] = null; // Must null out slot |
381 |
< |
head = (h + 1) & (elements.length - 1); |
382 |
< |
return result; |
380 |
> |
final Object[] elements = this.elements; |
381 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[h = head]; |
382 |
> |
elements[h] = null; |
383 |
> |
head = inc(h, elements.length); |
384 |
> |
size = s - 1; |
385 |
> |
return e; |
386 |
|
} |
387 |
|
|
388 |
|
public E pollLast() { |
389 |
< |
int t = (tail - 1) & (elements.length - 1); |
390 |
< |
@SuppressWarnings("unchecked") |
391 |
< |
E result = (E) elements[t]; |
266 |
< |
if (result == null) |
389 |
> |
// checkInvariants(); |
390 |
> |
final int s, tail; |
391 |
> |
if ((s = size) == 0) |
392 |
|
return null; |
393 |
< |
elements[t] = null; |
394 |
< |
tail = t; |
395 |
< |
return result; |
393 |
> |
final Object[] elements = this.elements; |
394 |
> |
@SuppressWarnings("unchecked") |
395 |
> |
E e = (E) elements[tail = add(head, s - 1, elements.length)]; |
396 |
> |
elements[tail] = null; |
397 |
> |
size = s - 1; |
398 |
> |
return e; |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
|
* @throws NoSuchElementException {@inheritDoc} |
403 |
|
*/ |
404 |
|
public E getFirst() { |
405 |
< |
@SuppressWarnings("unchecked") |
406 |
< |
E result = (E) elements[head]; |
407 |
< |
if (result == null) |
280 |
< |
throw new NoSuchElementException(); |
281 |
< |
return result; |
405 |
> |
// checkInvariants(); |
406 |
> |
if (size == 0) throw new NoSuchElementException(); |
407 |
> |
return elementAt(head); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
|
* @throws NoSuchElementException {@inheritDoc} |
412 |
|
*/ |
413 |
|
public E getLast() { |
414 |
< |
@SuppressWarnings("unchecked") |
415 |
< |
E result = (E) elements[(tail - 1) & (elements.length - 1)]; |
416 |
< |
if (result == null) |
291 |
< |
throw new NoSuchElementException(); |
292 |
< |
return result; |
414 |
> |
// checkInvariants(); |
415 |
> |
if (size == 0) throw new NoSuchElementException(); |
416 |
> |
return elementAt(tail()); |
417 |
|
} |
418 |
|
|
295 |
– |
@SuppressWarnings("unchecked") |
419 |
|
public E peekFirst() { |
420 |
< |
// elements[head] is null if deque empty |
421 |
< |
return (E) elements[head]; |
420 |
> |
// checkInvariants(); |
421 |
> |
return (size == 0) ? null : elementAt(head); |
422 |
|
} |
423 |
|
|
301 |
– |
@SuppressWarnings("unchecked") |
424 |
|
public E peekLast() { |
425 |
< |
return (E) elements[(tail - 1) & (elements.length - 1)]; |
425 |
> |
// checkInvariants(); |
426 |
> |
return (size == 0) ? null : elementAt(tail()); |
427 |
|
} |
428 |
|
|
429 |
|
/** |
439 |
|
* @return {@code true} if the deque contained the specified element |
440 |
|
*/ |
441 |
|
public boolean removeFirstOccurrence(Object o) { |
442 |
+ |
// checkInvariants(); |
443 |
|
if (o != null) { |
444 |
< |
int mask = elements.length - 1; |
445 |
< |
int i = head; |
446 |
< |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
447 |
< |
if (o.equals(x)) { |
444 |
> |
final Object[] elements = this.elements; |
445 |
> |
final int capacity = elements.length; |
446 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) { |
447 |
> |
if (o.equals(elements[i])) { |
448 |
|
delete(i); |
449 |
|
return true; |
450 |
|
} |
467 |
|
*/ |
468 |
|
public boolean removeLastOccurrence(Object o) { |
469 |
|
if (o != null) { |
470 |
< |
int mask = elements.length - 1; |
471 |
< |
int i = (tail - 1) & mask; |
472 |
< |
for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) { |
473 |
< |
if (o.equals(x)) { |
470 |
> |
final Object[] elements = this.elements; |
471 |
> |
final int capacity = elements.length; |
472 |
> |
for (int k = size, i = add(head, k - 1, capacity); |
473 |
> |
--k >= 0; i = dec(i, capacity)) { |
474 |
> |
if (o.equals(elements[i])) { |
475 |
|
delete(i); |
476 |
|
return true; |
477 |
|
} |
594 |
|
return removeFirst(); |
595 |
|
} |
596 |
|
|
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 |
– |
|
597 |
|
/** |
598 |
< |
* Removes the element at the specified position in the elements array, |
599 |
< |
* adjusting head and tail as necessary. This can result in motion of |
600 |
< |
* elements backwards or forwards in the array. |
598 |
> |
* Removes the element at the specified position in the elements array. |
599 |
> |
* This can result in forward or backwards motion of array elements. |
600 |
> |
* We optimize for least element motion. |
601 |
|
* |
602 |
|
* <p>This method is called delete rather than remove to emphasize |
603 |
|
* that its semantics differ from those of {@link List#remove(int)}. |
604 |
|
* |
605 |
|
* @return true if elements moved backwards |
606 |
|
*/ |
607 |
< |
private boolean delete(int i) { |
608 |
< |
checkInvariants(); |
607 |
> |
boolean delete(int i) { |
608 |
> |
// checkInvariants(); |
609 |
|
final Object[] elements = this.elements; |
610 |
< |
final int mask = elements.length - 1; |
610 |
> |
final int capacity = elements.length; |
611 |
|
final int h = head; |
612 |
< |
final int t = tail; |
613 |
< |
final int front = (i - h) & mask; |
614 |
< |
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 |
612 |
> |
int front; // number of elements before to-be-deleted elt |
613 |
> |
if ((front = i - h) < 0) front += capacity; |
614 |
> |
final int back = size - front - 1; // number of elements after |
615 |
|
if (front < back) { |
616 |
+ |
// move front elements forwards |
617 |
|
if (h <= i) { |
618 |
|
System.arraycopy(elements, h, elements, h + 1, front); |
619 |
|
} else { // Wrap around |
620 |
|
System.arraycopy(elements, 0, elements, 1, i); |
621 |
< |
elements[0] = elements[mask]; |
622 |
< |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
621 |
> |
elements[0] = elements[capacity - 1]; |
622 |
> |
System.arraycopy(elements, h, elements, h + 1, front - (i + 1)); |
623 |
|
} |
624 |
|
elements[h] = null; |
625 |
< |
head = (h + 1) & mask; |
625 |
> |
head = inc(h, capacity); |
626 |
> |
size--; |
627 |
> |
// checkInvariants(); |
628 |
|
return false; |
629 |
|
} else { |
630 |
< |
if (i < t) { // Copy the null tail as well |
630 |
> |
// move back elements backwards |
631 |
> |
int tail = tail(); |
632 |
> |
if (i <= tail) { |
633 |
|
System.arraycopy(elements, i + 1, elements, i, back); |
518 |
– |
tail = t - 1; |
634 |
|
} else { // Wrap around |
635 |
< |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
636 |
< |
elements[mask] = elements[0]; |
637 |
< |
System.arraycopy(elements, 1, elements, 0, t); |
638 |
< |
tail = (t - 1) & mask; |
635 |
> |
int firstLeg = capacity - (i + 1); |
636 |
> |
System.arraycopy(elements, i + 1, elements, i, firstLeg); |
637 |
> |
elements[capacity - 1] = elements[0]; |
638 |
> |
System.arraycopy(elements, 1, elements, 0, back - firstLeg - 1); |
639 |
|
} |
640 |
+ |
elements[tail] = null; |
641 |
+ |
size--; |
642 |
+ |
// checkInvariants(); |
643 |
|
return true; |
644 |
|
} |
645 |
|
} |
652 |
|
* @return the number of elements in this deque |
653 |
|
*/ |
654 |
|
public int size() { |
655 |
< |
return (tail - head) & (elements.length - 1); |
655 |
> |
return size; |
656 |
|
} |
657 |
|
|
658 |
|
/** |
661 |
|
* @return {@code true} if this deque contains no elements |
662 |
|
*/ |
663 |
|
public boolean isEmpty() { |
664 |
< |
return head == tail; |
664 |
> |
return size == 0; |
665 |
|
} |
666 |
|
|
667 |
|
/** |
681 |
|
} |
682 |
|
|
683 |
|
private class DeqIterator implements Iterator<E> { |
684 |
< |
/** |
685 |
< |
* Index of element to be returned by subsequent call to next. |
568 |
< |
*/ |
569 |
< |
private int cursor = head; |
684 |
> |
/** Index of element to be returned by subsequent call to next. */ |
685 |
> |
int cursor; |
686 |
|
|
687 |
< |
/** |
688 |
< |
* Tail recorded at construction (also in remove), to stop |
573 |
< |
* iterator and also to check for comodification. |
574 |
< |
*/ |
575 |
< |
private int fence = tail; |
687 |
> |
/** Number of elements yet to be returned. */ |
688 |
> |
int remaining = size; |
689 |
|
|
690 |
|
/** |
691 |
|
* Index of element returned by most recent call to next. |
692 |
|
* Reset to -1 if element is deleted by a call to remove. |
693 |
|
*/ |
694 |
< |
private int lastRet = -1; |
694 |
> |
int lastRet = -1; |
695 |
> |
|
696 |
> |
DeqIterator() { cursor = head; } |
697 |
|
|
698 |
< |
public boolean hasNext() { |
699 |
< |
return cursor != fence; |
698 |
> |
public final boolean hasNext() { |
699 |
> |
return remaining > 0; |
700 |
|
} |
701 |
|
|
702 |
|
public E next() { |
703 |
< |
if (cursor == fence) |
703 |
> |
if (remaining == 0) |
704 |
|
throw new NoSuchElementException(); |
705 |
< |
@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(); |
705 |
> |
E e = checkedElementAt(elements, cursor); |
706 |
|
lastRet = cursor; |
707 |
< |
cursor = (cursor + 1) & (elements.length - 1); |
708 |
< |
return result; |
707 |
> |
cursor = inc(cursor, elements.length); |
708 |
> |
remaining--; |
709 |
> |
return e; |
710 |
|
} |
711 |
|
|
712 |
< |
public void remove() { |
712 |
> |
void postDelete(boolean leftShifted) { |
713 |
> |
if (leftShifted) |
714 |
> |
cursor = dec(cursor, elements.length); // undo inc in next |
715 |
> |
} |
716 |
> |
|
717 |
> |
public final void remove() { |
718 |
|
if (lastRet < 0) |
719 |
|
throw new IllegalStateException(); |
720 |
< |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
605 |
< |
cursor = (cursor - 1) & (elements.length - 1); |
606 |
< |
fence = tail; |
607 |
< |
} |
720 |
> |
postDelete(delete(lastRet)); |
721 |
|
lastRet = -1; |
722 |
|
} |
723 |
+ |
|
724 |
+ |
public void forEachRemaining(Consumer<? super E> action) { |
725 |
+ |
Objects.requireNonNull(action); |
726 |
+ |
final Object[] elements = ArrayDeque.this.elements; |
727 |
+ |
final int capacity = elements.length; |
728 |
+ |
int k = remaining; |
729 |
+ |
remaining = 0; |
730 |
+ |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
731 |
+ |
action.accept(checkedElementAt(elements, i)); |
732 |
+ |
} |
733 |
+ |
} |
734 |
+ |
|
735 |
+ |
private class DescendingIterator extends DeqIterator { |
736 |
+ |
DescendingIterator() { cursor = tail(); } |
737 |
+ |
|
738 |
+ |
public final E next() { |
739 |
+ |
if (remaining == 0) |
740 |
+ |
throw new NoSuchElementException(); |
741 |
+ |
E e = checkedElementAt(elements, cursor); |
742 |
+ |
lastRet = cursor; |
743 |
+ |
cursor = dec(cursor, elements.length); |
744 |
+ |
remaining--; |
745 |
+ |
return e; |
746 |
+ |
} |
747 |
+ |
|
748 |
+ |
void postDelete(boolean leftShifted) { |
749 |
+ |
if (!leftShifted) |
750 |
+ |
cursor = inc(cursor, elements.length); // undo dec in next |
751 |
+ |
} |
752 |
+ |
|
753 |
+ |
public final void forEachRemaining(Consumer<? super E> action) { |
754 |
+ |
Objects.requireNonNull(action); |
755 |
+ |
final Object[] elements = ArrayDeque.this.elements; |
756 |
+ |
final int capacity = elements.length; |
757 |
+ |
int k = remaining; |
758 |
+ |
remaining = 0; |
759 |
+ |
for (int i = cursor; --k >= 0; i = dec(i, capacity)) |
760 |
+ |
action.accept(checkedElementAt(elements, i)); |
761 |
+ |
} |
762 |
|
} |
763 |
|
|
764 |
|
/** |
765 |
< |
* This class is nearly a mirror-image of DeqIterator, using tail |
766 |
< |
* instead of head for initial cursor, and head instead of tail |
767 |
< |
* for fence. |
768 |
< |
*/ |
769 |
< |
private class DescendingIterator implements Iterator<E> { |
770 |
< |
private int cursor = tail; |
771 |
< |
private int fence = head; |
772 |
< |
private int lastRet = -1; |
765 |
> |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
766 |
> |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
767 |
> |
* deque. |
768 |
> |
* |
769 |
> |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
770 |
> |
* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and |
771 |
> |
* {@link Spliterator#NONNULL}. Overriding implementations should document |
772 |
> |
* the reporting of additional characteristic values. |
773 |
> |
* |
774 |
> |
* @return a {@code Spliterator} over the elements in this deque |
775 |
> |
* @since 1.8 |
776 |
> |
*/ |
777 |
> |
public Spliterator<E> spliterator() { |
778 |
> |
return new ArrayDequeSpliterator(); |
779 |
> |
} |
780 |
|
|
781 |
< |
public boolean hasNext() { |
782 |
< |
return cursor != fence; |
781 |
> |
final class ArrayDequeSpliterator implements Spliterator<E> { |
782 |
> |
private int cursor; |
783 |
> |
private int remaining; // -1 until late-binding first use |
784 |
> |
|
785 |
> |
/** Constructs late-binding spliterator over all elements. */ |
786 |
> |
ArrayDequeSpliterator() { |
787 |
> |
this.remaining = -1; |
788 |
|
} |
789 |
|
|
790 |
< |
public E next() { |
791 |
< |
if (cursor == fence) |
792 |
< |
throw new NoSuchElementException(); |
793 |
< |
cursor = (cursor - 1) & (elements.length - 1); |
630 |
< |
@SuppressWarnings("unchecked") |
631 |
< |
E result = (E) elements[cursor]; |
632 |
< |
if (head != fence || result == null) |
633 |
< |
throw new ConcurrentModificationException(); |
634 |
< |
lastRet = cursor; |
635 |
< |
return result; |
790 |
> |
/** Constructs spliterator over the given slice. */ |
791 |
> |
ArrayDequeSpliterator(int cursor, int count) { |
792 |
> |
this.cursor = cursor; |
793 |
> |
this.remaining = count; |
794 |
|
} |
795 |
|
|
796 |
< |
public void remove() { |
797 |
< |
if (lastRet < 0) |
798 |
< |
throw new IllegalStateException(); |
799 |
< |
if (!delete(lastRet)) { |
800 |
< |
cursor = (cursor + 1) & (elements.length - 1); |
643 |
< |
fence = head; |
796 |
> |
/** Ensures late-binding initialization; then returns remaining. */ |
797 |
> |
private int remaining() { |
798 |
> |
if (remaining < 0) { |
799 |
> |
cursor = head; |
800 |
> |
remaining = size; |
801 |
|
} |
802 |
< |
lastRet = -1; |
802 |
> |
return remaining; |
803 |
> |
} |
804 |
> |
|
805 |
> |
public ArrayDequeSpliterator trySplit() { |
806 |
> |
final int mid; |
807 |
> |
if ((mid = remaining() >> 1) > 0) { |
808 |
> |
int oldCursor = cursor; |
809 |
> |
cursor = add(cursor, mid, elements.length); |
810 |
> |
remaining -= mid; |
811 |
> |
return new ArrayDequeSpliterator(oldCursor, mid); |
812 |
> |
} |
813 |
> |
return null; |
814 |
> |
} |
815 |
> |
|
816 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
817 |
> |
Objects.requireNonNull(action); |
818 |
> |
final Object[] elements = ArrayDeque.this.elements; |
819 |
> |
final int capacity = elements.length; |
820 |
> |
int k = remaining(); |
821 |
> |
remaining = 0; |
822 |
> |
for (int i = cursor; --k >= 0; i = inc(i, capacity)) |
823 |
> |
action.accept(checkedElementAt(elements, i)); |
824 |
> |
} |
825 |
> |
|
826 |
> |
public boolean tryAdvance(Consumer<? super E> action) { |
827 |
> |
Objects.requireNonNull(action); |
828 |
> |
if (remaining() == 0) |
829 |
> |
return false; |
830 |
> |
action.accept(checkedElementAt(elements, cursor)); |
831 |
> |
cursor = inc(cursor, elements.length); |
832 |
> |
remaining--; |
833 |
> |
return true; |
834 |
> |
} |
835 |
> |
|
836 |
> |
public long estimateSize() { |
837 |
> |
return remaining(); |
838 |
> |
} |
839 |
> |
|
840 |
> |
public int characteristics() { |
841 |
> |
return Spliterator.NONNULL |
842 |
> |
| Spliterator.ORDERED |
843 |
> |
| Spliterator.SIZED |
844 |
> |
| Spliterator.SUBSIZED; |
845 |
> |
} |
846 |
> |
} |
847 |
> |
|
848 |
> |
public void forEach(Consumer<? super E> action) { |
849 |
> |
// checkInvariants(); |
850 |
> |
Objects.requireNonNull(action); |
851 |
> |
final Object[] elements = this.elements; |
852 |
> |
final int capacity = elements.length; |
853 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
854 |
> |
action.accept(elementAt(i)); |
855 |
> |
// checkInvariants(); |
856 |
> |
} |
857 |
> |
|
858 |
> |
/** |
859 |
> |
* Replaces each element of this deque with the result of applying the |
860 |
> |
* operator to that element, as specified by {@link List#replaceAll}. |
861 |
> |
* |
862 |
> |
* @param operator the operator to apply to each element |
863 |
> |
* @since TBD |
864 |
> |
*/ |
865 |
> |
/* public */ void replaceAll(UnaryOperator<E> operator) { |
866 |
> |
Objects.requireNonNull(operator); |
867 |
> |
final Object[] elements = this.elements; |
868 |
> |
final int capacity = elements.length; |
869 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
870 |
> |
elements[i] = operator.apply(elementAt(i)); |
871 |
> |
// checkInvariants(); |
872 |
> |
} |
873 |
> |
|
874 |
> |
/** |
875 |
> |
* @throws NullPointerException {@inheritDoc} |
876 |
> |
*/ |
877 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
878 |
> |
Objects.requireNonNull(filter); |
879 |
> |
return bulkRemove(filter); |
880 |
> |
} |
881 |
> |
|
882 |
> |
/** |
883 |
> |
* @throws NullPointerException {@inheritDoc} |
884 |
> |
*/ |
885 |
> |
public boolean removeAll(Collection<?> c) { |
886 |
> |
Objects.requireNonNull(c); |
887 |
> |
return bulkRemove(e -> c.contains(e)); |
888 |
> |
} |
889 |
> |
|
890 |
> |
/** |
891 |
> |
* @throws NullPointerException {@inheritDoc} |
892 |
> |
*/ |
893 |
> |
public boolean retainAll(Collection<?> c) { |
894 |
> |
Objects.requireNonNull(c); |
895 |
> |
return bulkRemove(e -> !c.contains(e)); |
896 |
> |
} |
897 |
> |
|
898 |
> |
/** Implementation of bulk remove methods. */ |
899 |
> |
private boolean bulkRemove(Predicate<? super E> filter) { |
900 |
> |
// checkInvariants(); |
901 |
> |
final Object[] elements = this.elements; |
902 |
> |
final int capacity = elements.length; |
903 |
> |
int i = head, j = i, remaining = size, deleted = 0; |
904 |
> |
try { |
905 |
> |
for (; remaining > 0; remaining--, i = inc(i, capacity)) { |
906 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
907 |
> |
if (filter.test(e)) |
908 |
> |
deleted++; |
909 |
> |
else { |
910 |
> |
if (j != i) |
911 |
> |
elements[j] = e; |
912 |
> |
j = inc(j, capacity); |
913 |
> |
} |
914 |
> |
} |
915 |
> |
return deleted > 0; |
916 |
> |
} catch (Throwable ex) { |
917 |
> |
if (deleted > 0) |
918 |
> |
for (; remaining > 0; |
919 |
> |
remaining--, i = inc(i, capacity), j = inc(j, capacity)) |
920 |
> |
elements[j] = elements[i]; |
921 |
> |
throw ex; |
922 |
> |
} finally { |
923 |
> |
size -= deleted; |
924 |
> |
for (; --deleted >= 0; j = inc(j, capacity)) |
925 |
> |
elements[j] = null; |
926 |
> |
// checkInvariants(); |
927 |
|
} |
928 |
|
} |
929 |
|
|
937 |
|
*/ |
938 |
|
public boolean contains(Object o) { |
939 |
|
if (o != null) { |
940 |
< |
int mask = elements.length - 1; |
941 |
< |
int i = head; |
942 |
< |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
943 |
< |
if (o.equals(x)) |
940 |
> |
final Object[] elements = this.elements; |
941 |
> |
final int capacity = elements.length; |
942 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
943 |
> |
if (o.equals(elements[i])) |
944 |
|
return true; |
664 |
– |
} |
945 |
|
} |
946 |
|
return false; |
947 |
|
} |
968 |
|
* The deque will be empty after this call returns. |
969 |
|
*/ |
970 |
|
public void clear() { |
971 |
< |
int h = head; |
972 |
< |
int t = tail; |
973 |
< |
if (h != t) { // clear all cells |
974 |
< |
head = tail = 0; |
975 |
< |
int i = h; |
976 |
< |
int mask = elements.length - 1; |
977 |
< |
do { |
698 |
< |
elements[i] = null; |
699 |
< |
i = (i + 1) & mask; |
700 |
< |
} while (i != t); |
971 |
> |
final Object[] elements = this.elements; |
972 |
> |
final int capacity = elements.length, tail = head + size; |
973 |
> |
if (capacity - tail >= 0) |
974 |
> |
Arrays.fill(elements, head, tail, null); |
975 |
> |
else { |
976 |
> |
Arrays.fill(elements, head, capacity, null); |
977 |
> |
Arrays.fill(elements, 0, tail - capacity, null); |
978 |
|
} |
979 |
+ |
size = head = 0; |
980 |
+ |
// checkInvariants(); |
981 |
|
} |
982 |
|
|
983 |
|
/** |
994 |
|
* @return an array containing all of the elements in this deque |
995 |
|
*/ |
996 |
|
public Object[] toArray() { |
997 |
< |
final int head = this.head; |
998 |
< |
final int tail = this.tail; |
999 |
< |
boolean wrap = (tail < head); |
1000 |
< |
int end = wrap ? tail + elements.length : tail; |
1001 |
< |
Object[] a = Arrays.copyOfRange(elements, head, end); |
1002 |
< |
if (wrap) |
1003 |
< |
System.arraycopy(elements, 0, a, elements.length - head, tail); |
997 |
> |
return toArray(Object[].class); |
998 |
> |
} |
999 |
> |
|
1000 |
> |
private <T> T[] toArray(Class<T[]> klazz) { |
1001 |
> |
final Object[] elements = this.elements; |
1002 |
> |
final int capacity = elements.length; |
1003 |
> |
final int head = this.head, tail = head + size; |
1004 |
> |
final T[] a; |
1005 |
> |
if (tail >= 0) { |
1006 |
> |
a = Arrays.copyOfRange(elements, head, tail, klazz); |
1007 |
> |
} else { |
1008 |
> |
// integer overflow! |
1009 |
> |
a = Arrays.copyOfRange(elements, 0, size, klazz); |
1010 |
> |
System.arraycopy(elements, head, a, 0, capacity - head); |
1011 |
> |
} |
1012 |
> |
if (tail - capacity > 0) |
1013 |
> |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1014 |
|
return a; |
1015 |
|
} |
1016 |
|
|
1036 |
|
* The following code can be used to dump the deque into a newly |
1037 |
|
* allocated array of {@code String}: |
1038 |
|
* |
1039 |
< |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1039 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1040 |
|
* |
1041 |
|
* Note that {@code toArray(new Object[0])} is identical in function to |
1042 |
|
* {@code toArray()}. |
1052 |
|
*/ |
1053 |
|
@SuppressWarnings("unchecked") |
1054 |
|
public <T> T[] toArray(T[] a) { |
1055 |
< |
final int head = this.head; |
1056 |
< |
final int tail = this.tail; |
1057 |
< |
boolean wrap = (tail < head); |
1058 |
< |
int size = (tail - head) + (wrap ? elements.length : 0); |
1059 |
< |
int firstLeg = size - (wrap ? tail : 0); |
1060 |
< |
int len = a.length; |
1061 |
< |
if (size > len) { |
1062 |
< |
a = (T[]) Arrays.copyOfRange(elements, head, head + size, |
1063 |
< |
a.getClass()); |
1064 |
< |
} else { |
1065 |
< |
System.arraycopy(elements, head, a, 0, firstLeg); |
777 |
< |
if (size < len) |
778 |
< |
a[size] = null; |
1055 |
> |
final int size = this.size; |
1056 |
> |
if (size > a.length) |
1057 |
> |
return toArray((Class<T[]>) a.getClass()); |
1058 |
> |
final Object[] elements = this.elements; |
1059 |
> |
final int capacity = elements.length; |
1060 |
> |
final int head = this.head, tail = head + size; |
1061 |
> |
if (capacity - tail >= 0) |
1062 |
> |
System.arraycopy(elements, head, a, 0, size); |
1063 |
> |
else { |
1064 |
> |
System.arraycopy(elements, head, a, 0, capacity - head); |
1065 |
> |
System.arraycopy(elements, 0, a, capacity - head, tail - capacity); |
1066 |
|
} |
1067 |
< |
if (wrap) |
1068 |
< |
System.arraycopy(elements, 0, a, firstLeg, tail); |
1067 |
> |
if (size < a.length) |
1068 |
> |
a[size] = null; |
1069 |
|
return a; |
1070 |
|
} |
1071 |
|
|
1103 |
|
s.defaultWriteObject(); |
1104 |
|
|
1105 |
|
// Write out size |
1106 |
< |
s.writeInt(size()); |
1106 |
> |
s.writeInt(size); |
1107 |
|
|
1108 |
|
// Write out elements in order. |
1109 |
< |
int mask = elements.length - 1; |
1110 |
< |
for (int i = head; i != tail; i = (i + 1) & mask) |
1109 |
> |
final Object[] elements = this.elements; |
1110 |
> |
final int capacity = elements.length; |
1111 |
> |
for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) |
1112 |
|
s.writeObject(elements[i]); |
1113 |
|
} |
1114 |
|
|
1124 |
|
s.defaultReadObject(); |
1125 |
|
|
1126 |
|
// Read in size and allocate array |
1127 |
< |
int size = s.readInt(); |
840 |
< |
allocateElements(size); |
841 |
< |
head = 0; |
842 |
< |
tail = size; |
1127 |
> |
elements = new Object[size = s.readInt()]; |
1128 |
|
|
1129 |
|
// Read in all elements in the proper order. |
1130 |
|
for (int i = 0; i < size; i++) |
1131 |
|
elements[i] = s.readObject(); |
1132 |
|
} |
1133 |
|
|
1134 |
< |
public Spliterator<E> spliterator() { |
1135 |
< |
return new DeqSpliterator<E>(this, -1, -1); |
1136 |
< |
} |
1137 |
< |
|
1138 |
< |
static final class DeqSpliterator<E> implements Spliterator<E> { |
1139 |
< |
private final ArrayDeque<E> deq; |
1140 |
< |
private int fence; // -1 until first use |
1141 |
< |
private int index; // current index, modified on traverse/split |
1142 |
< |
|
1143 |
< |
/** Creates new spliterator covering the given array and range */ |
1144 |
< |
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
1145 |
< |
this.deq = deq; |
1146 |
< |
this.index = origin; |
1147 |
< |
this.fence = fence; |
1148 |
< |
} |
1149 |
< |
|
1150 |
< |
private int getFence() { // force initialization |
1151 |
< |
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; |
1134 |
> |
/** debugging */ |
1135 |
> |
private void checkInvariants() { |
1136 |
> |
try { |
1137 |
> |
int capacity = elements.length; |
1138 |
> |
assert size >= 0 && size <= capacity; |
1139 |
> |
assert head >= 0 && ((capacity == 0 && head == 0 && size == 0) |
1140 |
> |
|| head < capacity); |
1141 |
> |
assert size == 0 |
1142 |
> |
|| (elements[head] != null && elements[tail()] != null); |
1143 |
> |
assert size == capacity |
1144 |
> |
|| (elements[dec(head, capacity)] == null |
1145 |
> |
&& elements[inc(tail(), capacity)] == null); |
1146 |
> |
} catch (Throwable t) { |
1147 |
> |
System.err.printf("head=%d size=%d capacity=%d%n", |
1148 |
> |
head, size, elements.length); |
1149 |
> |
System.err.printf("elements=%s%n", |
1150 |
> |
Arrays.toString(elements)); |
1151 |
> |
throw t; |
1152 |
|
} |
1153 |
|
} |
1154 |
|
|