1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 |
> |
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
> |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
< |
import java.util.Spliterator; |
8 |
< |
import java.util.stream.Stream; |
10 |
< |
import java.util.stream.Streams; |
7 |
> |
|
8 |
> |
import java.io.Serializable; |
9 |
|
import java.util.function.Consumer; |
10 |
+ |
import java.util.function.Predicate; |
11 |
+ |
import java.util.function.UnaryOperator; |
12 |
|
|
13 |
|
/** |
14 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
20 |
|
* when used as a queue. |
21 |
|
* |
22 |
|
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
23 |
< |
* Exceptions include {@link #remove(Object) remove}, {@link |
24 |
< |
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
25 |
< |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
26 |
< |
* iterator.remove()}, and the bulk operations, all of which run in linear |
27 |
< |
* time. |
23 |
> |
* Exceptions include |
24 |
> |
* {@link #remove(Object) remove}, |
25 |
> |
* {@link #removeFirstOccurrence removeFirstOccurrence}, |
26 |
> |
* {@link #removeLastOccurrence removeLastOccurrence}, |
27 |
> |
* {@link #contains contains}, |
28 |
> |
* {@link #iterator iterator.remove()}, |
29 |
> |
* and the bulk operations, all of which run in linear time. |
30 |
|
* |
31 |
< |
* <p>The iterators returned by this class's {@code iterator} method are |
32 |
< |
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
33 |
< |
* is created, in any way except through the iterator's own {@code remove} |
34 |
< |
* method, the iterator will generally throw a {@link |
31 |
> |
* <p>The iterators returned by this class's {@link #iterator() iterator} |
32 |
> |
* method are <em>fail-fast</em>: If the deque is modified at any time after |
33 |
> |
* the iterator is created, in any way except through the iterator's own |
34 |
> |
* {@code remove} method, the iterator will generally throw a {@link |
35 |
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
36 |
|
* modification, the iterator fails quickly and cleanly, rather than risking |
37 |
|
* arbitrary, non-deterministic behavior at an undetermined time in the |
54 |
|
* Java Collections Framework</a>. |
55 |
|
* |
56 |
|
* @author Josh Bloch and Doug Lea |
57 |
+ |
* @param <E> the type of elements held in this deque |
58 |
|
* @since 1.6 |
56 |
– |
* @param <E> the type of elements held in this collection |
59 |
|
*/ |
60 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
61 |
< |
implements Deque<E>, Cloneable, java.io.Serializable |
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 |
65 |
< |
* full, except transiently within an addX method where it is |
66 |
< |
* resized (see doubleCapacity) immediately upon becoming full, |
67 |
< |
* thus avoiding head and tail wrapping around to equal each |
68 |
< |
* other. We also guarantee that all array cells not holding |
69 |
< |
* 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 |
< |
/** |
78 |
< |
* 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)). |
83 |
< |
*/ |
84 |
< |
transient int tail; |
85 |
< |
|
86 |
< |
/** |
87 |
< |
* The minimum capacity that we'll use for a newly created deque. |
88 |
< |
* Must be a power of 2. |
89 |
< |
*/ |
90 |
< |
private static final int MIN_INITIAL_CAPACITY = 8; |
91 |
< |
|
92 |
< |
// ****** Array allocation and resizing utilities ****** |
77 |
> |
/** Number of elements in this collection. */ |
78 |
> |
transient int size; |
79 |
|
|
80 |
|
/** |
81 |
< |
* Allocates empty array to hold the given number of elements. |
82 |
< |
* |
83 |
< |
* @param numElements the number of elements to hold |
84 |
< |
*/ |
85 |
< |
private void allocateElements(int numElements) { |
86 |
< |
int initialCapacity = MIN_INITIAL_CAPACITY; |
87 |
< |
// Find the best power of two to hold elements. |
88 |
< |
// Tests "<=" because arrays aren't kept full. |
89 |
< |
if (numElements >= initialCapacity) { |
90 |
< |
initialCapacity = numElements; |
91 |
< |
initialCapacity |= (initialCapacity >>> 1); |
92 |
< |
initialCapacity |= (initialCapacity >>> 2); |
93 |
< |
initialCapacity |= (initialCapacity >>> 4); |
94 |
< |
initialCapacity |= (initialCapacity >>> 8); |
95 |
< |
initialCapacity |= (initialCapacity >>> 16); |
96 |
< |
initialCapacity++; |
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 |
> |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
87 |
> |
|
88 |
> |
/** |
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 void grow(int needed) { |
94 |
> |
// overflow-conscious code |
95 |
> |
// checkInvariants(); |
96 |
> |
final 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 |
< |
if (initialCapacity < 0) // Too many elements, must back off |
117 |
< |
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements |
116 |
> |
/** Capacity calculation for edge conditions, especially overflow. */ |
117 |
> |
private int newCapacity(int needed, int jump) { |
118 |
> |
final int oldCapacity = elements.length, minCapacity; |
119 |
> |
if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) { |
120 |
> |
if (minCapacity < 0) |
121 |
> |
throw new IllegalStateException("Sorry, deque too big"); |
122 |
> |
return Integer.MAX_VALUE; |
123 |
|
} |
124 |
< |
elements = new Object[initialCapacity]; |
124 |
> |
if (needed > jump) |
125 |
> |
return minCapacity; |
126 |
> |
return (oldCapacity + jump - MAX_ARRAY_SIZE < 0) |
127 |
> |
? oldCapacity + jump |
128 |
> |
: MAX_ARRAY_SIZE; |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* Doubles the capacity of this deque. Call only when full, i.e., |
133 |
< |
* when head and tail have wrapped around to become equal. |
132 |
> |
* Increases the internal storage of this collection, if necessary, |
133 |
> |
* to ensure that it can hold at least the given number of elements. |
134 |
> |
* |
135 |
> |
* @param minCapacity the desired minimum capacity |
136 |
> |
* @since TBD |
137 |
|
*/ |
138 |
< |
private void doubleCapacity() { |
139 |
< |
assert head == tail; |
140 |
< |
int p = head; |
141 |
< |
int n = elements.length; |
126 |
< |
int r = n - p; // number of elements to the right of p |
127 |
< |
int newCapacity = n << 1; |
128 |
< |
if (newCapacity < 0) |
129 |
< |
throw new IllegalStateException("Sorry, deque too big"); |
130 |
< |
Object[] a = new Object[newCapacity]; |
131 |
< |
System.arraycopy(elements, p, a, 0, r); |
132 |
< |
System.arraycopy(elements, 0, a, r, p); |
133 |
< |
elements = a; |
134 |
< |
head = 0; |
135 |
< |
tail = n; |
138 |
> |
/* public */ void ensureCapacity(int minCapacity) { |
139 |
> |
if (minCapacity > elements.length) |
140 |
> |
grow(minCapacity - elements.length); |
141 |
> |
// checkInvariants(); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* Copies the elements from our element array into the specified array, |
140 |
< |
* in order (from first to last element in the deque). It is assumed |
141 |
< |
* that the array is large enough to hold all elements in the deque. |
145 |
> |
* Minimizes the internal storage of this collection. |
146 |
|
* |
147 |
< |
* @return its argument |
147 |
> |
* @since TBD |
148 |
|
*/ |
149 |
< |
private <T> T[] copyElements(T[] a) { |
150 |
< |
if (head < tail) { |
151 |
< |
System.arraycopy(elements, head, a, 0, size()); |
152 |
< |
} else if (head > tail) { |
149 |
< |
int headPortionLen = elements.length - head; |
150 |
< |
System.arraycopy(elements, head, a, 0, headPortionLen); |
151 |
< |
System.arraycopy(elements, 0, a, headPortionLen, tail); |
149 |
> |
/* public */ void trimToSize() { |
150 |
> |
if (size < elements.length) { |
151 |
> |
elements = toArray(); |
152 |
> |
head = 0; |
153 |
|
} |
154 |
< |
return a; |
154 |
> |
// checkInvariants(); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
166 |
|
* Constructs an empty array deque with an initial capacity |
167 |
|
* sufficient to hold the specified number of elements. |
168 |
|
* |
169 |
< |
* @param numElements lower bound on initial capacity of the deque |
169 |
> |
* @param numElements lower bound on initial capacity of the deque |
170 |
|
*/ |
171 |
|
public ArrayDeque(int numElements) { |
172 |
< |
allocateElements(numElements); |
172 |
> |
elements = new Object[numElements]; |
173 |
|
} |
174 |
|
|
175 |
|
/** |
183 |
|
* @throws NullPointerException if the specified collection is null |
184 |
|
*/ |
185 |
|
public ArrayDeque(Collection<? extends E> c) { |
186 |
< |
allocateElements(c.size()); |
187 |
< |
addAll(c); |
186 |
> |
Object[] elements = c.toArray(); |
187 |
> |
// defend against c.toArray (incorrectly) not returning Object[] |
188 |
> |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
189 |
> |
size = elements.length; |
190 |
> |
if (elements.getClass() != Object[].class) |
191 |
> |
elements = Arrays.copyOf(elements, size, Object[].class); |
192 |
> |
for (Object obj : elements) |
193 |
> |
Objects.requireNonNull(obj); |
194 |
> |
this.elements = elements; |
195 |
> |
} |
196 |
> |
|
197 |
> |
/** |
198 |
> |
* Increments i, mod modulus. |
199 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
200 |
> |
*/ |
201 |
> |
static final int inc(int i, int modulus) { |
202 |
> |
if (++i >= modulus) i = 0; |
203 |
> |
return i; |
204 |
> |
} |
205 |
> |
|
206 |
> |
/** |
207 |
> |
* Decrements i, mod modulus. |
208 |
> |
* Precondition and postcondition: 0 <= i < modulus. |
209 |
> |
*/ |
210 |
> |
static final int dec(int i, int modulus) { |
211 |
> |
if (--i < 0) i = modulus - 1; |
212 |
> |
return i; |
213 |
> |
} |
214 |
> |
|
215 |
> |
/** |
216 |
> |
* Adds i and j, mod modulus. |
217 |
> |
* Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus. |
218 |
> |
*/ |
219 |
> |
static final int add(int i, int j, int modulus) { |
220 |
> |
if ((i += j) - modulus >= 0) i -= modulus; |
221 |
> |
return i; |
222 |
> |
} |
223 |
> |
|
224 |
> |
/** |
225 |
> |
* Returns the array index of the last element. |
226 |
> |
* May return invalid index -1 if there are no elements. |
227 |
> |
*/ |
228 |
> |
final int tail() { |
229 |
> |
return add(head, size - 1, elements.length); |
230 |
> |
} |
231 |
> |
|
232 |
> |
/** |
233 |
> |
* Returns element at array index i. |
234 |
> |
*/ |
235 |
> |
@SuppressWarnings("unchecked") |
236 |
> |
private E elementAt(int i) { |
237 |
> |
return (E) elements[i]; |
238 |
> |
} |
239 |
> |
|
240 |
> |
/** |
241 |
> |
* A version of elementAt that checks for null elements. |
242 |
> |
* This check doesn't catch all possible comodifications, |
243 |
> |
* but does catch ones that corrupt traversal. |
244 |
> |
*/ |
245 |
> |
E checkedElementAt(Object[] elements, int i) { |
246 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
247 |
> |
if (e == null) |
248 |
> |
throw new ConcurrentModificationException(); |
249 |
> |
return e; |
250 |
|
} |
251 |
|
|
252 |
|
// The main insertion and extraction methods are addFirst, |
260 |
|
* @throws NullPointerException if the specified element is null |
261 |
|
*/ |
262 |
|
public void addFirst(E e) { |
263 |
< |
if (e == null) |
264 |
< |
throw new NullPointerException(); |
265 |
< |
elements[head = (head - 1) & (elements.length - 1)] = e; |
266 |
< |
if (head == tail) |
267 |
< |
doubleCapacity(); |
263 |
> |
// checkInvariants(); |
264 |
> |
Objects.requireNonNull(e); |
265 |
> |
Object[] elements; |
266 |
> |
int capacity, h; |
267 |
> |
final int s; |
268 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) { |
269 |
> |
grow(1); |
270 |
> |
capacity = (elements = this.elements).length; |
271 |
> |
} |
272 |
> |
if ((h = head - 1) < 0) h = capacity - 1; |
273 |
> |
elements[head = h] = e; |
274 |
> |
size = s + 1; |
275 |
> |
// checkInvariants(); |
276 |
|
} |
277 |
|
|
278 |
|
/** |
284 |
|
* @throws NullPointerException if the specified element is null |
285 |
|
*/ |
286 |
|
public void addLast(E e) { |
287 |
< |
if (e == null) |
288 |
< |
throw new NullPointerException(); |
289 |
< |
elements[tail] = e; |
290 |
< |
if ( (tail = (tail + 1) & (elements.length - 1)) == head) |
291 |
< |
doubleCapacity(); |
287 |
> |
// checkInvariants(); |
288 |
> |
Objects.requireNonNull(e); |
289 |
> |
Object[] elements; |
290 |
> |
int capacity; |
291 |
> |
final int s; |
292 |
> |
if ((s = size) == (capacity = (elements = this.elements).length)) { |
293 |
> |
grow(1); |
294 |
> |
capacity = (elements = this.elements).length; |
295 |
> |
} |
296 |
> |
elements[add(head, s, capacity)] = e; |
297 |
> |
size = s + 1; |
298 |
> |
// checkInvariants(); |
299 |
> |
} |
300 |
> |
|
301 |
> |
/** |
302 |
> |
* Adds all of the elements in the specified collection at the end |
303 |
> |
* of this deque, as if by calling {@link #addLast} on each one, |
304 |
> |
* in the order that they are returned by the collection's |
305 |
> |
* iterator. |
306 |
> |
* |
307 |
> |
* @param c the elements to be inserted into this deque |
308 |
> |
* @return {@code true} if this deque changed as a result of the call |
309 |
> |
* @throws NullPointerException if the specified collection or any |
310 |
> |
* of its elements are null |
311 |
> |
*/ |
312 |
> |
public boolean addAll(Collection<? extends E> c) { |
313 |
> |
final int s = size, needed = c.size() - (elements.length - s); |
314 |
> |
if (needed > 0) |
315 |
> |
grow(needed); |
316 |
> |
c.forEach((e) -> addLast(e)); |
317 |
> |
// checkInvariants(); |
318 |
> |
return size > s; |
319 |
|
} |
320 |
|
|
321 |
|
/** |
346 |
|
* @throws NoSuchElementException {@inheritDoc} |
347 |
|
*/ |
348 |
|
public E removeFirst() { |
349 |
< |
E x = pollFirst(); |
350 |
< |
if (x == null) |
349 |
> |
// checkInvariants(); |
350 |
> |
E e = pollFirst(); |
351 |
> |
if (e == null) |
352 |
|
throw new NoSuchElementException(); |
353 |
< |
return x; |
353 |
> |
return e; |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
|
* @throws NoSuchElementException {@inheritDoc} |
358 |
|
*/ |
359 |
|
public E removeLast() { |
360 |
< |
E x = pollLast(); |
361 |
< |
if (x == null) |
360 |
> |
// checkInvariants(); |
361 |
> |
E e = pollLast(); |
362 |
> |
if (e == null) |
363 |
|
throw new NoSuchElementException(); |
364 |
< |
return x; |
364 |
> |
return e; |
365 |
|
} |
366 |
|
|
367 |
|
public E pollFirst() { |
368 |
< |
int h = head; |
369 |
< |
@SuppressWarnings("unchecked") |
370 |
< |
E result = (E) elements[h]; |
271 |
< |
// Element is null if deque empty |
272 |
< |
if (result == null) |
368 |
> |
// checkInvariants(); |
369 |
> |
int s, h; |
370 |
> |
if ((s = size) <= 0) |
371 |
|
return null; |
372 |
< |
elements[h] = null; // Must null out slot |
373 |
< |
head = (h + 1) & (elements.length - 1); |
374 |
< |
return result; |
372 |
> |
final Object[] elements = this.elements; |
373 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[h = head]; |
374 |
> |
elements[h] = null; |
375 |
> |
if (++h >= elements.length) h = 0; |
376 |
> |
head = h; |
377 |
> |
size = s - 1; |
378 |
> |
return e; |
379 |
|
} |
380 |
|
|
381 |
|
public E pollLast() { |
382 |
< |
int t = (tail - 1) & (elements.length - 1); |
383 |
< |
@SuppressWarnings("unchecked") |
384 |
< |
E result = (E) elements[t]; |
283 |
< |
if (result == null) |
382 |
> |
// checkInvariants(); |
383 |
> |
final int s, tail; |
384 |
> |
if ((s = size) <= 0) |
385 |
|
return null; |
386 |
< |
elements[t] = null; |
387 |
< |
tail = t; |
388 |
< |
return result; |
386 |
> |
final Object[] elements = this.elements; |
387 |
> |
@SuppressWarnings("unchecked") |
388 |
> |
E e = (E) elements[tail = add(head, s - 1, elements.length)]; |
389 |
> |
elements[tail] = null; |
390 |
> |
size = s - 1; |
391 |
> |
return e; |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
|
* @throws NoSuchElementException {@inheritDoc} |
396 |
|
*/ |
397 |
|
public E getFirst() { |
398 |
< |
@SuppressWarnings("unchecked") |
399 |
< |
E result = (E) elements[head]; |
400 |
< |
if (result == null) |
297 |
< |
throw new NoSuchElementException(); |
298 |
< |
return result; |
398 |
> |
// checkInvariants(); |
399 |
> |
if (size <= 0) throw new NoSuchElementException(); |
400 |
> |
return elementAt(head); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
|
* @throws NoSuchElementException {@inheritDoc} |
405 |
|
*/ |
406 |
+ |
@SuppressWarnings("unchecked") |
407 |
|
public E getLast() { |
408 |
< |
@SuppressWarnings("unchecked") |
409 |
< |
E result = (E) elements[(tail - 1) & (elements.length - 1)]; |
410 |
< |
if (result == null) |
411 |
< |
throw new NoSuchElementException(); |
412 |
< |
return result; |
408 |
> |
// checkInvariants(); |
409 |
> |
final int s; |
410 |
> |
if ((s = size) <= 0) throw new NoSuchElementException(); |
411 |
> |
final Object[] elements = this.elements; |
412 |
> |
return (E) elements[add(head, s - 1, elements.length)]; |
413 |
|
} |
414 |
|
|
312 |
– |
@SuppressWarnings("unchecked") |
415 |
|
public E peekFirst() { |
416 |
< |
// elements[head] is null if deque empty |
417 |
< |
return (E) elements[head]; |
416 |
> |
// checkInvariants(); |
417 |
> |
return (size <= 0) ? null : elementAt(head); |
418 |
|
} |
419 |
|
|
420 |
|
@SuppressWarnings("unchecked") |
421 |
|
public E peekLast() { |
422 |
< |
return (E) elements[(tail - 1) & (elements.length - 1)]; |
422 |
> |
// checkInvariants(); |
423 |
> |
final int s; |
424 |
> |
if ((s = size) <= 0) return null; |
425 |
> |
final Object[] elements = this.elements; |
426 |
> |
return (E) elements[add(head, s - 1, elements.length)]; |
427 |
|
} |
428 |
|
|
429 |
|
/** |
439 |
|
* @return {@code true} if the deque contained the specified element |
440 |
|
*/ |
441 |
|
public boolean removeFirstOccurrence(Object o) { |
442 |
< |
if (o == null) |
443 |
< |
return false; |
444 |
< |
int mask = elements.length - 1; |
445 |
< |
int i = head; |
446 |
< |
Object x; |
447 |
< |
while ( (x = elements[i]) != null) { |
448 |
< |
if (o.equals(x)) { |
449 |
< |
delete(i); |
450 |
< |
return true; |
442 |
> |
if (o != null) { |
443 |
> |
final Object[] elements = this.elements; |
444 |
> |
final int capacity = elements.length; |
445 |
> |
int from, end, to, todo; |
446 |
> |
todo = (end = (from = head) + size) |
447 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
448 |
> |
for (;; from = 0, to = todo, todo = 0) { |
449 |
> |
for (int i = from; i < to; i++) |
450 |
> |
if (o.equals(elements[i])) { |
451 |
> |
delete(i); |
452 |
> |
return true; |
453 |
> |
} |
454 |
> |
if (todo == 0) break; |
455 |
|
} |
346 |
– |
i = (i + 1) & mask; |
456 |
|
} |
457 |
|
return false; |
458 |
|
} |
470 |
|
* @return {@code true} if the deque contained the specified element |
471 |
|
*/ |
472 |
|
public boolean removeLastOccurrence(Object o) { |
473 |
< |
if (o == null) |
474 |
< |
return false; |
475 |
< |
int mask = elements.length - 1; |
476 |
< |
int i = (tail - 1) & mask; |
477 |
< |
Object x; |
478 |
< |
while ( (x = elements[i]) != null) { |
479 |
< |
if (o.equals(x)) { |
480 |
< |
delete(i); |
481 |
< |
return true; |
473 |
> |
if (o != null) { |
474 |
> |
final Object[] elements = this.elements; |
475 |
> |
final int capacity = elements.length; |
476 |
> |
int from, to, end, todo; |
477 |
> |
todo = (to = ((end = (from = tail()) - size) >= -1) ? end : -1) - end; |
478 |
> |
for (;; from = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
479 |
> |
for (int i = from; i > to; i--) |
480 |
> |
if (o.equals(elements[i])) { |
481 |
> |
delete(i); |
482 |
> |
return true; |
483 |
> |
} |
484 |
> |
if (todo == 0) break; |
485 |
|
} |
374 |
– |
i = (i - 1) & mask; |
486 |
|
} |
487 |
|
return false; |
488 |
|
} |
601 |
|
return removeFirst(); |
602 |
|
} |
603 |
|
|
493 |
– |
private void checkInvariants() { |
494 |
– |
assert elements[tail] == null; |
495 |
– |
assert head == tail ? elements[head] == null : |
496 |
– |
(elements[head] != null && |
497 |
– |
elements[(tail - 1) & (elements.length - 1)] != null); |
498 |
– |
assert elements[(head - 1) & (elements.length - 1)] == null; |
499 |
– |
} |
500 |
– |
|
604 |
|
/** |
605 |
< |
* Removes the element at the specified position in the elements array, |
606 |
< |
* adjusting head and tail as necessary. This can result in motion of |
607 |
< |
* elements backwards or forwards in the array. |
605 |
> |
* Removes the element at the specified position in the elements array. |
606 |
> |
* This can result in forward or backwards motion of array elements. |
607 |
> |
* We optimize for least element motion. |
608 |
|
* |
609 |
|
* <p>This method is called delete rather than remove to emphasize |
610 |
|
* that its semantics differ from those of {@link List#remove(int)}. |
611 |
|
* |
612 |
|
* @return true if elements moved backwards |
613 |
|
*/ |
614 |
< |
private boolean delete(int i) { |
615 |
< |
checkInvariants(); |
614 |
> |
boolean delete(int i) { |
615 |
> |
// checkInvariants(); |
616 |
|
final Object[] elements = this.elements; |
617 |
< |
final int mask = elements.length - 1; |
617 |
> |
final int capacity = elements.length; |
618 |
|
final int h = head; |
619 |
< |
final int t = tail; |
620 |
< |
final int front = (i - h) & mask; |
621 |
< |
final int back = (t - i) & mask; |
519 |
< |
|
520 |
< |
// Invariant: head <= i < tail mod circularity |
521 |
< |
if (front >= ((t - h) & mask)) |
522 |
< |
throw new ConcurrentModificationException(); |
523 |
< |
|
524 |
< |
// Optimize for least element motion |
619 |
> |
int front; // number of elements before to-be-deleted elt |
620 |
> |
if ((front = i - h) < 0) front += capacity; |
621 |
> |
final int back = size - front - 1; // number of elements after |
622 |
|
if (front < back) { |
623 |
+ |
// move front elements forwards |
624 |
|
if (h <= i) { |
625 |
|
System.arraycopy(elements, h, elements, h + 1, front); |
626 |
|
} else { // Wrap around |
627 |
|
System.arraycopy(elements, 0, elements, 1, i); |
628 |
< |
elements[0] = elements[mask]; |
629 |
< |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
628 |
> |
elements[0] = elements[capacity - 1]; |
629 |
> |
System.arraycopy(elements, h, elements, h + 1, front - (i + 1)); |
630 |
|
} |
631 |
|
elements[h] = null; |
632 |
< |
head = (h + 1) & mask; |
632 |
> |
if ((head = (h + 1)) >= capacity) head = 0; |
633 |
> |
size--; |
634 |
> |
// checkInvariants(); |
635 |
|
return false; |
636 |
|
} else { |
637 |
< |
if (i < t) { // Copy the null tail as well |
637 |
> |
// move back elements backwards |
638 |
> |
int tail = tail(); |
639 |
> |
if (i <= tail) { |
640 |
|
System.arraycopy(elements, i + 1, elements, i, back); |
539 |
– |
tail = t - 1; |
641 |
|
} else { // Wrap around |
642 |
< |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
643 |
< |
elements[mask] = elements[0]; |
644 |
< |
System.arraycopy(elements, 1, elements, 0, t); |
645 |
< |
tail = (t - 1) & mask; |
642 |
> |
int firstLeg = capacity - (i + 1); |
643 |
> |
System.arraycopy(elements, i + 1, elements, i, firstLeg); |
644 |
> |
elements[capacity - 1] = elements[0]; |
645 |
> |
System.arraycopy(elements, 1, elements, 0, back - firstLeg - 1); |
646 |
|
} |
647 |
+ |
elements[tail] = null; |
648 |
+ |
size--; |
649 |
+ |
// checkInvariants(); |
650 |
|
return true; |
651 |
|
} |
652 |
|
} |
659 |
|
* @return the number of elements in this deque |
660 |
|
*/ |
661 |
|
public int size() { |
662 |
< |
return (tail - head) & (elements.length - 1); |
662 |
> |
return size; |
663 |
|
} |
664 |
|
|
665 |
|
/** |
668 |
|
* @return {@code true} if this deque contains no elements |
669 |
|
*/ |
670 |
|
public boolean isEmpty() { |
671 |
< |
return head == tail; |
671 |
> |
return size == 0; |
672 |
|
} |
673 |
|
|
674 |
|
/** |
688 |
|
} |
689 |
|
|
690 |
|
private class DeqIterator implements Iterator<E> { |
691 |
< |
/** |
692 |
< |
* Index of element to be returned by subsequent call to next. |
589 |
< |
*/ |
590 |
< |
private int cursor = head; |
691 |
> |
/** Index of element to be returned by subsequent call to next. */ |
692 |
> |
int cursor; |
693 |
|
|
694 |
< |
/** |
695 |
< |
* Tail recorded at construction (also in remove), to stop |
594 |
< |
* iterator and also to check for comodification. |
595 |
< |
*/ |
596 |
< |
private int fence = tail; |
694 |
> |
/** Number of elements yet to be returned. */ |
695 |
> |
int remaining = size; |
696 |
|
|
697 |
|
/** |
698 |
|
* Index of element returned by most recent call to next. |
699 |
|
* Reset to -1 if element is deleted by a call to remove. |
700 |
|
*/ |
701 |
< |
private int lastRet = -1; |
701 |
> |
int lastRet = -1; |
702 |
|
|
703 |
< |
public boolean hasNext() { |
704 |
< |
return cursor != fence; |
703 |
> |
DeqIterator() { cursor = head; } |
704 |
> |
|
705 |
> |
public final boolean hasNext() { |
706 |
> |
return remaining > 0; |
707 |
|
} |
708 |
|
|
709 |
|
public E next() { |
710 |
< |
if (cursor == fence) |
710 |
> |
if (remaining <= 0) |
711 |
|
throw new NoSuchElementException(); |
712 |
< |
@SuppressWarnings("unchecked") |
713 |
< |
E result = (E) elements[cursor]; |
613 |
< |
// This check doesn't catch all possible comodifications, |
614 |
< |
// but does catch the ones that corrupt traversal |
615 |
< |
if (tail != fence || result == null) |
616 |
< |
throw new ConcurrentModificationException(); |
712 |
> |
final Object[] elements = ArrayDeque.this.elements; |
713 |
> |
E e = checkedElementAt(elements, cursor); |
714 |
|
lastRet = cursor; |
715 |
< |
cursor = (cursor + 1) & (elements.length - 1); |
716 |
< |
return result; |
715 |
> |
if (++cursor >= elements.length) cursor = 0; |
716 |
> |
remaining--; |
717 |
> |
return e; |
718 |
|
} |
719 |
|
|
720 |
< |
public void remove() { |
720 |
> |
void postDelete(boolean leftShifted) { |
721 |
> |
if (leftShifted) |
722 |
> |
if (--cursor < 0) cursor = elements.length - 1; |
723 |
> |
} |
724 |
> |
|
725 |
> |
public final void remove() { |
726 |
|
if (lastRet < 0) |
727 |
|
throw new IllegalStateException(); |
728 |
< |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
626 |
< |
cursor = (cursor - 1) & (elements.length - 1); |
627 |
< |
fence = tail; |
628 |
< |
} |
728 |
> |
postDelete(delete(lastRet)); |
729 |
|
lastRet = -1; |
730 |
|
} |
631 |
– |
} |
731 |
|
|
732 |
< |
private class DescendingIterator implements Iterator<E> { |
733 |
< |
/* |
734 |
< |
* This class is nearly a mirror-image of DeqIterator, using |
735 |
< |
* tail instead of head for initial cursor, and head instead of |
736 |
< |
* tail for fence. |
737 |
< |
*/ |
738 |
< |
private int cursor = tail; |
739 |
< |
private int fence = head; |
641 |
< |
private int lastRet = -1; |
642 |
< |
|
643 |
< |
public boolean hasNext() { |
644 |
< |
return cursor != fence; |
732 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
733 |
> |
final int k; |
734 |
> |
if ((k = remaining) > 0) { |
735 |
> |
remaining = 0; |
736 |
> |
ArrayDeque.forEachRemaining(action, elements, cursor, k); |
737 |
> |
if ((lastRet = cursor + k - 1) >= elements.length) |
738 |
> |
lastRet -= elements.length; |
739 |
> |
} |
740 |
|
} |
741 |
+ |
} |
742 |
|
|
743 |
< |
public E next() { |
744 |
< |
if (cursor == fence) |
743 |
> |
private class DescendingIterator extends DeqIterator { |
744 |
> |
DescendingIterator() { cursor = tail(); } |
745 |
> |
|
746 |
> |
public final E next() { |
747 |
> |
if (remaining <= 0) |
748 |
|
throw new NoSuchElementException(); |
749 |
< |
cursor = (cursor - 1) & (elements.length - 1); |
750 |
< |
@SuppressWarnings("unchecked") |
652 |
< |
E result = (E) elements[cursor]; |
653 |
< |
if (head != fence || result == null) |
654 |
< |
throw new ConcurrentModificationException(); |
749 |
> |
final Object[] elements = ArrayDeque.this.elements; |
750 |
> |
E e = checkedElementAt(elements, cursor); |
751 |
|
lastRet = cursor; |
752 |
< |
return result; |
752 |
> |
if (--cursor < 0) cursor = elements.length - 1; |
753 |
> |
remaining--; |
754 |
> |
return e; |
755 |
|
} |
756 |
|
|
757 |
< |
public void remove() { |
758 |
< |
if (lastRet < 0) |
759 |
< |
throw new IllegalStateException(); |
760 |
< |
if (!delete(lastRet)) { |
761 |
< |
cursor = (cursor + 1) & (elements.length - 1); |
762 |
< |
fence = head; |
757 |
> |
void postDelete(boolean leftShifted) { |
758 |
> |
if (!leftShifted) |
759 |
> |
if (++cursor >= elements.length) cursor = 0; |
760 |
> |
} |
761 |
> |
|
762 |
> |
public final void forEachRemaining(Consumer<? super E> action) { |
763 |
> |
final int k; |
764 |
> |
if ((k = remaining) > 0) { |
765 |
> |
remaining = 0; |
766 |
> |
forEachRemainingDescending(action, elements, cursor, k); |
767 |
> |
if ((lastRet = cursor - (k - 1)) < 0) |
768 |
> |
lastRet += elements.length; |
769 |
|
} |
770 |
< |
lastRet = -1; |
770 |
> |
} |
771 |
> |
} |
772 |
> |
|
773 |
> |
/** |
774 |
> |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
775 |
> |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
776 |
> |
* deque. |
777 |
> |
* |
778 |
> |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, |
779 |
> |
* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and |
780 |
> |
* {@link Spliterator#NONNULL}. Overriding implementations should document |
781 |
> |
* the reporting of additional characteristic values. |
782 |
> |
* |
783 |
> |
* @return a {@code Spliterator} over the elements in this deque |
784 |
> |
* @since 1.8 |
785 |
> |
*/ |
786 |
> |
public Spliterator<E> spliterator() { |
787 |
> |
return new ArrayDequeSpliterator(); |
788 |
> |
} |
789 |
> |
|
790 |
> |
final class ArrayDequeSpliterator implements Spliterator<E> { |
791 |
> |
private int cursor; |
792 |
> |
private int remaining; // -1 until late-binding first use |
793 |
> |
|
794 |
> |
/** Constructs late-binding spliterator over all elements. */ |
795 |
> |
ArrayDequeSpliterator() { |
796 |
> |
this.remaining = -1; |
797 |
> |
} |
798 |
> |
|
799 |
> |
/** Constructs spliterator over the given slice. */ |
800 |
> |
ArrayDequeSpliterator(int cursor, int count) { |
801 |
> |
this.cursor = cursor; |
802 |
> |
this.remaining = count; |
803 |
> |
} |
804 |
> |
|
805 |
> |
/** Ensures late-binding initialization; then returns remaining. */ |
806 |
> |
private int remaining() { |
807 |
> |
if (remaining < 0) { |
808 |
> |
cursor = head; |
809 |
> |
remaining = size; |
810 |
> |
} |
811 |
> |
return remaining; |
812 |
> |
} |
813 |
> |
|
814 |
> |
public ArrayDequeSpliterator trySplit() { |
815 |
> |
final int mid; |
816 |
> |
if ((mid = remaining() >> 1) > 0) { |
817 |
> |
int oldCursor = cursor; |
818 |
> |
cursor = add(cursor, mid, elements.length); |
819 |
> |
remaining -= mid; |
820 |
> |
return new ArrayDequeSpliterator(oldCursor, mid); |
821 |
> |
} |
822 |
> |
return null; |
823 |
> |
} |
824 |
> |
|
825 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
826 |
> |
final int k = remaining(); // side effect! |
827 |
> |
remaining = 0; |
828 |
> |
ArrayDeque.forEachRemaining(action, elements, cursor, k); |
829 |
> |
} |
830 |
> |
|
831 |
> |
public boolean tryAdvance(Consumer<? super E> action) { |
832 |
> |
Objects.requireNonNull(action); |
833 |
> |
final int k; |
834 |
> |
if ((k = remaining()) <= 0) |
835 |
> |
return false; |
836 |
> |
action.accept(checkedElementAt(elements, cursor)); |
837 |
> |
if (++cursor >= elements.length) cursor = 0; |
838 |
> |
remaining = k - 1; |
839 |
> |
return true; |
840 |
> |
} |
841 |
> |
|
842 |
> |
public long estimateSize() { |
843 |
> |
return remaining(); |
844 |
> |
} |
845 |
> |
|
846 |
> |
public int characteristics() { |
847 |
> |
return Spliterator.NONNULL |
848 |
> |
| Spliterator.ORDERED |
849 |
> |
| Spliterator.SIZED |
850 |
> |
| Spliterator.SUBSIZED; |
851 |
> |
} |
852 |
> |
} |
853 |
> |
|
854 |
> |
@SuppressWarnings("unchecked") |
855 |
> |
public void forEach(Consumer<? super E> action) { |
856 |
> |
Objects.requireNonNull(action); |
857 |
> |
final Object[] elements = this.elements; |
858 |
> |
final int capacity = elements.length; |
859 |
> |
int from, end, to, todo; |
860 |
> |
todo = (end = (from = head) + size) |
861 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
862 |
> |
for (;; from = 0, to = todo, todo = 0) { |
863 |
> |
for (int i = from; i < to; i++) |
864 |
> |
action.accept((E) elements[i]); |
865 |
> |
if (todo == 0) break; |
866 |
> |
} |
867 |
> |
// checkInvariants(); |
868 |
> |
} |
869 |
> |
|
870 |
> |
/** |
871 |
> |
* A variant of forEach that also checks for concurrent |
872 |
> |
* modification, for use in iterators. |
873 |
> |
*/ |
874 |
> |
static <E> void forEachRemaining( |
875 |
> |
Consumer<? super E> action, Object[] elements, int from, int remaining) { |
876 |
> |
Objects.requireNonNull(action); |
877 |
> |
final int capacity = elements.length; |
878 |
> |
int end, to, todo; |
879 |
> |
todo = (end = from + remaining) |
880 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
881 |
> |
for (;; from = 0, to = todo, todo = 0) { |
882 |
> |
for (int i = from; i < to; i++) { |
883 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
884 |
> |
if (e == null) |
885 |
> |
throw new ConcurrentModificationException(); |
886 |
> |
action.accept(e); |
887 |
> |
} |
888 |
> |
if (todo == 0) break; |
889 |
> |
} |
890 |
> |
} |
891 |
> |
|
892 |
> |
static <E> void forEachRemainingDescending( |
893 |
> |
Consumer<? super E> action, Object[] elements, int from, int remaining) { |
894 |
> |
Objects.requireNonNull(action); |
895 |
> |
final int capacity = elements.length; |
896 |
> |
int end, to, todo; |
897 |
> |
todo = (to = ((end = from - remaining) >= -1) ? end : -1) - end; |
898 |
> |
for (;; from = capacity - 1, to = capacity - 1 - todo, todo = 0) { |
899 |
> |
for (int i = from; i > to; i--) { |
900 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
901 |
> |
if (e == null) |
902 |
> |
throw new ConcurrentModificationException(); |
903 |
> |
action.accept(e); |
904 |
> |
} |
905 |
> |
if (todo == 0) break; |
906 |
> |
} |
907 |
> |
} |
908 |
> |
|
909 |
> |
/** |
910 |
> |
* Replaces each element of this deque with the result of applying the |
911 |
> |
* operator to that element, as specified by {@link List#replaceAll}. |
912 |
> |
* |
913 |
> |
* @param operator the operator to apply to each element |
914 |
> |
* @since TBD |
915 |
> |
*/ |
916 |
> |
/* public */ void replaceAll(UnaryOperator<E> operator) { |
917 |
> |
Objects.requireNonNull(operator); |
918 |
> |
final Object[] elements = this.elements; |
919 |
> |
final int capacity = elements.length; |
920 |
> |
int from, end, to, todo; |
921 |
> |
todo = (end = (from = head) + size) |
922 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
923 |
> |
for (;; from = 0, to = todo, todo = 0) { |
924 |
> |
for (int i = from; i < to; i++) |
925 |
> |
elements[i] = operator.apply(elementAt(i)); |
926 |
> |
if (todo == 0) break; |
927 |
> |
} |
928 |
> |
// checkInvariants(); |
929 |
> |
} |
930 |
> |
|
931 |
> |
/** |
932 |
> |
* @throws NullPointerException {@inheritDoc} |
933 |
> |
*/ |
934 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
935 |
> |
Objects.requireNonNull(filter); |
936 |
> |
return bulkRemove(filter); |
937 |
> |
} |
938 |
> |
|
939 |
> |
/** |
940 |
> |
* @throws NullPointerException {@inheritDoc} |
941 |
> |
*/ |
942 |
> |
public boolean removeAll(Collection<?> c) { |
943 |
> |
Objects.requireNonNull(c); |
944 |
> |
return bulkRemove(e -> c.contains(e)); |
945 |
> |
} |
946 |
> |
|
947 |
> |
/** |
948 |
> |
* @throws NullPointerException {@inheritDoc} |
949 |
> |
*/ |
950 |
> |
public boolean retainAll(Collection<?> c) { |
951 |
> |
Objects.requireNonNull(c); |
952 |
> |
return bulkRemove(e -> !c.contains(e)); |
953 |
> |
} |
954 |
> |
|
955 |
> |
/** Implementation of bulk remove methods. */ |
956 |
> |
private boolean bulkRemove(Predicate<? super E> filter) { |
957 |
> |
// checkInvariants(); |
958 |
> |
final Object[] elements = this.elements; |
959 |
> |
final int capacity = elements.length; |
960 |
> |
int i = head, j = i, remaining = size, deleted = 0; |
961 |
> |
try { |
962 |
> |
for (; remaining > 0; remaining--) { |
963 |
> |
@SuppressWarnings("unchecked") E e = (E) elements[i]; |
964 |
> |
if (filter.test(e)) |
965 |
> |
deleted++; |
966 |
> |
else { |
967 |
> |
if (j != i) |
968 |
> |
elements[j] = e; |
969 |
> |
if (++j >= capacity) j = 0; |
970 |
> |
} |
971 |
> |
if (++i >= capacity) i = 0; |
972 |
> |
} |
973 |
> |
return deleted > 0; |
974 |
> |
} catch (Throwable ex) { |
975 |
> |
if (deleted > 0) |
976 |
> |
for (; remaining > 0; remaining--) { |
977 |
> |
elements[j] = elements[i]; |
978 |
> |
if (++i >= capacity) i = 0; |
979 |
> |
if (++j >= capacity) j = 0; |
980 |
> |
} |
981 |
> |
throw ex; |
982 |
> |
} finally { |
983 |
> |
size -= deleted; |
984 |
> |
clearSlice(elements, j, deleted); |
985 |
> |
// checkInvariants(); |
986 |
|
} |
987 |
|
} |
988 |
|
|
995 |
|
* @return {@code true} if this deque contains the specified element |
996 |
|
*/ |
997 |
|
public boolean contains(Object o) { |
998 |
< |
if (o == null) |
999 |
< |
return false; |
1000 |
< |
int mask = elements.length - 1; |
1001 |
< |
int i = head; |
1002 |
< |
Object x; |
1003 |
< |
while ( (x = elements[i]) != null) { |
1004 |
< |
if (o.equals(x)) |
1005 |
< |
return true; |
1006 |
< |
i = (i + 1) & mask; |
998 |
> |
if (o != null) { |
999 |
> |
final Object[] elements = this.elements; |
1000 |
> |
final int capacity = elements.length; |
1001 |
> |
int from, end, to, todo; |
1002 |
> |
todo = (end = (from = head) + size) |
1003 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
1004 |
> |
for (;; from = 0, to = todo, todo = 0) { |
1005 |
> |
for (int i = from; i < to; i++) |
1006 |
> |
if (o.equals(elements[i])) |
1007 |
> |
return true; |
1008 |
> |
if (todo == 0) break; |
1009 |
> |
} |
1010 |
|
} |
1011 |
|
return false; |
1012 |
|
} |
1019 |
|
* Returns {@code true} if this deque contained the specified element |
1020 |
|
* (or equivalently, if this deque changed as a result of the call). |
1021 |
|
* |
1022 |
< |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
1022 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
1023 |
|
* |
1024 |
|
* @param o element to be removed from this deque, if present |
1025 |
|
* @return {@code true} if this deque contained the specified element |
1033 |
|
* The deque will be empty after this call returns. |
1034 |
|
*/ |
1035 |
|
public void clear() { |
1036 |
< |
int h = head; |
1037 |
< |
int t = tail; |
1038 |
< |
if (h != t) { // clear all cells |
1039 |
< |
head = tail = 0; |
1040 |
< |
int i = h; |
1041 |
< |
int mask = elements.length - 1; |
1042 |
< |
do { |
1043 |
< |
elements[i] = null; |
1044 |
< |
i = (i + 1) & mask; |
1045 |
< |
} while (i != t); |
1046 |
< |
} |
1036 |
> |
clearSlice(elements, head, size); |
1037 |
> |
size = head = 0; |
1038 |
> |
// checkInvariants(); |
1039 |
> |
} |
1040 |
> |
|
1041 |
> |
/** |
1042 |
> |
* Nulls out count elements, starting at array index from. |
1043 |
> |
*/ |
1044 |
> |
private static void clearSlice(Object[] elements, int from, int count) { |
1045 |
> |
final int capacity = elements.length, end = from + count; |
1046 |
> |
final int leg = (capacity - end >= 0) ? end : capacity; |
1047 |
> |
Arrays.fill(elements, from, leg, null); |
1048 |
> |
if (leg != end) |
1049 |
> |
Arrays.fill(elements, 0, end - capacity, null); |
1050 |
|
} |
1051 |
|
|
1052 |
|
/** |
1063 |
|
* @return an array containing all of the elements in this deque |
1064 |
|
*/ |
1065 |
|
public Object[] toArray() { |
1066 |
< |
return copyElements(new Object[size()]); |
1066 |
> |
return toArray(Object[].class); |
1067 |
> |
} |
1068 |
> |
|
1069 |
> |
private <T> T[] toArray(Class<T[]> klazz) { |
1070 |
> |
final Object[] elements = this.elements; |
1071 |
> |
final int capacity = elements.length; |
1072 |
> |
final int head = this.head, end = head + size; |
1073 |
> |
final T[] a; |
1074 |
> |
if (end >= 0) { |
1075 |
> |
a = Arrays.copyOfRange(elements, head, end, klazz); |
1076 |
> |
} else { |
1077 |
> |
// integer overflow! |
1078 |
> |
a = Arrays.copyOfRange(elements, 0, size, klazz); |
1079 |
> |
System.arraycopy(elements, head, a, 0, capacity - head); |
1080 |
> |
} |
1081 |
> |
if (end - capacity > 0) |
1082 |
> |
System.arraycopy(elements, 0, a, capacity - head, end - capacity); |
1083 |
> |
return a; |
1084 |
|
} |
1085 |
|
|
1086 |
|
/** |
1105 |
|
* The following code can be used to dump the deque into a newly |
1106 |
|
* allocated array of {@code String}: |
1107 |
|
* |
1108 |
< |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1108 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
1109 |
|
* |
1110 |
|
* Note that {@code toArray(new Object[0])} is identical in function to |
1111 |
|
* {@code toArray()}. |
1121 |
|
*/ |
1122 |
|
@SuppressWarnings("unchecked") |
1123 |
|
public <T> T[] toArray(T[] a) { |
1124 |
< |
int size = size(); |
1125 |
< |
if (a.length < size) |
1126 |
< |
a = (T[])java.lang.reflect.Array.newInstance( |
1127 |
< |
a.getClass().getComponentType(), size); |
1128 |
< |
copyElements(a); |
1129 |
< |
if (a.length > size) |
1124 |
> |
final int size = this.size; |
1125 |
> |
if (size > a.length) |
1126 |
> |
return toArray((Class<T[]>) a.getClass()); |
1127 |
> |
final Object[] elements = this.elements; |
1128 |
> |
final int capacity = elements.length; |
1129 |
> |
final int head = this.head, end = head + size; |
1130 |
> |
final int front = (capacity - end >= 0) ? size : capacity - head; |
1131 |
> |
System.arraycopy(elements, head, a, 0, front); |
1132 |
> |
if (front != size) |
1133 |
> |
System.arraycopy(elements, 0, a, capacity - head, end - capacity); |
1134 |
> |
if (size < a.length) |
1135 |
|
a[size] = null; |
1136 |
|
return a; |
1137 |
|
} |
1159 |
|
/** |
1160 |
|
* Saves this deque to a stream (that is, serializes it). |
1161 |
|
* |
1162 |
+ |
* @param s the stream |
1163 |
+ |
* @throws java.io.IOException if an I/O error occurs |
1164 |
|
* @serialData The current size ({@code int}) of the deque, |
1165 |
|
* followed by all of its elements (each an object reference) in |
1166 |
|
* first-to-last order. |
1170 |
|
s.defaultWriteObject(); |
1171 |
|
|
1172 |
|
// Write out size |
1173 |
< |
s.writeInt(size()); |
1173 |
> |
s.writeInt(size); |
1174 |
|
|
1175 |
|
// Write out elements in order. |
1176 |
< |
int mask = elements.length - 1; |
1177 |
< |
for (int i = head; i != tail; i = (i + 1) & mask) |
1178 |
< |
s.writeObject(elements[i]); |
1176 |
> |
final Object[] elements = this.elements; |
1177 |
> |
final int capacity = elements.length; |
1178 |
> |
int from, end, to, todo; |
1179 |
> |
todo = (end = (from = head) + size) |
1180 |
> |
- (to = (capacity - end >= 0) ? end : capacity); |
1181 |
> |
for (;; from = 0, to = todo, todo = 0) { |
1182 |
> |
for (int i = from; i < to; i++) |
1183 |
> |
s.writeObject(elements[i]); |
1184 |
> |
if (todo == 0) break; |
1185 |
> |
} |
1186 |
|
} |
1187 |
|
|
1188 |
|
/** |
1189 |
|
* Reconstitutes this deque from a stream (that is, deserializes it). |
1190 |
+ |
* @param s the stream |
1191 |
+ |
* @throws ClassNotFoundException if the class of a serialized object |
1192 |
+ |
* could not be found |
1193 |
+ |
* @throws java.io.IOException if an I/O error occurs |
1194 |
|
*/ |
1195 |
|
private void readObject(java.io.ObjectInputStream s) |
1196 |
|
throws java.io.IOException, ClassNotFoundException { |
1197 |
|
s.defaultReadObject(); |
1198 |
|
|
1199 |
|
// Read in size and allocate array |
1200 |
< |
int size = s.readInt(); |
841 |
< |
allocateElements(size); |
842 |
< |
head = 0; |
843 |
< |
tail = size; |
1200 |
> |
elements = new Object[size = s.readInt()]; |
1201 |
|
|
1202 |
|
// Read in all elements in the proper order. |
1203 |
|
for (int i = 0; i < size; i++) |
1204 |
|
elements[i] = s.readObject(); |
1205 |
|
} |
1206 |
|
|
1207 |
< |
public Stream<E> stream() { |
1208 |
< |
int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED; |
1209 |
< |
return Streams.stream |
1210 |
< |
(() -> new DeqSpliterator<E>(this, head, tail), flags); |
1211 |
< |
} |
1212 |
< |
public Stream<E> parallelStream() { |
1213 |
< |
int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED; |
1214 |
< |
return Streams.parallelStream |
1215 |
< |
(() -> new DeqSpliterator<E>(this, head, tail), flags); |
1216 |
< |
} |
1217 |
< |
|
1218 |
< |
|
1219 |
< |
static final class DeqSpliterator<E> implements Spliterator<E> { |
1220 |
< |
private final ArrayDeque<E> deq; |
1221 |
< |
private final int fence; // initially tail |
1222 |
< |
private int index; // current index, modified on traverse/split |
1223 |
< |
|
867 |
< |
/** Create new spliterator covering the given array and range */ |
868 |
< |
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
869 |
< |
this.deq = deq; this.index = origin; this.fence = fence; |
870 |
< |
} |
871 |
< |
|
872 |
< |
public DeqSpliterator<E> trySplit() { |
873 |
< |
int n = deq.elements.length; |
874 |
< |
int h = index, t = fence; |
875 |
< |
if (h != t && ((h + 1) & (n - 1)) != t) { |
876 |
< |
if (h > t) |
877 |
< |
t += n; |
878 |
< |
int m = ((h + t) >>> 1) & (n - 1); |
879 |
< |
return new DeqSpliterator<E>(deq, h, index = m); |
880 |
< |
} |
881 |
< |
return null; |
882 |
< |
} |
883 |
< |
|
884 |
< |
public void forEach(Consumer<? super E> block) { |
885 |
< |
if (block == null) |
886 |
< |
throw new NullPointerException(); |
887 |
< |
Object[] a = deq.elements; |
888 |
< |
int m = a.length - 1, f = fence, i = index; |
889 |
< |
index = f; |
890 |
< |
while (i != f) { |
891 |
< |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
892 |
< |
i = (i + 1) & m; |
893 |
< |
if (e == null) |
894 |
< |
throw new ConcurrentModificationException(); |
895 |
< |
block.accept(e); |
896 |
< |
} |
897 |
< |
} |
898 |
< |
|
899 |
< |
public boolean tryAdvance(Consumer<? super E> block) { |
900 |
< |
if (block == null) |
901 |
< |
throw new NullPointerException(); |
902 |
< |
Object[] a = deq.elements; |
903 |
< |
int m = a.length - 1, i = index; |
904 |
< |
if (i != fence) { |
905 |
< |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
906 |
< |
index = (i + 1) & m; |
907 |
< |
if (e == null) |
908 |
< |
throw new ConcurrentModificationException(); |
909 |
< |
block.accept(e); |
910 |
< |
return true; |
911 |
< |
} |
912 |
< |
return false; |
913 |
< |
} |
914 |
< |
|
915 |
< |
// Other spliterator methods |
916 |
< |
public long estimateSize() { |
917 |
< |
int n = fence - index; |
918 |
< |
if (n < 0) |
919 |
< |
n += deq.elements.length; |
920 |
< |
return (long)n; |
1207 |
> |
/** debugging */ |
1208 |
> |
void checkInvariants() { |
1209 |
> |
try { |
1210 |
> |
int capacity = elements.length; |
1211 |
> |
// assert size >= 0 && size <= capacity; |
1212 |
> |
// assert head >= 0; |
1213 |
> |
// assert capacity == 0 || head < capacity; |
1214 |
> |
// assert size == 0 || elements[head] != null; |
1215 |
> |
// assert size == 0 || elements[tail()] != null; |
1216 |
> |
// assert size == capacity || elements[dec(head, capacity)] == null; |
1217 |
> |
// assert size == capacity || elements[inc(tail(), capacity)] == null; |
1218 |
> |
} catch (Throwable t) { |
1219 |
> |
System.err.printf("head=%d size=%d capacity=%d%n", |
1220 |
> |
head, size, elements.length); |
1221 |
> |
System.err.printf("elements=%s%n", |
1222 |
> |
Arrays.toString(elements)); |
1223 |
> |
throw t; |
1224 |
|
} |
922 |
– |
public boolean hasExactSize() { return true; } |
923 |
– |
public boolean hasExactSplits() { return true; } |
1225 |
|
} |
1226 |
|
|
1227 |
|
} |