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.110 by jsr166, Sat Nov 5 17:45:44 2016 UTC vs.
Revision 1.125 by jsr166, Sun Dec 4 23:42:56 2016 UTC

# 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 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];
210 >        this(c.size());
211          addAll(c);
212      }
213  
# 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 add(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 diambiguated to "empty", returning 0.
248       */
249      static final int sub(int i, int j, int modulus) {
250          if ((i -= j) < 0) i += modulus;
# 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 >        c.forEach(this::addLast);
329          // checkInvariants();
330          return size() > s;
331      }
# Line 608 | Line 616 | public class ArrayDeque<E> extends Abstr
616          // checkInvariants();
617          final Object[] es = elements;
618          final int capacity = es.length;
619 <        final int h = head;
619 >        final int h, t;
620          // number of elements before to-be-deleted elt
621 <        final int front = sub(i, h, capacity);
622 <        final int back = size() - front - 1; // number of elements after
621 >        final int front = sub(i, h = head, capacity);
622 >        // number of elements after to-be-deleted elt
623 >        final int back = sub(t = tail, i, capacity) - 1;
624          if (front < back) {
625              // move front elements forwards
626              if (h <= i) {
# Line 627 | Line 636 | public class ArrayDeque<E> extends Abstr
636              return false;
637          } else {
638              // move back elements backwards
639 <            tail = dec(tail, capacity);
639 >            tail = dec(t, capacity);
640              if (i <= tail) {
641                  System.arraycopy(es, i + 1, es, i, back);
642              } else { // Wrap around
643 <                int firstLeg = capacity - (i + 1);
635 <                System.arraycopy(es, i + 1, es, i, firstLeg);
643 >                System.arraycopy(es, i + 1, es, i, capacity - (i + 1));
644                  es[capacity - 1] = es[0];
645 <                System.arraycopy(es, 1, es, 0, back - firstLeg - 1);
645 >                System.arraycopy(es, 1, es, 0, t - 1);
646              }
647              es[tail] = null;
648              // checkInvariants();
# Line 702 | Line 710 | public class ArrayDeque<E> extends Abstr
710                  throw new NoSuchElementException();
711              final Object[] es = elements;
712              E e = nonNullElementAt(es, cursor);
713 <            lastRet = cursor;
706 <            cursor = inc(cursor, es.length);
713 >            cursor = inc(lastRet = cursor, es.length);
714              remaining--;
715              return e;
716          }
# Line 751 | Line 758 | public class ArrayDeque<E> extends Abstr
758                  throw new NoSuchElementException();
759              final Object[] es = elements;
760              E e = nonNullElementAt(es, cursor);
761 <            lastRet = cursor;
755 <            cursor = dec(cursor, es.length);
761 >            cursor = dec(lastRet = cursor, es.length);
762              remaining--;
763              return e;
764          }
# Line 779 | Line 785 | public class ArrayDeque<E> extends Abstr
785                  if (to == end) {
786                      if (end != head)
787                          throw new ConcurrentModificationException();
788 <                    lastRet = head;
788 >                    lastRet = end;
789                      break;
790                  }
791              }
# Line 814 | 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 856 | Line 864 | public class ArrayDeque<E> extends Abstr
864          }
865  
866          public boolean tryAdvance(Consumer<? super E> action) {
867 <            if (action == null)
868 <                throw new NullPointerException();
869 <            int t, i;
870 <            if ((t = fence) < 0) t = getFence();
871 <            if (t == (i = cursor))
867 >            Objects.requireNonNull(action);
868 >            final Object[] es = elements;
869 >            if (fence < 0) { fence = tail; cursor = head; } // late-binding
870 >            final int i;
871 >            if ((i = cursor) == fence)
872                  return false;
873 <            final Object[] es;
866 <            action.accept(nonNullElementAt(es = elements, i));
873 >            E e = nonNullElementAt(es, i);
874              cursor = inc(i, es.length);
875 +            action.accept(e);
876              return true;
877          }
878  
# Line 950 | Line 958 | public class ArrayDeque<E> extends Abstr
958               ; i = 0, to = end) {
959              for (; i < to; i++)
960                  if (filter.test(elementAt(es, i)))
961 <                    return bulkRemoveModified(filter, i, to);
961 >                    return bulkRemoveModified(filter, i);
962              if (to == end) {
963                  if (end != tail) throw new ConcurrentModificationException();
964                  break;
# Line 959 | Line 967 | public class ArrayDeque<E> extends Abstr
967          return false;
968      }
969  
970 +    // A tiny bit set implementation
971 +
972 +    private static long[] nBits(int n) {
973 +        return new long[((n - 1) >> 6) + 1];
974 +    }
975 +    private static void setBit(long[] bits, int i) {
976 +        bits[i >> 6] |= 1L << i;
977 +    }
978 +    private static boolean isClear(long[] bits, int i) {
979 +        return (bits[i >> 6] & (1L << i)) == 0;
980 +    }
981 +
982      /**
983       * Helper for bulkRemove, in case of at least one deletion.
984 <     * @param i valid index of first element to be deleted
984 >     * Tolerate predicates that reentrantly access the collection for
985 >     * read (but writers still get CME), so traverse once to find
986 >     * elements to delete, a second pass to physically expunge.
987 >     *
988 >     * @param beg valid index of first element to be deleted
989       */
990      private boolean bulkRemoveModified(
991 <        Predicate<? super E> filter, int i, int to) {
991 >        Predicate<? super E> filter, final int beg) {
992          final Object[] es = elements;
993          final int capacity = es.length;
970        // a two-finger algorithm, with hare i reading, tortoise j writing
971        int j = i++;
994          final int end = tail;
995 <        try {
996 <            for (;; j = 0) {    // j rejoins i on second leg
997 <                E e;
998 <                // In this loop, i and j are on the same leg, with i > j
999 <                for (; i < to; i++)
1000 <                    if (!filter.test(e = elementAt(es, i)))
1001 <                        es[j++] = e;
1002 <                if (to == end) break;
1003 <                // In this loop, j is on the first leg, i on the second
1004 <                for (i = 0, to = end; i < to && j < capacity; i++)
1005 <                    if (!filter.test(e = elementAt(es, i)))
1006 <                        es[j++] = e;
1007 <                if (i >= to) {
1008 <                    if (j == capacity) j = 0; // "corner" case
1009 <                    break;
1010 <                }
995 >        final long[] deathRow = nBits(sub(end, beg, capacity));
996 >        deathRow[0] = 1L;   // set bit 0
997 >        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
998 >             ; i = 0, to = end, k -= capacity) {
999 >            for (; i < to; i++)
1000 >                if (filter.test(elementAt(es, i)))
1001 >                    setBit(deathRow, i - k);
1002 >            if (to == end) break;
1003 >        }
1004 >        // a two-finger traversal, with hare i reading, tortoise w writing
1005 >        int w = beg;
1006 >        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
1007 >             ; w = 0) { // w rejoins i on second leg
1008 >            // In this loop, i and w are on the same leg, with i > w
1009 >            for (; i < to; i++)
1010 >                if (isClear(deathRow, i - k))
1011 >                    es[w++] = es[i];
1012 >            if (to == end) break;
1013 >            // In this loop, w is on the first leg, i on the second
1014 >            for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
1015 >                if (isClear(deathRow, i - k))
1016 >                    es[w++] = es[i];
1017 >            if (i >= to) {
1018 >                if (w == capacity) w = 0; // "corner" case
1019 >                break;
1020              }
990            return true;
991        } catch (Throwable ex) {
992            // copy remaining elements
993            for (; i != end; i = inc(i, capacity), j = inc(j, capacity))
994                es[j] = es[i];
995            throw ex;
996        } finally {
997            if (end != tail) throw new ConcurrentModificationException();
998            circularClear(es, tail = j, end);
999            // checkInvariants();
1021          }
1022 +        if (end != tail) throw new ConcurrentModificationException();
1023 +        circularClear(es, tail = w, end);
1024 +        // checkInvariants();
1025 +        return true;
1026      }
1027  
1028      /**
# Line 1051 | Line 1076 | public class ArrayDeque<E> extends Abstr
1076  
1077      /**
1078       * Nulls out slots starting at array index i, upto index end.
1079 +     * Condition i == end means "empty" - nothing to do.
1080       */
1081      private static void circularClear(Object[] es, int i, int end) {
1082 +        // assert 0 <= i && i < es.length;
1083 +        // assert 0 <= end && end < es.length;
1084          for (int to = (i <= end) ? end : es.length;
1085               ; i = 0, to = end) {
1086 <            Arrays.fill(es, i, to, null);
1086 >            for (; i < to; i++) es[i] = null;
1087              if (to == end) break;
1088          }
1089      }
# Line 1080 | Line 1108 | public class ArrayDeque<E> extends Abstr
1108      private <T> T[] toArray(Class<T[]> klazz) {
1109          final Object[] es = elements;
1110          final T[] a;
1111 <        final int size = size(), head = this.head, end;
1112 <        final int len = Math.min(size, es.length - head);
1113 <        if ((end = head + size) >= 0) {
1111 >        final int head = this.head, tail = this.tail, end;
1112 >        if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) {
1113 >            // Uses null extension feature of copyOfRange
1114              a = Arrays.copyOfRange(es, head, end, klazz);
1115          } else {
1116              // integer overflow!
1117 <            a = Arrays.copyOfRange(es, 0, size, klazz);
1118 <            System.arraycopy(es, head, a, 0, len);
1117 >            a = Arrays.copyOfRange(es, 0, end - head, klazz);
1118 >            System.arraycopy(es, head, a, 0, es.length - head);
1119          }
1120 <        if (tail < head)
1121 <            System.arraycopy(es, 0, a, len, tail);
1120 >        if (end != tail)
1121 >            System.arraycopy(es, 0, a, es.length - head, tail);
1122          return a;
1123      }
1124  
# Line 1215 | Line 1243 | public class ArrayDeque<E> extends Abstr
1243  
1244      /** debugging */
1245      void checkInvariants() {
1246 +        // Use head and tail fields with empty slot at tail strategy.
1247 +        // head == tail disambiguates to "empty".
1248          try {
1249              int capacity = elements.length;
1250 <            // assert head >= 0 && head < capacity;
1251 <            // assert tail >= 0 && tail < capacity;
1250 >            // assert 0 <= head && head < capacity;
1251 >            // assert 0 <= tail && tail < capacity;
1252              // assert capacity > 0;
1253              // assert size() < capacity;
1254              // assert head == tail || elements[head] != null;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines