ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayList.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayList.java (file contents):
Revision 1.34 by jsr166, Tue Oct 18 17:31:18 2016 UTC vs.
Revision 1.43 by jsr166, Mon Nov 14 22:46:22 2016 UTC

# Line 104 | Line 104 | import java.util.function.UnaryOperator;
104   * @see     Vector
105   * @since   1.2
106   */
107
107   public class ArrayList<E> extends AbstractList<E>
108          implements List<E>, RandomAccess, Cloneable, java.io.Serializable
109   {
# Line 424 | Line 423 | public class ArrayList<E> extends Abstra
423          return (E) elementData[index];
424      }
425  
426 +    @SuppressWarnings("unchecked")
427 +    static <E> E elementAt(Object[] es, int index) {
428 +        return (E) es[index];
429 +    }
430 +
431      /**
432       * Returns the element at the specified position in this list.
433       *
# Line 497 | Line 501 | public class ArrayList<E> extends Abstra
501                           s - index);
502          elementData[index] = element;
503          size = s + 1;
504 +        // checkInvariants();
505      }
506  
507      /**
# Line 520 | Line 525 | public class ArrayList<E> extends Abstra
525                               numMoved);
526          elementData[--size] = null; // clear to let GC do its work
527  
528 +        // checkInvariants();
529          return oldValue;
530      }
531  
# Line 553 | Line 559 | public class ArrayList<E> extends Abstra
559          return false;
560      }
561  
562 <    /*
562 >    /**
563       * Private remove method that skips bounds checking and does not
564       * return the value removed.
565       */
# Line 572 | Line 578 | public class ArrayList<E> extends Abstra
578       */
579      public void clear() {
580          modCount++;
581 <
576 <        // clear to let GC do its work
577 <        for (int i = 0; i < size; i++)
578 <            elementData[i] = null;
579 <
581 >        Arrays.fill(elementData, 0, size, null);
582          size = 0;
583      }
584  
# Line 605 | Line 607 | public class ArrayList<E> extends Abstra
607              elementData = grow(s + numNew);
608          System.arraycopy(a, 0, elementData, s, numNew);
609          size = s + numNew;
610 +        // checkInvariants();
611          return true;
612      }
613  
# Line 643 | Line 646 | public class ArrayList<E> extends Abstra
646                               numMoved);
647          System.arraycopy(a, 0, elementData, index, numNew);
648          size = s + numNew;
649 +        // checkInvariants();
650          return true;
651      }
652  
# Line 665 | Line 669 | public class ArrayList<E> extends Abstra
669                      outOfBoundsMsg(fromIndex, toIndex));
670          }
671          modCount++;
672 <        int numMoved = size - toIndex;
673 <        System.arraycopy(elementData, toIndex, elementData, fromIndex,
674 <                         numMoved);
675 <
676 <        // clear to let GC do its work
673 <        int newSize = size - (toIndex-fromIndex);
674 <        for (int i = newSize; i < size; i++) {
675 <            elementData[i] = null;
676 <        }
677 <        size = newSize;
672 >        final Object[] es = elementData;
673 >        final int oldSize = size;
674 >        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
675 >        Arrays.fill(es, size -= (toIndex - fromIndex), oldSize, null);
676 >        // checkInvariants();
677      }
678  
679      /**
# Line 717 | Line 716 | public class ArrayList<E> extends Abstra
716       * @see Collection#contains(Object)
717       */
718      public boolean removeAll(Collection<?> c) {
719 <        Objects.requireNonNull(c);
721 <        return batchRemove(c, false);
719 >        return batchRemove(c, false, 0, size);
720      }
721  
722      /**
# Line 738 | Line 736 | public class ArrayList<E> extends Abstra
736       * @see Collection#contains(Object)
737       */
738      public boolean retainAll(Collection<?> c) {
739 <        Objects.requireNonNull(c);
742 <        return batchRemove(c, true);
739 >        return batchRemove(c, true, 0, size);
740      }
741  
742 <    private boolean batchRemove(Collection<?> c, boolean complement) {
743 <        final Object[] elementData = this.elementData;
744 <        int r = 0, w = 0;
745 <        boolean modified = false;
746 <        try {
747 <            for (; r < size; r++)
748 <                if (c.contains(elementData[r]) == complement)
749 <                    elementData[w++] = elementData[r];
750 <        } finally {
751 <            // Preserve behavioral compatibility with AbstractCollection,
752 <            // even if c.contains() throws.
753 <            if (r != size) {
754 <                System.arraycopy(elementData, r,
755 <                                 elementData, w,
756 <                                 size - r);
757 <                w += size - r;
758 <            }
759 <            if (w != size) {
760 <                // clear to let GC do its work
761 <                for (int i = w; i < size; i++)
762 <                    elementData[i] = null;
763 <                modCount += size - w;
764 <                size = w;
765 <                modified = true;
742 >    boolean batchRemove(Collection<?> c, boolean complement,
743 >                        final int from, final int end) {
744 >        Objects.requireNonNull(c);
745 >        final Object[] es = elementData;
746 >        final boolean modified;
747 >        int r;
748 >        // Optimize for initial run of survivors
749 >        for (r = from; r < end && c.contains(es[r]) == complement; r++)
750 >            ;
751 >        if (modified = (r < end)) {
752 >            int w = r++;
753 >            try {
754 >                for (Object e; r < end; r++)
755 >                    if (c.contains(e = es[r]) == complement)
756 >                        es[w++] = e;
757 >            } catch (Throwable ex) {
758 >                // Preserve behavioral compatibility with AbstractCollection,
759 >                // even if c.contains() throws.
760 >                System.arraycopy(es, r, es, w, end - r);
761 >                w += end - r;
762 >                throw ex;
763 >            } finally {
764 >                final int oldSize = size, deleted = end - w;
765 >                modCount += deleted;
766 >                System.arraycopy(es, end, es, w, oldSize - end);
767 >                Arrays.fill(es, size -= deleted, oldSize, null);
768              }
769          }
770 +        // checkInvariants();
771          return modified;
772      }
773  
# Line 1117 | Line 1117 | public class ArrayList<E> extends Abstra
1117              return true;
1118          }
1119  
1120 +        public boolean removeAll(Collection<?> c) {
1121 +            return batchRemove(c, false);
1122 +        }
1123 +
1124 +        public boolean retainAll(Collection<?> c) {
1125 +            return batchRemove(c, true);
1126 +        }
1127 +
1128 +        private boolean batchRemove(Collection<?> c, boolean complement) {
1129 +            checkForComodification();
1130 +            int oldSize = root.size;
1131 +            boolean modified =
1132 +                root.batchRemove(c, complement, offset, offset + size);
1133 +            if (modified)
1134 +                updateSizeAndModCount(root.size - oldSize);
1135 +            return modified;
1136 +        }
1137 +
1138 +        public boolean removeIf(Predicate<? super E> filter) {
1139 +            checkForComodification();
1140 +            int oldSize = root.size;
1141 +            boolean modified = root.removeIf(filter, offset, offset + size);
1142 +            if (modified)
1143 +                updateSizeAndModCount(root.size - oldSize);
1144 +            return modified;
1145 +        }
1146 +
1147          public Iterator<E> iterator() {
1148              return listIterator();
1149          }
# Line 1180 | Line 1207 | public class ArrayList<E> extends Abstra
1207                          consumer.accept((E) elementData[offset + (i++)]);
1208                      }
1209                      // update once at end of iteration to reduce heap write traffic
1210 <                    lastRet = cursor = i;
1210 >                    cursor = i;
1211 >                    lastRet = i - 1;
1212                      checkForComodification();
1213                  }
1214  
# Line 1348 | Line 1376 | public class ArrayList<E> extends Abstra
1376      public void forEach(Consumer<? super E> action) {
1377          Objects.requireNonNull(action);
1378          final int expectedModCount = modCount;
1379 <        @SuppressWarnings("unchecked")
1352 <        final E[] elementData = (E[]) this.elementData;
1379 >        final Object[] es = elementData;
1380          final int size = this.size;
1381 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1382 <            action.accept(elementData[i]);
1383 <        }
1357 <        if (modCount != expectedModCount) {
1381 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1382 >            action.accept(elementAt(es, i));
1383 >        if (modCount != expectedModCount)
1384              throw new ConcurrentModificationException();
1359        }
1385      }
1386  
1387      /**
# Line 1417 | Line 1442 | public class ArrayList<E> extends Abstra
1442          private int fence; // -1 until used; then one past last index
1443          private int expectedModCount; // initialized when fence set
1444  
1445 <        /** Create new spliterator covering the given  range */
1445 >        /** Create new spliterator covering the given range */
1446          ArrayListSpliterator(ArrayList<E> list, int origin, int fence,
1447                               int expectedModCount) {
1448              this.list = list; // OK if null unless traversed
# Line 1495 | Line 1520 | public class ArrayList<E> extends Abstra
1520          }
1521      }
1522  
1523 +    // A tiny bit set implementation
1524 +
1525 +    private static long[] nBits(int n) {
1526 +        return new long[((n - 1) >> 6) + 1];
1527 +    }
1528 +    private static void setBit(long[] bits, int i) {
1529 +        bits[i >> 6] |= 1L << i;
1530 +    }
1531 +    private static boolean isClear(long[] bits, int i) {
1532 +        return (bits[i >> 6] & (1L << i)) == 0;
1533 +    }
1534 +
1535      @Override
1536      public boolean removeIf(Predicate<? super E> filter) {
1537 +        return removeIf(filter, 0, size);
1538 +    }
1539 +
1540 +    /**
1541 +     * Removes all elements satisfying the given predicate, from index
1542 +     * i (inclusive) to index end (exclusive).
1543 +     */
1544 +    boolean removeIf(Predicate<? super E> filter, int i, final int end) {
1545          Objects.requireNonNull(filter);
1546 <        final int expectedModCount = modCount;
1547 <        final Object[] elementData = this.elementData;
1548 <        int r = 0, w = 0, remaining = size, deleted = 0;
1549 <        try {
1550 <            for (; remaining > 0; remaining--, r++) {
1551 <                @SuppressWarnings("unchecked") E e = (E) elementData[r];
1552 <                if (filter.test(e))
1553 <                    deleted++;
1554 <                else {
1555 <                    if (r != w)
1556 <                        elementData[w] = e;
1557 <                    w++;
1558 <                }
1559 <            }
1546 >        int expectedModCount = modCount;
1547 >        final Object[] es = elementData;
1548 >        // Optimize for initial run of survivors
1549 >        for (; i < end && !filter.test(elementAt(es, i)); i++)
1550 >            ;
1551 >        // Tolerate predicates that reentrantly access the collection for
1552 >        // read (but writers still get CME), so traverse once to find
1553 >        // elements to delete, a second pass to physically expunge.
1554 >        if (i < end) {
1555 >            final int beg = i;
1556 >            final long[] deathRow = nBits(end - beg);
1557 >            deathRow[0] = 1L;   // set bit 0
1558 >            for (i = beg + 1; i < end; i++)
1559 >                if (filter.test(elementAt(es, i)))
1560 >                    setBit(deathRow, i - beg);
1561              if (modCount != expectedModCount)
1562                  throw new ConcurrentModificationException();
1563 <            return deleted > 0;
1564 <        } catch (Throwable ex) {
1565 <            if (deleted > 0)
1566 <                for (; remaining > 0; remaining--, r++, w++)
1567 <                    elementData[w] = elementData[r];
1568 <            throw ex;
1569 <        } finally {
1570 <            if (deleted > 0) {
1571 <                modCount++;
1572 <                size -= deleted;
1573 <                while (--deleted >= 0)
1574 <                    elementData[w++] = null;
1575 <            }
1563 >            expectedModCount++;
1564 >            modCount++;
1565 >            int w = beg;
1566 >            for (i = beg; i < end; i++)
1567 >                if (isClear(deathRow, i - beg))
1568 >                    es[w++] = es[i];
1569 >            final int oldSize = size;
1570 >            System.arraycopy(es, end, es, w, oldSize - end);
1571 >            Arrays.fill(es, size -= (end - w), oldSize, null);
1572 >            // checkInvariants();
1573 >            return true;
1574 >        } else {
1575 >            if (modCount != expectedModCount)
1576 >                throw new ConcurrentModificationException();
1577 >            // checkInvariants();
1578 >            return false;
1579          }
1580      }
1581  
1582      @Override
1534    @SuppressWarnings("unchecked")
1583      public void replaceAll(UnaryOperator<E> operator) {
1584          Objects.requireNonNull(operator);
1585          final int expectedModCount = modCount;
1586 +        final Object[] es = elementData;
1587          final int size = this.size;
1588 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1589 <            elementData[i] = operator.apply((E) elementData[i]);
1590 <        }
1542 <        if (modCount != expectedModCount) {
1588 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1589 >            es[i] = operator.apply(elementAt(es, i));
1590 >        if (modCount != expectedModCount)
1591              throw new ConcurrentModificationException();
1544        }
1592          modCount++;
1593 +        // checkInvariants();
1594      }
1595  
1596      @Override
# Line 1550 | Line 1598 | public class ArrayList<E> extends Abstra
1598      public void sort(Comparator<? super E> c) {
1599          final int expectedModCount = modCount;
1600          Arrays.sort((E[]) elementData, 0, size, c);
1601 <        if (modCount != expectedModCount) {
1601 >        if (modCount != expectedModCount)
1602              throw new ConcurrentModificationException();
1555        }
1603          modCount++;
1604 +        // checkInvariants();
1605 +    }
1606 +
1607 +    void checkInvariants() {
1608 +        // assert size >= 0;
1609 +        // assert size == elementData.length || elementData[size] == null;
1610      }
1611   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines