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.121 by jsr166, Mon Nov 21 15:30:44 2016 UTC vs.
Revision 1.131 by jsr166, Wed Aug 23 22:41:49 2017 UTC

# 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/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 212 | Line 212 | public class ArrayDeque<E> extends Abstr
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 520 | Line 519 | public class ArrayDeque<E> extends Abstr
519      /**
520       * Retrieves and removes the head of the queue represented by this deque.
521       *
522 <     * This method differs from {@link #poll poll} only in that it throws an
523 <     * exception if this deque is empty.
522 >     * This method differs from {@link #poll() poll()} only in that it
523 >     * throws an exception if this deque is empty.
524       *
525       * <p>This method is equivalent to {@link #removeFirst}.
526       *
# Line 710 | Line 709 | public class ArrayDeque<E> extends Abstr
709                  throw new NoSuchElementException();
710              final Object[] es = elements;
711              E e = nonNullElementAt(es, cursor);
712 <            lastRet = cursor;
714 <            cursor = inc(cursor, es.length);
712 >            cursor = inc(lastRet = cursor, es.length);
713              remaining--;
714              return e;
715          }
# Line 759 | Line 757 | public class ArrayDeque<E> extends Abstr
757                  throw new NoSuchElementException();
758              final Object[] es = elements;
759              E e = nonNullElementAt(es, cursor);
760 <            lastRet = cursor;
763 <            cursor = dec(cursor, es.length);
760 >            cursor = dec(lastRet = cursor, es.length);
761              remaining--;
762              return e;
763          }
# Line 822 | Line 819 | public class ArrayDeque<E> extends Abstr
819  
820          /** Constructs spliterator over the given range. */
821          DeqSpliterator(int origin, int fence) {
822 +            // assert 0 <= origin && origin < elements.length;
823 +            // assert 0 <= fence && fence < elements.length;
824              this.cursor = origin;
825              this.fence = fence;
826          }
# Line 841 | Line 840 | public class ArrayDeque<E> extends Abstr
840              final int i, n;
841              return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
842                  ? null
843 <                : new DeqSpliterator(i, cursor = add(i, n, es.length));
843 >                : new DeqSpliterator(i, cursor = inc(i, n, es.length));
844          }
845  
846          public void forEachRemaining(Consumer<? super E> action) {
# Line 888 | Line 887 | public class ArrayDeque<E> extends Abstr
887          }
888      }
889  
890 +    /**
891 +     * @throws NullPointerException {@inheritDoc}
892 +     */
893      public void forEach(Consumer<? super E> action) {
894          Objects.requireNonNull(action);
895          final Object[] es = elements;
# Line 1076 | Line 1078 | public class ArrayDeque<E> extends Abstr
1078  
1079      /**
1080       * Nulls out slots starting at array index i, upto index end.
1081 +     * Condition i == end means "empty" - nothing to do.
1082       */
1083      private static void circularClear(Object[] es, int i, int end) {
1084 +        // assert 0 <= i && i < es.length;
1085 +        // assert 0 <= end && end < es.length;
1086          for (int to = (i <= end) ? end : es.length;
1087               ; i = 0, to = end) {
1088 <            Arrays.fill(es, i, to, null);
1088 >            for (; i < to; i++) es[i] = null;
1089              if (to == end) break;
1090          }
1091      }
# Line 1244 | Line 1249 | public class ArrayDeque<E> extends Abstr
1249          // head == tail disambiguates to "empty".
1250          try {
1251              int capacity = elements.length;
1252 <            // assert head >= 0 && head < capacity;
1253 <            // assert tail >= 0 && tail < capacity;
1252 >            // assert 0 <= head && head < capacity;
1253 >            // assert 0 <= tail && tail < capacity;
1254              // assert capacity > 0;
1255              // assert size() < capacity;
1256              // assert head == tail || elements[head] != null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines