ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayDeque.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayDeque.java (file contents):
Revision 1.1 by dl, Tue Dec 28 12:14:07 2004 UTC vs.
Revision 1.39 by jsr166, Tue Feb 21 01:54:03 2012 UTC

# Line 1 | Line 1
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  
8   /**
9   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 12 | Line 11 | import java.io.*;
11   * usage.  They are not thread-safe; in the absence of external
12   * synchronization, they do not support concurrent access by multiple threads.
13   * Null elements are prohibited.  This class is likely to be faster than
14 < * {@link Stack} when used as as a stack, and faster than {@link LinkedList}
14 > * {@link Stack} when used as a stack, and faster than {@link LinkedList}
15   * when used as a queue.
16   *
17   * <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time.
18   * Exceptions include {@link #remove(Object) remove}, {@link
19   * #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence
20 < * removeLastOccurrence}, {@link #contains contains }, {@link #iterator
20 > * removeLastOccurrence}, {@link #contains contains}, {@link #iterator
21   * iterator.remove()}, and the bulk operations, all of which run in linear
22   * time.
23   *
24   * <p>The iterators returned by this class's <tt>iterator</tt> method are
25   * <i>fail-fast</i>: If the deque is modified at any time after the iterator
26 < * is created, in any way except through the iterator's own remove method, the
27 < * iterator will generally throw a {@link ConcurrentModificationException}.
28 < * Thus, in the face of concurrent modification, the iterator fails quickly
29 < * and cleanly, rather than risking arbitrary, non-deterministic behavior at
30 < * an undetermined time in the future.
26 > * is created, in any way except through the iterator's own <tt>remove</tt>
27 > * method, the iterator will generally throw a {@link
28 > * ConcurrentModificationException}.  Thus, in the face of concurrent
29 > * modification, the iterator fails quickly and cleanly, rather than risking
30 > * arbitrary, non-deterministic behavior at an undetermined time in the
31 > * future.
32   *
33   * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
34   * as it is, generally speaking, impossible to make any hard guarantees in the
35   * presence of unsynchronized concurrent modification.  Fail-fast iterators
36 < * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
36 > * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
37   * Therefore, it would be wrong to write a program that depended on this
38   * exception for its correctness: <i>the fail-fast behavior of iterators
39   * should be used only to detect bugs.</i>
40   *
41   * <p>This class and its iterator implement all of the
42 < * optional methods of the {@link Collection} and {@link
43 < * Iterator} interfaces.  This class is a member of the <a
44 < * href="{@docRoot}/../guide/collections/index.html"> Java Collections
45 < * Framework</a>.
42 > * <em>optional</em> methods of the {@link Collection} and {@link
43 > * Iterator} interfaces.
44 > *
45 > * <p>This class is a member of the
46 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
47 > * Java Collections Framework</a>.
48   *
49   * @author  Josh Bloch and Doug Lea
50   * @since   1.6
51   * @param <E> the type of elements held in this collection
52   */
53   public class ArrayDeque<E> extends AbstractCollection<E>
54 <                           implements Deque<E>, Cloneable, Serializable
54 >                           implements Deque<E>, Cloneable, java.io.Serializable
55   {
56      /**
57 <     * The array in which the elements of in the deque are stored.
57 >     * The array in which the elements of the deque are stored.
58       * The capacity of the deque is the length of this array, which is
59       * always a power of two. The array is never allowed to become
60       * full, except transiently within an addX method where it is
# Line 61 | Line 63 | public class ArrayDeque<E> extends Abstr
63       * other.  We also guarantee that all array cells not holding
64       * deque elements are always null.
65       */
66 <    private transient E[] elements;
66 >    private transient Object[] elements;
67  
68      /**
69       * The index of the element at the head of the deque (which is the
# Line 85 | Line 87 | public class ArrayDeque<E> extends Abstr
87      // ******  Array allocation and resizing utilities ******
88  
89      /**
90 <     * Allocate empty array to hold the given number of elements.
90 >     * Allocates empty array to hold the given number of elements.
91       *
92 <     * @param numElements  the number of elements to hold.
92 >     * @param numElements  the number of elements to hold
93       */
94 <    private void allocateElements(int numElements) {  
94 >    private void allocateElements(int numElements) {
95          int initialCapacity = MIN_INITIAL_CAPACITY;
96          // Find the best power of two to hold elements.
97          // Tests "<=" because arrays aren't kept full.
# Line 105 | Line 107 | public class ArrayDeque<E> extends Abstr
107              if (initialCapacity < 0)   // Too many elements, must back off
108                  initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
109          }
110 <        elements = (E[]) new Object[initialCapacity];
110 >        elements = new Object[initialCapacity];
111      }
112  
113      /**
114 <     * Double the capacity of this deque.  Call only when full, i.e.,
114 >     * Doubles the capacity of this deque.  Call only when full, i.e.,
115       * when head and tail have wrapped around to become equal.
116       */
117      private void doubleCapacity() {
118 <        assert head == tail;
118 >        assert head == tail;
119          int p = head;
120          int n = elements.length;
121          int r = n - p; // number of elements to the right of p
# Line 123 | Line 125 | public class ArrayDeque<E> extends Abstr
125          Object[] a = new Object[newCapacity];
126          System.arraycopy(elements, p, a, 0, r);
127          System.arraycopy(elements, 0, a, r, p);
128 <        elements = (E[])a;
128 >        elements = a;
129          head = 0;
130          tail = n;
131      }
132  
133      /**
134 <     * Copy the elements from our element array into the specified array,
134 >     * Copies the elements from our element array into the specified array,
135       * in order (from first to last element in the deque).  It is assumed
136       * that the array is large enough to hold all elements in the deque.
137       *
# Line 147 | Line 149 | public class ArrayDeque<E> extends Abstr
149      }
150  
151      /**
152 <     * Constructs an empty array deque with the an initial capacity
152 >     * Constructs an empty array deque with an initial capacity
153       * sufficient to hold 16 elements.
154       */
155      public ArrayDeque() {
156 <        elements = (E[]) new Object[16];
156 >        elements = new Object[16];
157      }
158  
159      /**
# Line 184 | Line 186 | public class ArrayDeque<E> extends Abstr
186      // terms of these.
187  
188      /**
189 <     * Inserts the specified element to the front this deque.
189 >     * Inserts the specified element at the front of this deque.
190       *
191 <     * @param e the element to insert
192 <     * @throws NullPointerException if <tt>e</tt> is null
191 >     * @param e the element to add
192 >     * @throws NullPointerException if the specified element is null
193       */
194      public void addFirst(E e) {
195          if (e == null)
196              throw new NullPointerException();
197          elements[head = (head - 1) & (elements.length - 1)] = e;
198 <        if (head == tail)
198 >        if (head == tail)
199              doubleCapacity();
200      }
201  
202      /**
203 <     * Inserts the specified element to the end this deque.
204 <     * This method is equivalent to {@link Collection#add} and
205 <     * {@link #push}.
203 >     * Inserts the specified element at the end of this deque.
204 >     *
205 >     * <p>This method is equivalent to {@link #add}.
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)
# Line 214 | Line 216 | public class ArrayDeque<E> extends Abstr
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
222 <     *     this deque is empty
223 <     */
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
221 >     * @param e the element to add
222 >     * @return <tt>true</tt> (as specified by {@link Deque#offerFirst})
223 >     * @throws NullPointerException if the specified element is null
224       */
225      public boolean offerFirst(E e) {
226          addFirst(e);
# Line 260 | Line 228 | public class ArrayDeque<E> extends Abstr
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
234 <     * @return <tt>true</tt> (as per the spec for {@link Deque#offerLast})
235 <     * @throws NullPointerException if <tt>e</tt> is null
233 >     * @param e the element to add
234 >     * @return <tt>true</tt> (as specified by {@link Deque#offerLast})
235 >     * @throws NullPointerException if the specified element is null
236       */
237      public boolean offerLast(E e) {
238          addLast(e);
# Line 272 | Line 240 | public class ArrayDeque<E> extends Abstr
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();
# Line 287 | Line 250 | public class ArrayDeque<E> extends Abstr
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();
# Line 301 | Line 259 | public class ArrayDeque<E> extends Abstr
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() {
270 <        return elements[head]; // elements[head] is null if deque empty
262 >    public E pollFirst() {
263 >        int h = head;
264 >        @SuppressWarnings("unchecked")
265 >        E result = (E) elements[h];
266 >        // 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() {
282 <        return elements[(tail - 1) & (elements.length - 1)];
274 >    public E pollLast() {
275 >        int t = (tail - 1) & (elements.length - 1);
276 >        @SuppressWarnings("unchecked")
277 >        E result = (E) elements[t];
278 >        if (result == null)
279 >            return null;
280 >        elements[t] = null;
281 >        tail = t;
282 >        return result;
283      }
284  
285      /**
286 <     * 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
286 >     * @throws NoSuchElementException {@inheritDoc}
287       */
288      public E getFirst() {
289 <        E x = elements[head];
290 <        if (x == null)
289 >        @SuppressWarnings("unchecked")
290 >        E result = (E) elements[head];
291 >        if (result == null)
292              throw new NoSuchElementException();
293 <        return x;
293 >        return result;
294      }
295  
296      /**
297 <     * 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
297 >     * @throws NoSuchElementException {@inheritDoc}
298       */
299      public E getLast() {
300 <        E x = elements[(tail - 1) & (elements.length - 1)];
301 <        if (x == null)
300 >        @SuppressWarnings("unchecked")
301 >        E result = (E) elements[(tail - 1) & (elements.length - 1)];
302 >        if (result == null)
303              throw new NoSuchElementException();
304 <        return x;
304 >        return result;
305 >    }
306 >
307 >    @SuppressWarnings("unchecked")
308 >    public E peekFirst() {
309 >        // elements[head] is null if deque empty
310 >        return (E) elements[head];
311 >    }
312 >
313 >    @SuppressWarnings("unchecked")
314 >    public E peekLast() {
315 >        return (E) elements[(tail - 1) & (elements.length - 1)];
316      }
317  
318      /**
319       * Removes the first occurrence of the specified element in this
320 <     * deque (when traversing the deque from head to tail).  If the deque
321 <     * does not contain the element, it is unchanged.
320 >     * deque (when traversing the deque from head to tail).
321 >     * If the deque does not contain the element, it is unchanged.
322 >     * More formally, removes the first element <tt>e</tt> such that
323 >     * <tt>o.equals(e)</tt> (if such an element exists).
324 >     * Returns <tt>true</tt> if this deque contained the specified element
325 >     * (or equivalently, if this deque changed as a result of the call).
326       *
327 <     * @param e element to be removed from this deque, if present
327 >     * @param o element to be removed from this deque, if present
328       * @return <tt>true</tt> if the deque contained the specified element
329       */
330 <    public boolean removeFirstOccurrence(Object e) {
331 <        if (e == null)
330 >    public boolean removeFirstOccurrence(Object o) {
331 >        if (o == null)
332              return false;
333          int mask = elements.length - 1;
334          int i = head;
335 <        E x;
335 >        Object x;
336          while ( (x = elements[i]) != null) {
337 <            if (e.equals(x)) {
337 >            if (o.equals(x)) {
338                  delete(i);
339                  return true;
340              }
# Line 379 | Line 345 | public class ArrayDeque<E> extends Abstr
345  
346      /**
347       * Removes the last occurrence of the specified element in this
348 <     * deque (when traversing the deque from head to tail).  If the deque
349 <     * does not contain the element, it is unchanged.
348 >     * deque (when traversing the deque from head to tail).
349 >     * If the deque does not contain the element, it is unchanged.
350 >     * More formally, removes the last element <tt>e</tt> such that
351 >     * <tt>o.equals(e)</tt> (if such an element exists).
352 >     * Returns <tt>true</tt> if this deque contained the specified element
353 >     * (or equivalently, if this deque changed as a result of the call).
354       *
355 <     * @param e element to be removed from this deque, if present
355 >     * @param o element to be removed from this deque, if present
356       * @return <tt>true</tt> if the deque contained the specified element
357       */
358 <    public boolean removeLastOccurrence(Object e) {
359 <        if (e == null)
358 >    public boolean removeLastOccurrence(Object o) {
359 >        if (o == null)
360              return false;
361          int mask = elements.length - 1;
362          int i = (tail - 1) & mask;
363 <        E x;
363 >        Object x;
364          while ( (x = elements[i]) != null) {
365 <            if (e.equals(x)) {
365 >            if (o.equals(x)) {
366                  delete(i);
367                  return true;
368              }
# Line 404 | Line 374 | public class ArrayDeque<E> extends Abstr
374      // *** Queue methods ***
375  
376      /**
377 <     * 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.
377 >     * Inserts the specified element at the end of this deque.
378       *
379       * <p>This method is equivalent to {@link #addLast}.
380       *
381 <     * @param e the element to insert
382 <     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
383 <     * @throws NullPointerException if <tt>e</tt> is null
381 >     * @param e the element to add
382 >     * @return <tt>true</tt> (as specified by {@link Collection#add})
383 >     * @throws NullPointerException if the specified element is null
384       */
385      public boolean add(E e) {
386          addLast(e);
# Line 431 | Line 388 | public class ArrayDeque<E> extends Abstr
388      }
389  
390      /**
391 <     * 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.
391 >     * Inserts the specified element at the end of this deque.
392       *
393 <     * <p>This method is equivalent to {@link #pollFirst}.
393 >     * <p>This method is equivalent to {@link #offerLast}.
394       *
395 <     * @return the first element of this deque, or <tt>null</tt> if
396 <     *     this deque is empty
395 >     * @param e the element to add
396 >     * @return <tt>true</tt> (as specified by {@link Queue#offer})
397 >     * @throws NullPointerException if the specified element is null
398       */
399 <    public E poll() {
400 <        return pollFirst();
399 >    public boolean offer(E e) {
400 >        return offerLast(e);
401      }
402  
403      /**
404       * Retrieves and removes the head of the queue represented by this deque.
405 <     * This method differs from the <tt>poll</tt> method in that it throws an
405 >     *
406 >     * This method differs from {@link #poll poll} only in that it throws an
407       * exception if this deque is empty.
408       *
409       * <p>This method is equivalent to {@link #removeFirst}.
410       *
411       * @return the head of the queue represented by this deque
412 <     * @throws NoSuchElementException if this deque is empty
412 >     * @throws NoSuchElementException {@inheritDoc}
413       */
414      public E remove() {
415          return removeFirst();
416      }
417  
418      /**
419 <     * Retrieves, but does not remove, the head of the queue represented by
420 <     * this deque, returning <tt>null</tt> if this deque is empty.
419 >     * Retrieves and removes the head of the queue represented by this deque
420 >     * (in other words, the first element of this deque), or returns
421 >     * <tt>null</tt> if this deque is empty.
422       *
423 <     * <p>This method is equivalent to {@link #peekFirst}
423 >     * <p>This method is equivalent to {@link #pollFirst}.
424       *
425       * @return the head of the queue represented by this deque, or
426 <     *     <tt>null</tt> if this deque is empty
426 >     *         <tt>null</tt> if this deque is empty
427       */
428 <    public E peek() {
429 <        return peekFirst();
428 >    public E poll() {
429 >        return pollFirst();
430      }
431  
432      /**
433       * Retrieves, but does not remove, the head of the queue represented by
434 <     * this deque.  This method differs from the <tt>peek</tt> method only in
434 >     * this deque.  This method differs from {@link #peek peek} only in
435       * that it throws an exception if this deque is empty.
436       *
437 <     * <p>This method is equivalent to {@link #getFirst}
437 >     * <p>This method is equivalent to {@link #getFirst}.
438       *
439       * @return the head of the queue represented by this deque
440 <     * @throws NoSuchElementException if this deque is empty
440 >     * @throws NoSuchElementException {@inheritDoc}
441       */
442      public E element() {
443          return getFirst();
444      }
445  
446 +    /**
447 +     * Retrieves, but does not remove, the head of the queue represented by
448 +     * this deque, or returns <tt>null</tt> if this deque is empty.
449 +     *
450 +     * <p>This method is equivalent to {@link #peekFirst}.
451 +     *
452 +     * @return the head of the queue represented by this deque, or
453 +     *         <tt>null</tt> if this deque is empty
454 +     */
455 +    public E peek() {
456 +        return peekFirst();
457 +    }
458 +
459      // *** Stack methods ***
460  
461      /**
462       * Pushes an element onto the stack represented by this deque.  In other
463 <     * words, inserts the element to the front this deque.
463 >     * words, inserts the element at the front of this deque.
464       *
465       * <p>This method is equivalent to {@link #addFirst}.
466       *
467       * @param e the element to push
468 <     * @throws NullPointerException if <tt>e</tt> is null
468 >     * @throws NullPointerException if the specified element is null
469       */
470      public void push(E e) {
471          addFirst(e);
# Line 503 | Line 473 | public class ArrayDeque<E> extends Abstr
473  
474      /**
475       * Pops an element from the stack represented by this deque.  In other
476 <     * words, removes and returns the the first element of this deque.
476 >     * words, removes and returns the first element of this deque.
477       *
478       * <p>This method is equivalent to {@link #removeFirst()}.
479       *
480       * @return the element at the front of this deque (which is the top
481 <     *     of the stack represented by this deque)
482 <     * @throws NoSuchElementException if this deque is empty
481 >     *         of the stack represented by this deque)
482 >     * @throws NoSuchElementException {@inheritDoc}
483       */
484      public E pop() {
485          return removeFirst();
486      }
487  
488 +    private void checkInvariants() {
489 +        assert elements[tail] == null;
490 +        assert head == tail ? elements[head] == null :
491 +            (elements[head] != null &&
492 +             elements[(tail - 1) & (elements.length - 1)] != null);
493 +        assert elements[(head - 1) & (elements.length - 1)] == null;
494 +    }
495 +
496      /**
497 <     * Remove the element at the specified position in the elements array,
498 <     * adjusting head, tail, and size as necessary.  This can result in
499 <     * motion of elements backwards or forwards in the array.
500 <     *
501 <     * <p>This method is called delete rather than remove to emphasize the
502 <     * that that its semantics differ from those of List.remove(int).
503 <     *
497 >     * Removes the element at the specified position in the elements array,
498 >     * adjusting head and tail as necessary.  This can result in motion of
499 >     * elements backwards or forwards in the array.
500 >     *
501 >     * <p>This method is called delete rather than remove to emphasize
502 >     * that its semantics differ from those of {@link List#remove(int)}.
503 >     *
504       * @return true if elements moved backwards
505       */
506      private boolean delete(int i) {
507 <        // Case 1: Deque doesn't wrap
508 <        // Case 2: Deque does wrap and removed element is in the head portion
509 <        if ((head < tail || tail == 0) || i >= head) {
510 <            System.arraycopy(elements, head, elements, head + 1, i - head);
511 <            elements[head] = null;
512 <            head = (head + 1) & (elements.length - 1);
507 >        checkInvariants();
508 >        final Object[] elements = this.elements;
509 >        final int mask = elements.length - 1;
510 >        final int h = head;
511 >        final int t = tail;
512 >        final int front = (i - h) & mask;
513 >        final int back  = (t - i) & mask;
514 >
515 >        // Invariant: head <= i < tail mod circularity
516 >        if (front >= ((t - h) & mask))
517 >            throw new ConcurrentModificationException();
518 >
519 >        // Optimize for least element motion
520 >        if (front < back) {
521 >            if (h <= i) {
522 >                System.arraycopy(elements, h, elements, h + 1, front);
523 >            } else { // Wrap around
524 >                System.arraycopy(elements, 0, elements, 1, i);
525 >                elements[0] = elements[mask];
526 >                System.arraycopy(elements, h, elements, h + 1, mask - h);
527 >            }
528 >            elements[h] = null;
529 >            head = (h + 1) & mask;
530              return false;
531 +        } else {
532 +            if (i < t) { // Copy the null tail as well
533 +                System.arraycopy(elements, i + 1, elements, i, back);
534 +                tail = t - 1;
535 +            } else { // Wrap around
536 +                System.arraycopy(elements, i + 1, elements, i, mask - i);
537 +                elements[mask] = elements[0];
538 +                System.arraycopy(elements, 1, elements, 0, t);
539 +                tail = (t - 1) & mask;
540 +            }
541 +            return true;
542          }
537
538        // Case 3: Deque wraps and removed element is in the tail portion
539        tail--;
540        System.arraycopy(elements, i + 1, elements, i, tail - i);
541        elements[tail] = null;
542        return true;
543      }
544  
545      // *** Collection Methods ***
# Line 554 | Line 554 | public class ArrayDeque<E> extends Abstr
554      }
555  
556      /**
557 <     * Returns <tt>true</tt> if this collection contains no elements.<p>
557 >     * Returns <tt>true</tt> if this deque contains no elements.
558       *
559 <     * @return <tt>true</tt> if this collection contains no elements.
559 >     * @return <tt>true</tt> if this deque contains no elements
560       */
561      public boolean isEmpty() {
562          return head == tail;
# Line 567 | Line 567 | public class ArrayDeque<E> extends Abstr
567       * will be ordered from first (head) to last (tail).  This is the same
568       * order that elements would be dequeued (via successive calls to
569       * {@link #remove} or popped (via successive calls to {@link #pop}).
570 <     *
571 <     * @return an <tt>Iterator</tt> over the elements in this deque
570 >     *
571 >     * @return an iterator over the elements in this deque
572       */
573      public Iterator<E> iterator() {
574          return new DeqIterator();
575      }
576  
577 +    public Iterator<E> descendingIterator() {
578 +        return new DescendingIterator();
579 +    }
580 +
581      private class DeqIterator implements Iterator<E> {
582          /**
583           * Index of element to be returned by subsequent call to next.
# Line 597 | Line 601 | public class ArrayDeque<E> extends Abstr
601          }
602  
603          public E next() {
600            E result;
604              if (cursor == fence)
605                  throw new NoSuchElementException();
606 +            @SuppressWarnings("unchecked")
607 +            E result = (E) elements[cursor];
608              // This check doesn't catch all possible comodifications,
609              // but does catch the ones that corrupt traversal
610 <            if (tail != fence || (result = elements[cursor]) == null)
610 >            if (tail != fence || result == null)
611                  throw new ConcurrentModificationException();
612              lastRet = cursor;
613              cursor = (cursor + 1) & (elements.length - 1);
# Line 612 | Line 617 | public class ArrayDeque<E> extends Abstr
617          public void remove() {
618              if (lastRet < 0)
619                  throw new IllegalStateException();
620 <            if (delete(lastRet))
621 <                cursor--;
620 >            if (delete(lastRet)) { // if left-shifted, undo increment in next()
621 >                cursor = (cursor - 1) & (elements.length - 1);
622 >                fence = tail;
623 >            }
624 >            lastRet = -1;
625 >        }
626 >    }
627 >
628 >    private class DescendingIterator implements Iterator<E> {
629 >        /*
630 >         * This class is nearly a mirror-image of DeqIterator, using
631 >         * tail instead of head for initial cursor, and head instead of
632 >         * tail for fence.
633 >         */
634 >        private int cursor = tail;
635 >        private int fence = head;
636 >        private int lastRet = -1;
637 >
638 >        public boolean hasNext() {
639 >            return cursor != fence;
640 >        }
641 >
642 >        public E next() {
643 >            if (cursor == fence)
644 >                throw new NoSuchElementException();
645 >            cursor = (cursor - 1) & (elements.length - 1);
646 >            @SuppressWarnings("unchecked")
647 >            E result = (E) elements[cursor];
648 >            if (head != fence || result == null)
649 >                throw new ConcurrentModificationException();
650 >            lastRet = cursor;
651 >            return result;
652 >        }
653 >
654 >        public void remove() {
655 >            if (lastRet < 0)
656 >                throw new IllegalStateException();
657 >            if (!delete(lastRet)) {
658 >                cursor = (cursor + 1) & (elements.length - 1);
659 >                fence = head;
660 >            }
661              lastRet = -1;
618            fence = tail;
662          }
663      }
664  
665      /**
666 <     * Returns <tt>true</tt> if this deque contains the specified
667 <     * element.  More formally, returns <tt>true</tt> if and only if this
668 <     * deque contains at least one element <tt>e</tt> such that
626 <     * <tt>e.equals(o)</tt>.
666 >     * Returns <tt>true</tt> if this deque contains the specified element.
667 >     * More formally, returns <tt>true</tt> if and only if this deque contains
668 >     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
669       *
670       * @param o object to be checked for containment in this deque
671       * @return <tt>true</tt> if this deque contains the specified element
# Line 633 | Line 675 | public class ArrayDeque<E> extends Abstr
675              return false;
676          int mask = elements.length - 1;
677          int i = head;
678 <        E x;
678 >        Object x;
679          while ( (x = elements[i]) != null) {
680              if (o.equals(x))
681                  return true;
# Line 644 | Line 686 | public class ArrayDeque<E> extends Abstr
686  
687      /**
688       * Removes a single instance of the specified element from this deque.
689 <     * This method is equivalent to {@link #removeFirstOccurrence}.
689 >     * If the deque does not contain the element, it is unchanged.
690 >     * More formally, removes the first element <tt>e</tt> such that
691 >     * <tt>o.equals(e)</tt> (if such an element exists).
692 >     * Returns <tt>true</tt> if this deque contained the specified element
693 >     * (or equivalently, if this deque changed as a result of the call).
694 >     *
695 >     * <p>This method is equivalent to {@link #removeFirstOccurrence}.
696       *
697 <     * @param e element to be removed from this deque, if present
697 >     * @param o element to be removed from this deque, if present
698       * @return <tt>true</tt> if this deque contained the specified element
699       */
700 <    public boolean remove(Object e) {
701 <        return removeFirstOccurrence(e);
700 >    public boolean remove(Object o) {
701 >        return removeFirstOccurrence(o);
702      }
703  
704      /**
705       * Removes all of the elements from this deque.
706 +     * The deque will be empty after this call returns.
707       */
708      public void clear() {
709          int h = head;
# Line 666 | Line 715 | public class ArrayDeque<E> extends Abstr
715              do {
716                  elements[i] = null;
717                  i = (i + 1) & mask;
718 <            } while(i != t);
718 >            } while (i != t);
719          }
720      }
721  
722      /**
723 <     * Returns an array containing all of the elements in this list
724 <     * in the correct order.
723 >     * Returns an array containing all of the elements in this deque
724 >     * in proper sequence (from first to last element).
725       *
726 <     * @return an array containing all of the elements in this list
727 <     *         in the correct order
726 >     * <p>The returned array will be "safe" in that no references to it are
727 >     * maintained by this deque.  (In other words, this method must allocate
728 >     * a new array).  The caller is thus free to modify the returned array.
729 >     *
730 >     * <p>This method acts as bridge between array-based and collection-based
731 >     * APIs.
732 >     *
733 >     * @return an array containing all of the elements in this deque
734       */
735      public Object[] toArray() {
736 <        return copyElements(new Object[size()]);
736 >        return copyElements(new Object[size()]);
737      }
738  
739      /**
740 <     * Returns an array containing all of the elements in this deque in the
741 <     * correct order; the runtime type of the returned array is that of the
742 <     * specified array.  If the deque fits in the specified array, it is
743 <     * returned therein.  Otherwise, a new array is allocated with the runtime
744 <     * type of the specified array and the size of this deque.
740 >     * Returns an array containing all of the elements in this deque in
741 >     * proper sequence (from first to last element); the runtime type of the
742 >     * returned array is that of the specified array.  If the deque fits in
743 >     * the specified array, it is returned therein.  Otherwise, a new array
744 >     * is allocated with the runtime type of the specified array and the
745 >     * size of this deque.
746 >     *
747 >     * <p>If this deque fits in the specified array with room to spare
748 >     * (i.e., the array has more elements than this deque), the element in
749 >     * the array immediately following the end of the deque is set to
750 >     * <tt>null</tt>.
751 >     *
752 >     * <p>Like the {@link #toArray()} method, this method acts as bridge between
753 >     * array-based and collection-based APIs.  Further, this method allows
754 >     * precise control over the runtime type of the output array, and may,
755 >     * under certain circumstances, be used to save allocation costs.
756 >     *
757 >     * <p>Suppose <tt>x</tt> is a deque known to contain only strings.
758 >     * The following code can be used to dump the deque into a newly
759 >     * allocated array of <tt>String</tt>:
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 >     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
762 >     *
763 >     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
764 >     * <tt>toArray()</tt>.
765       *
766       * @param a the array into which the elements of the deque are to
767 <     *          be stored, if it is big enough; otherwise, a new array of the
768 <     *          same runtime type is allocated for this purpose
769 <     * @return an array containing the elements of the deque
770 <     * @throws ArrayStoreException if the runtime type of a is not a supertype
771 <     *         of the runtime type of every element in this deque
767 >     *          be stored, if it is big enough; otherwise, a new array of the
768 >     *          same runtime type is allocated for this purpose
769 >     * @return an array containing all of the elements in this deque
770 >     * @throws ArrayStoreException if the runtime type of the specified array
771 >     *         is not a supertype of the runtime type of every element in
772 >     *         this deque
773 >     * @throws NullPointerException if the specified array is null
774       */
775 +    @SuppressWarnings("unchecked")
776      public <T> T[] toArray(T[] a) {
777          int size = size();
778          if (a.length < size)
779              a = (T[])java.lang.reflect.Array.newInstance(
780                      a.getClass().getComponentType(), size);
781 <        copyElements(a);
781 >        copyElements(a);
782          if (a.length > size)
783              a[size] = null;
784          return a;
# Line 718 | Line 792 | public class ArrayDeque<E> extends Abstr
792       * @return a copy of this deque
793       */
794      public ArrayDeque<E> clone() {
795 <        try {
795 >        try {
796 >            @SuppressWarnings("unchecked")
797              ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
798 <            // These two lines are currently faster than cloning the array:
724 <            result.elements = (E[]) new Object[elements.length];
725 <            System.arraycopy(elements, 0, result.elements, 0, elements.length);
798 >            result.elements = Arrays.copyOf(elements, elements.length);
799              return result;
800 <
728 <        } catch (CloneNotSupportedException e) {
800 >        } catch (CloneNotSupportedException e) {
801              throw new AssertionError();
802          }
803      }
804  
733    /**
734     * Appease the serialization gods.
735     */
805      private static final long serialVersionUID = 2340985798034038923L;
806  
807      /**
808 <     * Serialize this deque.
808 >     * Saves this deque to a stream (that is, serializes it).
809       *
810       * @serialData The current size (<tt>int</tt>) of the deque,
811       * followed by all of its elements (each an object reference) in
812       * first-to-last order.
813       */
814 <    private void writeObject(ObjectOutputStream s) throws IOException {
814 >    private void writeObject(java.io.ObjectOutputStream s)
815 >            throws java.io.IOException {
816          s.defaultWriteObject();
817  
818          // Write out size
819 <        int size = size();
750 <        s.writeInt(size);
819 >        s.writeInt(size());
820  
821          // Write out elements in order.
753        int i = head;
822          int mask = elements.length - 1;
823 <        for (int j = 0; j < size; j++) {
823 >        for (int i = head; i != tail; i = (i + 1) & mask)
824              s.writeObject(elements[i]);
757            i = (i + 1) & mask;
758        }
825      }
826  
827      /**
828 <     * Deserialize this deque.
828 >     * Reconstitutes this deque from a stream (that is, deserializes it).
829       */
830 <    private void readObject(ObjectInputStream s)
831 <            throws IOException, ClassNotFoundException {
830 >    private void readObject(java.io.ObjectInputStream s)
831 >            throws java.io.IOException, ClassNotFoundException {
832          s.defaultReadObject();
833  
834          // Read in size and allocate array
# Line 773 | Line 839 | public class ArrayDeque<E> extends Abstr
839  
840          // Read in all elements in the proper order.
841          for (int i = 0; i < size; i++)
842 <            elements[i] = (E)s.readObject();
777 <
842 >            elements[i] = s.readObject();
843      }
844   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines