18 |
|
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
19 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
20 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
21 |
< |
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
21 |
> |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
22 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
23 |
|
* time. |
24 |
|
* |
25 |
|
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
26 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
27 |
< |
* is created, in any way except through the iterator's own remove method, the |
28 |
< |
* iterator will generally throw a {@link ConcurrentModificationException}. |
29 |
< |
* Thus, in the face of concurrent modification, the iterator fails quickly |
30 |
< |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
31 |
< |
* an undetermined time in the future. |
27 |
> |
* is created, in any way except through the iterator's own <tt>remove</tt> |
28 |
> |
* method, the iterator will generally throw a {@link |
29 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
30 |
> |
* modification, the iterator fails quickly and cleanly, rather than risking |
31 |
> |
* arbitrary, non-deterministic behavior at an undetermined time in the |
32 |
> |
* future. |
33 |
|
* |
34 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
35 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
36 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
37 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
37 |
> |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
38 |
|
* Therefore, it would be wrong to write a program that depended on this |
39 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
40 |
|
* should be used only to detect bugs.</i> |
41 |
|
* |
42 |
|
* <p>This class and its iterator implement all of the |
43 |
< |
* optional methods of the {@link Collection} and {@link |
44 |
< |
* Iterator} interfaces. This class is a member of the <a |
45 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
46 |
< |
* Framework</a>. |
43 |
> |
* <em>optional</em> methods of the {@link Collection} and {@link |
44 |
> |
* Iterator} interfaces. |
45 |
> |
* |
46 |
> |
* <p>This class is a member of the |
47 |
> |
* <a href="{@docRoot}/../guide/collections/index.html"> |
48 |
> |
* Java Collections Framework</a>. |
49 |
|
* |
50 |
|
* @author Josh Bloch and Doug Lea |
51 |
|
* @since 1.6 |
55 |
|
implements Deque<E>, Cloneable, Serializable |
56 |
|
{ |
57 |
|
/** |
58 |
< |
* The array in which the elements of in the deque are stored. |
58 |
> |
* The array in which the elements of the deque are stored. |
59 |
|
* The capacity of the deque is the length of this array, which is |
60 |
|
* always a power of two. The array is never allowed to become |
61 |
|
* full, except transiently within an addX method where it is |
90 |
|
/** |
91 |
|
* Allocate empty array to hold the given number of elements. |
92 |
|
* |
93 |
< |
* @param numElements the number of elements to hold. |
93 |
> |
* @param numElements the number of elements to hold |
94 |
|
*/ |
95 |
< |
private void allocateElements(int numElements) { |
95 |
> |
private void allocateElements(int numElements) { |
96 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
97 |
|
// Find the best power of two to hold elements. |
98 |
|
// Tests "<=" because arrays aren't kept full. |
116 |
|
* when head and tail have wrapped around to become equal. |
117 |
|
*/ |
118 |
|
private void doubleCapacity() { |
119 |
< |
assert head == tail; |
119 |
> |
assert head == tail; |
120 |
|
int p = head; |
121 |
|
int n = elements.length; |
122 |
|
int r = n - p; // number of elements to the right of p |
132 |
|
} |
133 |
|
|
134 |
|
/** |
135 |
< |
* Copy the elements from our element array into the specified array, |
135 |
> |
* Copies the elements from our element array into the specified array, |
136 |
|
* in order (from first to last element in the deque). It is assumed |
137 |
|
* that the array is large enough to hold all elements in the deque. |
138 |
|
* |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
< |
* Constructs an empty array deque with the an initial capacity |
153 |
> |
* Constructs an empty array deque with an initial capacity |
154 |
|
* sufficient to hold 16 elements. |
155 |
|
*/ |
156 |
|
public ArrayDeque() { |
187 |
|
// terms of these. |
188 |
|
|
189 |
|
/** |
190 |
< |
* Inserts the specified element to the front this deque. |
190 |
> |
* Inserts the specified element at the front of this deque. |
191 |
|
* |
192 |
< |
* @param e the element to insert |
193 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
192 |
> |
* @param e the element to add |
193 |
> |
* @throws NullPointerException if the specified element is null |
194 |
|
*/ |
195 |
|
public void addFirst(E e) { |
196 |
|
if (e == null) |
197 |
|
throw new NullPointerException(); |
198 |
|
elements[head = (head - 1) & (elements.length - 1)] = e; |
199 |
< |
if (head == tail) |
199 |
> |
if (head == tail) |
200 |
|
doubleCapacity(); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
< |
* Inserts the specified element to the end this deque. |
205 |
< |
* This method is equivalent to {@link Collection#add} and |
203 |
< |
* {@link #push}. |
204 |
> |
* Inserts the specified element at the end of this deque. |
205 |
> |
* This method is equivalent to {@link #add} and {@link #push}. |
206 |
|
* |
207 |
< |
* @param e the element to insert |
208 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
207 |
> |
* @param e the element to add |
208 |
> |
* @throws NullPointerException if the specified element is null |
209 |
|
*/ |
210 |
|
public void addLast(E e) { |
211 |
|
if (e == null) |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* 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. |
219 |
> |
* Inserts the specified element at the front of this deque. |
220 |
|
* |
221 |
< |
* @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 |
221 |
> |
* @param e the element to add |
222 |
|
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
223 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
223 |
> |
* @throws NullPointerException if the specified element is null |
224 |
|
*/ |
225 |
|
public boolean offerFirst(E e) { |
226 |
|
addFirst(e); |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* Inserts the specified element to the end this deque. |
231 |
> |
* Inserts the specified element at the end of this deque. |
232 |
|
* |
233 |
< |
* @param e the element to insert |
233 |
> |
* @param e the element to add |
234 |
|
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
235 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
235 |
> |
* @throws NullPointerException if the specified element is null |
236 |
|
*/ |
237 |
|
public boolean offerLast(E e) { |
238 |
|
addLast(e); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
< |
* 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 |
243 |
> |
* @throws NoSuchElementException {@inheritDoc} |
244 |
|
*/ |
245 |
|
public E removeFirst() { |
246 |
|
E x = pollFirst(); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* 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 |
253 |
> |
* @throws NoSuchElementException {@inheritDoc} |
254 |
|
*/ |
255 |
|
public E removeLast() { |
256 |
|
E x = pollLast(); |
259 |
|
return x; |
260 |
|
} |
261 |
|
|
262 |
< |
/** |
263 |
< |
* Retrieves, but does not remove, the first element of this deque, |
264 |
< |
* returning <tt>null</tt> if this deque is empty. |
265 |
< |
* |
266 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
267 |
< |
* this deque is empty |
268 |
< |
*/ |
269 |
< |
public E peekFirst() { |
312 |
< |
return elements[head]; // elements[head] is null if deque empty |
262 |
> |
public E pollFirst() { |
263 |
> |
int h = head; |
264 |
> |
E result = elements[h]; // Element is null if deque empty |
265 |
> |
if (result == null) |
266 |
> |
return null; |
267 |
> |
elements[h] = null; // Must null out slot |
268 |
> |
head = (h + 1) & (elements.length - 1); |
269 |
> |
return result; |
270 |
|
} |
271 |
|
|
272 |
< |
/** |
273 |
< |
* Retrieves, but does not remove, the last element of this deque, |
274 |
< |
* returning <tt>null</tt> if this deque is empty. |
275 |
< |
* |
276 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
277 |
< |
* is empty |
278 |
< |
*/ |
279 |
< |
public E peekLast() { |
323 |
< |
return elements[(tail - 1) & (elements.length - 1)]; |
272 |
> |
public E pollLast() { |
273 |
> |
int t = (tail - 1) & (elements.length - 1); |
274 |
> |
E result = elements[t]; |
275 |
> |
if (result == null) |
276 |
> |
return null; |
277 |
> |
elements[t] = null; |
278 |
> |
tail = t; |
279 |
> |
return result; |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
< |
* 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 |
283 |
> |
* @throws NoSuchElementException {@inheritDoc} |
284 |
|
*/ |
285 |
|
public E getFirst() { |
286 |
|
E x = elements[head]; |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
< |
* 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 |
293 |
> |
* @throws NoSuchElementException {@inheritDoc} |
294 |
|
*/ |
295 |
|
public E getLast() { |
296 |
|
E x = elements[(tail - 1) & (elements.length - 1)]; |
299 |
|
return x; |
300 |
|
} |
301 |
|
|
302 |
+ |
public E peekFirst() { |
303 |
+ |
return elements[head]; // elements[head] is null if deque empty |
304 |
+ |
} |
305 |
+ |
|
306 |
+ |
public E peekLast() { |
307 |
+ |
return elements[(tail - 1) & (elements.length - 1)]; |
308 |
+ |
} |
309 |
+ |
|
310 |
|
/** |
311 |
|
* Removes the first occurrence of the specified element in this |
312 |
< |
* deque (when traversing the deque from head to tail). If the deque |
313 |
< |
* does not contain the element, it is unchanged. |
312 |
> |
* deque (when traversing the deque from head to tail). |
313 |
> |
* If the deque does not contain the element, it is unchanged. |
314 |
> |
* More formally, removes the first element <tt>e</tt> such that |
315 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
316 |
> |
* Returns true if this deque contained the specified element (or |
317 |
> |
* equivalently, if this deque changed as a result of the call). |
318 |
|
* |
319 |
< |
* @param e element to be removed from this deque, if present |
319 |
> |
* @param o element to be removed from this deque, if present |
320 |
|
* @return <tt>true</tt> if the deque contained the specified element |
321 |
|
*/ |
322 |
< |
public boolean removeFirstOccurrence(Object e) { |
323 |
< |
if (e == null) |
322 |
> |
public boolean removeFirstOccurrence(Object o) { |
323 |
> |
if (o == null) |
324 |
|
return false; |
325 |
|
int mask = elements.length - 1; |
326 |
|
int i = head; |
327 |
|
E x; |
328 |
|
while ( (x = elements[i]) != null) { |
329 |
< |
if (e.equals(x)) { |
329 |
> |
if (o.equals(x)) { |
330 |
|
delete(i); |
331 |
|
return true; |
332 |
|
} |
337 |
|
|
338 |
|
/** |
339 |
|
* Removes the last occurrence of the specified element in this |
340 |
< |
* deque (when traversing the deque from head to tail). If the deque |
341 |
< |
* does not contain the element, it is unchanged. |
340 |
> |
* deque (when traversing the deque from head to tail). |
341 |
> |
* If the deque does not contain the element, it is unchanged. |
342 |
> |
* More formally, removes the last element <tt>e</tt> such that |
343 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
344 |
> |
* Returns true if this deque contained the specified element (or |
345 |
> |
* equivalently, if this deque changed as a result of the call). |
346 |
|
* |
347 |
< |
* @param e element to be removed from this deque, if present |
347 |
> |
* @param o element to be removed from this deque, if present |
348 |
|
* @return <tt>true</tt> if the deque contained the specified element |
349 |
|
*/ |
350 |
< |
public boolean removeLastOccurrence(Object e) { |
351 |
< |
if (e == null) |
350 |
> |
public boolean removeLastOccurrence(Object o) { |
351 |
> |
if (o == null) |
352 |
|
return false; |
353 |
|
int mask = elements.length - 1; |
354 |
|
int i = (tail - 1) & mask; |
355 |
|
E x; |
356 |
|
while ( (x = elements[i]) != null) { |
357 |
< |
if (e.equals(x)) { |
357 |
> |
if (o.equals(x)) { |
358 |
|
delete(i); |
359 |
|
return true; |
360 |
|
} |
366 |
|
// *** Queue methods *** |
367 |
|
|
368 |
|
/** |
369 |
< |
* 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. |
369 |
> |
* Inserts the specified element at the end of this deque. |
370 |
|
* |
371 |
|
* <p>This method is equivalent to {@link #addLast}. |
372 |
|
* |
373 |
< |
* @param e the element to insert |
373 |
> |
* @param e the element to add |
374 |
|
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
375 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
375 |
> |
* @throws NullPointerException if the specified element is null |
376 |
|
*/ |
377 |
|
public boolean add(E e) { |
378 |
|
addLast(e); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
< |
* 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. |
383 |
> |
* Inserts the specified element at the end of this deque. |
384 |
|
* |
385 |
< |
* <p>This method is equivalent to {@link #pollFirst}. |
385 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
386 |
|
* |
387 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
388 |
< |
* this deque is empty |
387 |
> |
* @param e the element to add |
388 |
> |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
389 |
> |
* @throws NullPointerException if the specified element is null |
390 |
|
*/ |
391 |
< |
public E poll() { |
392 |
< |
return pollFirst(); |
391 |
> |
public boolean offer(E e) { |
392 |
> |
return offerLast(e); |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* Retrieves and removes the head of the queue represented by this deque. |
397 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
397 |
> |
* This method differs from {@link #poll} only in that it throws an |
398 |
|
* exception if this deque is empty. |
399 |
|
* |
400 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
401 |
|
* |
402 |
|
* @return the head of the queue represented by this deque |
403 |
< |
* @throws NoSuchElementException if this deque is empty |
403 |
> |
* @throws NoSuchElementException {@inheritDoc} |
404 |
|
*/ |
405 |
|
public E remove() { |
406 |
|
return removeFirst(); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
411 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
410 |
> |
* Retrieves and removes the head of the queue represented by this deque |
411 |
> |
* (in other words, the first element of this deque), or returns |
412 |
> |
* <tt>null</tt> if this deque is empty. |
413 |
|
* |
414 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
414 |
> |
* <p>This method is equivalent to {@link #pollFirst}. |
415 |
|
* |
416 |
|
* @return the head of the queue represented by this deque, or |
417 |
< |
* <tt>null</tt> if this deque is empty |
417 |
> |
* <tt>null</tt> if this deque is empty |
418 |
|
*/ |
419 |
< |
public E peek() { |
420 |
< |
return peekFirst(); |
419 |
> |
public E poll() { |
420 |
> |
return pollFirst(); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
|
* Retrieves, but does not remove, the head of the queue represented by |
425 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
426 |
< |
* that it throws an exception if this deque is empty. |
425 |
> |
* this deque. This method differs from {@link #peek} only in that it |
426 |
> |
* throws an exception if this deque is empty. |
427 |
|
* |
428 |
< |
* <p>This method is equivalent to {@link #getFirst} |
428 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
429 |
|
* |
430 |
|
* @return the head of the queue represented by this deque |
431 |
< |
* @throws NoSuchElementException if this deque is empty |
431 |
> |
* @throws NoSuchElementException {@inheritDoc} |
432 |
|
*/ |
433 |
|
public E element() { |
434 |
|
return getFirst(); |
435 |
|
} |
436 |
|
|
437 |
+ |
/** |
438 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
439 |
+ |
* this deque, or returns <tt>null</tt> if this deque is empty. |
440 |
+ |
* |
441 |
+ |
* <p>This method is equivalent to {@link #peekFirst}. |
442 |
+ |
* |
443 |
+ |
* @return the head of the queue represented by this deque, or |
444 |
+ |
* <tt>null</tt> if this deque is empty |
445 |
+ |
*/ |
446 |
+ |
public E peek() { |
447 |
+ |
return peekFirst(); |
448 |
+ |
} |
449 |
+ |
|
450 |
|
// *** Stack methods *** |
451 |
|
|
452 |
|
/** |
453 |
|
* Pushes an element onto the stack represented by this deque. In other |
454 |
< |
* words, inserts the element to the front this deque. |
454 |
> |
* words, inserts the element at the front of this deque. |
455 |
|
* |
456 |
|
* <p>This method is equivalent to {@link #addFirst}. |
457 |
|
* |
458 |
|
* @param e the element to push |
459 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
459 |
> |
* @throws NullPointerException if the specified element is null |
460 |
|
*/ |
461 |
|
public void push(E e) { |
462 |
|
addFirst(e); |
469 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
470 |
|
* |
471 |
|
* @return the element at the front of this deque (which is the top |
472 |
< |
* of the stack represented by this deque) |
473 |
< |
* @throws NoSuchElementException if this deque is empty |
472 |
> |
* of the stack represented by this deque) |
473 |
> |
* @throws NoSuchElementException {@inheritDoc} |
474 |
|
*/ |
475 |
|
public E pop() { |
476 |
|
return removeFirst(); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* Remove the element at the specified position in the elements array, |
481 |
< |
* adjusting head, tail, and size as necessary. This can result in |
482 |
< |
* motion of elements backwards or forwards in the array. |
480 |
> |
* Removes the element at the specified position in the elements array, |
481 |
> |
* adjusting head and tail as necessary. This can result in motion of |
482 |
> |
* elements backwards or forwards in the array. |
483 |
> |
* |
484 |
> |
* <p>This method is called delete rather than remove to emphasize |
485 |
> |
* that its semantics differ from those of {@link List#remove(int)}. |
486 |
|
* |
523 |
– |
* <p>This method is called delete rather than remove to emphasize |
524 |
– |
* that its semantics differ from those of List.remove(int). |
525 |
– |
* |
487 |
|
* @return true if elements moved backwards |
488 |
|
*/ |
489 |
|
private boolean delete(int i) { |
490 |
+ |
int mask = elements.length - 1; |
491 |
+ |
|
492 |
+ |
// Invariant: head <= i < tail mod circularity |
493 |
+ |
if (((i - head) & mask) >= ((tail - head) & mask)) |
494 |
+ |
throw new ConcurrentModificationException(); |
495 |
+ |
|
496 |
|
// Case 1: Deque doesn't wrap |
497 |
|
// Case 2: Deque does wrap and removed element is in the head portion |
498 |
< |
if ((head < tail || tail == 0) || i >= head) { |
498 |
> |
if (i >= head) { |
499 |
|
System.arraycopy(elements, head, elements, head + 1, i - head); |
500 |
|
elements[head] = null; |
501 |
< |
head = (head + 1) & (elements.length - 1); |
501 |
> |
head = (head + 1) & mask; |
502 |
|
return false; |
503 |
|
} |
504 |
|
|
521 |
|
} |
522 |
|
|
523 |
|
/** |
524 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
524 |
> |
* Returns <tt>true</tt> if this deque contains no elements. |
525 |
|
* |
526 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
526 |
> |
* @return <tt>true</tt> if this deque contains no elements |
527 |
|
*/ |
528 |
|
public boolean isEmpty() { |
529 |
|
return head == tail; |
534 |
|
* will be ordered from first (head) to last (tail). This is the same |
535 |
|
* order that elements would be dequeued (via successive calls to |
536 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
537 |
< |
* |
537 |
> |
* |
538 |
|
* @return an <tt>Iterator</tt> over the elements in this deque |
539 |
|
*/ |
540 |
|
public Iterator<E> iterator() { |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
591 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
592 |
< |
* deque contains at least one element <tt>e</tt> such that |
626 |
< |
* <tt>e.equals(o)</tt>. |
590 |
> |
* Returns <tt>true</tt> if this deque contains the specified element. |
591 |
> |
* More formally, returns <tt>true</tt> if and only if this deque contains |
592 |
> |
* at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. |
593 |
|
* |
594 |
|
* @param o object to be checked for containment in this deque |
595 |
|
* @return <tt>true</tt> if this deque contains the specified element |
610 |
|
|
611 |
|
/** |
612 |
|
* Removes a single instance of the specified element from this deque. |
613 |
< |
* This method is equivalent to {@link #removeFirstOccurrence}. |
613 |
> |
* If the deque does not contain the element, it is unchanged. |
614 |
> |
* More formally, removes the first element <tt>e</tt> such that |
615 |
> |
* <tt>o.equals(e)</tt> (if such an element exists). |
616 |
> |
* Returns true if this deque contained the specified element (or |
617 |
> |
* equivalently, if this deque changed as a result of the call). |
618 |
|
* |
619 |
< |
* @param e element to be removed from this deque, if present |
619 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
620 |
> |
* |
621 |
> |
* @param o element to be removed from this deque, if present |
622 |
|
* @return <tt>true</tt> if this deque contained the specified element |
623 |
|
*/ |
624 |
< |
public boolean remove(Object e) { |
625 |
< |
return removeFirstOccurrence(e); |
624 |
> |
public boolean remove(Object o) { |
625 |
> |
return removeFirstOccurrence(o); |
626 |
|
} |
627 |
|
|
628 |
|
/** |
629 |
|
* Removes all of the elements from this deque. |
630 |
+ |
* The deque will be empty after this call returns. |
631 |
|
*/ |
632 |
|
public void clear() { |
633 |
|
int h = head; |
639 |
|
do { |
640 |
|
elements[i] = null; |
641 |
|
i = (i + 1) & mask; |
642 |
< |
} while(i != t); |
642 |
> |
} while (i != t); |
643 |
|
} |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* Returns an array containing all of the elements in this list |
648 |
< |
* in the correct order. |
647 |
> |
* Returns an array containing all of the elements in this deque |
648 |
> |
* in proper sequence (from first to last element). |
649 |
|
* |
650 |
< |
* @return an array containing all of the elements in this list |
651 |
< |
* in the correct order |
650 |
> |
* <p>The returned array will be "safe" in that no references to it are |
651 |
> |
* maintained by this deque. (In other words, this method must allocate |
652 |
> |
* a new array). The caller is thus free to modify the returned array. |
653 |
> |
* |
654 |
> |
* <p>This method acts as bridge between array-based and collection-based |
655 |
> |
* APIs. |
656 |
> |
* |
657 |
> |
* @return an array containing all of the elements in this deque |
658 |
|
*/ |
659 |
|
public Object[] toArray() { |
660 |
|
return copyElements(new Object[size()]); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
664 |
< |
* Returns an array containing all of the elements in this deque in the |
665 |
< |
* correct order; the runtime type of the returned array is that of the |
666 |
< |
* specified array. If the deque fits in the specified array, it is |
667 |
< |
* returned therein. Otherwise, a new array is allocated with the runtime |
668 |
< |
* type of the specified array and the size of this deque. |
669 |
< |
* |
670 |
< |
* <p>If the deque fits in the specified array with room to spare (i.e., |
671 |
< |
* the array has more elements than the deque), the element in the array |
672 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
664 |
> |
* Returns an array containing all of the elements in this deque in |
665 |
> |
* proper sequence (from first to last element); the runtime type of the |
666 |
> |
* returned array is that of the specified array. If the deque fits in |
667 |
> |
* the specified array, it is returned therein. Otherwise, a new array |
668 |
> |
* is allocated with the runtime type of the specified array and the |
669 |
> |
* size of this deque. |
670 |
> |
* |
671 |
> |
* <p>If this deque fits in the specified array with room to spare |
672 |
> |
* (i.e., the array has more elements than this deque), the element in |
673 |
> |
* the array immediately following the end of the deque is set to |
674 |
> |
* <tt>null</tt>. |
675 |
> |
* |
676 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
677 |
> |
* array-based and collection-based APIs. Further, this method allows |
678 |
> |
* precise control over the runtime type of the output array, and may, |
679 |
> |
* under certain circumstances, be used to save allocation costs. |
680 |
> |
* |
681 |
> |
* <p>Suppose <tt>x</tt> is a deque known to contain only strings. |
682 |
> |
* The following code can be used to dump the deque into a newly |
683 |
> |
* allocated array of <tt>String</tt>: |
684 |
> |
* |
685 |
> |
* <pre> |
686 |
> |
* String[] y = x.toArray(new String[0]);</pre> |
687 |
> |
* |
688 |
> |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
689 |
> |
* <tt>toArray()</tt>. |
690 |
|
* |
691 |
|
* @param a the array into which the elements of the deque are to |
692 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
693 |
< |
* same runtime type is allocated for this purpose |
694 |
< |
* @return an array containing the elements of the deque |
695 |
< |
* @throws ArrayStoreException if the runtime type of a is not a supertype |
696 |
< |
* of the runtime type of every element in this deque |
692 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
693 |
> |
* same runtime type is allocated for this purpose |
694 |
> |
* @return an array containing all of the elements in this deque |
695 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
696 |
> |
* is not a supertype of the runtime type of every element in |
697 |
> |
* this deque |
698 |
> |
* @throws NullPointerException if the specified array is null |
699 |
|
*/ |
700 |
|
public <T> T[] toArray(T[] a) { |
701 |
|
int size = size(); |
716 |
|
* @return a copy of this deque |
717 |
|
*/ |
718 |
|
public ArrayDeque<E> clone() { |
719 |
< |
try { |
719 |
> |
try { |
720 |
|
ArrayDeque<E> result = (ArrayDeque<E>) super.clone(); |
721 |
|
// These two lines are currently faster than cloning the array: |
722 |
|
result.elements = (E[]) new Object[elements.length]; |
723 |
|
System.arraycopy(elements, 0, result.elements, 0, elements.length); |
724 |
|
return result; |
725 |
|
|
726 |
< |
} catch (CloneNotSupportedException e) { |
726 |
> |
} catch (CloneNotSupportedException e) { |
727 |
|
throw new AssertionError(); |
728 |
|
} |
729 |
|
} |