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.6 by dl, Tue Mar 22 16:48:32 2005 UTC vs.
Revision 1.14 by jsr166, Sat Jun 18 01:56:01 2005 UTC

# Line 4 | Line 4
4   */
5  
6   package java.util;
7 + import java.util.*; // for javadoc
8   import java.io.*;
9  
10   /**
# Line 18 | Line 19 | import java.io.*;
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
# Line 39 | Line 41 | import java.io.*;
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
# Line 87 | Line 91 | public class ArrayDeque<E> extends Abstr
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) {
97          int initialCapacity = MIN_INITIAL_CAPACITY;
# Line 129 | Line 133 | public class ArrayDeque<E> extends Abstr
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       *
# Line 186 | Line 190 | public class ArrayDeque<E> extends Abstr
190      /**
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)
# Line 199 | Line 203 | public class ArrayDeque<E> extends Abstr
203  
204      /**
205       * Inserts the specified element at the end of this deque.
202     * This method is equivalent to {@link Collection#add} and
203     * {@link #push}.
206       *
207 <     * @param e the element to insert
208 <     * @throws NullPointerException if <tt>e</tt> is null
207 >     * <p>This method is equivalent to {@link #add}.
208 >     *
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)
# Line 214 | Line 218 | public class ArrayDeque<E> extends Abstr
218      }
219  
220      /**
217     * Retrieves and removes the first element of this deque, or
218     * <tt>null</tt> if this deque is empty.
219     *
220     * @return the first element of this deque, or <tt>null</tt> if
221     *     this deque is empty
222     */
223    public E pollFirst() {
224        int h = head;
225        E result = elements[h]; // Element is null if deque empty
226        if (result == null)
227            return null;
228        elements[h] = null;     // Must null out slot
229        head = (h + 1) & (elements.length - 1);
230        return result;
231    }
232
233    /**
234     * Retrieves and removes the last element of this deque, or
235     * <tt>null</tt> if this deque is empty.
236     *
237     * @return the last element of this deque, or <tt>null</tt> if
238     *     this deque is empty
239     */
240    public E pollLast() {
241        int t = (tail - 1) & (elements.length - 1);
242        E result = elements[t];
243        if (result == null)
244            return null;
245        elements[t] = null;
246        tail = t;
247        return result;
248    }
249
250    /**
221       * Inserts the specified element at the front of this deque.
222       *
223 <     * @param e the element to insert
223 >     * @param e the element to add
224       * @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst})
225 <     * @throws NullPointerException if <tt>e</tt> is null
225 >     * @throws NullPointerException if the specified element is null
226       */
227      public boolean offerFirst(E e) {
228          addFirst(e);
# Line 262 | Line 232 | public class ArrayDeque<E> extends Abstr
232      /**
233       * Inserts the specified element at the end of this deque.
234       *
235 <     * @param e the element to insert
235 >     * @param e the element to add
236       * @return <tt>true</tt> (as per the spec for {@link Deque#offerLast})
237 <     * @throws NullPointerException if <tt>e</tt> is null
237 >     * @throws NullPointerException if the specified element is null
238       */
239      public boolean offerLast(E e) {
240          addLast(e);
# Line 272 | Line 242 | public class ArrayDeque<E> extends Abstr
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();
# Line 287 | Line 252 | public class ArrayDeque<E> extends Abstr
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();
# Line 301 | Line 261 | public class ArrayDeque<E> extends Abstr
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>peekFirst</tt> method only
329 <     * in that it throws an exception if this deque is empty.
330 <     *
331 <     * @return the first element of this deque
332 <     * @throws NoSuchElementException if this deque is empty
285 >     * @throws NoSuchElementException {@inheritDoc}
286       */
287      public E getFirst() {
288          E x = elements[head];
# Line 339 | Line 292 | public class ArrayDeque<E> extends Abstr
292      }
293  
294      /**
295 <     * Retrieves, but does not remove, the last element of this
343 <     * deque.  This method differs from the <tt>peekLast</tt> method only
344 <     * in that it throws an exception if this deque is empty.
345 <     *
346 <     * @return the last element of this deque
347 <     * @throws NoSuchElementException if this deque is empty
295 >     * @throws NoSuchElementException {@inheritDoc}
296       */
297      public E getLast() {
298          E x = elements[(tail - 1) & (elements.length - 1)];
# Line 353 | Line 301 | public class ArrayDeque<E> extends Abstr
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).  More
315 <     * formally, removes the first element e such that (o==null ?
316 <     * e==null : o.equals(e)). If the deque does not contain the
317 <     * 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 o element to be removed from this deque, if present
322       * @return <tt>true</tt> if the deque contained the specified element
# Line 381 | Line 339 | public class ArrayDeque<E> extends Abstr
339  
340      /**
341       * Removes the last occurrence of the specified element in this
342 <     * deque (when traversing the deque from head to tail). More
343 <     * formally, removes the last element e such that (o==null ?
344 <     * e==null : o.equals(e)). If the deque
345 <     * 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 o element to be removed from this deque, if present
350       * @return <tt>true</tt> if the deque contained the specified element
# Line 410 | Line 370 | public class ArrayDeque<E> extends Abstr
370      /**
371       * Inserts the specified element at the end of this deque.
372       *
413     * <p>This method is equivalent to {@link #offerLast}.
414     *
415     * @param e the element to insert
416     * @return <tt>true</tt> (as per the spec for {@link Queue#offer})
417     * @throws NullPointerException if <tt>e</tt> is null
418     */
419    public boolean offer(E e) {
420        return offerLast(e);
421    }
422
423    /**
424     * Inserts the specified element at the end of this deque.
425     *
373       * <p>This method is equivalent to {@link #addLast}.
374       *
375 <     * @param e the element to insert
375 >     * @param e the element to add
376       * @return <tt>true</tt> (as per the spec for {@link Collection#add})
377 <     * @throws NullPointerException if <tt>e</tt> is null
377 >     * @throws NullPointerException if the specified element is null
378       */
379      public boolean add(E e) {
380          addLast(e);
# Line 435 | Line 382 | public class ArrayDeque<E> extends Abstr
382      }
383  
384      /**
385 <     * Retrieves and removes the head of the queue represented by
439 <     * this deque, or <tt>null</tt> if this deque is empty.  In other words,
440 <     * retrieves and removes the first element of this deque, or <tt>null</tt>
441 <     * if this deque is empty.
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 per the spec for {@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 >     * This method differs from {@link #poll} only in that it throws an
400       * exception if this deque is empty.
401       *
402       * <p>This method is equivalent to {@link #removeFirst}.
403       *
404       * @return the head of the queue represented by this deque
405 <     * @throws NoSuchElementException if this deque is empty
405 >     * @throws NoSuchElementException {@inheritDoc}
406       */
407      public E remove() {
408          return removeFirst();
409      }
410  
411      /**
412 <     * Retrieves, but does not remove, the head of the queue represented by
413 <     * this deque, returning <tt>null</tt> if this deque is empty.
412 >     * Retrieves and removes the head of the queue represented by this deque
413 >     * (in other words, the first element of this deque), or returns
414 >     * <tt>null</tt> if this deque is empty.
415       *
416 <     * <p>This method is equivalent to {@link #peekFirst}
416 >     * <p>This method is equivalent to {@link #pollFirst}.
417       *
418       * @return the head of the queue represented by this deque, or
419 <     *     <tt>null</tt> if this deque is empty
419 >     *         <tt>null</tt> if this deque is empty
420       */
421 <    public E peek() {
422 <        return peekFirst();
421 >    public E poll() {
422 >        return pollFirst();
423      }
424  
425      /**
426       * Retrieves, but does not remove, the head of the queue represented by
427 <     * this deque.  This method differs from the <tt>peek</tt> method only in
428 <     * that it throws an exception if this deque is empty.
427 >     * this deque.  This method differs from {@link #peek} only in that it
428 >     * throws an exception if this deque is empty.
429       *
430 <     * <p>This method is equivalent to {@link #getFirst}
430 >     * <p>This method is equivalent to {@link #getFirst}.
431       *
432       * @return the head of the queue represented by this deque
433 <     * @throws NoSuchElementException if this deque is empty
433 >     * @throws NoSuchElementException {@inheritDoc}
434       */
435      public E element() {
436          return getFirst();
437      }
438  
439 +    /**
440 +     * Retrieves, but does not remove, the head of the queue represented by
441 +     * this deque, or returns <tt>null</tt> if this deque is empty.
442 +     *
443 +     * <p>This method is equivalent to {@link #peekFirst}.
444 +     *
445 +     * @return the head of the queue represented by this deque, or
446 +     *         <tt>null</tt> if this deque is empty
447 +     */
448 +    public E peek() {
449 +        return peekFirst();
450 +    }
451 +
452      // *** Stack methods ***
453  
454      /**
# Line 499 | Line 458 | public class ArrayDeque<E> extends Abstr
458       * <p>This method is equivalent to {@link #addFirst}.
459       *
460       * @param e the element to push
461 <     * @throws NullPointerException if <tt>e</tt> is null
461 >     * @throws NullPointerException if the specified element is null
462       */
463      public void push(E e) {
464          addFirst(e);
# Line 512 | Line 471 | public class ArrayDeque<E> extends Abstr
471       * <p>This method is equivalent to {@link #removeFirst()}.
472       *
473       * @return the element at the front of this deque (which is the top
474 <     *     of the stack represented by this deque)
475 <     * @throws NoSuchElementException if this deque is empty
474 >     *         of the stack represented by this deque)
475 >     * @throws NoSuchElementException {@inheritDoc}
476       */
477      public E pop() {
478          return removeFirst();
479      }
480  
481      /**
482 <     * Remove the element at the specified position in the elements array,
483 <     * adjusting head, tail, and size as necessary.  This can result in
484 <     * motion of elements backwards or forwards in the array.
482 >     * Removes the element at the specified position in the elements array,
483 >     * adjusting head and tail as necessary.  This can result in motion of
484 >     * elements backwards or forwards in the array.
485       *
486       * <p>This method is called delete rather than remove to emphasize
487 <     * that its semantics differ from those of List.remove(int).
487 >     * that its semantics differ from those of {@link List#remove(int)}.
488       *
489       * @return true if elements moved backwards
490       */
491      private boolean delete(int i) {
492 +        int mask = elements.length - 1;
493 +
494 +        // Invariant: head <= i < tail mod circularity
495 +        if (((i - head) & mask) >= ((tail - head) & mask))
496 +            throw new ConcurrentModificationException();
497 +
498          // Case 1: Deque doesn't wrap
499          // Case 2: Deque does wrap and removed element is in the head portion
500 <        if ((head < tail || tail == 0) || i >= head) {
500 >        if (i >= head) {
501              System.arraycopy(elements, head, elements, head + 1, i - head);
502              elements[head] = null;
503 <            head = (head + 1) & (elements.length - 1);
503 >            head = (head + 1) & mask;
504              return false;
505          }
506  
# Line 558 | Line 523 | public class ArrayDeque<E> extends Abstr
523      }
524  
525      /**
526 <     * Returns <tt>true</tt> if this collection contains no elements.<p>
526 >     * Returns <tt>true</tt> if this deque contains no elements.
527       *
528 <     * @return <tt>true</tt> if this collection contains no elements.
528 >     * @return <tt>true</tt> if this deque contains no elements
529       */
530      public boolean isEmpty() {
531          return head == tail;
# Line 624 | Line 589 | public class ArrayDeque<E> extends Abstr
589      }
590  
591      /**
592 <     * Returns <tt>true</tt> if this deque contains the specified
593 <     * element.  More formally, returns <tt>true</tt> if and only if this
594 <     * deque contains at least one element <tt>e</tt> such that
630 <     * <tt>e.equals(o)</tt>.
592 >     * Returns <tt>true</tt> if this deque contains the specified element.
593 >     * More formally, returns <tt>true</tt> if and only if this deque contains
594 >     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
595       *
596       * @param o object to be checked for containment in this deque
597       * @return <tt>true</tt> if this deque contains the specified element
# Line 648 | Line 612 | public class ArrayDeque<E> extends Abstr
612  
613      /**
614       * Removes a single instance of the specified element from this deque.
615 <     * This method is equivalent to {@link #removeFirstOccurrence}.
615 >     * If the deque does not contain the element, it is unchanged.
616 >     * More formally, removes the first element <tt>e</tt> such that
617 >     * <tt>o.equals(e)</tt> (if such an element exists).
618 >     * Returns <tt>true</tt> if this deque contained the specified element
619 >     * (or equivalently, if this deque changed as a result of the call).
620       *
621 <     * @param e element to be removed from this deque, if present
621 >     * <p>This method is equivalent to {@link #removeFirstOccurrence}.
622 >     *
623 >     * @param o element to be removed from this deque, if present
624       * @return <tt>true</tt> if this deque contained the specified element
625       */
626 <    public boolean remove(Object e) {
627 <        return removeFirstOccurrence(e);
626 >    public boolean remove(Object o) {
627 >        return removeFirstOccurrence(o);
628      }
629  
630      /**
631       * Removes all of the elements from this deque.
632 +     * The deque will be empty after this call returns.
633       */
634      public void clear() {
635          int h = head;
# Line 670 | Line 641 | public class ArrayDeque<E> extends Abstr
641              do {
642                  elements[i] = null;
643                  i = (i + 1) & mask;
644 <            } while(i != t);
644 >            } while (i != t);
645          }
646      }
647  
648      /**
649       * Returns an array containing all of the elements in this deque
650 <     * in the correct order.
650 >     * in proper sequence (from first to last element).
651 >     *
652 >     * <p>The returned array will be "safe" in that no references to it are
653 >     * maintained by this deque.  (In other words, this method must allocate
654 >     * a new array).  The caller is thus free to modify the returned array.
655 >     *
656 >     * <p>This method acts as bridge between array-based and collection-based
657 >     * APIs.
658       *
659       * @return an array containing all of the elements in this deque
682     *         in the correct order
660       */
661      public Object[] toArray() {
662          return copyElements(new Object[size()]);
663      }
664  
665      /**
666 <     * Returns an array containing all of the elements in this deque in the
667 <     * correct order; the runtime type of the returned array is that of the
668 <     * specified array.  If the deque fits in the specified array, it is
669 <     * returned therein.  Otherwise, a new array is allocated with the runtime
670 <     * type of the specified array and the size of this deque.
671 <     *
672 <     * <p>If the deque fits in the specified array with room to spare (i.e.,
673 <     * the array has more elements than the deque), the element in the array
674 <     * immediately following the end of the collection is set to <tt>null</tt>.
666 >     * Returns an array containing all of the elements in this deque in
667 >     * proper sequence (from first to last element); the runtime type of the
668 >     * returned array is that of the specified array.  If the deque fits in
669 >     * the specified array, it is returned therein.  Otherwise, a new array
670 >     * is allocated with the runtime type of the specified array and the
671 >     * size of this deque.
672 >     *
673 >     * <p>If this deque fits in the specified array with room to spare
674 >     * (i.e., the array has more elements than this deque), the element in
675 >     * the array immediately following the end of the deque is set to
676 >     * <tt>null</tt>.
677 >     *
678 >     * <p>Like the {@link #toArray()} method, this method acts as bridge between
679 >     * array-based and collection-based APIs.  Further, this method allows
680 >     * precise control over the runtime type of the output array, and may,
681 >     * under certain circumstances, be used to save allocation costs.
682 >     *
683 >     * <p>Suppose <tt>x</tt> is a deque known to contain only strings.
684 >     * The following code can be used to dump the deque into a newly
685 >     * allocated array of <tt>String</tt>:
686 >     *
687 >     * <pre>
688 >     *     String[] y = x.toArray(new String[0]);</pre>
689 >     *
690 >     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
691 >     * <tt>toArray()</tt>.
692       *
693       * @param a the array into which the elements of the deque are to
694 <     *          be stored, if it is big enough; otherwise, a new array of the
695 <     *          same runtime type is allocated for this purpose
696 <     * @return an array containing the elements of the deque
697 <     * @throws ArrayStoreException if the runtime type of a is not a supertype
698 <     *         of the runtime type of every element in this deque
694 >     *          be stored, if it is big enough; otherwise, a new array of the
695 >     *          same runtime type is allocated for this purpose
696 >     * @return an array containing all of the elements in this deque
697 >     * @throws ArrayStoreException if the runtime type of the specified array
698 >     *         is not a supertype of the runtime type of every element in
699 >     *         this deque
700 >     * @throws NullPointerException if the specified array is null
701       */
702      public <T> T[] toArray(T[] a) {
703          int size = size();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines