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.40 by jsr166, Sun Feb 26 22:43:03 2012 UTC vs.
Revision 1.51 by jsr166, Wed Feb 20 12:39:06 2013 UTC

# Line 4 | Line 4
4   */
5  
6   package java.util;
7 + import java.io.Serializable;
8 + import java.util.function.Consumer;
9 + import java.util.stream.Stream;
10 + import java.util.stream.Streams;
11  
12   /**
13   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 53 | Line 57 | package java.util;
57   * @param <E> the type of elements held in this collection
58   */
59   public class ArrayDeque<E> extends AbstractCollection<E>
60 <                           implements Deque<E>, Cloneable, java.io.Serializable
60 >                           implements Deque<E>, Cloneable, Serializable
61   {
62      /**
63       * The array in which the elements of the deque are stored.
# Line 65 | Line 69 | public class ArrayDeque<E> extends Abstr
69       * other.  We also guarantee that all array cells not holding
70       * deque elements are always null.
71       */
72 <    private transient Object[] elements;
72 >    transient Object[] elements; // non-private to simplify nested class access
73  
74      /**
75       * The index of the element at the head of the deque (which is the
76       * element that would be removed by remove() or pop()); or an
77       * arbitrary number equal to tail if the deque is empty.
78       */
79 <    private transient int head;
79 >    transient int head;
80  
81      /**
82       * The index at which the next element would be added to the tail
83       * of the deque (via addLast(E), add(E), or push(E)).
84       */
85 <    private transient int tail;
85 >    transient int tail;
86  
87      /**
88       * The minimum capacity that we'll use for a newly created deque.
# Line 133 | Line 137 | public class ArrayDeque<E> extends Abstr
137      }
138  
139      /**
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     *
140     * @return its argument
141     */
142    private <T> T[] copyElements(T[] a) {
143        if (head < tail) {
144            System.arraycopy(elements, head, a, 0, size());
145        } else if (head > tail) {
146            int headPortionLen = elements.length - head;
147            System.arraycopy(elements, head, a, 0, headPortionLen);
148            System.arraycopy(elements, 0, a, headPortionLen, tail);
149        }
150        return a;
151    }
152
153    /**
140       * Constructs an empty array deque with an initial capacity
141       * sufficient to hold 16 elements.
142       */
# Line 694 | Line 680 | public class ArrayDeque<E> extends Abstr
680       * Returns {@code true} if this deque contained the specified element
681       * (or equivalently, if this deque changed as a result of the call).
682       *
683 <     * <p>This method is equivalent to {@link #removeFirstOccurrence}.
683 >     * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
684       *
685       * @param o element to be removed from this deque, if present
686       * @return {@code true} if this deque contained the specified element
# Line 735 | Line 721 | public class ArrayDeque<E> extends Abstr
721       * @return an array containing all of the elements in this deque
722       */
723      public Object[] toArray() {
724 <        return copyElements(new Object[size()]);
724 >        final int head = this.head;
725 >        final int tail = this.tail;
726 >        boolean wrap = (tail < head);
727 >        int end = wrap ? tail + elements.length : tail;
728 >        Object[] a = Arrays.copyOfRange(elements, head, end);
729 >        if (wrap)
730 >            System.arraycopy(elements, 0, a, elements.length - head, tail);
731 >        return a;
732      }
733  
734      /**
# Line 776 | Line 769 | public class ArrayDeque<E> extends Abstr
769       */
770      @SuppressWarnings("unchecked")
771      public <T> T[] toArray(T[] a) {
772 <        int size = size();
773 <        if (a.length < size)
774 <            a = (T[])java.lang.reflect.Array.newInstance(
775 <                    a.getClass().getComponentType(), size);
776 <        copyElements(a);
777 <        if (a.length > size)
778 <            a[size] = null;
772 >        final int head = this.head;
773 >        final int tail = this.tail;
774 >        boolean wrap = (tail < head);
775 >        int size = (tail - head) + (wrap ? elements.length : 0);
776 >        int firstLeg = size - (wrap ? tail : 0);
777 >        int len = a.length;
778 >        if (size > len) {
779 >            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
780 >                                         a.getClass());
781 >        } else {
782 >            System.arraycopy(elements, head, a, 0, firstLeg);
783 >            if (size < len)
784 >                a[size] = null;
785 >        }
786 >        if (wrap)
787 >            System.arraycopy(elements, 0, a, firstLeg, tail);
788          return a;
789      }
790  
# Line 843 | Line 845 | public class ArrayDeque<E> extends Abstr
845          for (int i = 0; i < size; i++)
846              elements[i] = s.readObject();
847      }
848 +
849 +    Spliterator<E> spliterator() {
850 +        return new DeqSpliterator<E>(this, -1, -1);
851 +    }
852 +
853 +    public Stream<E> stream() {
854 +        return Streams.stream(spliterator());
855 +    }
856 +
857 +    public Stream<E> parallelStream() {
858 +        return Streams.parallelStream(spliterator());
859 +    }
860 +
861 +    static final class DeqSpliterator<E> implements Spliterator<E> {
862 +        private final ArrayDeque<E> deq;
863 +        private int fence;  // -1 until first use
864 +        private int index;  // current index, modified on traverse/split
865 +
866 +        /** Creates new spliterator covering the given array and range */
867 +        DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) {
868 +            this.deq = deq;
869 +            this.index = origin;
870 +            this.fence = fence;
871 +        }
872 +
873 +        private int getFence() { // force initialization
874 +            int t;
875 +            if ((t = fence) < 0) {
876 +                t = fence = deq.tail;
877 +                index = deq.head;
878 +            }
879 +            return t;
880 +        }
881 +
882 +        public DeqSpliterator<E> trySplit() {
883 +            int t = getFence(), h = index, n = deq.elements.length;
884 +            if (h != t && ((h + 1) & (n - 1)) != t) {
885 +                if (h > t)
886 +                    t += n;
887 +                int m = ((h + t) >>> 1) & (n - 1);
888 +                return new DeqSpliterator<>(deq, h, index = m);
889 +            }
890 +            return null;
891 +        }
892 +
893 +        public void forEach(Consumer<? super E> consumer) {
894 +            if (consumer == null)
895 +                throw new NullPointerException();
896 +            Object[] a = deq.elements;
897 +            int m = a.length - 1, f = getFence(), i = index;
898 +            index = f;
899 +            while (i != f) {
900 +                @SuppressWarnings("unchecked") E e = (E)a[i];
901 +                i = (i + 1) & m;
902 +                if (e == null)
903 +                    throw new ConcurrentModificationException();
904 +                consumer.accept(e);
905 +            }
906 +        }
907 +
908 +        public boolean tryAdvance(Consumer<? super E> consumer) {
909 +            if (consumer == null)
910 +                throw new NullPointerException();
911 +            Object[] a = deq.elements;
912 +            int m = a.length - 1, f = getFence(), i = index;
913 +            if (i != fence) {
914 +                @SuppressWarnings("unchecked") E e = (E)a[i];
915 +                index = (i + 1) & m;
916 +                if (e == null)
917 +                    throw new ConcurrentModificationException();
918 +                consumer.accept(e);
919 +                return true;
920 +            }
921 +            return false;
922 +        }
923 +
924 +        public long estimateSize() {
925 +            int n = getFence() - index;
926 +            if (n < 0)
927 +                n += deq.elements.length;
928 +            return (long) n;
929 +        }
930 +
931 +        @Override
932 +        public int characteristics() {
933 +            return Spliterator.ORDERED | Spliterator.SIZED |
934 +                Spliterator.NONNULL | Spliterator.SUBSIZED;
935 +        }
936 +    }
937 +
938   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines