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.37 by jsr166, Fri Nov 4 03:09:27 2016 UTC vs.
Revision 1.40 by jsr166, Sun Nov 13 20:03:11 2016 UTC

# Line 423 | 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 716 | Line 721 | public class ArrayList<E> extends Abstra
721       * @see Collection#contains(Object)
722       */
723      public boolean removeAll(Collection<?> c) {
724 <        return batchRemove(c, false);
724 >        return batchRemove(c, false, 0, size);
725      }
726  
727      /**
# Line 736 | Line 741 | public class ArrayList<E> extends Abstra
741       * @see Collection#contains(Object)
742       */
743      public boolean retainAll(Collection<?> c) {
744 <        return batchRemove(c, true);
744 >        return batchRemove(c, true, 0, size);
745      }
746  
747 <    private boolean batchRemove(Collection<?> c, boolean complement) {
747 >    boolean batchRemove(Collection<?> c, boolean complement,
748 >                        final int from, final int end) {
749          Objects.requireNonNull(c);
750          final Object[] es = elementData;
745        final int size = this.size;
751          final boolean modified;
752          int r;
753          // Optimize for initial run of survivors
754 <        for (r = 0; r < size; r++)
755 <            if (c.contains(es[r]) != complement)
756 <                break;
752 <        if (modified = (r < size)) {
754 >        for (r = from; r < end && c.contains(es[r]) == complement; r++)
755 >            ;
756 >        if (modified = (r < end)) {
757              int w = r++;
758              try {
759 <                for (Object e; r < size; r++)
759 >                for (Object e; r < end; r++)
760                      if (c.contains(e = es[r]) == complement)
761                          es[w++] = e;
762              } catch (Throwable ex) {
763                  // Preserve behavioral compatibility with AbstractCollection,
764                  // even if c.contains() throws.
765 <                System.arraycopy(es, r, es, w, size - r);
766 <                w += size - r;
765 >                System.arraycopy(es, r, es, w, end - r);
766 >                w += end - r;
767                  throw ex;
768              } finally {
769 <                modCount += size - w;
770 <                Arrays.fill(es, (this.size = w), size, null);
769 >                final int oldSize = size, deleted = end - w;
770 >                modCount += deleted;
771 >                System.arraycopy(es, end, es, w, oldSize - end);
772 >                Arrays.fill(es, size -= deleted, oldSize, null);
773              }
774          }
775          return modified;
# Line 1115 | Line 1121 | public class ArrayList<E> extends Abstra
1121              return true;
1122          }
1123  
1124 +        public boolean removeAll(Collection<?> c) {
1125 +            return batchRemove(c, false);
1126 +        }
1127 +        public boolean retainAll(Collection<?> c) {
1128 +            return batchRemove(c, true);
1129 +        }
1130 +
1131 +        private boolean batchRemove(Collection<?> c, boolean complement) {
1132 +            checkForComodification();
1133 +            int oldSize = root.size;
1134 +            boolean modified =
1135 +                root.batchRemove(c, complement, offset, offset + size);
1136 +            if (modified)
1137 +                updateSizeAndModCount(root.size - oldSize);
1138 +            return modified;
1139 +        }
1140 +
1141 +        public boolean removeIf(Predicate<? super E> filter) {
1142 +            checkForComodification();
1143 +            int oldSize = root.size;
1144 +            boolean modified = root.removeIf(filter, offset, offset + size);
1145 +            if (modified)
1146 +                updateSizeAndModCount(root.size - oldSize);
1147 +            return modified;
1148 +        }
1149 +
1150          public Iterator<E> iterator() {
1151              return listIterator();
1152          }
# Line 1346 | Line 1378 | public class ArrayList<E> extends Abstra
1378      public void forEach(Consumer<? super E> action) {
1379          Objects.requireNonNull(action);
1380          final int expectedModCount = modCount;
1381 <        @SuppressWarnings("unchecked")
1350 <        final E[] elementData = (E[]) this.elementData;
1381 >        final Object[] es = elementData;
1382          final int size = this.size;
1383 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1384 <            action.accept(elementData[i]);
1383 >        for (int i = 0; modCount == expectedModCount && i < size; i++) {
1384 >            action.accept(elementAt(es, i));
1385          }
1386          if (modCount != expectedModCount) {
1387              throw new ConcurrentModificationException();
# Line 1415 | Line 1446 | public class ArrayList<E> extends Abstra
1446          private int fence; // -1 until used; then one past last index
1447          private int expectedModCount; // initialized when fence set
1448  
1449 <        /** Create new spliterator covering the given  range */
1449 >        /** Create new spliterator covering the given range */
1450          ArrayListSpliterator(ArrayList<E> list, int origin, int fence,
1451                               int expectedModCount) {
1452              this.list = list; // OK if null unless traversed
# Line 1493 | Line 1524 | public class ArrayList<E> extends Abstra
1524          }
1525      }
1526  
1527 <    @SuppressWarnings("unchecked")
1527 >    // A tiny bit set implementation
1528 >
1529 >    private static long[] nBits(int n) {
1530 >        return new long[((n - 1) >> 6) + 1];
1531 >    }
1532 >    private static void setBit(long[] bits, int i) {
1533 >        bits[i >> 6] |= 1L << i;
1534 >    }
1535 >    private static boolean isClear(long[] bits, int i) {
1536 >        return (bits[i >> 6] & (1L << i)) == 0;
1537 >    }
1538 >
1539      @Override
1540      public boolean removeIf(Predicate<? super E> filter) {
1541 +        return removeIf(filter, 0, size);
1542 +    }
1543 +
1544 +    boolean removeIf(Predicate<? super E> filter,
1545 +                     final int from, final int end) {
1546          Objects.requireNonNull(filter);
1547          int expectedModCount = modCount;
1548          final Object[] es = elementData;
1502        final int size = this.size;
1549          final boolean modified;
1550 <        int r;
1550 >        int i;
1551          // Optimize for initial run of survivors
1552 <        for (r = 0; r < size; r++)
1553 <            if (filter.test((E) es[r]))
1554 <                break;
1555 <        if (modified = (r < size)) {
1552 >        for (i = from; i < end && !filter.test(elementAt(es, i)); i++)
1553 >            ;
1554 >        // Tolerate predicates that reentrantly access the collection for
1555 >        // read (but writers still get CME), so traverse once to find
1556 >        // elements to delete, a second pass to physically expunge.
1557 >        if (modified = (i < end)) {
1558              expectedModCount++;
1559              modCount++;
1560 <            int w = r++;
1561 <            try {
1562 <                for (E e; r < size; r++)
1563 <                    if (!filter.test(e = (E) es[r]))
1564 <                        es[w++] = e;
1565 <            } catch (Throwable ex) {
1566 <                // copy remaining elements
1567 <                System.arraycopy(es, r, es, w, size - r);
1568 <                w += size - r;
1569 <                throw ex;
1570 <            } finally {
1571 <                Arrays.fill(es, (this.size = w), size, null);
1572 <            }
1560 >            final int beg = i;
1561 >            final long[] deathRow = nBits(end - beg);
1562 >            deathRow[0] = 1L;   // set bit 0
1563 >            for (i = beg + 1; i < end; i++)
1564 >                if (filter.test(elementAt(es, i)))
1565 >                    setBit(deathRow, i - beg);
1566 >            if (modCount != expectedModCount)
1567 >                throw new ConcurrentModificationException();
1568 >            int w = beg;
1569 >            for (i = beg; i < end; i++)
1570 >                if (isClear(deathRow, i - beg))
1571 >                    es[w++] = es[i];
1572 >            final int oldSize = size;
1573 >            System.arraycopy(es, end, es, w, oldSize - end);
1574 >            Arrays.fill(es, size -= (end - w), oldSize, null);
1575          }
1576          if (modCount != expectedModCount)
1577              throw new ConcurrentModificationException();
# Line 1529 | Line 1579 | public class ArrayList<E> extends Abstra
1579      }
1580  
1581      @Override
1532    @SuppressWarnings("unchecked")
1582      public void replaceAll(UnaryOperator<E> operator) {
1583          Objects.requireNonNull(operator);
1584          final int expectedModCount = modCount;
1585 +        final Object[] es = elementData;
1586          final int size = this.size;
1587          for (int i=0; modCount == expectedModCount && i < size; i++) {
1588 <            elementData[i] = operator.apply((E) elementData[i]);
1588 >            es[i] = operator.apply(elementAt(es, i));
1589          }
1590          if (modCount != expectedModCount) {
1591              throw new ConcurrentModificationException();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines