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.43 by jsr166, Wed Jan 16 21:25:33 2013 UTC vs.
Revision 1.50 by jsr166, Wed Feb 20 12:32:01 2013 UTC

# Line 1 | Line 1
1   /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /*
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
29 * file:
30 *
2   * Written by Josh Bloch of Google Inc. and released to the public domain,
3   * as explained at http://creativecommons.org/publicdomain/zero/1.0/.
4   */
5  
6   package java.util;
7 < import java.util.Spliterator;
7 > import java.io.Serializable;
8 > import java.util.function.Consumer;
9   import java.util.stream.Stream;
10   import java.util.stream.Streams;
39 import java.util.function.Block;
11  
12   /**
13   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 84 | Line 55 | import java.util.function.Block;
55   * @param <E> the type of elements held in this collection
56   */
57   public class ArrayDeque<E> extends AbstractCollection<E>
58 <                           implements Deque<E>, Cloneable, java.io.Serializable
58 >                           implements Deque<E>, Cloneable, Serializable
59   {
60      /**
61       * The array in which the elements of the deque are stored.
# Line 164 | Line 135 | public class ArrayDeque<E> extends Abstr
135      }
136  
137      /**
167     * Copies the elements from our element array into the specified array,
168     * in order (from first to last element in the deque).  It is assumed
169     * that the array is large enough to hold all elements in the deque.
170     *
171     * @return its argument
172     */
173    private <T> T[] copyElements(T[] a) {
174        if (head < tail) {
175            System.arraycopy(elements, head, a, 0, size());
176        } else if (head > tail) {
177            int headPortionLen = elements.length - head;
178            System.arraycopy(elements, head, a, 0, headPortionLen);
179            System.arraycopy(elements, 0, a, headPortionLen, tail);
180        }
181        return a;
182    }
183
184    /**
138       * Constructs an empty array deque with an initial capacity
139       * sufficient to hold 16 elements.
140       */
# Line 725 | Line 678 | public class ArrayDeque<E> extends Abstr
678       * Returns {@code true} if this deque contained the specified element
679       * (or equivalently, if this deque changed as a result of the call).
680       *
681 <     * <p>This method is equivalent to {@link #removeFirstOccurrence}.
681 >     * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
682       *
683       * @param o element to be removed from this deque, if present
684       * @return {@code true} if this deque contained the specified element
# Line 766 | Line 719 | public class ArrayDeque<E> extends Abstr
719       * @return an array containing all of the elements in this deque
720       */
721      public Object[] toArray() {
722 <        return copyElements(new Object[size()]);
722 >        final int head = this.head;
723 >        final int tail = this.tail;
724 >        boolean wrap = (tail < head);
725 >        int end = wrap ? tail + elements.length : tail;
726 >        Object[] a = Arrays.copyOfRange(elements, head, end);
727 >        if (wrap)
728 >            System.arraycopy(elements, 0, a, elements.length - head, tail);
729 >        return a;
730      }
731  
732      /**
# Line 807 | Line 767 | public class ArrayDeque<E> extends Abstr
767       */
768      @SuppressWarnings("unchecked")
769      public <T> T[] toArray(T[] a) {
770 <        int size = size();
771 <        if (a.length < size)
772 <            a = (T[])java.lang.reflect.Array.newInstance(
773 <                    a.getClass().getComponentType(), size);
774 <        copyElements(a);
775 <        if (a.length > size)
776 <            a[size] = null;
770 >        final int head = this.head;
771 >        final int tail = this.tail;
772 >        boolean wrap = (tail < head);
773 >        int size = (tail - head) + (wrap ? elements.length : 0);
774 >        int firstLeg = size - (wrap ? tail : 0);
775 >        int len = a.length;
776 >        if (size > len) {
777 >            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
778 >                                         a.getClass());
779 >        } else {
780 >            System.arraycopy(elements, head, a, 0, firstLeg);
781 >            if (size < len)
782 >                a[size] = null;
783 >        }
784 >        if (wrap)
785 >            System.arraycopy(elements, 0, a, firstLeg, tail);
786          return a;
787      }
788  
# Line 875 | Line 844 | public class ArrayDeque<E> extends Abstr
844              elements[i] = s.readObject();
845      }
846  
847 +    Spliterator<E> spliterator() {
848 +        return new DeqSpliterator<E>(this, -1, -1);
849 +    }
850 +
851      public Stream<E> stream() {
852 <        int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED;
880 <        return Streams.stream
881 <            (() -> new DeqSpliterator<E>(this, head, tail), flags);
852 >        return Streams.stream(spliterator());
853      }
854 +
855      public Stream<E> parallelStream() {
856 <        int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED;
885 <        return Streams.parallelStream
886 <            (() -> new DeqSpliterator<E>(this, head, tail), flags);
856 >        return Streams.parallelStream(spliterator());
857      }
858  
859 <
890 <    static final class DeqSpliterator<E> implements Spliterator<E>, Iterator<E> {
859 >    static final class DeqSpliterator<E> implements Spliterator<E> {
860          private final ArrayDeque<E> deq;
861 <        private final Object[] array;
862 <        private final int fence;  // initially tail
894 <        private int index;        // current index, modified on traverse/split
861 >        private int fence;  // -1 until first use
862 >        private int index;  // current index, modified on traverse/split
863  
864 <        /** Create new spliterator covering the given array and range */
864 >        /** Creates new spliterator covering the given array and range */
865          DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) {
866 <            this.deq = deq; this.array = deq.elements;
867 <            this.index = origin; this.fence = fence;
866 >            this.deq = deq;
867 >            this.index = origin;
868 >            this.fence = fence;
869 >        }
870 >
871 >        private int getFence() { // force initialization
872 >            int t;
873 >            if ((t = fence) < 0) {
874 >                t = fence = deq.tail;
875 >                index = deq.head;
876 >            }
877 >            return t;
878          }
879  
880          public DeqSpliterator<E> trySplit() {
881 <            int n = array.length;
904 <            int h = index, t = fence;
881 >            int t = getFence(), h = index, n = deq.elements.length;
882              if (h != t && ((h + 1) & (n - 1)) != t) {
883                  if (h > t)
884                      t += n;
885                  int m = ((h + t) >>> 1) & (n - 1);
886 <                return new DeqSpliterator<E>(deq, h, index = m);
886 >                return new DeqSpliterator<>(deq, h, index = m);
887              }
888              return null;
889          }
890  
891 <        @SuppressWarnings("unchecked")
892 <        public void forEach(Block<? super E> block) {
916 <            if (block == null)
891 >        public void forEach(Consumer<? super E> consumer) {
892 >            if (consumer == null)
893                  throw new NullPointerException();
894 <            Object[] a = array;
895 <            if (a != deq.elements)
920 <                throw new ConcurrentModificationException();
921 <            int m = a.length - 1, f = fence, i = index;
894 >            Object[] a = deq.elements;
895 >            int m = a.length - 1, f = getFence(), i = index;
896              index = f;
897              while (i != f) {
898 <                Object e = a[i];
898 >                @SuppressWarnings("unchecked") E e = (E)a[i];
899 >                i = (i + 1) & m;
900                  if (e == null)
901                      throw new ConcurrentModificationException();
902 <                block.accept((E)e);
928 <                i = (i + 1) & m;
902 >                consumer.accept(e);
903              }
904          }
905  
906 <        @SuppressWarnings("unchecked")
907 <        public boolean tryAdvance(Block<? super E> block) {
934 <            if (block == null)
906 >        public boolean tryAdvance(Consumer<? super E> consumer) {
907 >            if (consumer == null)
908                  throw new NullPointerException();
909 <            Object[] a = array;
910 <            if (a != deq.elements)
938 <                throw new ConcurrentModificationException();
939 <            int m = a.length - 1, i = index;
909 >            Object[] a = deq.elements;
910 >            int m = a.length - 1, f = getFence(), i = index;
911              if (i != fence) {
912 <                Object e = a[i];
912 >                @SuppressWarnings("unchecked") E e = (E)a[i];
913 >                index = (i + 1) & m;
914                  if (e == null)
915                      throw new ConcurrentModificationException();
916 <                block.accept((E)e);
945 <                index = (i + 1) & m;
916 >                consumer.accept(e);
917                  return true;
918              }
919              return false;
920          }
921  
951        // Iterator support
952        public Iterator<E> iterator() {
953            return this;
954        }
955
956        public boolean hasNext() {
957            return index >= 0 && index != fence;
958        }
959
960        @SuppressWarnings("unchecked")
961            public E next() {
962            if (index < 0 || index == fence)
963                throw new NoSuchElementException();
964            Object[] a = array;
965            if (a != deq.elements)
966                throw new ConcurrentModificationException();
967            Object e = a[index];
968            if (e == null)
969                throw new ConcurrentModificationException();
970            index = (index + 1) & (a.length - 1);
971            return (E) e;
972        }
973
974        public void remove() { throw new UnsupportedOperationException(); }
975
976        // Other spliterator methods
922          public long estimateSize() {
923 <            int n = fence - index;
923 >            int n = getFence() - index;
924              if (n < 0)
925 <                n += array.length;
926 <            return (long)n;
925 >                n += deq.elements.length;
926 >            return (long) n;
927 >        }
928 >
929 >        @Override
930 >        public int characteristics() {
931 >            return Spliterator.ORDERED | Spliterator.SIZED |
932 >                Spliterator.NONNULL | Spliterator.SUBSIZED;
933          }
983        public boolean hasExactSize() { return true; }
984        public boolean hasExactSplits() { return true; }
934      }
935  
936   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines