1 |
|
/* |
2 |
|
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
< |
* as explained at http://creativecommons.org/licenses/publicdomain. |
3 |
> |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
< |
import java.io.*; |
7 |
> |
import java.io.Serializable; |
8 |
> |
import java.util.function.Consumer; |
9 |
> |
import java.util.stream.Stream; |
10 |
> |
import java.util.stream.Streams; |
11 |
|
|
12 |
|
/** |
13 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
18 |
|
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
19 |
|
* when used as a queue. |
20 |
|
* |
21 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
21 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
22 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
23 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
24 |
< |
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
24 |
> |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
25 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
26 |
|
* time. |
27 |
|
* |
28 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
28 |
> |
* <p>The iterators returned by this class's {@code iterator} method are |
29 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
30 |
< |
* is created, in any way except through the iterator's own remove method, the |
31 |
< |
* iterator will generally throw a {@link ConcurrentModificationException}. |
32 |
< |
* Thus, in the face of concurrent modification, the iterator fails quickly |
33 |
< |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
34 |
< |
* an undetermined time in the future. |
30 |
> |
* is created, in any way except through the iterator's own {@code remove} |
31 |
> |
* method, the iterator will generally throw a {@link |
32 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
33 |
> |
* modification, the iterator fails quickly and cleanly, rather than risking |
34 |
> |
* arbitrary, non-deterministic behavior at an undetermined time in the |
35 |
> |
* future. |
36 |
|
* |
37 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
38 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
39 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
40 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
40 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
41 |
|
* Therefore, it would be wrong to write a program that depended on this |
42 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
43 |
|
* should be used only to detect bugs.</i> |
44 |
|
* |
45 |
|
* <p>This class and its iterator implement all of the |
46 |
< |
* optional methods of the {@link Collection} and {@link |
47 |
< |
* Iterator} interfaces. This class is a member of the <a |
48 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
49 |
< |
* Framework</a>. |
46 |
> |
* <em>optional</em> methods of the {@link Collection} and {@link |
47 |
> |
* Iterator} interfaces. |
48 |
> |
* |
49 |
> |
* <p>This class is a member of the |
50 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
51 |
> |
* Java Collections Framework</a>. |
52 |
|
* |
53 |
|
* @author Josh Bloch and Doug Lea |
54 |
|
* @since 1.6 |
67 |
|
* other. We also guarantee that all array cells not holding |
68 |
|
* deque elements are always null. |
69 |
|
*/ |
70 |
< |
private transient E[] elements; |
70 |
> |
transient Object[] elements; // non-private to simplify nested class access |
71 |
|
|
72 |
|
/** |
73 |
|
* The index of the element at the head of the deque (which is the |
74 |
|
* element that would be removed by remove() or pop()); or an |
75 |
|
* arbitrary number equal to tail if the deque is empty. |
76 |
|
*/ |
77 |
< |
private transient int head; |
77 |
> |
transient int head; |
78 |
|
|
79 |
|
/** |
80 |
|
* The index at which the next element would be added to the tail |
81 |
|
* of the deque (via addLast(E), add(E), or push(E)). |
82 |
|
*/ |
83 |
< |
private transient int tail; |
83 |
> |
transient int tail; |
84 |
|
|
85 |
|
/** |
86 |
|
* The minimum capacity that we'll use for a newly created deque. |
91 |
|
// ****** Array allocation and resizing utilities ****** |
92 |
|
|
93 |
|
/** |
94 |
< |
* Allocate empty array to hold the given number of elements. |
94 |
> |
* Allocates empty array to hold the given number of elements. |
95 |
|
* |
96 |
< |
* @param numElements the number of elements to hold. |
96 |
> |
* @param numElements the number of elements to hold |
97 |
|
*/ |
98 |
|
private void allocateElements(int numElements) { |
99 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
111 |
|
if (initialCapacity < 0) // Too many elements, must back off |
112 |
|
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements |
113 |
|
} |
114 |
< |
elements = (E[]) new Object[initialCapacity]; |
114 |
> |
elements = new Object[initialCapacity]; |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* Double the capacity of this deque. Call only when full, i.e., |
118 |
> |
* Doubles the capacity of this deque. Call only when full, i.e., |
119 |
|
* when head and tail have wrapped around to become equal. |
120 |
|
*/ |
121 |
|
private void doubleCapacity() { |
129 |
|
Object[] a = new Object[newCapacity]; |
130 |
|
System.arraycopy(elements, p, a, 0, r); |
131 |
|
System.arraycopy(elements, 0, a, r, p); |
132 |
< |
elements = (E[])a; |
132 |
> |
elements = a; |
133 |
|
head = 0; |
134 |
|
tail = n; |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
< |
* Copy the elements from our element array into the specified array, |
138 |
> |
* Copies the elements from our element array into the specified array, |
139 |
|
* in order (from first to last element in the deque). It is assumed |
140 |
|
* that the array is large enough to hold all elements in the deque. |
141 |
|
* |
157 |
|
* sufficient to hold 16 elements. |
158 |
|
*/ |
159 |
|
public ArrayDeque() { |
160 |
< |
elements = (E[]) new Object[16]; |
160 |
> |
elements = new Object[16]; |
161 |
|
} |
162 |
|
|
163 |
|
/** |
192 |
|
/** |
193 |
|
* Inserts the specified element at the front of this deque. |
194 |
|
* |
195 |
< |
* @param e the element to insert |
196 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
195 |
> |
* @param e the element to add |
196 |
> |
* @throws NullPointerException if the specified element is null |
197 |
|
*/ |
198 |
|
public void addFirst(E e) { |
199 |
|
if (e == null) |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* Inserts the specified element to the end of this deque. |
208 |
< |
* This method is equivalent to {@link Collection#add} and |
209 |
< |
* {@link #push}. |
207 |
> |
* Inserts the specified element at the end of this deque. |
208 |
> |
* |
209 |
> |
* <p>This method is equivalent to {@link #add}. |
210 |
|
* |
211 |
< |
* @param e the element to insert |
212 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
211 |
> |
* @param e the element to add |
212 |
> |
* @throws NullPointerException if the specified element is null |
213 |
|
*/ |
214 |
|
public void addLast(E e) { |
215 |
|
if (e == null) |
220 |
|
} |
221 |
|
|
222 |
|
/** |
217 |
– |
* Retrieves and removes the first element of this deque, or |
218 |
– |
* <tt>null</tt> if this deque is empty. |
219 |
– |
* |
220 |
– |
* @return the first element of this deque, or <tt>null</tt> if |
221 |
– |
* this deque is empty |
222 |
– |
*/ |
223 |
– |
public E pollFirst() { |
224 |
– |
int h = head; |
225 |
– |
E result = elements[h]; // Element is null if deque empty |
226 |
– |
if (result == null) |
227 |
– |
return null; |
228 |
– |
elements[h] = null; // Must null out slot |
229 |
– |
head = (h + 1) & (elements.length - 1); |
230 |
– |
return result; |
231 |
– |
} |
232 |
– |
|
233 |
– |
/** |
234 |
– |
* Retrieves and removes the last element of this deque, or |
235 |
– |
* <tt>null</tt> if this deque is empty. |
236 |
– |
* |
237 |
– |
* @return the last element of this deque, or <tt>null</tt> if |
238 |
– |
* this deque is empty |
239 |
– |
*/ |
240 |
– |
public E pollLast() { |
241 |
– |
int t = (tail - 1) & (elements.length - 1); |
242 |
– |
E result = elements[t]; |
243 |
– |
if (result == null) |
244 |
– |
return null; |
245 |
– |
elements[t] = null; |
246 |
– |
tail = t; |
247 |
– |
return result; |
248 |
– |
} |
249 |
– |
|
250 |
– |
/** |
223 |
|
* Inserts the specified element at the front of this deque. |
224 |
|
* |
225 |
< |
* @param e the element to insert |
226 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
227 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
225 |
> |
* @param e the element to add |
226 |
> |
* @return {@code true} (as specified by {@link Deque#offerFirst}) |
227 |
> |
* @throws NullPointerException if the specified element is null |
228 |
|
*/ |
229 |
|
public boolean offerFirst(E e) { |
230 |
|
addFirst(e); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
< |
* Inserts the specified element to the end of this deque. |
235 |
> |
* Inserts the specified element at the end of this deque. |
236 |
|
* |
237 |
< |
* @param e the element to insert |
238 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
239 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
237 |
> |
* @param e the element to add |
238 |
> |
* @return {@code true} (as specified by {@link Deque#offerLast}) |
239 |
> |
* @throws NullPointerException if the specified element is null |
240 |
|
*/ |
241 |
|
public boolean offerLast(E e) { |
242 |
|
addLast(e); |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* Retrieves and removes the first element of this deque. This method |
276 |
< |
* differs from the <tt>pollFirst</tt> method in that it throws an |
277 |
< |
* exception if this deque is empty. |
278 |
< |
* |
279 |
< |
* @return the first element of this deque |
280 |
< |
* @throws NoSuchElementException if this deque is empty |
247 |
> |
* @throws NoSuchElementException {@inheritDoc} |
248 |
|
*/ |
249 |
|
public E removeFirst() { |
250 |
|
E x = pollFirst(); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* Retrieves and removes the last element of this deque. This method |
291 |
< |
* differs from the <tt>pollLast</tt> method in that it throws an |
292 |
< |
* exception if this deque is empty. |
293 |
< |
* |
294 |
< |
* @return the last element of this deque |
295 |
< |
* @throws NoSuchElementException if this deque is empty |
257 |
> |
* @throws NoSuchElementException {@inheritDoc} |
258 |
|
*/ |
259 |
|
public E removeLast() { |
260 |
|
E x = pollLast(); |
263 |
|
return x; |
264 |
|
} |
265 |
|
|
266 |
< |
/** |
267 |
< |
* Retrieves, but does not remove, the first element of this deque, |
268 |
< |
* returning <tt>null</tt> if this deque is empty. |
269 |
< |
* |
270 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
271 |
< |
* this deque is empty |
272 |
< |
*/ |
273 |
< |
public E peekFirst() { |
274 |
< |
return elements[head]; // elements[head] is null if deque empty |
266 |
> |
public E pollFirst() { |
267 |
> |
int h = head; |
268 |
> |
@SuppressWarnings("unchecked") |
269 |
> |
E result = (E) elements[h]; |
270 |
> |
// Element is null if deque empty |
271 |
> |
if (result == null) |
272 |
> |
return null; |
273 |
> |
elements[h] = null; // Must null out slot |
274 |
> |
head = (h + 1) & (elements.length - 1); |
275 |
> |
return result; |
276 |
|
} |
277 |
|
|
278 |
< |
/** |
279 |
< |
* Retrieves, but does not remove, the last element of this deque, |
280 |
< |
* returning <tt>null</tt> if this deque is empty. |
281 |
< |
* |
282 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
283 |
< |
* is empty |
284 |
< |
*/ |
285 |
< |
public E peekLast() { |
286 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
278 |
> |
public E pollLast() { |
279 |
> |
int t = (tail - 1) & (elements.length - 1); |
280 |
> |
@SuppressWarnings("unchecked") |
281 |
> |
E result = (E) elements[t]; |
282 |
> |
if (result == null) |
283 |
> |
return null; |
284 |
> |
elements[t] = null; |
285 |
> |
tail = t; |
286 |
> |
return result; |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
< |
* Retrieves, but does not remove, the first element of this |
328 |
< |
* deque. This method differs from the <tt>peekFirst</tt> method only |
329 |
< |
* in that it throws an exception if this deque is empty. |
330 |
< |
* |
331 |
< |
* @return the first element of this deque |
332 |
< |
* @throws NoSuchElementException if this deque is empty |
290 |
> |
* @throws NoSuchElementException {@inheritDoc} |
291 |
|
*/ |
292 |
|
public E getFirst() { |
293 |
< |
E x = elements[head]; |
294 |
< |
if (x == null) |
293 |
> |
@SuppressWarnings("unchecked") |
294 |
> |
E result = (E) elements[head]; |
295 |
> |
if (result == null) |
296 |
|
throw new NoSuchElementException(); |
297 |
< |
return x; |
297 |
> |
return result; |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
< |
* Retrieves, but does not remove, the last element of this |
343 |
< |
* deque. This method differs from the <tt>peekLast</tt> method only |
344 |
< |
* in that it throws an exception if this deque is empty. |
345 |
< |
* |
346 |
< |
* @return the last element of this deque |
347 |
< |
* @throws NoSuchElementException if this deque is empty |
301 |
> |
* @throws NoSuchElementException {@inheritDoc} |
302 |
|
*/ |
303 |
|
public E getLast() { |
304 |
< |
E x = elements[(tail - 1) & (elements.length - 1)]; |
305 |
< |
if (x == null) |
304 |
> |
@SuppressWarnings("unchecked") |
305 |
> |
E result = (E) elements[(tail - 1) & (elements.length - 1)]; |
306 |
> |
if (result == null) |
307 |
|
throw new NoSuchElementException(); |
308 |
< |
return x; |
308 |
> |
return result; |
309 |
> |
} |
310 |
> |
|
311 |
> |
@SuppressWarnings("unchecked") |
312 |
> |
public E peekFirst() { |
313 |
> |
// elements[head] is null if deque empty |
314 |
> |
return (E) elements[head]; |
315 |
> |
} |
316 |
> |
|
317 |
> |
@SuppressWarnings("unchecked") |
318 |
> |
public E peekLast() { |
319 |
> |
return (E) elements[(tail - 1) & (elements.length - 1)]; |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
|
* Removes the first occurrence of the specified element in this |
324 |
< |
* deque (when traversing the deque from head to tail). More |
325 |
< |
* formally, removes the first element e such that (o==null ? |
326 |
< |
* e==null : o.equals(e)). If the deque does not contain the |
327 |
< |
* element, it is unchanged. |
324 |
> |
* deque (when traversing the deque from head to tail). |
325 |
> |
* If the deque does not contain the element, it is unchanged. |
326 |
> |
* More formally, removes the first element {@code e} such that |
327 |
> |
* {@code o.equals(e)} (if such an element exists). |
328 |
> |
* Returns {@code true} if this deque contained the specified element |
329 |
> |
* (or equivalently, if this deque changed as a result of the call). |
330 |
|
* |
331 |
|
* @param o element to be removed from this deque, if present |
332 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
332 |
> |
* @return {@code true} if the deque contained the specified element |
333 |
|
*/ |
334 |
|
public boolean removeFirstOccurrence(Object o) { |
335 |
|
if (o == null) |
336 |
|
return false; |
337 |
|
int mask = elements.length - 1; |
338 |
|
int i = head; |
339 |
< |
E x; |
339 |
> |
Object x; |
340 |
|
while ( (x = elements[i]) != null) { |
341 |
|
if (o.equals(x)) { |
342 |
|
delete(i); |
349 |
|
|
350 |
|
/** |
351 |
|
* Removes the last occurrence of the specified element in this |
352 |
< |
* deque (when traversing the deque from head to tail). More |
353 |
< |
* formally, removes the last element e such that (o==null ? |
354 |
< |
* e==null : o.equals(e)). If the deque |
355 |
< |
* does not contain the element, it is unchanged. |
352 |
> |
* deque (when traversing the deque from head to tail). |
353 |
> |
* If the deque does not contain the element, it is unchanged. |
354 |
> |
* More formally, removes the last element {@code e} such that |
355 |
> |
* {@code o.equals(e)} (if such an element exists). |
356 |
> |
* Returns {@code true} if this deque contained the specified element |
357 |
> |
* (or equivalently, if this deque changed as a result of the call). |
358 |
|
* |
359 |
|
* @param o element to be removed from this deque, if present |
360 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
360 |
> |
* @return {@code true} if the deque contained the specified element |
361 |
|
*/ |
362 |
|
public boolean removeLastOccurrence(Object o) { |
363 |
|
if (o == null) |
364 |
|
return false; |
365 |
|
int mask = elements.length - 1; |
366 |
|
int i = (tail - 1) & mask; |
367 |
< |
E x; |
367 |
> |
Object x; |
368 |
|
while ( (x = elements[i]) != null) { |
369 |
|
if (o.equals(x)) { |
370 |
|
delete(i); |
378 |
|
// *** Queue methods *** |
379 |
|
|
380 |
|
/** |
381 |
< |
* Inserts the specified element to the end of this deque. |
412 |
< |
* |
413 |
< |
* <p>This method is equivalent to {@link #offerLast}. |
414 |
< |
* |
415 |
< |
* @param e the element to insert |
416 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
417 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
418 |
< |
*/ |
419 |
< |
public boolean offer(E e) { |
420 |
< |
return offerLast(e); |
421 |
< |
} |
422 |
< |
|
423 |
< |
/** |
424 |
< |
* Inserts the specified element to the end of this deque. |
381 |
> |
* Inserts the specified element at the end of this deque. |
382 |
|
* |
383 |
|
* <p>This method is equivalent to {@link #addLast}. |
384 |
|
* |
385 |
< |
* @param e the element to insert |
386 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
387 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
385 |
> |
* @param e the element to add |
386 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
387 |
> |
* @throws NullPointerException if the specified element is null |
388 |
|
*/ |
389 |
|
public boolean add(E e) { |
390 |
|
addLast(e); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* Retrieves and removes the head of the queue represented by |
439 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
440 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
441 |
< |
* if this deque is empty. |
395 |
> |
* Inserts the specified element at the end of this deque. |
396 |
|
* |
397 |
< |
* <p>This method is equivalent to {@link #pollFirst}. |
397 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
398 |
|
* |
399 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
400 |
< |
* this deque is empty |
399 |
> |
* @param e the element to add |
400 |
> |
* @return {@code true} (as specified by {@link Queue#offer}) |
401 |
> |
* @throws NullPointerException if the specified element is null |
402 |
|
*/ |
403 |
< |
public E poll() { |
404 |
< |
return pollFirst(); |
403 |
> |
public boolean offer(E e) { |
404 |
> |
return offerLast(e); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
|
* Retrieves and removes the head of the queue represented by this deque. |
409 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
409 |
> |
* |
410 |
> |
* This method differs from {@link #poll poll} only in that it throws an |
411 |
|
* exception if this deque is empty. |
412 |
|
* |
413 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
414 |
|
* |
415 |
|
* @return the head of the queue represented by this deque |
416 |
< |
* @throws NoSuchElementException if this deque is empty |
416 |
> |
* @throws NoSuchElementException {@inheritDoc} |
417 |
|
*/ |
418 |
|
public E remove() { |
419 |
|
return removeFirst(); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
424 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
423 |
> |
* Retrieves and removes the head of the queue represented by this deque |
424 |
> |
* (in other words, the first element of this deque), or returns |
425 |
> |
* {@code null} if this deque is empty. |
426 |
|
* |
427 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
427 |
> |
* <p>This method is equivalent to {@link #pollFirst}. |
428 |
|
* |
429 |
|
* @return the head of the queue represented by this deque, or |
430 |
< |
* <tt>null</tt> if this deque is empty |
430 |
> |
* {@code null} if this deque is empty |
431 |
|
*/ |
432 |
< |
public E peek() { |
433 |
< |
return peekFirst(); |
432 |
> |
public E poll() { |
433 |
> |
return pollFirst(); |
434 |
|
} |
435 |
|
|
436 |
|
/** |
437 |
|
* Retrieves, but does not remove, the head of the queue represented by |
438 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
438 |
> |
* this deque. This method differs from {@link #peek peek} only in |
439 |
|
* that it throws an exception if this deque is empty. |
440 |
|
* |
441 |
< |
* <p>This method is equivalent to {@link #getFirst} |
441 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
442 |
|
* |
443 |
|
* @return the head of the queue represented by this deque |
444 |
< |
* @throws NoSuchElementException if this deque is empty |
444 |
> |
* @throws NoSuchElementException {@inheritDoc} |
445 |
|
*/ |
446 |
|
public E element() { |
447 |
|
return getFirst(); |
448 |
|
} |
449 |
|
|
450 |
+ |
/** |
451 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
452 |
+ |
* this deque, or returns {@code null} if this deque is empty. |
453 |
+ |
* |
454 |
+ |
* <p>This method is equivalent to {@link #peekFirst}. |
455 |
+ |
* |
456 |
+ |
* @return the head of the queue represented by this deque, or |
457 |
+ |
* {@code null} if this deque is empty |
458 |
+ |
*/ |
459 |
+ |
public E peek() { |
460 |
+ |
return peekFirst(); |
461 |
+ |
} |
462 |
+ |
|
463 |
|
// *** Stack methods *** |
464 |
|
|
465 |
|
/** |
469 |
|
* <p>This method is equivalent to {@link #addFirst}. |
470 |
|
* |
471 |
|
* @param e the element to push |
472 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
472 |
> |
* @throws NullPointerException if the specified element is null |
473 |
|
*/ |
474 |
|
public void push(E e) { |
475 |
|
addFirst(e); |
482 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
483 |
|
* |
484 |
|
* @return the element at the front of this deque (which is the top |
485 |
< |
* of the stack represented by this deque) |
486 |
< |
* @throws NoSuchElementException if this deque is empty |
485 |
> |
* of the stack represented by this deque) |
486 |
> |
* @throws NoSuchElementException {@inheritDoc} |
487 |
|
*/ |
488 |
|
public E pop() { |
489 |
|
return removeFirst(); |
490 |
|
} |
491 |
|
|
492 |
+ |
private void checkInvariants() { |
493 |
+ |
assert elements[tail] == null; |
494 |
+ |
assert head == tail ? elements[head] == null : |
495 |
+ |
(elements[head] != null && |
496 |
+ |
elements[(tail - 1) & (elements.length - 1)] != null); |
497 |
+ |
assert elements[(head - 1) & (elements.length - 1)] == null; |
498 |
+ |
} |
499 |
+ |
|
500 |
|
/** |
501 |
< |
* Remove the element at the specified position in the elements array, |
502 |
< |
* adjusting head, tail, and size as necessary. This can result in |
503 |
< |
* motion of elements backwards or forwards in the array. |
501 |
> |
* Removes the element at the specified position in the elements array, |
502 |
> |
* adjusting head and tail as necessary. This can result in motion of |
503 |
> |
* elements backwards or forwards in the array. |
504 |
|
* |
505 |
|
* <p>This method is called delete rather than remove to emphasize |
506 |
< |
* that its semantics differ from those of List.remove(int). |
506 |
> |
* that its semantics differ from those of {@link List#remove(int)}. |
507 |
|
* |
508 |
|
* @return true if elements moved backwards |
509 |
|
*/ |
510 |
|
private boolean delete(int i) { |
511 |
< |
// Case 1: Deque doesn't wrap |
512 |
< |
// Case 2: Deque does wrap and removed element is in the head portion |
513 |
< |
if ((head < tail || tail == 0) || i >= head) { |
514 |
< |
System.arraycopy(elements, head, elements, head + 1, i - head); |
515 |
< |
elements[head] = null; |
516 |
< |
head = (head + 1) & (elements.length - 1); |
511 |
> |
checkInvariants(); |
512 |
> |
final Object[] elements = this.elements; |
513 |
> |
final int mask = elements.length - 1; |
514 |
> |
final int h = head; |
515 |
> |
final int t = tail; |
516 |
> |
final int front = (i - h) & mask; |
517 |
> |
final int back = (t - i) & mask; |
518 |
> |
|
519 |
> |
// Invariant: head <= i < tail mod circularity |
520 |
> |
if (front >= ((t - h) & mask)) |
521 |
> |
throw new ConcurrentModificationException(); |
522 |
> |
|
523 |
> |
// Optimize for least element motion |
524 |
> |
if (front < back) { |
525 |
> |
if (h <= i) { |
526 |
> |
System.arraycopy(elements, h, elements, h + 1, front); |
527 |
> |
} else { // Wrap around |
528 |
> |
System.arraycopy(elements, 0, elements, 1, i); |
529 |
> |
elements[0] = elements[mask]; |
530 |
> |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
531 |
> |
} |
532 |
> |
elements[h] = null; |
533 |
> |
head = (h + 1) & mask; |
534 |
|
return false; |
535 |
+ |
} else { |
536 |
+ |
if (i < t) { // Copy the null tail as well |
537 |
+ |
System.arraycopy(elements, i + 1, elements, i, back); |
538 |
+ |
tail = t - 1; |
539 |
+ |
} else { // Wrap around |
540 |
+ |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
541 |
+ |
elements[mask] = elements[0]; |
542 |
+ |
System.arraycopy(elements, 1, elements, 0, t); |
543 |
+ |
tail = (t - 1) & mask; |
544 |
+ |
} |
545 |
+ |
return true; |
546 |
|
} |
541 |
– |
|
542 |
– |
// Case 3: Deque wraps and removed element is in the tail portion |
543 |
– |
tail--; |
544 |
– |
System.arraycopy(elements, i + 1, elements, i, tail - i); |
545 |
– |
elements[tail] = null; |
546 |
– |
return true; |
547 |
|
} |
548 |
|
|
549 |
|
// *** Collection Methods *** |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
561 |
> |
* Returns {@code true} if this deque contains no elements. |
562 |
|
* |
563 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
563 |
> |
* @return {@code true} if this deque contains no elements |
564 |
|
*/ |
565 |
|
public boolean isEmpty() { |
566 |
|
return head == tail; |
572 |
|
* order that elements would be dequeued (via successive calls to |
573 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
574 |
|
* |
575 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
575 |
> |
* @return an iterator over the elements in this deque |
576 |
|
*/ |
577 |
|
public Iterator<E> iterator() { |
578 |
|
return new DeqIterator(); |
579 |
|
} |
580 |
|
|
581 |
+ |
public Iterator<E> descendingIterator() { |
582 |
+ |
return new DescendingIterator(); |
583 |
+ |
} |
584 |
+ |
|
585 |
|
private class DeqIterator implements Iterator<E> { |
586 |
|
/** |
587 |
|
* Index of element to be returned by subsequent call to next. |
605 |
|
} |
606 |
|
|
607 |
|
public E next() { |
604 |
– |
E result; |
608 |
|
if (cursor == fence) |
609 |
|
throw new NoSuchElementException(); |
610 |
+ |
@SuppressWarnings("unchecked") |
611 |
+ |
E result = (E) elements[cursor]; |
612 |
|
// This check doesn't catch all possible comodifications, |
613 |
|
// but does catch the ones that corrupt traversal |
614 |
< |
if (tail != fence || (result = elements[cursor]) == null) |
614 |
> |
if (tail != fence || result == null) |
615 |
|
throw new ConcurrentModificationException(); |
616 |
|
lastRet = cursor; |
617 |
|
cursor = (cursor + 1) & (elements.length - 1); |
621 |
|
public void remove() { |
622 |
|
if (lastRet < 0) |
623 |
|
throw new IllegalStateException(); |
624 |
< |
if (delete(lastRet)) |
625 |
< |
cursor--; |
624 |
> |
if (delete(lastRet)) { // if left-shifted, undo increment in next() |
625 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
626 |
> |
fence = tail; |
627 |
> |
} |
628 |
> |
lastRet = -1; |
629 |
> |
} |
630 |
> |
} |
631 |
> |
|
632 |
> |
private class DescendingIterator implements Iterator<E> { |
633 |
> |
/* |
634 |
> |
* This class is nearly a mirror-image of DeqIterator, using |
635 |
> |
* tail instead of head for initial cursor, and head instead of |
636 |
> |
* tail for fence. |
637 |
> |
*/ |
638 |
> |
private int cursor = tail; |
639 |
> |
private int fence = head; |
640 |
> |
private int lastRet = -1; |
641 |
> |
|
642 |
> |
public boolean hasNext() { |
643 |
> |
return cursor != fence; |
644 |
> |
} |
645 |
> |
|
646 |
> |
public E next() { |
647 |
> |
if (cursor == fence) |
648 |
> |
throw new NoSuchElementException(); |
649 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
650 |
> |
@SuppressWarnings("unchecked") |
651 |
> |
E result = (E) elements[cursor]; |
652 |
> |
if (head != fence || result == null) |
653 |
> |
throw new ConcurrentModificationException(); |
654 |
> |
lastRet = cursor; |
655 |
> |
return result; |
656 |
> |
} |
657 |
> |
|
658 |
> |
public void remove() { |
659 |
> |
if (lastRet < 0) |
660 |
> |
throw new IllegalStateException(); |
661 |
> |
if (!delete(lastRet)) { |
662 |
> |
cursor = (cursor + 1) & (elements.length - 1); |
663 |
> |
fence = head; |
664 |
> |
} |
665 |
|
lastRet = -1; |
622 |
– |
fence = tail; |
666 |
|
} |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
671 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
672 |
< |
* deque contains at least one element <tt>e</tt> such that |
630 |
< |
* <tt>e.equals(o)</tt>. |
670 |
> |
* Returns {@code true} if this deque contains the specified element. |
671 |
> |
* More formally, returns {@code true} if and only if this deque contains |
672 |
> |
* at least one element {@code e} such that {@code o.equals(e)}. |
673 |
|
* |
674 |
|
* @param o object to be checked for containment in this deque |
675 |
< |
* @return <tt>true</tt> if this deque contains the specified element |
675 |
> |
* @return {@code true} if this deque contains the specified element |
676 |
|
*/ |
677 |
|
public boolean contains(Object o) { |
678 |
|
if (o == null) |
679 |
|
return false; |
680 |
|
int mask = elements.length - 1; |
681 |
|
int i = head; |
682 |
< |
E x; |
682 |
> |
Object x; |
683 |
|
while ( (x = elements[i]) != null) { |
684 |
|
if (o.equals(x)) |
685 |
|
return true; |
690 |
|
|
691 |
|
/** |
692 |
|
* Removes a single instance of the specified element from this deque. |
693 |
< |
* This method is equivalent to {@link #removeFirstOccurrence}. |
693 |
> |
* If the deque does not contain the element, it is unchanged. |
694 |
> |
* More formally, removes the first element {@code e} such that |
695 |
> |
* {@code o.equals(e)} (if such an element exists). |
696 |
> |
* Returns {@code true} if this deque contained the specified element |
697 |
> |
* (or equivalently, if this deque changed as a result of the call). |
698 |
|
* |
699 |
< |
* @param e element to be removed from this deque, if present |
700 |
< |
* @return <tt>true</tt> if this deque contained the specified element |
699 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. |
700 |
> |
* |
701 |
> |
* @param o element to be removed from this deque, if present |
702 |
> |
* @return {@code true} if this deque contained the specified element |
703 |
|
*/ |
704 |
< |
public boolean remove(Object e) { |
705 |
< |
return removeFirstOccurrence(e); |
704 |
> |
public boolean remove(Object o) { |
705 |
> |
return removeFirstOccurrence(o); |
706 |
|
} |
707 |
|
|
708 |
|
/** |
709 |
|
* Removes all of the elements from this deque. |
710 |
+ |
* The deque will be empty after this call returns. |
711 |
|
*/ |
712 |
|
public void clear() { |
713 |
|
int h = head; |
719 |
|
do { |
720 |
|
elements[i] = null; |
721 |
|
i = (i + 1) & mask; |
722 |
< |
} while(i != t); |
722 |
> |
} while (i != t); |
723 |
|
} |
724 |
|
} |
725 |
|
|
726 |
|
/** |
727 |
|
* Returns an array containing all of the elements in this deque |
728 |
< |
* in the correct order. |
728 |
> |
* in proper sequence (from first to last element). |
729 |
> |
* |
730 |
> |
* <p>The returned array will be "safe" in that no references to it are |
731 |
> |
* maintained by this deque. (In other words, this method must allocate |
732 |
> |
* a new array). The caller is thus free to modify the returned array. |
733 |
> |
* |
734 |
> |
* <p>This method acts as bridge between array-based and collection-based |
735 |
> |
* APIs. |
736 |
|
* |
737 |
|
* @return an array containing all of the elements in this deque |
682 |
– |
* in the correct order |
738 |
|
*/ |
739 |
|
public Object[] toArray() { |
740 |
< |
return copyElements(new Object[size()]); |
740 |
> |
return copyElements(new Object[size()]); |
741 |
|
} |
742 |
|
|
743 |
|
/** |
744 |
< |
* Returns an array containing all of the elements in this deque in the |
745 |
< |
* correct order; the runtime type of the returned array is that of the |
746 |
< |
* specified array. If the deque fits in the specified array, it is |
747 |
< |
* returned therein. Otherwise, a new array is allocated with the runtime |
748 |
< |
* type of the specified array and the size of this deque. |
744 |
> |
* Returns an array containing all of the elements in this deque in |
745 |
> |
* proper sequence (from first to last element); the runtime type of the |
746 |
> |
* returned array is that of the specified array. If the deque fits in |
747 |
> |
* the specified array, it is returned therein. Otherwise, a new array |
748 |
> |
* is allocated with the runtime type of the specified array and the |
749 |
> |
* size of this deque. |
750 |
> |
* |
751 |
> |
* <p>If this deque fits in the specified array with room to spare |
752 |
> |
* (i.e., the array has more elements than this deque), the element in |
753 |
> |
* the array immediately following the end of the deque is set to |
754 |
> |
* {@code null}. |
755 |
> |
* |
756 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
757 |
> |
* array-based and collection-based APIs. Further, this method allows |
758 |
> |
* precise control over the runtime type of the output array, and may, |
759 |
> |
* under certain circumstances, be used to save allocation costs. |
760 |
|
* |
761 |
< |
* <p>If the deque fits in the specified array with room to spare (i.e., |
762 |
< |
* the array has more elements than the deque), the element in the array |
763 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
761 |
> |
* <p>Suppose {@code x} is a deque known to contain only strings. |
762 |
> |
* The following code can be used to dump the deque into a newly |
763 |
> |
* allocated array of {@code String}: |
764 |
> |
* |
765 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
766 |
> |
* |
767 |
> |
* Note that {@code toArray(new Object[0])} is identical in function to |
768 |
> |
* {@code toArray()}. |
769 |
|
* |
770 |
|
* @param a the array into which the elements of the deque are to |
771 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
772 |
< |
* same runtime type is allocated for this purpose |
773 |
< |
* @return an array containing the elements of the deque |
774 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
775 |
< |
* of the runtime type of every element in this deque |
771 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
772 |
> |
* same runtime type is allocated for this purpose |
773 |
> |
* @return an array containing all of the elements in this deque |
774 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
775 |
> |
* is not a supertype of the runtime type of every element in |
776 |
> |
* this deque |
777 |
> |
* @throws NullPointerException if the specified array is null |
778 |
|
*/ |
779 |
+ |
@SuppressWarnings("unchecked") |
780 |
|
public <T> T[] toArray(T[] a) { |
781 |
|
int size = size(); |
782 |
|
if (a.length < size) |
783 |
|
a = (T[])java.lang.reflect.Array.newInstance( |
784 |
|
a.getClass().getComponentType(), size); |
785 |
< |
copyElements(a); |
785 |
> |
copyElements(a); |
786 |
|
if (a.length > size) |
787 |
|
a[size] = null; |
788 |
|
return a; |
797 |
|
*/ |
798 |
|
public ArrayDeque<E> clone() { |
799 |
|
try { |
800 |
+ |
@SuppressWarnings("unchecked") |
801 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
802 |
< |
// These two lines are currently faster than cloning the array: |
728 |
< |
result.elements = (E[]) new Object[elements.length]; |
729 |
< |
System.arraycopy(elements, 0, result.elements, 0, elements.length); |
802 |
> |
result.elements = Arrays.copyOf(elements, elements.length); |
803 |
|
return result; |
731 |
– |
|
804 |
|
} catch (CloneNotSupportedException e) { |
805 |
|
throw new AssertionError(); |
806 |
|
} |
807 |
|
} |
808 |
|
|
737 |
– |
/** |
738 |
– |
* Appease the serialization gods. |
739 |
– |
*/ |
809 |
|
private static final long serialVersionUID = 2340985798034038923L; |
810 |
|
|
811 |
|
/** |
812 |
< |
* Serialize this deque. |
812 |
> |
* Saves this deque to a stream (that is, serializes it). |
813 |
|
* |
814 |
< |
* @serialData The current size (<tt>int</tt>) of the deque, |
814 |
> |
* @serialData The current size ({@code int}) of the deque, |
815 |
|
* followed by all of its elements (each an object reference) in |
816 |
|
* first-to-last order. |
817 |
|
*/ |
818 |
< |
private void writeObject(ObjectOutputStream s) throws IOException { |
818 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
819 |
> |
throws java.io.IOException { |
820 |
|
s.defaultWriteObject(); |
821 |
|
|
822 |
|
// Write out size |
823 |
< |
int size = size(); |
754 |
< |
s.writeInt(size); |
823 |
> |
s.writeInt(size()); |
824 |
|
|
825 |
|
// Write out elements in order. |
757 |
– |
int i = head; |
826 |
|
int mask = elements.length - 1; |
827 |
< |
for (int j = 0; j < size; j++) { |
827 |
> |
for (int i = head; i != tail; i = (i + 1) & mask) |
828 |
|
s.writeObject(elements[i]); |
761 |
– |
i = (i + 1) & mask; |
762 |
– |
} |
829 |
|
} |
830 |
|
|
831 |
|
/** |
832 |
< |
* Deserialize this deque. |
832 |
> |
* Reconstitutes this deque from a stream (that is, deserializes it). |
833 |
|
*/ |
834 |
< |
private void readObject(ObjectInputStream s) |
835 |
< |
throws IOException, ClassNotFoundException { |
834 |
> |
private void readObject(java.io.ObjectInputStream s) |
835 |
> |
throws java.io.IOException, ClassNotFoundException { |
836 |
|
s.defaultReadObject(); |
837 |
|
|
838 |
|
// Read in size and allocate array |
843 |
|
|
844 |
|
// Read in all elements in the proper order. |
845 |
|
for (int i = 0; i < size; i++) |
846 |
< |
elements[i] = (E)s.readObject(); |
846 |
> |
elements[i] = s.readObject(); |
847 |
> |
} |
848 |
> |
|
849 |
> |
Spliterator<E> spliterator() { |
850 |
> |
return new DeqSpliterator<E>(this, -1, -1); |
851 |
> |
} |
852 |
> |
|
853 |
> |
public Stream<E> stream() { |
854 |
> |
return Streams.stream(spliterator()); |
855 |
> |
} |
856 |
> |
|
857 |
> |
public Stream<E> parallelStream() { |
858 |
> |
return Streams.parallelStream(spliterator()); |
859 |
> |
} |
860 |
> |
|
861 |
> |
static final class DeqSpliterator<E> implements Spliterator<E> { |
862 |
> |
private final ArrayDeque<E> deq; |
863 |
> |
private int fence; // -1 until first use |
864 |
> |
private int index; // current index, modified on traverse/split |
865 |
> |
|
866 |
> |
/** Create new spliterator covering the given array and range */ |
867 |
> |
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
868 |
> |
this.deq = deq; |
869 |
> |
this.index = origin; |
870 |
> |
this.fence = fence; |
871 |
> |
} |
872 |
> |
|
873 |
> |
private int getFence() { // force initialization |
874 |
> |
int t; |
875 |
> |
if ((t = fence) < 0) { |
876 |
> |
t = fence = deq.tail; |
877 |
> |
index = deq.head; |
878 |
> |
} |
879 |
> |
return t; |
880 |
> |
} |
881 |
> |
|
882 |
> |
public DeqSpliterator<E> trySplit() { |
883 |
> |
int t = getFence(), h = index, n = deq.elements.length; |
884 |
> |
if (h != t && ((h + 1) & (n - 1)) != t) { |
885 |
> |
if (h > t) |
886 |
> |
t += n; |
887 |
> |
int m = ((h + t) >>> 1) & (n - 1); |
888 |
> |
return new DeqSpliterator<>(deq, h, index = m); |
889 |
> |
} |
890 |
> |
return null; |
891 |
> |
} |
892 |
> |
|
893 |
> |
public void forEach(Consumer<? super E> consumer) { |
894 |
> |
if (consumer == null) |
895 |
> |
throw new NullPointerException(); |
896 |
> |
Object[] a = deq.elements; |
897 |
> |
int m = a.length - 1, f = getFence(), i = index; |
898 |
> |
index = f; |
899 |
> |
while (i != f) { |
900 |
> |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
901 |
> |
i = (i + 1) & m; |
902 |
> |
if (e == null) |
903 |
> |
throw new ConcurrentModificationException(); |
904 |
> |
consumer.accept(e); |
905 |
> |
} |
906 |
> |
} |
907 |
|
|
908 |
+ |
public boolean tryAdvance(Consumer<? super E> consumer) { |
909 |
+ |
if (consumer == null) |
910 |
+ |
throw new NullPointerException(); |
911 |
+ |
Object[] a = deq.elements; |
912 |
+ |
int m = a.length - 1, f = getFence(), i = index; |
913 |
+ |
if (i != fence) { |
914 |
+ |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
915 |
+ |
index = (i + 1) & m; |
916 |
+ |
if (e == null) |
917 |
+ |
throw new ConcurrentModificationException(); |
918 |
+ |
consumer.accept(e); |
919 |
+ |
return true; |
920 |
+ |
} |
921 |
+ |
return false; |
922 |
+ |
} |
923 |
+ |
|
924 |
+ |
public long estimateSize() { |
925 |
+ |
int n = getFence() - index; |
926 |
+ |
if (n < 0) |
927 |
+ |
n += deq.elements.length; |
928 |
+ |
return (long) n; |
929 |
+ |
} |
930 |
+ |
|
931 |
+ |
@Override |
932 |
+ |
public int characteristics() { |
933 |
+ |
return Spliterator.ORDERED | Spliterator.SIZED | |
934 |
+ |
Spliterator.NONNULL | Spliterator.SUBSIZED; |
935 |
+ |
} |
936 |
|
} |
937 |
+ |
|
938 |
|
} |