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.47 by dl, Sun Feb 17 23:36:16 2013 UTC vs.
Revision 1.65 by jsr166, Sat Feb 28 20:35:47 2015 UTC

# Line 4 | Line 4
4   */
5  
6   package java.util;
7 +
8   import java.io.Serializable;
9   import java.util.function.Consumer;
9 import java.util.stream.Stream;
10 import java.util.stream.Streams;
10  
11   /**
12   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 19 | Line 18 | import java.util.stream.Streams;
18   * when used as a queue.
19   *
20   * <p>Most {@code ArrayDeque} operations run in amortized constant time.
21 < * Exceptions include {@link #remove(Object) remove}, {@link
22 < * #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence
23 < * removeLastOccurrence}, {@link #contains contains}, {@link #iterator
24 < * iterator.remove()}, and the bulk operations, all of which run in linear
25 < * time.
21 > * Exceptions include
22 > * {@link #remove(Object) remove},
23 > * {@link #removeFirstOccurrence removeFirstOccurrence},
24 > * {@link #removeLastOccurrence removeLastOccurrence},
25 > * {@link #contains contains},
26 > * {@link #iterator iterator.remove()},
27 > * and the bulk operations, all of which run in linear time.
28   *
29 < * <p>The iterators returned by this class's {@code iterator} method are
30 < * <i>fail-fast</i>: If the deque is modified at any time after the iterator
31 < * is created, in any way except through the iterator's own {@code remove}
32 < * method, the iterator will generally throw a {@link
29 > * <p>The iterators returned by this class's {@link #iterator() iterator}
30 > * method are <em>fail-fast</em>: If the deque is modified at any time after
31 > * the iterator is created, in any way except through the iterator's own
32 > * {@code remove} method, the iterator will generally throw a {@link
33   * ConcurrentModificationException}.  Thus, in the face of concurrent
34   * modification, the iterator fails quickly and cleanly, rather than risking
35   * arbitrary, non-deterministic behavior at an undetermined time in the
# Line 52 | Line 53 | import java.util.stream.Streams;
53   *
54   * @author  Josh Bloch and Doug Lea
55   * @since   1.6
56 < * @param <E> the type of elements held in this collection
56 > * @param <E> the type of elements held in this deque
57   */
58   public class ArrayDeque<E> extends AbstractCollection<E>
59                             implements Deque<E>, Cloneable, Serializable
# Line 135 | Line 136 | public class ArrayDeque<E> extends Abstr
136      }
137  
138      /**
138     * Copies the elements from our element array into the specified array,
139     * in order (from first to last element in the deque).  It is assumed
140     * that the array is large enough to hold all elements in the deque.
141     *
142     * @return its argument
143     */
144    private <T> T[] copyElements(T[] a) {
145        if (head < tail) {
146            System.arraycopy(elements, head, a, 0, size());
147        } else if (head > tail) {
148            int headPortionLen = elements.length - head;
149            System.arraycopy(elements, head, a, 0, headPortionLen);
150            System.arraycopy(elements, 0, a, headPortionLen, tail);
151        }
152        return a;
153    }
154
155    /**
139       * Constructs an empty array deque with an initial capacity
140       * sufficient to hold 16 elements.
141       */
# Line 268 | Line 251 | public class ArrayDeque<E> extends Abstr
251          @SuppressWarnings("unchecked")
252          E result = (E) elements[h];
253          // Element is null if deque empty
254 <        if (result == null)
255 <            return null;
256 <        elements[h] = null;     // Must null out slot
257 <        head = (h + 1) & (elements.length - 1);
254 >        if (result != null) {
255 >            elements[h] = null; // Must null out slot
256 >            head = (h + 1) & (elements.length - 1);
257 >        }
258          return result;
259      }
260  
# Line 279 | Line 262 | public class ArrayDeque<E> extends Abstr
262          int t = (tail - 1) & (elements.length - 1);
263          @SuppressWarnings("unchecked")
264          E result = (E) elements[t];
265 <        if (result == null)
266 <            return null;
267 <        elements[t] = null;
268 <        tail = t;
265 >        if (result != null) {
266 >            elements[t] = null;
267 >            tail = t;
268 >        }
269          return result;
270      }
271  
# Line 332 | Line 315 | public class ArrayDeque<E> extends Abstr
315       * @return {@code true} if the deque contained the specified element
316       */
317      public boolean removeFirstOccurrence(Object o) {
318 <        if (o == null)
319 <            return false;
320 <        int mask = elements.length - 1;
321 <        int i = head;
322 <        Object x;
323 <        while ( (x = elements[i]) != null) {
324 <            if (o.equals(x)) {
325 <                delete(i);
343 <                return true;
318 >        if (o != null) {
319 >            int mask = elements.length - 1;
320 >            int i = head;
321 >            for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) {
322 >                if (o.equals(x)) {
323 >                    delete(i);
324 >                    return true;
325 >                }
326              }
345            i = (i + 1) & mask;
327          }
328          return false;
329      }
# Line 360 | Line 341 | public class ArrayDeque<E> extends Abstr
341       * @return {@code true} if the deque contained the specified element
342       */
343      public boolean removeLastOccurrence(Object o) {
344 <        if (o == null)
345 <            return false;
346 <        int mask = elements.length - 1;
347 <        int i = (tail - 1) & mask;
348 <        Object x;
349 <        while ( (x = elements[i]) != null) {
350 <            if (o.equals(x)) {
351 <                delete(i);
371 <                return true;
344 >        if (o != null) {
345 >            int mask = elements.length - 1;
346 >            int i = (tail - 1) & mask;
347 >            for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) {
348 >                if (o.equals(x)) {
349 >                    delete(i);
350 >                    return true;
351 >                }
352              }
373            i = (i - 1) & mask;
353          }
354          return false;
355      }
# Line 629 | Line 608 | public class ArrayDeque<E> extends Abstr
608          }
609      }
610  
611 +    /**
612 +     * This class is nearly a mirror-image of DeqIterator, using tail
613 +     * instead of head for initial cursor, and head instead of tail
614 +     * for fence.
615 +     */
616      private class DescendingIterator implements Iterator<E> {
633        /*
634         * This class is nearly a mirror-image of DeqIterator, using
635         * tail instead of head for initial cursor, and head instead of
636         * tail for fence.
637         */
617          private int cursor = tail;
618          private int fence = head;
619          private int lastRet = -1;
# Line 675 | Line 654 | public class ArrayDeque<E> extends Abstr
654       * @return {@code true} if this deque contains the specified element
655       */
656      public boolean contains(Object o) {
657 <        if (o == null)
658 <            return false;
659 <        int mask = elements.length - 1;
660 <        int i = head;
661 <        Object x;
662 <        while ( (x = elements[i]) != null) {
663 <            if (o.equals(x))
685 <                return true;
686 <            i = (i + 1) & mask;
657 >        if (o != null) {
658 >            int mask = elements.length - 1;
659 >            int i = head;
660 >            for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) {
661 >                if (o.equals(x))
662 >                    return true;
663 >            }
664          }
665          return false;
666      }
# Line 737 | Line 714 | public class ArrayDeque<E> extends Abstr
714       * @return an array containing all of the elements in this deque
715       */
716      public Object[] toArray() {
717 <        return copyElements(new Object[size()]);
717 >        final int head = this.head;
718 >        final int tail = this.tail;
719 >        boolean wrap = (tail < head);
720 >        int end = wrap ? tail + elements.length : tail;
721 >        Object[] a = Arrays.copyOfRange(elements, head, end);
722 >        if (wrap)
723 >            System.arraycopy(elements, 0, a, elements.length - head, tail);
724 >        return a;
725      }
726  
727      /**
# Line 762 | Line 746 | public class ArrayDeque<E> extends Abstr
746       * The following code can be used to dump the deque into a newly
747       * allocated array of {@code String}:
748       *
749 <     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
749 >     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
750       *
751       * Note that {@code toArray(new Object[0])} is identical in function to
752       * {@code toArray()}.
# Line 778 | Line 762 | public class ArrayDeque<E> extends Abstr
762       */
763      @SuppressWarnings("unchecked")
764      public <T> T[] toArray(T[] a) {
765 <        int size = size();
766 <        if (a.length < size)
767 <            a = (T[])java.lang.reflect.Array.newInstance(
768 <                    a.getClass().getComponentType(), size);
769 <        copyElements(a);
770 <        if (a.length > size)
771 <            a[size] = null;
765 >        final int head = this.head;
766 >        final int tail = this.tail;
767 >        boolean wrap = (tail < head);
768 >        int size = (tail - head) + (wrap ? elements.length : 0);
769 >        int firstLeg = size - (wrap ? tail : 0);
770 >        int len = a.length;
771 >        if (size > len) {
772 >            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
773 >                                         a.getClass());
774 >        } else {
775 >            System.arraycopy(elements, head, a, 0, firstLeg);
776 >            if (size < len)
777 >                a[size] = null;
778 >        }
779 >        if (wrap)
780 >            System.arraycopy(elements, 0, a, firstLeg, tail);
781          return a;
782      }
783  
# Line 811 | Line 804 | public class ArrayDeque<E> extends Abstr
804      /**
805       * Saves this deque to a stream (that is, serializes it).
806       *
807 +     * @param s the stream
808 +     * @throws java.io.IOException if an I/O error occurs
809       * @serialData The current size ({@code int}) of the deque,
810       * followed by all of its elements (each an object reference) in
811       * first-to-last order.
# Line 830 | Line 825 | public class ArrayDeque<E> extends Abstr
825  
826      /**
827       * Reconstitutes this deque from a stream (that is, deserializes it).
828 +     * @param s the stream
829 +     * @throws ClassNotFoundException if the class of a serialized object
830 +     *         could not be found
831 +     * @throws java.io.IOException if an I/O error occurs
832       */
833      private void readObject(java.io.ObjectInputStream s)
834              throws java.io.IOException, ClassNotFoundException {
# Line 846 | Line 845 | public class ArrayDeque<E> extends Abstr
845              elements[i] = s.readObject();
846      }
847  
848 <    Spliterator<E> spliterator() {
848 >    public Spliterator<E> spliterator() {
849          return new DeqSpliterator<E>(this, -1, -1);
850      }
851  
853    public Stream<E> stream() {
854        return Streams.stream(spliterator());
855    }
856
857    public Stream<E> parallelStream() {
858        return Streams.parallelStream(spliterator());
859    }
860
852      static final class DeqSpliterator<E> implements Spliterator<E> {
853          private final ArrayDeque<E> deq;
854          private int fence;  // -1 until first use
855          private int index;  // current index, modified on traverse/split
856 <        
857 <        /** Create new spliterator covering the given array and range */
856 >
857 >        /** Creates new spliterator covering the given array and range */
858          DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) {
859              this.deq = deq;
860              this.index = origin;
# Line 879 | Line 870 | public class ArrayDeque<E> extends Abstr
870              return t;
871          }
872  
873 <        public DeqSpliterator<E> trySplit() {
873 >        public Spliterator<E> trySplit() {
874              int t = getFence(), h = index, n = deq.elements.length;
875              if (h != t && ((h + 1) & (n - 1)) != t) {
876                  if (h > t)
# Line 890 | Line 881 | public class ArrayDeque<E> extends Abstr
881              return null;
882          }
883  
884 <        public void forEach(Consumer<? super E> consumer) {
884 >        public void forEachRemaining(Consumer<? super E> consumer) {
885              if (consumer == null)
886                  throw new NullPointerException();
887              Object[] a = deq.elements;
# Line 910 | Line 901 | public class ArrayDeque<E> extends Abstr
901                  throw new NullPointerException();
902              Object[] a = deq.elements;
903              int m = a.length - 1, f = getFence(), i = index;
904 <            if (i != fence) {
904 >            if (i != f) {
905                  @SuppressWarnings("unchecked") E e = (E)a[i];
906                  index = (i + 1) & m;
907                  if (e == null)
# Line 930 | Line 921 | public class ArrayDeque<E> extends Abstr
921  
922          @Override
923          public int characteristics() {
924 <            return Spliterator.ORDERED | Spliterator.SIZED |
924 >            return Spliterator.ORDERED | Spliterator.SIZED |
925                  Spliterator.NONNULL | Spliterator.SUBSIZED;
926          }
927      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines