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.109 by jsr166, Sat Nov 5 16:21:06 2016 UTC vs.
Revision 1.135 by jsr166, Mon Oct 1 00:10:52 2018 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.base/java/util/package-summary.html#CollectionsFramework">
55   * Java Collections Framework</a>.
56   *
57   * @author  Josh Bloch and Doug Lea
# Line 68 | Line 69 | public class ArrayDeque<E> extends Abstr
69       *
70       * Because in a circular array, elements are in general stored in
71       * two disjoint such slices, we help the VM by writing unusual
72 <     * nested loops for all traversals over the elements.
72 >     * nested loops for all traversals over the elements.  Having only
73 >     * one hot inner loop body instead of two or three eases human
74 >     * maintenance and encourages VM loop inlining into the caller.
75       */
76  
77      /**
78       * The array in which the elements of the deque are stored.
79 <     * We guarantee that all array cells not holding deque elements
80 <     * are always null.
79 >     * All array cells not holding deque elements are always null.
80 >     * The array always has at least one null slot (at tail).
81       */
82      transient Object[] elements;
83  
# Line 88 | Line 91 | public class ArrayDeque<E> extends Abstr
91  
92      /**
93       * The index at which the next element would be added to the tail
94 <     * of the deque (via addLast(E), add(E), or push(E)).
94 >     * of the deque (via addLast(E), add(E), or push(E));
95 >     * elements[tail] is always null.
96       */
97      transient int tail;
98  
# Line 114 | 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 177 | Line 181 | public class ArrayDeque<E> extends Abstr
181       * sufficient to hold 16 elements.
182       */
183      public ArrayDeque() {
184 <        elements = new Object[16];
184 >        elements = new Object[16 + 1];
185      }
186  
187      /**
# Line 187 | Line 191 | public class ArrayDeque<E> extends Abstr
191       * @param numElements lower bound on initial capacity of the deque
192       */
193      public ArrayDeque(int numElements) {
194 <        elements = new Object[Math.max(1, numElements + 1)];
194 >        elements =
195 >            new Object[(numElements < 1) ? 1 :
196 >                       (numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE :
197 >                       numElements + 1];
198      }
199  
200      /**
# Line 201 | Line 208 | public class ArrayDeque<E> extends Abstr
208       * @throws NullPointerException if the specified collection is null
209       */
210      public ArrayDeque(Collection<? extends E> c) {
211 <        elements = new Object[c.size() + 1];
212 <        addAll(c);
211 >        this(c.size());
212 >        copyElements(c);
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 215 | 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 224 | Line 231 | public class ArrayDeque<E> extends Abstr
231      }
232  
233      /**
234 <     * Adds i and j, mod modulus.
235 <     * Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus.
234 >     * Circularly adds the given distance to index i, mod modulus.
235 >     * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
236 >     * @return index 0 <= i < modulus
237       */
238 <    static final int add(int i, int j, int modulus) {
239 <        if ((i += j) - modulus >= 0) i -= 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  
243      /**
244       * Subtracts j from i, mod modulus.
245 <     * Index i must be logically ahead of j.
246 <     * Returns the "circular distance" from j to i.
247 <     * Precondition and postcondition: 0 <= i < modulus, 0 <= j < modulus.
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 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 305 | 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
309 <     * 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 314 | Line 322 | public class ArrayDeque<E> extends Abstr
322       *         of its elements are null
323       */
324      public boolean addAll(Collection<? extends E> c) {
325 <        final int s = size(), needed;
326 <        if ((needed = s + c.size() - elements.length + 1) > 0)
325 >        final int s, needed;
326 >        if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
327              grow(needed);
328 <        c.forEach((e) -> addLast(e));
328 >        copyElements(c);
329          // checkInvariants();
330          return size() > s;
331      }
332  
333 +    private void copyElements(Collection<? extends E> c) {
334 +        c.forEach(this::addLast);
335 +    }
336 +
337      /**
338       * Inserts the specified element at the front of this deque.
339       *
# Line 469 | Line 481 | public class ArrayDeque<E> extends Abstr
481              final Object[] es = elements;
482              for (int i = tail, end = head, to = (i >= end) ? end : 0;
483                   ; i = es.length, to = end) {
484 <                while (--i >= to)
484 >                for (i--; i > to - 1; i--)
485                      if (o.equals(es[i])) {
486                          delete(i);
487                          return true;
# Line 512 | Line 524 | public class ArrayDeque<E> extends Abstr
524      /**
525       * Retrieves and removes the head of the queue represented by this deque.
526       *
527 <     * This method differs from {@link #poll poll} only in that it throws an
528 <     * exception if this deque is empty.
527 >     * This method differs from {@link #poll() poll()} only in that it
528 >     * throws an exception if this deque is empty.
529       *
530       * <p>This method is equivalent to {@link #removeFirst}.
531       *
# Line 608 | Line 620 | public class ArrayDeque<E> extends Abstr
620          // checkInvariants();
621          final Object[] es = elements;
622          final int capacity = es.length;
623 <        final int h = head;
623 >        final int h, t;
624          // number of elements before to-be-deleted elt
625 <        final int front = sub(i, h, capacity);
626 <        final int back = size() - front - 1; // number of elements after
625 >        final int front = sub(i, h = head, capacity);
626 >        // number of elements after to-be-deleted elt
627 >        final int back = sub(t = tail, i, capacity) - 1;
628          if (front < back) {
629              // move front elements forwards
630              if (h <= i) {
# Line 627 | Line 640 | public class ArrayDeque<E> extends Abstr
640              return false;
641          } else {
642              // move back elements backwards
643 <            tail = dec(tail, capacity);
643 >            tail = dec(t, capacity);
644              if (i <= tail) {
645                  System.arraycopy(es, i + 1, es, i, back);
646              } else { // Wrap around
647 <                int firstLeg = capacity - (i + 1);
635 <                System.arraycopy(es, i + 1, es, i, firstLeg);
647 >                System.arraycopy(es, i + 1, es, i, capacity - (i + 1));
648                  es[capacity - 1] = es[0];
649 <                System.arraycopy(es, 1, es, 0, back - firstLeg - 1);
649 >                System.arraycopy(es, 1, es, 0, t - 1);
650              }
651              es[tail] = null;
652              // checkInvariants();
# Line 702 | Line 714 | public class ArrayDeque<E> extends Abstr
714                  throw new NoSuchElementException();
715              final Object[] es = elements;
716              E e = nonNullElementAt(es, cursor);
717 <            lastRet = cursor;
706 <            cursor = inc(cursor, es.length);
717 >            cursor = inc(lastRet = cursor, es.length);
718              remaining--;
719              return e;
720          }
# Line 751 | Line 762 | public class ArrayDeque<E> extends Abstr
762                  throw new NoSuchElementException();
763              final Object[] es = elements;
764              E e = nonNullElementAt(es, cursor);
765 <            lastRet = cursor;
755 <            cursor = dec(cursor, es.length);
765 >            cursor = dec(lastRet = cursor, es.length);
766              remaining--;
767              return e;
768          }
# Line 773 | Line 783 | public class ArrayDeque<E> extends Abstr
783                  throw new ConcurrentModificationException();
784              for (int i = cursor, end = head, to = (i >= end) ? end : 0;
785                   ; i = es.length - 1, to = end) {
786 <                for (; i >= to; i--)
786 >                // hotspot generates faster code than for: i >= to !
787 >                for (; i > to - 1; i--)
788                      action.accept(elementAt(es, i));
789                  if (to == end) {
790                      if (end != head)
791                          throw new ConcurrentModificationException();
792 <                    lastRet = head;
792 >                    lastRet = end;
793                      break;
794                  }
795              }
# Line 813 | Line 824 | public class ArrayDeque<E> extends Abstr
824  
825          /** Constructs spliterator over the given range. */
826          DeqSpliterator(int origin, int fence) {
827 +            // assert 0 <= origin && origin < elements.length;
828 +            // assert 0 <= fence && fence < elements.length;
829              this.cursor = origin;
830              this.fence = fence;
831          }
# Line 832 | Line 845 | public class ArrayDeque<E> extends Abstr
845              final int i, n;
846              return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
847                  ? null
848 <                : new DeqSpliterator(i, cursor = add(i, n, es.length));
848 >                : new DeqSpliterator(i, cursor = inc(i, n, es.length));
849          }
850  
851          public void forEachRemaining(Consumer<? super E> action) {
# Line 855 | Line 868 | public class ArrayDeque<E> extends Abstr
868          }
869  
870          public boolean tryAdvance(Consumer<? super E> action) {
871 <            if (action == null)
872 <                throw new NullPointerException();
873 <            int t, i;
874 <            if ((t = fence) < 0) t = getFence();
875 <            if (t == (i = cursor))
871 >            Objects.requireNonNull(action);
872 >            final Object[] es = elements;
873 >            if (fence < 0) { fence = tail; cursor = head; } // late-binding
874 >            final int i;
875 >            if ((i = cursor) == fence)
876                  return false;
877 <            final Object[] es;
865 <            action.accept(nonNullElementAt(es = elements, i));
877 >            E e = nonNullElementAt(es, i);
878              cursor = inc(i, es.length);
879 +            action.accept(e);
880              return true;
881          }
882  
# Line 879 | Line 892 | public class ArrayDeque<E> extends Abstr
892          }
893      }
894  
895 +    /**
896 +     * @throws NullPointerException {@inheritDoc}
897 +     */
898      public void forEach(Consumer<? super E> action) {
899          Objects.requireNonNull(action);
900          final Object[] es = elements;
# Line 949 | Line 965 | public class ArrayDeque<E> extends Abstr
965               ; i = 0, to = end) {
966              for (; i < to; i++)
967                  if (filter.test(elementAt(es, i)))
968 <                    return bulkRemoveModified(filter, i, to);
968 >                    return bulkRemoveModified(filter, i);
969              if (to == end) {
970                  if (end != tail) throw new ConcurrentModificationException();
971                  break;
# Line 958 | Line 974 | public class ArrayDeque<E> extends Abstr
974          return false;
975      }
976  
977 +    // A tiny bit set implementation
978 +
979 +    private static long[] nBits(int n) {
980 +        return new long[((n - 1) >> 6) + 1];
981 +    }
982 +    private static void setBit(long[] bits, int i) {
983 +        bits[i >> 6] |= 1L << i;
984 +    }
985 +    private static boolean isClear(long[] bits, int i) {
986 +        return (bits[i >> 6] & (1L << i)) == 0;
987 +    }
988 +
989      /**
990       * Helper for bulkRemove, in case of at least one deletion.
991 <     * @param i valid index of first element to be deleted
991 >     * Tolerate predicates that reentrantly access the collection for
992 >     * read (but writers still get CME), so traverse once to find
993 >     * elements to delete, a second pass to physically expunge.
994 >     *
995 >     * @param beg valid index of first element to be deleted
996       */
997      private boolean bulkRemoveModified(
998 <        Predicate<? super E> filter, int i, int to) {
998 >        Predicate<? super E> filter, final int beg) {
999          final Object[] es = elements;
1000          final int capacity = es.length;
969        // a two-finger algorithm, with hare i reading, tortoise j writing
970        int j = i++;
1001          final int end = tail;
1002 <        try {
1003 <            for (;; j = 0) {    // j rejoins i on second leg
1004 <                E e;
1005 <                // In this loop, i and j are on the same leg, with i > j
1006 <                for (; i < to; i++)
1007 <                    if (!filter.test(e = elementAt(es, i)))
1008 <                        es[j++] = e;
1009 <                if (to == end) break;
1010 <                // In this loop, j is on the first leg, i on the second
1011 <                for (i = 0, to = end; i < to && j < capacity; i++)
1012 <                    if (!filter.test(e = elementAt(es, i)))
1013 <                        es[j++] = e;
1014 <                if (i >= to) {
1015 <                    if (j == capacity) j = 0; // "corner" case
1016 <                    break;
1017 <                }
1002 >        final long[] deathRow = nBits(sub(end, beg, capacity));
1003 >        deathRow[0] = 1L;   // set bit 0
1004 >        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
1005 >             ; i = 0, to = end, k -= capacity) {
1006 >            for (; i < to; i++)
1007 >                if (filter.test(elementAt(es, i)))
1008 >                    setBit(deathRow, i - k);
1009 >            if (to == end) break;
1010 >        }
1011 >        // a two-finger traversal, with hare i reading, tortoise w writing
1012 >        int w = beg;
1013 >        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
1014 >             ; w = 0) { // w rejoins i on second leg
1015 >            // In this loop, i and w are on the same leg, with i > w
1016 >            for (; i < to; i++)
1017 >                if (isClear(deathRow, i - k))
1018 >                    es[w++] = es[i];
1019 >            if (to == end) break;
1020 >            // In this loop, w is on the first leg, i on the second
1021 >            for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
1022 >                if (isClear(deathRow, i - k))
1023 >                    es[w++] = es[i];
1024 >            if (i >= to) {
1025 >                if (w == capacity) w = 0; // "corner" case
1026 >                break;
1027              }
989            return true;
990        } catch (Throwable ex) {
991            // copy remaining elements
992            for (; i != end; i = inc(i, capacity), j = inc(j, capacity))
993                es[j] = es[i];
994            throw ex;
995        } finally {
996            if (end != tail) throw new ConcurrentModificationException();
997            circularClear(es, tail = j, end);
998            // checkInvariants();
1028          }
1029 +        if (end != tail) throw new ConcurrentModificationException();
1030 +        circularClear(es, tail = w, end);
1031 +        // checkInvariants();
1032 +        return true;
1033      }
1034  
1035      /**
# Line 1050 | Line 1083 | public class ArrayDeque<E> extends Abstr
1083  
1084      /**
1085       * Nulls out slots starting at array index i, upto index end.
1086 +     * Condition i == end means "empty" - nothing to do.
1087       */
1088      private static void circularClear(Object[] es, int i, int end) {
1089 +        // assert 0 <= i && i < es.length;
1090 +        // assert 0 <= end && end < es.length;
1091          for (int to = (i <= end) ? end : es.length;
1092               ; i = 0, to = end) {
1093 <            Arrays.fill(es, i, to, null);
1093 >            for (; i < to; i++) es[i] = null;
1094              if (to == end) break;
1095          }
1096      }
# Line 1079 | Line 1115 | public class ArrayDeque<E> extends Abstr
1115      private <T> T[] toArray(Class<T[]> klazz) {
1116          final Object[] es = elements;
1117          final T[] a;
1118 <        final int size = size(), head = this.head, end;
1119 <        final int len = Math.min(size, es.length - head);
1120 <        if ((end = head + size) >= 0) {
1118 >        final int head = this.head, tail = this.tail, end;
1119 >        if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) {
1120 >            // Uses null extension feature of copyOfRange
1121              a = Arrays.copyOfRange(es, head, end, klazz);
1122          } else {
1123              // integer overflow!
1124 <            a = Arrays.copyOfRange(es, 0, size, klazz);
1125 <            System.arraycopy(es, head, a, 0, len);
1124 >            a = Arrays.copyOfRange(es, 0, end - head, klazz);
1125 >            System.arraycopy(es, head, a, 0, es.length - head);
1126          }
1127 <        if (tail < head)
1128 <            System.arraycopy(es, 0, a, len, tail);
1127 >        if (end != tail)
1128 >            System.arraycopy(es, 0, a, es.length - head, tail);
1129          return a;
1130      }
1131  
# Line 1204 | Line 1240 | public class ArrayDeque<E> extends Abstr
1240  
1241          // Read in size and allocate array
1242          int size = s.readInt();
1243 +        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1);
1244          elements = new Object[size + 1];
1245          this.tail = size;
1246  
# Line 1214 | Line 1251 | public class ArrayDeque<E> extends Abstr
1251  
1252      /** debugging */
1253      void checkInvariants() {
1254 +        // Use head and tail fields with empty slot at tail strategy.
1255 +        // head == tail disambiguates to "empty".
1256          try {
1257              int capacity = elements.length;
1258 <            // assert head >= 0 && head < capacity;
1259 <            // assert tail >= 0 && tail < capacity;
1258 >            // assert 0 <= head && head < capacity;
1259 >            // assert 0 <= tail && tail < capacity;
1260              // assert capacity > 0;
1261              // assert size() < capacity;
1262              // assert head == tail || elements[head] != null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines