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.108 by jsr166, Sat Nov 5 14:41:14 2016 UTC vs.
Revision 1.138 by jsr166, Fri Aug 30 18:05:38 2019 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 68 | Line 68 | public class ArrayDeque<E> extends Abstr
68       *
69       * Because in a circular array, elements are in general stored in
70       * two disjoint such slices, we help the VM by writing unusual
71 <     * nested loops for all traversals over the elements.
71 >     * nested loops for all traversals over the elements.  Having only
72 >     * one hot inner loop body instead of two or three eases human
73 >     * maintenance and encourages VM loop inlining into the caller.
74       */
75  
76      /**
77       * The array in which the elements of the deque are stored.
78 <     * We guarantee that all array cells not holding deque elements
79 <     * are always null.
78 >     * All array cells not holding deque elements are always null.
79 >     * The array always has at least one null slot (at tail).
80       */
81      transient Object[] elements;
82  
# Line 88 | Line 90 | public class ArrayDeque<E> extends Abstr
90  
91      /**
92       * The index at which the next element would be added to the tail
93 <     * of the deque (via addLast(E), add(E), or push(E)).
93 >     * of the deque (via addLast(E), add(E), or push(E));
94 >     * elements[tail] is always null.
95       */
96      transient int tail;
97  
# Line 114 | 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 177 | 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 187 | Line 190 | public class ArrayDeque<E> extends Abstr
190       * @param numElements lower bound on initial capacity of the deque
191       */
192      public ArrayDeque(int numElements) {
193 <        elements = new Object[Math.max(1, numElements + 1)];
193 >        elements =
194 >            new Object[(numElements < 1) ? 1 :
195 >                       (numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE :
196 >                       numElements + 1];
197      }
198  
199      /**
# Line 201 | Line 207 | public class ArrayDeque<E> extends Abstr
207       * @throws NullPointerException if the specified collection is null
208       */
209      public ArrayDeque(Collection<? extends E> c) {
210 <        elements = new Object[c.size() + 1];
211 <        addAll(c);
210 >        this(c.size());
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 215 | 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 224 | Line 230 | public class ArrayDeque<E> extends Abstr
230      }
231  
232      /**
233 <     * Adds i and j, mod modulus.
234 <     * Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus.
233 >     * Circularly adds the given distance to index i, mod modulus.
234 >     * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
235 >     * @return index 0 <= i < modulus
236       */
237 <    static final int add(int i, int j, int modulus) {
238 <        if ((i += j) - modulus >= 0) i -= 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  
242      /**
243       * Subtracts j from i, mod modulus.
244 <     * Index i must be logically ahead of j.
245 <     * Returns the "circular distance" from j to i.
246 <     * Precondition and postcondition: 0 <= i < modulus, 0 <= j < modulus.
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 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 244 | Line 252 | public class ArrayDeque<E> extends Abstr
252      }
253  
254      /**
247     * Returns the array index of the last element.
248     * May return invalid index -1 if there are no elements.
249     */
250    final int last() {
251        return dec(tail, elements.length);
252    }
253
254    /**
255       * Returns element at array index i.
256       * This is a slight abuse of generics, accepted by javac.
257       */
# 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 322 | Line 321 | public class ArrayDeque<E> extends Abstr
321       *         of its elements are null
322       */
323      public boolean addAll(Collection<? extends E> c) {
324 <        final int s = size(), needed;
325 <        if ((needed = s + c.size() - elements.length + 1) > 0)
324 >        final int s, needed;
325 >        if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
326              grow(needed);
327 <        c.forEach((e) -> addLast(e));
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 477 | Line 480 | public class ArrayDeque<E> extends Abstr
480              final Object[] es = elements;
481              for (int i = tail, end = head, to = (i >= end) ? end : 0;
482                   ; i = es.length, to = end) {
483 <                while (--i >= to)
483 >                for (i--; i > to - 1; i--)
484                      if (o.equals(es[i])) {
485                          delete(i);
486                          return true;
# 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 616 | Line 619 | public class ArrayDeque<E> extends Abstr
619          // checkInvariants();
620          final Object[] es = elements;
621          final int capacity = es.length;
622 <        final int h = head;
622 >        final int h, t;
623          // number of elements before to-be-deleted elt
624 <        final int front = sub(i, h, capacity);
625 <        final int back = size() - front - 1; // number of elements after
624 >        final int front = sub(i, h = head, capacity);
625 >        // number of elements after to-be-deleted elt
626 >        final int back = sub(t = tail, i, capacity) - 1;
627          if (front < back) {
628              // move front elements forwards
629              if (h <= i) {
# Line 635 | Line 639 | public class ArrayDeque<E> extends Abstr
639              return false;
640          } else {
641              // move back elements backwards
642 <            tail = dec(tail, capacity);
642 >            tail = dec(t, capacity);
643              if (i <= tail) {
644                  System.arraycopy(es, i + 1, es, i, back);
645              } else { // Wrap around
646 <                int firstLeg = capacity - (i + 1);
643 <                System.arraycopy(es, i + 1, es, i, firstLeg);
646 >                System.arraycopy(es, i + 1, es, i, capacity - (i + 1));
647                  es[capacity - 1] = es[0];
648 <                System.arraycopy(es, 1, es, 0, back - firstLeg - 1);
648 >                System.arraycopy(es, 1, es, 0, t - 1);
649              }
650              es[tail] = null;
651              // checkInvariants();
# Line 710 | Line 713 | public class ArrayDeque<E> extends Abstr
713                  throw new NoSuchElementException();
714              final Object[] es = elements;
715              E e = nonNullElementAt(es, cursor);
716 <            lastRet = cursor;
714 <            cursor = inc(cursor, es.length);
716 >            cursor = inc(lastRet = cursor, es.length);
717              remaining--;
718              return e;
719          }
# Line 752 | Line 754 | public class ArrayDeque<E> extends Abstr
754      }
755  
756      private class DescendingIterator extends DeqIterator {
757 <        DescendingIterator() { cursor = last(); }
757 >        DescendingIterator() { cursor = dec(tail, elements.length); }
758  
759          public final E next() {
760              if (remaining <= 0)
761                  throw new NoSuchElementException();
762              final Object[] es = elements;
763              E e = nonNullElementAt(es, cursor);
764 <            lastRet = cursor;
763 <            cursor = dec(cursor, es.length);
764 >            cursor = dec(lastRet = cursor, es.length);
765              remaining--;
766              return e;
767          }
# Line 781 | Line 782 | public class ArrayDeque<E> extends Abstr
782                  throw new ConcurrentModificationException();
783              for (int i = cursor, end = head, to = (i >= end) ? end : 0;
784                   ; i = es.length - 1, to = end) {
785 <                for (; i >= to; i--)
785 >                // hotspot generates faster code than for: i >= to !
786 >                for (; i > to - 1; i--)
787                      action.accept(elementAt(es, i));
788                  if (to == end) {
789                      if (end != head)
790                          throw new ConcurrentModificationException();
791 <                    lastRet = head;
791 >                    lastRet = end;
792                      break;
793                  }
794              }
# Line 821 | 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 840 | 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 863 | Line 867 | public class ArrayDeque<E> extends Abstr
867          }
868  
869          public boolean tryAdvance(Consumer<? super E> action) {
870 <            if (action == null)
871 <                throw new NullPointerException();
872 <            int t, i;
873 <            if ((t = fence) < 0) t = getFence();
874 <            if (t == (i = cursor))
870 >            Objects.requireNonNull(action);
871 >            final Object[] es = elements;
872 >            if (fence < 0) { fence = tail; cursor = head; } // late-binding
873 >            final int i;
874 >            if ((i = cursor) == fence)
875                  return false;
876 <            final Object[] es;
873 <            action.accept(nonNullElementAt(es = elements, i));
876 >            E e = nonNullElementAt(es, i);
877              cursor = inc(i, es.length);
878 +            action.accept(e);
879              return true;
880          }
881  
# Line 887 | 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 909 | 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 957 | Line 964 | public class ArrayDeque<E> extends Abstr
964               ; i = 0, to = end) {
965              for (; i < to; i++)
966                  if (filter.test(elementAt(es, i)))
967 <                    return bulkRemoveModified(filter, i, to);
967 >                    return bulkRemoveModified(filter, i);
968              if (to == end) {
969                  if (end != tail) throw new ConcurrentModificationException();
970                  break;
# Line 966 | Line 973 | public class ArrayDeque<E> extends Abstr
973          return false;
974      }
975  
976 +    // A tiny bit set implementation
977 +
978 +    private static long[] nBits(int n) {
979 +        return new long[((n - 1) >> 6) + 1];
980 +    }
981 +    private static void setBit(long[] bits, int i) {
982 +        bits[i >> 6] |= 1L << i;
983 +    }
984 +    private static boolean isClear(long[] bits, int i) {
985 +        return (bits[i >> 6] & (1L << i)) == 0;
986 +    }
987 +
988      /**
989       * Helper for bulkRemove, in case of at least one deletion.
990 <     * @param i valid index of first element to be deleted
990 >     * Tolerate predicates that reentrantly access the collection for
991 >     * read (but writers still get CME), so traverse once to find
992 >     * elements to delete, a second pass to physically expunge.
993 >     *
994 >     * @param beg valid index of first element to be deleted
995       */
996      private boolean bulkRemoveModified(
997 <        Predicate<? super E> filter, int i, int to) {
997 >        Predicate<? super E> filter, final int beg) {
998          final Object[] es = elements;
999          final int capacity = es.length;
977        // a two-finger algorithm, with hare i reading, tortoise j writing
978        int j = i++;
1000          final int end = tail;
1001 <        try {
1002 <            for (;; j = 0) {    // j rejoins i on second leg
1003 <                E e;
1004 <                // In this loop, i and j are on the same leg, with i > j
1005 <                for (; i < to; i++)
1006 <                    if (!filter.test(e = elementAt(es, i)))
1007 <                        es[j++] = e;
1008 <                if (to == end) break;
1009 <                // In this loop, j is on the first leg, i on the second
1010 <                for (i = 0, to = end; i < to && j < capacity; i++)
1011 <                    if (!filter.test(e = elementAt(es, i)))
1012 <                        es[j++] = e;
1013 <                if (i >= to) {
1014 <                    if (j == capacity) j = 0; // "corner" case
1015 <                    break;
1016 <                }
1001 >        final long[] deathRow = nBits(sub(end, beg, capacity));
1002 >        deathRow[0] = 1L;   // set bit 0
1003 >        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
1004 >             ; i = 0, to = end, k -= capacity) {
1005 >            for (; i < to; i++)
1006 >                if (filter.test(elementAt(es, i)))
1007 >                    setBit(deathRow, i - k);
1008 >            if (to == end) break;
1009 >        }
1010 >        // a two-finger traversal, with hare i reading, tortoise w writing
1011 >        int w = beg;
1012 >        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
1013 >             ; w = 0) { // w rejoins i on second leg
1014 >            // In this loop, i and w are on the same leg, with i > w
1015 >            for (; i < to; i++)
1016 >                if (isClear(deathRow, i - k))
1017 >                    es[w++] = es[i];
1018 >            if (to == end) break;
1019 >            // In this loop, w is on the first leg, i on the second
1020 >            for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
1021 >                if (isClear(deathRow, i - k))
1022 >                    es[w++] = es[i];
1023 >            if (i >= to) {
1024 >                if (w == capacity) w = 0; // "corner" case
1025 >                break;
1026              }
997            return true;
998        } catch (Throwable ex) {
999            // copy remaining elements
1000            for (; i != end; i = inc(i, capacity), j = inc(j, capacity))
1001                es[j] = es[i];
1002            throw ex;
1003        } finally {
1004            if (end != tail) throw new ConcurrentModificationException();
1005            circularClear(es, tail = j, end);
1006            // checkInvariants();
1027          }
1028 +        if (end != tail) throw new ConcurrentModificationException();
1029 +        circularClear(es, tail = w, end);
1030 +        // checkInvariants();
1031 +        return true;
1032      }
1033  
1034      /**
# Line 1058 | 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 1087 | Line 1114 | public class ArrayDeque<E> extends Abstr
1114      private <T> T[] toArray(Class<T[]> klazz) {
1115          final Object[] es = elements;
1116          final T[] a;
1117 <        final int size = size(), head = this.head, end;
1118 <        final int len = Math.min(size, es.length - head);
1119 <        if ((end = head + size) >= 0) {
1117 >        final int head = this.head, tail = this.tail, end;
1118 >        if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) {
1119 >            // Uses null extension feature of copyOfRange
1120              a = Arrays.copyOfRange(es, head, end, klazz);
1121          } else {
1122              // integer overflow!
1123 <            a = Arrays.copyOfRange(es, 0, size, klazz);
1124 <            System.arraycopy(es, head, a, 0, len);
1123 >            a = Arrays.copyOfRange(es, 0, end - head, klazz);
1124 >            System.arraycopy(es, head, a, 0, es.length - head);
1125          }
1126 <        if (tail < head)
1127 <            System.arraycopy(es, 0, a, len, tail);
1126 >        if (end != tail)
1127 >            System.arraycopy(es, 0, a, es.length - head, tail);
1128          return a;
1129      }
1130  
# Line 1171 | Line 1198 | public class ArrayDeque<E> extends Abstr
1198          }
1199      }
1200  
1201 +    // OPENJDK @java.io.Serial
1202      private static final long serialVersionUID = 2340985798034038923L;
1203  
1204      /**
# Line 1182 | Line 1210 | public class ArrayDeque<E> extends Abstr
1210       * followed by all of its elements (each an object reference) in
1211       * first-to-last order.
1212       */
1213 +    // OPENJDK @java.io.Serial
1214      private void writeObject(java.io.ObjectOutputStream s)
1215              throws java.io.IOException {
1216          s.defaultWriteObject();
# Line 1206 | Line 1235 | public class ArrayDeque<E> extends Abstr
1235       *         could not be found
1236       * @throws java.io.IOException if an I/O error occurs
1237       */
1238 +    // OPENJDK @java.io.Serial
1239      private void readObject(java.io.ObjectInputStream s)
1240              throws java.io.IOException, ClassNotFoundException {
1241          s.defaultReadObject();
1242  
1243          // Read in size and allocate array
1244          int size = s.readInt();
1245 +        jsr166.Platform.checkArray(s, Object[].class, size + 1);
1246          elements = new Object[size + 1];
1247          this.tail = size;
1248  
# Line 1222 | Line 1253 | public class ArrayDeque<E> extends Abstr
1253  
1254      /** debugging */
1255      void checkInvariants() {
1256 +        // Use head and tail fields with empty slot at tail strategy.
1257 +        // head == tail disambiguates to "empty".
1258          try {
1259              int capacity = elements.length;
1260 <            // assert head >= 0 && head < capacity;
1261 <            // assert tail >= 0 && tail < capacity;
1260 >            // assert 0 <= head && head < capacity;
1261 >            // assert 0 <= tail && tail < capacity;
1262              // assert capacity > 0;
1263              // assert size() < capacity;
1264              // assert head == tail || elements[head] != null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines