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.123 by jsr166, Sat Nov 26 14:16:18 2016 UTC vs.
Revision 1.137 by jsr166, Sun Nov 11 17:37:30 2018 UTC

# Line 8 | Line 8 | package java.util;
8   import java.io.Serializable;
9   import java.util.function.Consumer;
10   import java.util.function.Predicate;
11 < import java.util.function.UnaryOperator;
11 > // OPENJDK import jdk.internal.access.SharedSecrets;
12  
13   /**
14   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 50 | Line 50 | import java.util.function.UnaryOperator;
50   * Iterator} interfaces.
51   *
52   * <p>This class is a member of the
53 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
53 > * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
54   * Java Collections Framework</a>.
55   *
56   * @author  Josh Bloch and Doug Lea
# Line 117 | Line 117 | public class ArrayDeque<E> extends Abstr
117          if (jump < needed
118              || (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0)
119              newCapacity = newCapacity(needed, jump);
120 <        elements = Arrays.copyOf(elements, newCapacity);
120 >        final Object[] es = elements = Arrays.copyOf(elements, newCapacity);
121          // Exceptionally, here tail == head needs to be disambiguated
122 <        if (tail < head || (tail == head && elements[head] != null)) {
122 >        if (tail < head || (tail == head && es[head] != null)) {
123              // wrap around; slide first leg forward to end of array
124              int newSpace = newCapacity - oldCapacity;
125 <            System.arraycopy(elements, head,
126 <                             elements, head + newSpace,
125 >            System.arraycopy(es, head,
126 >                             es, head + newSpace,
127                               oldCapacity - head);
128 <            Arrays.fill(elements, head, head + newSpace, null);
129 <            head += newSpace;
128 >            for (int i = head, to = (head += newSpace); i < to; i++)
129 >                es[i] = null;
130          }
131          // checkInvariants();
132      }
# Line 180 | Line 180 | public class ArrayDeque<E> extends Abstr
180       * sufficient to hold 16 elements.
181       */
182      public ArrayDeque() {
183 <        elements = new Object[16];
183 >        elements = new Object[16 + 1];
184      }
185  
186      /**
# Line 208 | Line 208 | public class ArrayDeque<E> extends Abstr
208       */
209      public ArrayDeque(Collection<? extends E> c) {
210          this(c.size());
211 <        addAll(c);
211 >        copyElements(c);
212      }
213  
214      /**
215 <     * Increments i, mod modulus.
215 >     * Circularly increments i, mod modulus.
216       * Precondition and postcondition: 0 <= i < modulus.
217       */
218      static final int inc(int i, int modulus) {
# Line 221 | Line 221 | public class ArrayDeque<E> extends Abstr
221      }
222  
223      /**
224 <     * Decrements i, mod modulus.
224 >     * Circularly decrements i, mod modulus.
225       * Precondition and postcondition: 0 <= i < modulus.
226       */
227      static final int dec(int i, int modulus) {
# Line 234 | Line 234 | public class ArrayDeque<E> extends Abstr
234       * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
235       * @return index 0 <= i < modulus
236       */
237 <    static final int add(int i, int distance, int modulus) {
238 <        if ((i += distance) - modulus >= 0) distance -= modulus;
237 >    static final int inc(int i, int distance, int modulus) {
238 >        if ((i += distance) - modulus >= 0) i -= modulus;
239          return i;
240      }
241  
# Line 244 | Line 244 | public class ArrayDeque<E> extends Abstr
244       * Index i must be logically ahead of index j.
245       * Precondition: 0 <= i < modulus, 0 <= j < modulus.
246       * @return the "circular distance" from j to i; corner case i == j
247 <     * is diambiguated to "empty", returning 0.
247 >     * is disambiguated to "empty", returning 0.
248       */
249      static final int sub(int i, int j, int modulus) {
250          if ((i -= j) < 0) i += modulus;
# Line 313 | Line 313 | public class ArrayDeque<E> extends Abstr
313      /**
314       * Adds all of the elements in the specified collection at the end
315       * of this deque, as if by calling {@link #addLast} on each one,
316 <     * in the order that they are returned by the collection's
317 <     * iterator.
316 >     * in the order that they are returned by the collection's iterator.
317       *
318       * @param c the elements to be inserted into this deque
319       * @return {@code true} if this deque changed as a result of the call
# Line 325 | Line 324 | public class ArrayDeque<E> extends Abstr
324          final int s, needed;
325          if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
326              grow(needed);
327 <        c.forEach(this::addLast);
327 >        copyElements(c);
328          // checkInvariants();
329          return size() > s;
330      }
331  
332 +    private void copyElements(Collection<? extends E> c) {
333 +        c.forEach(this::addLast);
334 +    }
335 +
336      /**
337       * Inserts the specified element at the front of this deque.
338       *
# Line 520 | Line 523 | public class ArrayDeque<E> extends Abstr
523      /**
524       * Retrieves and removes the head of the queue represented by this deque.
525       *
526 <     * This method differs from {@link #poll poll} only in that it throws an
527 <     * exception if this deque is empty.
526 >     * This method differs from {@link #poll() poll()} only in that it
527 >     * throws an exception if this deque is empty.
528       *
529       * <p>This method is equivalent to {@link #removeFirst}.
530       *
# Line 820 | Line 823 | public class ArrayDeque<E> extends Abstr
823  
824          /** Constructs spliterator over the given range. */
825          DeqSpliterator(int origin, int fence) {
826 +            // assert 0 <= origin && origin < elements.length;
827 +            // assert 0 <= fence && fence < elements.length;
828              this.cursor = origin;
829              this.fence = fence;
830          }
# Line 839 | Line 844 | public class ArrayDeque<E> extends Abstr
844              final int i, n;
845              return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
846                  ? null
847 <                : new DeqSpliterator(i, cursor = add(i, n, es.length));
847 >                : new DeqSpliterator(i, cursor = inc(i, n, es.length));
848          }
849  
850          public void forEachRemaining(Consumer<? super E> action) {
# Line 886 | Line 891 | public class ArrayDeque<E> extends Abstr
891          }
892      }
893  
894 +    /**
895 +     * @throws NullPointerException {@inheritDoc}
896 +     */
897      public void forEach(Consumer<? super E> action) {
898          Objects.requireNonNull(action);
899          final Object[] es = elements;
# Line 908 | Line 916 | public class ArrayDeque<E> extends Abstr
916       * @param operator the operator to apply to each element
917       * @since TBD
918       */
919 <    /* public */ void replaceAll(UnaryOperator<E> operator) {
919 >    /* public */ void replaceAll(java.util.function.UnaryOperator<E> operator) {
920          Objects.requireNonNull(operator);
921          final Object[] es = elements;
922          for (int i = head, end = tail, to = (i <= end) ? end : es.length;
# Line 1074 | Line 1082 | public class ArrayDeque<E> extends Abstr
1082  
1083      /**
1084       * Nulls out slots starting at array index i, upto index end.
1085 +     * Condition i == end means "empty" - nothing to do.
1086       */
1087      private static void circularClear(Object[] es, int i, int end) {
1088 +        // assert 0 <= i && i < es.length;
1089 +        // assert 0 <= end && end < es.length;
1090          for (int to = (i <= end) ? end : es.length;
1091               ; i = 0, to = end) {
1092 <            Arrays.fill(es, i, to, null);
1092 >            for (; i < to; i++) es[i] = null;
1093              if (to == end) break;
1094          }
1095      }
# Line 1228 | Line 1239 | public class ArrayDeque<E> extends Abstr
1239  
1240          // Read in size and allocate array
1241          int size = s.readInt();
1242 +        jsr166.Platform.checkArray(s, Object[].class, size + 1);
1243          elements = new Object[size + 1];
1244          this.tail = size;
1245  
# Line 1242 | Line 1254 | public class ArrayDeque<E> extends Abstr
1254          // head == tail disambiguates to "empty".
1255          try {
1256              int capacity = elements.length;
1257 <            // assert head >= 0 && head < capacity;
1258 <            // assert tail >= 0 && tail < capacity;
1257 >            // assert 0 <= head && head < capacity;
1258 >            // assert 0 <= tail && tail < capacity;
1259              // assert capacity > 0;
1260              // assert size() < capacity;
1261              // assert head == tail || elements[head] != null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines