4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
+ |
import java.util.*; // for javadoc (till 6280605 is fixed) |
8 |
|
import java.io.*; |
9 |
|
|
10 |
|
/** |
13 |
|
* usage. They are not thread-safe; in the absence of external |
14 |
|
* synchronization, they do not support concurrent access by multiple threads. |
15 |
|
* Null elements are prohibited. This class is likely to be faster than |
16 |
< |
* {@link Stack} when used as as a stack, and faster than {@link LinkedList} |
16 |
> |
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
17 |
|
* when used as a queue. |
18 |
|
* |
19 |
|
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
20 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
21 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
22 |
< |
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
22 |
> |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
23 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
24 |
|
* time. |
25 |
|
* |
26 |
|
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
27 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
28 |
< |
* is created, in any way except through the iterator's own remove method, the |
29 |
< |
* iterator will generally throw a {@link ConcurrentModificationException}. |
30 |
< |
* Thus, in the face of concurrent modification, the iterator fails quickly |
31 |
< |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
32 |
< |
* an undetermined time in the future. |
28 |
> |
* is created, in any way except through the iterator's own <tt>remove</tt> |
29 |
> |
* method, the iterator will generally throw a {@link |
30 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
31 |
> |
* modification, the iterator fails quickly and cleanly, rather than risking |
32 |
> |
* arbitrary, non-deterministic behavior at an undetermined time in the |
33 |
> |
* future. |
34 |
|
* |
35 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
36 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
37 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
38 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
38 |
> |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
39 |
|
* Therefore, it would be wrong to write a program that depended on this |
40 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
41 |
|
* should be used only to detect bugs.</i> |
42 |
|
* |
43 |
|
* <p>This class and its iterator implement all of the |
44 |
< |
* optional methods of the {@link Collection} and {@link |
45 |
< |
* Iterator} interfaces. This class is a member of the <a |
46 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
47 |
< |
* Framework</a>. |
44 |
> |
* <em>optional</em> methods of the {@link Collection} and {@link |
45 |
> |
* Iterator} interfaces. |
46 |
> |
* |
47 |
> |
* <p>This class is a member of the |
48 |
> |
* <a href="{@docRoot}/../guide/collections/index.html"> |
49 |
> |
* Java Collections Framework</a>. |
50 |
|
* |
51 |
|
* @author Josh Bloch and Doug Lea |
52 |
|
* @since 1.6 |
56 |
|
implements Deque<E>, Cloneable, Serializable |
57 |
|
{ |
58 |
|
/** |
59 |
< |
* The array in which the elements of in the deque are stored. |
59 |
> |
* The array in which the elements of the deque are stored. |
60 |
|
* The capacity of the deque is the length of this array, which is |
61 |
|
* always a power of two. The array is never allowed to become |
62 |
|
* full, except transiently within an addX method where it is |
91 |
|
/** |
92 |
|
* Allocate empty array to hold the given number of elements. |
93 |
|
* |
94 |
< |
* @param numElements the number of elements to hold. |
94 |
> |
* @param numElements the number of elements to hold |
95 |
|
*/ |
96 |
< |
private void allocateElements(int numElements) { |
96 |
> |
private void allocateElements(int numElements) { |
97 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
98 |
|
// Find the best power of two to hold elements. |
99 |
|
// Tests "<=" because arrays aren't kept full. |
117 |
|
* when head and tail have wrapped around to become equal. |
118 |
|
*/ |
119 |
|
private void doubleCapacity() { |
120 |
< |
assert head == tail; |
120 |
> |
assert head == tail; |
121 |
|
int p = head; |
122 |
|
int n = elements.length; |
123 |
|
int r = n - p; // number of elements to the right of p |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* Copy the elements from our element array into the specified array, |
136 |
> |
* Copies the elements from our element array into the specified array, |
137 |
|
* in order (from first to last element in the deque). It is assumed |
138 |
|
* that the array is large enough to hold all elements in the deque. |
139 |
|
* |
151 |
|
} |
152 |
|
|
153 |
|
/** |
154 |
< |
* Constructs an empty array deque with the an initial capacity |
154 |
> |
* Constructs an empty array deque with an initial capacity |
155 |
|
* sufficient to hold 16 elements. |
156 |
|
*/ |
157 |
|
public ArrayDeque() { |
188 |
|
// terms of these. |
189 |
|
|
190 |
|
/** |
191 |
< |
* Inserts the specified element to the front this deque. |
191 |
> |
* Inserts the specified element at the front of this deque. |
192 |
|
* |
193 |
< |
* @param e the element to insert |
194 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
193 |
> |
* @param e the element to add |
194 |
> |
* @throws NullPointerException if the specified element is null |
195 |
|
*/ |
196 |
|
public void addFirst(E e) { |
197 |
|
if (e == null) |
198 |
|
throw new NullPointerException(); |
199 |
|
elements[head = (head - 1) & (elements.length - 1)] = e; |
200 |
< |
if (head == tail) |
200 |
> |
if (head == tail) |
201 |
|
doubleCapacity(); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* Inserts the specified element to the end this deque. |
206 |
< |
* This method is equivalent to {@link Collection#add} and |
207 |
< |
* {@link #push}. |
205 |
> |
* Inserts the specified element at the end of this deque. |
206 |
> |
* |
207 |
> |
* <p>This method is equivalent to {@link #add}. |
208 |
|
* |
209 |
< |
* @param e the element to insert |
210 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
209 |
> |
* @param e the element to add |
210 |
> |
* @throws NullPointerException if the specified element is null |
211 |
|
*/ |
212 |
|
public void addLast(E e) { |
213 |
|
if (e == null) |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* Retrieves and removes the first element of this deque, or |
218 |
< |
* <tt>null</tt> if this deque is empty. |
221 |
> |
* Inserts the specified element at the front of this deque. |
222 |
|
* |
223 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
224 |
< |
* this deque is empty |
225 |
< |
*/ |
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 |
< |
/** |
251 |
< |
* Inserts the specified element to the front this deque. |
252 |
< |
* |
253 |
< |
* @param e the element to insert |
254 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
255 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
223 |
> |
* @param e the element to add |
224 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
225 |
> |
* @throws NullPointerException if the specified element is null |
226 |
|
*/ |
227 |
|
public boolean offerFirst(E e) { |
228 |
|
addFirst(e); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
233 |
< |
* Inserts the specified element to the end this deque. |
233 |
> |
* Inserts the specified element at the end of this deque. |
234 |
|
* |
235 |
< |
* @param e the element to insert |
236 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
237 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
235 |
> |
* @param e the element to add |
236 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerLast}) |
237 |
> |
* @throws NullPointerException if the specified element is null |
238 |
|
*/ |
239 |
|
public boolean offerLast(E e) { |
240 |
|
addLast(e); |
242 |
|
} |
243 |
|
|
244 |
|
/** |
245 |
< |
* 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 |
245 |
> |
* @throws NoSuchElementException {@inheritDoc} |
246 |
|
*/ |
247 |
|
public E removeFirst() { |
248 |
|
E x = pollFirst(); |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* 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 |
255 |
> |
* @throws NoSuchElementException {@inheritDoc} |
256 |
|
*/ |
257 |
|
public E removeLast() { |
258 |
|
E x = pollLast(); |
261 |
|
return x; |
262 |
|
} |
263 |
|
|
264 |
< |
/** |
265 |
< |
* Retrieves, but does not remove, the first element of this deque, |
266 |
< |
* returning <tt>null</tt> if this deque is empty. |
267 |
< |
* |
268 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
269 |
< |
* this deque is empty |
270 |
< |
*/ |
271 |
< |
public E peekFirst() { |
312 |
< |
return elements[head]; // elements[head] is null if deque empty |
264 |
> |
public E pollFirst() { |
265 |
> |
int h = head; |
266 |
> |
E result = elements[h]; // Element is null if deque empty |
267 |
> |
if (result == null) |
268 |
> |
return null; |
269 |
> |
elements[h] = null; // Must null out slot |
270 |
> |
head = (h + 1) & (elements.length - 1); |
271 |
> |
return result; |
272 |
|
} |
273 |
|
|
274 |
< |
/** |
275 |
< |
* Retrieves, but does not remove, the last element of this deque, |
276 |
< |
* returning <tt>null</tt> if this deque is empty. |
277 |
< |
* |
278 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
279 |
< |
* is empty |
280 |
< |
*/ |
281 |
< |
public E peekLast() { |
323 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
274 |
> |
public E pollLast() { |
275 |
> |
int t = (tail - 1) & (elements.length - 1); |
276 |
> |
E result = elements[t]; |
277 |
> |
if (result == null) |
278 |
> |
return null; |
279 |
> |
elements[t] = null; |
280 |
> |
tail = t; |
281 |
> |
return result; |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
< |
* Retrieves, but does not remove, the first element of this |
328 |
< |
* deque. This method differs from the <tt>peek</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 |
285 |
> |
* @throws NoSuchElementException {@inheritDoc} |
286 |
|
*/ |
287 |
|
public E getFirst() { |
288 |
|
E x = elements[head]; |
292 |
|
} |
293 |
|
|
294 |
|
/** |
295 |
< |
* Retrieves, but does not remove, the last element of this |
343 |
< |
* deque. This method differs from the <tt>peek</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 |
295 |
> |
* @throws NoSuchElementException {@inheritDoc} |
296 |
|
*/ |
297 |
|
public E getLast() { |
298 |
|
E x = elements[(tail - 1) & (elements.length - 1)]; |
301 |
|
return x; |
302 |
|
} |
303 |
|
|
304 |
+ |
public E peekFirst() { |
305 |
+ |
return elements[head]; // elements[head] is null if deque empty |
306 |
+ |
} |
307 |
+ |
|
308 |
+ |
public E peekLast() { |
309 |
+ |
return elements[(tail - 1) & (elements.length - 1)]; |
310 |
+ |
} |
311 |
+ |
|
312 |
|
/** |
313 |
|
* Removes the first occurrence of the specified element in this |
314 |
< |
* deque (when traversing the deque from head to tail). If the deque |
315 |
< |
* does not contain the element, it is unchanged. |
314 |
> |
* deque (when traversing the deque from head to tail). |
315 |
> |
* If the deque does not contain the element, it is unchanged. |
316 |
> |
* More formally, removes the first element <tt>e</tt> such that |
317 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
318 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
319 |
> |
* (or equivalently, if this deque changed as a result of the call). |
320 |
|
* |
321 |
< |
* @param e element to be removed from this deque, if present |
321 |
> |
* @param o element to be removed from this deque, if present |
322 |
|
* @return <tt>true</tt> if the deque contained the specified element |
323 |
|
*/ |
324 |
< |
public boolean removeFirstOccurrence(Object e) { |
325 |
< |
if (e == null) |
324 |
> |
public boolean removeFirstOccurrence(Object o) { |
325 |
> |
if (o == null) |
326 |
|
return false; |
327 |
|
int mask = elements.length - 1; |
328 |
|
int i = head; |
329 |
|
E x; |
330 |
|
while ( (x = elements[i]) != null) { |
331 |
< |
if (e.equals(x)) { |
331 |
> |
if (o.equals(x)) { |
332 |
|
delete(i); |
333 |
|
return true; |
334 |
|
} |
339 |
|
|
340 |
|
/** |
341 |
|
* Removes the last occurrence of the specified element in this |
342 |
< |
* deque (when traversing the deque from head to tail). If the deque |
343 |
< |
* does not contain the element, it is unchanged. |
342 |
> |
* deque (when traversing the deque from head to tail). |
343 |
> |
* If the deque does not contain the element, it is unchanged. |
344 |
> |
* More formally, removes the last element <tt>e</tt> such that |
345 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
346 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
347 |
> |
* (or equivalently, if this deque changed as a result of the call). |
348 |
|
* |
349 |
< |
* @param e element to be removed from this deque, if present |
349 |
> |
* @param o element to be removed from this deque, if present |
350 |
|
* @return <tt>true</tt> if the deque contained the specified element |
351 |
|
*/ |
352 |
< |
public boolean removeLastOccurrence(Object e) { |
353 |
< |
if (e == null) |
352 |
> |
public boolean removeLastOccurrence(Object o) { |
353 |
> |
if (o == null) |
354 |
|
return false; |
355 |
|
int mask = elements.length - 1; |
356 |
|
int i = (tail - 1) & mask; |
357 |
|
E x; |
358 |
|
while ( (x = elements[i]) != null) { |
359 |
< |
if (e.equals(x)) { |
359 |
> |
if (o.equals(x)) { |
360 |
|
delete(i); |
361 |
|
return true; |
362 |
|
} |
368 |
|
// *** Queue methods *** |
369 |
|
|
370 |
|
/** |
371 |
< |
* Inserts the specified element to the end of this deque. |
408 |
< |
* |
409 |
< |
* <p>This method is equivalent to {@link #offerLast}. |
410 |
< |
* |
411 |
< |
* @param e the element to insert |
412 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
413 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
414 |
< |
*/ |
415 |
< |
public boolean offer(E e) { |
416 |
< |
return offerLast(e); |
417 |
< |
} |
418 |
< |
|
419 |
< |
/** |
420 |
< |
* Inserts the specified element to the end of this deque. |
371 |
> |
* Inserts the specified element at the end of this deque. |
372 |
|
* |
373 |
|
* <p>This method is equivalent to {@link #addLast}. |
374 |
|
* |
375 |
< |
* @param e the element to insert |
376 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
377 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
375 |
> |
* @param e the element to add |
376 |
> |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
377 |
> |
* @throws NullPointerException if the specified element is null |
378 |
|
*/ |
379 |
|
public boolean add(E e) { |
380 |
|
addLast(e); |
382 |
|
} |
383 |
|
|
384 |
|
/** |
385 |
< |
* Retrieves and removes the head of the queue represented by |
435 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
436 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
437 |
< |
* if this deque is empty. |
385 |
> |
* Inserts the specified element at the end of this deque. |
386 |
|
* |
387 |
< |
* <p>This method is equivalent to {@link #pollFirst}. |
387 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
388 |
|
* |
389 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
390 |
< |
* this deque is empty |
389 |
> |
* @param e the element to add |
390 |
> |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
391 |
> |
* @throws NullPointerException if the specified element is null |
392 |
|
*/ |
393 |
< |
public E poll() { |
394 |
< |
return pollFirst(); |
393 |
> |
public boolean offer(E e) { |
394 |
> |
return offerLast(e); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
|
* Retrieves and removes the head of the queue represented by this deque. |
399 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
399 |
> |
* |
400 |
> |
* This method differs from {@link #poll poll} only in that it throws an |
401 |
|
* exception if this deque is empty. |
402 |
|
* |
403 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
404 |
|
* |
405 |
|
* @return the head of the queue represented by this deque |
406 |
< |
* @throws NoSuchElementException if this deque is empty |
406 |
> |
* @throws NoSuchElementException {@inheritDoc} |
407 |
|
*/ |
408 |
|
public E remove() { |
409 |
|
return removeFirst(); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
414 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
413 |
> |
* Retrieves and removes the head of the queue represented by this deque |
414 |
> |
* (in other words, the first element of this deque), or returns |
415 |
> |
* <tt>null</tt> if this deque is empty. |
416 |
|
* |
417 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
417 |
> |
* <p>This method is equivalent to {@link #pollFirst}. |
418 |
|
* |
419 |
|
* @return the head of the queue represented by this deque, or |
420 |
< |
* <tt>null</tt> if this deque is empty |
420 |
> |
* <tt>null</tt> if this deque is empty |
421 |
|
*/ |
422 |
< |
public E peek() { |
423 |
< |
return peekFirst(); |
422 |
> |
public E poll() { |
423 |
> |
return pollFirst(); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
|
* Retrieves, but does not remove, the head of the queue represented by |
428 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
428 |
> |
* this deque. This method differs from {@link #peek peek} only in |
429 |
|
* that it throws an exception if this deque is empty. |
430 |
|
* |
431 |
< |
* <p>This method is equivalent to {@link #getFirst} |
431 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
432 |
|
* |
433 |
|
* @return the head of the queue represented by this deque |
434 |
< |
* @throws NoSuchElementException if this deque is empty |
434 |
> |
* @throws NoSuchElementException {@inheritDoc} |
435 |
|
*/ |
436 |
|
public E element() { |
437 |
|
return getFirst(); |
438 |
|
} |
439 |
|
|
440 |
+ |
/** |
441 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
442 |
+ |
* this deque, or returns <tt>null</tt> if this deque is empty. |
443 |
+ |
* |
444 |
+ |
* <p>This method is equivalent to {@link #peekFirst}. |
445 |
+ |
* |
446 |
+ |
* @return the head of the queue represented by this deque, or |
447 |
+ |
* <tt>null</tt> if this deque is empty |
448 |
+ |
*/ |
449 |
+ |
public E peek() { |
450 |
+ |
return peekFirst(); |
451 |
+ |
} |
452 |
+ |
|
453 |
|
// *** Stack methods *** |
454 |
|
|
455 |
|
/** |
456 |
|
* Pushes an element onto the stack represented by this deque. In other |
457 |
< |
* words, inserts the element to the front this deque. |
457 |
> |
* words, inserts the element at the front of this deque. |
458 |
|
* |
459 |
|
* <p>This method is equivalent to {@link #addFirst}. |
460 |
|
* |
461 |
|
* @param e the element to push |
462 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
462 |
> |
* @throws NullPointerException if the specified element is null |
463 |
|
*/ |
464 |
|
public void push(E e) { |
465 |
|
addFirst(e); |
467 |
|
|
468 |
|
/** |
469 |
|
* Pops an element from the stack represented by this deque. In other |
470 |
< |
* words, removes and returns the the first element of this deque. |
470 |
> |
* words, removes and returns the first element of this deque. |
471 |
|
* |
472 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
473 |
|
* |
474 |
|
* @return the element at the front of this deque (which is the top |
475 |
< |
* of the stack represented by this deque) |
476 |
< |
* @throws NoSuchElementException if this deque is empty |
475 |
> |
* of the stack represented by this deque) |
476 |
> |
* @throws NoSuchElementException {@inheritDoc} |
477 |
|
*/ |
478 |
|
public E pop() { |
479 |
|
return removeFirst(); |
480 |
|
} |
481 |
|
|
482 |
+ |
private void checkInvariants() { |
483 |
+ |
assert elements[tail] == null; |
484 |
+ |
assert head == tail ? elements[head] == null : |
485 |
+ |
(elements[head] != null && |
486 |
+ |
elements[(tail - 1) & (elements.length - 1)] != null); |
487 |
+ |
assert elements[(head - 1) & (elements.length - 1)] == null; |
488 |
+ |
} |
489 |
+ |
|
490 |
|
/** |
491 |
< |
* Remove the element at the specified position in the elements array, |
492 |
< |
* adjusting head, tail, and size as necessary. This can result in |
493 |
< |
* motion of elements backwards or forwards in the array. |
494 |
< |
* |
495 |
< |
* <p>This method is called delete rather than remove to emphasize the |
496 |
< |
* that that its semantics differ from those of List.remove(int). |
497 |
< |
* |
491 |
> |
* Removes the element at the specified position in the elements array, |
492 |
> |
* adjusting head and tail as necessary. This can result in motion of |
493 |
> |
* elements backwards or forwards in the array. |
494 |
> |
* |
495 |
> |
* <p>This method is called delete rather than remove to emphasize |
496 |
> |
* that its semantics differ from those of {@link List#remove(int)}. |
497 |
> |
* |
498 |
|
* @return true if elements moved backwards |
499 |
|
*/ |
500 |
|
private boolean delete(int i) { |
501 |
< |
// Case 1: Deque doesn't wrap |
502 |
< |
// Case 2: Deque does wrap and removed element is in the head portion |
503 |
< |
if ((head < tail || tail == 0) || i >= head) { |
504 |
< |
System.arraycopy(elements, head, elements, head + 1, i - head); |
505 |
< |
elements[head] = null; |
506 |
< |
head = (head + 1) & (elements.length - 1); |
507 |
< |
return false; |
508 |
< |
} |
509 |
< |
|
510 |
< |
// Case 3: Deque wraps and removed element is in the tail portion |
511 |
< |
tail--; |
512 |
< |
System.arraycopy(elements, i + 1, elements, i, tail - i); |
513 |
< |
elements[tail] = null; |
514 |
< |
return true; |
501 |
> |
checkInvariants(); |
502 |
> |
final E[] elements = this.elements; |
503 |
> |
final int mask = elements.length - 1; |
504 |
> |
final int h = head; |
505 |
> |
final int t = tail; |
506 |
> |
final int front = (i - h) & mask; |
507 |
> |
final int back = (t - i) & mask; |
508 |
> |
|
509 |
> |
// Invariant: head <= i < tail mod circularity |
510 |
> |
if (front >= ((t - h) & mask)) |
511 |
> |
throw new ConcurrentModificationException(); |
512 |
> |
|
513 |
> |
// Optimize for least element motion |
514 |
> |
if (front < back) { |
515 |
> |
if (h <= i) { |
516 |
> |
System.arraycopy(elements, h, elements, h + 1, front); |
517 |
> |
} else { // Wrap around |
518 |
> |
System.arraycopy(elements, 0, elements, 1, i); |
519 |
> |
elements[0] = elements[mask]; |
520 |
> |
System.arraycopy(elements, h, elements, h + 1, mask - h); |
521 |
> |
} |
522 |
> |
elements[h] = null; |
523 |
> |
head = (h + 1) & mask; |
524 |
> |
return false; |
525 |
> |
} else { |
526 |
> |
if (i < t) { // Copy the null tail as well |
527 |
> |
System.arraycopy(elements, i + 1, elements, i, back); |
528 |
> |
tail = t - 1; |
529 |
> |
} else { // Wrap around |
530 |
> |
System.arraycopy(elements, i + 1, elements, i, mask - i); |
531 |
> |
elements[mask] = elements[0]; |
532 |
> |
System.arraycopy(elements, 1, elements, 0, t); |
533 |
> |
tail = (t - 1) & mask; |
534 |
> |
} |
535 |
> |
return true; |
536 |
> |
} |
537 |
|
} |
538 |
|
|
539 |
|
// *** Collection Methods *** |
548 |
|
} |
549 |
|
|
550 |
|
/** |
551 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
551 |
> |
* Returns <tt>true</tt> if this deque contains no elements. |
552 |
|
* |
553 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
553 |
> |
* @return <tt>true</tt> if this deque contains no elements |
554 |
|
*/ |
555 |
|
public boolean isEmpty() { |
556 |
|
return head == tail; |
561 |
|
* will be ordered from first (head) to last (tail). This is the same |
562 |
|
* order that elements would be dequeued (via successive calls to |
563 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
564 |
< |
* |
565 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
564 |
> |
* |
565 |
> |
* @return an iterator over the elements in this deque |
566 |
|
*/ |
567 |
|
public Iterator<E> iterator() { |
568 |
|
return new DeqIterator(); |
569 |
|
} |
570 |
|
|
571 |
+ |
public Iterator<E> descendingIterator() { |
572 |
+ |
return new DescendingIterator(); |
573 |
+ |
} |
574 |
+ |
|
575 |
|
private class DeqIterator implements Iterator<E> { |
576 |
|
/** |
577 |
|
* Index of element to be returned by subsequent call to next. |
610 |
|
public void remove() { |
611 |
|
if (lastRet < 0) |
612 |
|
throw new IllegalStateException(); |
613 |
< |
if (delete(lastRet)) |
614 |
< |
cursor--; |
613 |
> |
if (delete(lastRet)) // if left-shifted, undo increment in next() |
614 |
> |
cursor = (cursor - 1) & (elements.length - 1); |
615 |
|
lastRet = -1; |
616 |
|
fence = tail; |
617 |
|
} |
618 |
|
} |
619 |
|
|
620 |
+ |
private class DescendingIterator implements Iterator<E> { |
621 |
+ |
/* |
622 |
+ |
* This class is nearly a mirror-image of DeqIterator, using |
623 |
+ |
* (tail-1) instead of head for initial cursor, (head-1) |
624 |
+ |
* instead of tail for fence, and elements.length instead of -1 |
625 |
+ |
* for sentinel. It shares the same structure, but not many |
626 |
+ |
* actual lines of code. |
627 |
+ |
*/ |
628 |
+ |
private int cursor = (tail - 1) & (elements.length - 1); |
629 |
+ |
private int fence = (head - 1) & (elements.length - 1); |
630 |
+ |
private int lastRet = elements.length; |
631 |
+ |
|
632 |
+ |
public boolean hasNext() { |
633 |
+ |
return cursor != fence; |
634 |
+ |
} |
635 |
+ |
|
636 |
+ |
public E next() { |
637 |
+ |
E result; |
638 |
+ |
if (cursor == fence) |
639 |
+ |
throw new NoSuchElementException(); |
640 |
+ |
if (((head - 1) & (elements.length - 1)) != fence || |
641 |
+ |
(result = elements[cursor]) == null) |
642 |
+ |
throw new ConcurrentModificationException(); |
643 |
+ |
lastRet = cursor; |
644 |
+ |
cursor = (cursor - 1) & (elements.length - 1); |
645 |
+ |
return result; |
646 |
+ |
} |
647 |
+ |
|
648 |
+ |
public void remove() { |
649 |
+ |
if (lastRet >= elements.length) |
650 |
+ |
throw new IllegalStateException(); |
651 |
+ |
if (!delete(lastRet)) |
652 |
+ |
cursor = (cursor + 1) & (elements.length - 1); |
653 |
+ |
lastRet = elements.length; |
654 |
+ |
fence = (head - 1) & (elements.length - 1); |
655 |
+ |
} |
656 |
+ |
} |
657 |
+ |
|
658 |
|
/** |
659 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
660 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
661 |
< |
* deque contains at least one element <tt>e</tt> such that |
626 |
< |
* <tt>e.equals(o)</tt>. |
659 |
> |
* Returns <tt>true</tt> if this deque contains the specified element. |
660 |
> |
* More formally, returns <tt>true</tt> if and only if this deque contains |
661 |
> |
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
662 |
|
* |
663 |
|
* @param o object to be checked for containment in this deque |
664 |
|
* @return <tt>true</tt> if this deque contains the specified element |
679 |
|
|
680 |
|
/** |
681 |
|
* Removes a single instance of the specified element from this deque. |
682 |
< |
* This method is equivalent to {@link #removeFirstOccurrence}. |
682 |
> |
* If the deque does not contain the element, it is unchanged. |
683 |
> |
* More formally, removes the first element <tt>e</tt> such that |
684 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
685 |
> |
* Returns <tt>true</tt> if this deque contained the specified element |
686 |
> |
* (or equivalently, if this deque changed as a result of the call). |
687 |
> |
* |
688 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
689 |
|
* |
690 |
< |
* @param e element to be removed from this deque, if present |
690 |
> |
* @param o element to be removed from this deque, if present |
691 |
|
* @return <tt>true</tt> if this deque contained the specified element |
692 |
|
*/ |
693 |
< |
public boolean remove(Object e) { |
694 |
< |
return removeFirstOccurrence(e); |
693 |
> |
public boolean remove(Object o) { |
694 |
> |
return removeFirstOccurrence(o); |
695 |
|
} |
696 |
|
|
697 |
|
/** |
698 |
|
* Removes all of the elements from this deque. |
699 |
+ |
* The deque will be empty after this call returns. |
700 |
|
*/ |
701 |
|
public void clear() { |
702 |
|
int h = head; |
708 |
|
do { |
709 |
|
elements[i] = null; |
710 |
|
i = (i + 1) & mask; |
711 |
< |
} while(i != t); |
711 |
> |
} while (i != t); |
712 |
|
} |
713 |
|
} |
714 |
|
|
715 |
|
/** |
716 |
< |
* Returns an array containing all of the elements in this list |
717 |
< |
* in the correct order. |
716 |
> |
* Returns an array containing all of the elements in this deque |
717 |
> |
* in proper sequence (from first to last element). |
718 |
|
* |
719 |
< |
* @return an array containing all of the elements in this list |
720 |
< |
* in the correct order |
719 |
> |
* <p>The returned array will be "safe" in that no references to it are |
720 |
> |
* maintained by this deque. (In other words, this method must allocate |
721 |
> |
* a new array). The caller is thus free to modify the returned array. |
722 |
> |
* |
723 |
> |
* <p>This method acts as bridge between array-based and collection-based |
724 |
> |
* APIs. |
725 |
> |
* |
726 |
> |
* @return an array containing all of the elements in this deque |
727 |
|
*/ |
728 |
|
public Object[] toArray() { |
729 |
|
return copyElements(new Object[size()]); |
730 |
|
} |
731 |
|
|
732 |
|
/** |
733 |
< |
* Returns an array containing all of the elements in this deque in the |
734 |
< |
* correct order; the runtime type of the returned array is that of the |
735 |
< |
* specified array. If the deque fits in the specified array, it is |
736 |
< |
* returned therein. Otherwise, a new array is allocated with the runtime |
737 |
< |
* type of the specified array and the size of this deque. |
738 |
< |
* |
739 |
< |
* <p>If the deque fits in the specified array with room to spare (i.e., |
740 |
< |
* the array has more elements than the deque), the element in the array |
741 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
733 |
> |
* Returns an array containing all of the elements in this deque in |
734 |
> |
* proper sequence (from first to last element); the runtime type of the |
735 |
> |
* returned array is that of the specified array. If the deque fits in |
736 |
> |
* the specified array, it is returned therein. Otherwise, a new array |
737 |
> |
* is allocated with the runtime type of the specified array and the |
738 |
> |
* size of this deque. |
739 |
> |
* |
740 |
> |
* <p>If this deque fits in the specified array with room to spare |
741 |
> |
* (i.e., the array has more elements than this deque), the element in |
742 |
> |
* the array immediately following the end of the deque is set to |
743 |
> |
* <tt>null</tt>. |
744 |
> |
* |
745 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
746 |
> |
* array-based and collection-based APIs. Further, this method allows |
747 |
> |
* precise control over the runtime type of the output array, and may, |
748 |
> |
* under certain circumstances, be used to save allocation costs. |
749 |
> |
* |
750 |
> |
* <p>Suppose <tt>x</tt> is a deque known to contain only strings. |
751 |
> |
* The following code can be used to dump the deque into a newly |
752 |
> |
* allocated array of <tt>String</tt>: |
753 |
> |
* |
754 |
> |
* <pre> |
755 |
> |
* String[] y = x.toArray(new String[0]);</pre> |
756 |
> |
* |
757 |
> |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
758 |
> |
* <tt>toArray()</tt>. |
759 |
|
* |
760 |
|
* @param a the array into which the elements of the deque are to |
761 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
762 |
< |
* same runtime type is allocated for this purpose |
763 |
< |
* @return an array containing the elements of the deque |
764 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
765 |
< |
* of the runtime type of every element in this deque |
761 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
762 |
> |
* same runtime type is allocated for this purpose |
763 |
> |
* @return an array containing all of the elements in this deque |
764 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
765 |
> |
* is not a supertype of the runtime type of every element in |
766 |
> |
* this deque |
767 |
> |
* @throws NullPointerException if the specified array is null |
768 |
|
*/ |
769 |
|
public <T> T[] toArray(T[] a) { |
770 |
|
int size = size(); |
785 |
|
* @return a copy of this deque |
786 |
|
*/ |
787 |
|
public ArrayDeque<E> clone() { |
788 |
< |
try { |
788 |
> |
try { |
789 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
790 |
|
// These two lines are currently faster than cloning the array: |
791 |
|
result.elements = (E[]) new Object[elements.length]; |
792 |
|
System.arraycopy(elements, 0, result.elements, 0, elements.length); |
793 |
|
return result; |
794 |
|
|
795 |
< |
} catch (CloneNotSupportedException e) { |
795 |
> |
} catch (CloneNotSupportedException e) { |
796 |
|
throw new AssertionError(); |
797 |
|
} |
798 |
|
} |
813 |
|
s.defaultWriteObject(); |
814 |
|
|
815 |
|
// Write out size |
816 |
< |
int size = size(); |
750 |
< |
s.writeInt(size); |
816 |
> |
s.writeInt(size()); |
817 |
|
|
818 |
|
// Write out elements in order. |
753 |
– |
int i = head; |
819 |
|
int mask = elements.length - 1; |
820 |
< |
for (int j = 0; j < size; j++) { |
820 |
> |
for (int i = head; i != tail; i = (i + 1) & mask) |
821 |
|
s.writeObject(elements[i]); |
757 |
– |
i = (i + 1) & mask; |
758 |
– |
} |
822 |
|
} |
823 |
|
|
824 |
|
/** |
837 |
|
// Read in all elements in the proper order. |
838 |
|
for (int i = 0; i < size; i++) |
839 |
|
elements[i] = (E)s.readObject(); |
777 |
– |
|
840 |
|
} |
841 |
|
} |