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.132 by jsr166, Sun Oct 22 17:44:02 2017 UTC

# Line 9 | Line 9 | import java.io.Serializable;
9   import java.util.function.Consumer;
10   import java.util.function.Predicate;
11   import java.util.function.UnaryOperator;
12 + import jdk.internal.misc.SharedSecrets;
13  
14   /**
15   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 50 | Line 51 | import java.util.function.UnaryOperator;
51   * Iterator} interfaces.
52   *
53   * <p>This class is a member of the
54 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
54 > * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
55   * Java Collections Framework</a>.
56   *
57   * @author  Josh Bloch and Doug Lea
# Line 117 | Line 118 | public class ArrayDeque<E> extends Abstr
118          if (jump < needed
119              || (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0)
120              newCapacity = newCapacity(needed, jump);
121 <        elements = Arrays.copyOf(elements, newCapacity);
121 >        final Object[] es = elements = Arrays.copyOf(elements, newCapacity);
122          // Exceptionally, here tail == head needs to be disambiguated
123 <        if (tail < head || (tail == head && elements[head] != null)) {
123 >        if (tail < head || (tail == head && es[head] != null)) {
124              // wrap around; slide first leg forward to end of array
125              int newSpace = newCapacity - oldCapacity;
126 <            System.arraycopy(elements, head,
127 <                             elements, head + newSpace,
126 >            System.arraycopy(es, head,
127 >                             es, head + newSpace,
128                               oldCapacity - head);
129 <            Arrays.fill(elements, head, head + newSpace, null);
130 <            head += newSpace;
129 >            for (int i = head, to = (head += newSpace); i < to; i++)
130 >                es[i] = null;
131          }
132          // checkInvariants();
133      }
# Line 212 | Line 213 | public class ArrayDeque<E> extends Abstr
213      }
214  
215      /**
216 <     * Increments i, mod modulus.
216 >     * Circularly increments i, mod modulus.
217       * Precondition and postcondition: 0 <= i < modulus.
218       */
219      static final int inc(int i, int modulus) {
# Line 221 | Line 222 | public class ArrayDeque<E> extends Abstr
222      }
223  
224      /**
225 <     * Decrements i, mod modulus.
225 >     * Circularly decrements i, mod modulus.
226       * Precondition and postcondition: 0 <= i < modulus.
227       */
228      static final int dec(int i, int modulus) {
# Line 234 | Line 235 | public class ArrayDeque<E> extends Abstr
235       * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
236       * @return index 0 <= i < modulus
237       */
238 <    static final int add(int i, int distance, int modulus) {
239 <        if ((i += distance) - modulus >= 0) distance -= modulus;
238 >    static final int inc(int i, int distance, int modulus) {
239 >        if ((i += distance) - modulus >= 0) i -= modulus;
240          return i;
241      }
242  
# Line 244 | Line 245 | public class ArrayDeque<E> extends Abstr
245       * Index i must be logically ahead of index j.
246       * Precondition: 0 <= i < modulus, 0 <= j < modulus.
247       * @return the "circular distance" from j to i; corner case i == j
248 <     * is diambiguated to "empty", returning 0.
248 >     * is disambiguated to "empty", returning 0.
249       */
250      static final int sub(int i, int j, int modulus) {
251          if ((i -= j) < 0) i += modulus;
# Line 313 | Line 314 | public class ArrayDeque<E> extends Abstr
314      /**
315       * Adds all of the elements in the specified collection at the end
316       * of this deque, as if by calling {@link #addLast} on each one,
317 <     * in the order that they are returned by the collection's
317 <     * iterator.
317 >     * in the order that they are returned by the collection's iterator.
318       *
319       * @param c the elements to be inserted into this deque
320       * @return {@code true} if this deque changed as a result of the call
# Line 520 | Line 520 | public class ArrayDeque<E> extends Abstr
520      /**
521       * Retrieves and removes the head of the queue represented by this deque.
522       *
523 <     * This method differs from {@link #poll poll} only in that it throws an
524 <     * exception if this deque is empty.
523 >     * This method differs from {@link #poll() poll()} only in that it
524 >     * throws an exception if this deque is empty.
525       *
526       * <p>This method is equivalent to {@link #removeFirst}.
527       *
# Line 820 | Line 820 | public class ArrayDeque<E> extends Abstr
820  
821          /** Constructs spliterator over the given range. */
822          DeqSpliterator(int origin, int fence) {
823 +            // assert 0 <= origin && origin < elements.length;
824 +            // assert 0 <= fence && fence < elements.length;
825              this.cursor = origin;
826              this.fence = fence;
827          }
# Line 839 | Line 841 | public class ArrayDeque<E> extends Abstr
841              final int i, n;
842              return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
843                  ? null
844 <                : new DeqSpliterator(i, cursor = add(i, n, es.length));
844 >                : new DeqSpliterator(i, cursor = inc(i, n, es.length));
845          }
846  
847          public void forEachRemaining(Consumer<? super E> action) {
# Line 886 | Line 888 | public class ArrayDeque<E> extends Abstr
888          }
889      }
890  
891 +    /**
892 +     * @throws NullPointerException {@inheritDoc}
893 +     */
894      public void forEach(Consumer<? super E> action) {
895          Objects.requireNonNull(action);
896          final Object[] es = elements;
# Line 1074 | Line 1079 | public class ArrayDeque<E> extends Abstr
1079  
1080      /**
1081       * Nulls out slots starting at array index i, upto index end.
1082 +     * Condition i == end means "empty" - nothing to do.
1083       */
1084      private static void circularClear(Object[] es, int i, int end) {
1085 +        // assert 0 <= i && i < es.length;
1086 +        // assert 0 <= end && end < es.length;
1087          for (int to = (i <= end) ? end : es.length;
1088               ; i = 0, to = end) {
1089 <            Arrays.fill(es, i, to, null);
1089 >            for (; i < to; i++) es[i] = null;
1090              if (to == end) break;
1091          }
1092      }
# Line 1228 | Line 1236 | public class ArrayDeque<E> extends Abstr
1236  
1237          // Read in size and allocate array
1238          int size = s.readInt();
1239 +        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1);
1240          elements = new Object[size + 1];
1241          this.tail = size;
1242  
# Line 1242 | Line 1251 | public class ArrayDeque<E> extends Abstr
1251          // head == tail disambiguates to "empty".
1252          try {
1253              int capacity = elements.length;
1254 <            // assert head >= 0 && head < capacity;
1255 <            // assert tail >= 0 && tail < capacity;
1254 >            // assert 0 <= head && head < capacity;
1255 >            // assert 0 <= tail && tail < capacity;
1256              // assert capacity > 0;
1257              // assert size() < capacity;
1258              // assert head == tail || elements[head] != null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines