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.38 by jsr166, Sat Nov 12 20:51:59 2016 UTC vs.
Revision 1.41 by jsr166, Sun Nov 13 21:07:40 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 496 | 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 519 | 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 552 | 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 571 | Line 578 | public class ArrayList<E> extends Abstra
578       */
579      public void clear() {
580          modCount++;
581 <
575 <        // clear to let GC do its work
576 <        for (int i = 0; i < size; i++)
577 <            elementData[i] = null;
578 <
581 >        Arrays.fill(elementData, 0, size, null);
582          size = 0;
583      }
584  
# Line 604 | 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 642 | 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 664 | 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
672 <        int newSize = size - (toIndex-fromIndex);
673 <        for (int i = newSize; i < size; i++) {
674 <            elementData[i] = null;
675 <        }
676 <        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 716 | Line 716 | public class ArrayList<E> extends Abstra
716       * @see Collection#contains(Object)
717       */
718      public boolean removeAll(Collection<?> c) {
719 <        return batchRemove(c, false);
719 >        return batchRemove(c, false, 0, size);
720      }
721  
722      /**
# Line 736 | Line 736 | public class ArrayList<E> extends Abstra
736       * @see Collection#contains(Object)
737       */
738      public boolean retainAll(Collection<?> c) {
739 <        return batchRemove(c, true);
739 >        return batchRemove(c, true, 0, size);
740      }
741  
742 <    private boolean batchRemove(Collection<?> c, boolean complement) {
742 >    boolean batchRemove(Collection<?> c, boolean complement,
743 >                        final int from, final int end) {
744          Objects.requireNonNull(c);
745          final Object[] es = elementData;
745        final int size = this.size;
746          final boolean modified;
747          int r;
748          // Optimize for initial run of survivors
749 <        for (r = 0; r < size && c.contains(es[r]) == complement; r++)
749 >        for (r = from; r < end && c.contains(es[r]) == complement; r++)
750              ;
751 <        if (modified = (r < size)) {
751 >        if (modified = (r < end)) {
752              int w = r++;
753              try {
754 <                for (Object e; r < size; r++)
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, size - r);
761 <                w += size - r;
760 >                System.arraycopy(es, r, es, w, end - r);
761 >                w += end - r;
762                  throw ex;
763              } finally {
764 <                modCount += size - w;
765 <                Arrays.fill(es, (this.size = w), size, null);
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 1114 | 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 1345 | Line 1375 | public class ArrayList<E> extends Abstra
1375      public void forEach(Consumer<? super E> action) {
1376          Objects.requireNonNull(action);
1377          final int expectedModCount = modCount;
1378 <        @SuppressWarnings("unchecked")
1349 <        final E[] elementData = (E[]) this.elementData;
1378 >        final Object[] es = elementData;
1379          final int size = this.size;
1380 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1381 <            action.accept(elementData[i]);
1382 <        }
1354 <        if (modCount != expectedModCount) {
1380 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1381 >            action.accept(elementAt(es, i));
1382 >        if (modCount != expectedModCount)
1383              throw new ConcurrentModificationException();
1356        }
1384      }
1385  
1386      /**
# Line 1414 | Line 1441 | public class ArrayList<E> extends Abstra
1441          private int fence; // -1 until used; then one past last index
1442          private int expectedModCount; // initialized when fence set
1443  
1444 <        /** Create new spliterator covering the given  range */
1444 >        /** Create new spliterator covering the given range */
1445          ArrayListSpliterator(ArrayList<E> list, int origin, int fence,
1446                               int expectedModCount) {
1447              this.list = list; // OK if null unless traversed
# Line 1492 | Line 1519 | public class ArrayList<E> extends Abstra
1519          }
1520      }
1521  
1522 <    @SuppressWarnings("unchecked")
1522 >    // A tiny bit set implementation
1523 >
1524 >    private static long[] nBits(int n) {
1525 >        return new long[((n - 1) >> 6) + 1];
1526 >    }
1527 >    private static void setBit(long[] bits, int i) {
1528 >        bits[i >> 6] |= 1L << i;
1529 >    }
1530 >    private static boolean isClear(long[] bits, int i) {
1531 >        return (bits[i >> 6] & (1L << i)) == 0;
1532 >    }
1533 >
1534      @Override
1535      public boolean removeIf(Predicate<? super E> filter) {
1536 +        return removeIf(filter, 0, size);
1537 +    }
1538 +
1539 +    boolean removeIf(Predicate<? super E> filter,
1540 +                     final int from, final int end) {
1541          Objects.requireNonNull(filter);
1542          int expectedModCount = modCount;
1543          final Object[] es = elementData;
1501        final int size = this.size;
1544          final boolean modified;
1545 <        int r;
1545 >        int i;
1546          // Optimize for initial run of survivors
1547 <        for (r = 0; r < size && !filter.test((E) es[r]); r++)
1547 >        for (i = from; i < end && !filter.test(elementAt(es, i)); i++)
1548              ;
1549 <        if (modified = (r < size)) {
1549 >        // Tolerate predicates that reentrantly access the collection for
1550 >        // read (but writers still get CME), so traverse once to find
1551 >        // elements to delete, a second pass to physically expunge.
1552 >        if (modified = (i < end)) {
1553              expectedModCount++;
1554              modCount++;
1555 <            int w = r++;
1556 <            try {
1557 <                for (E e; r < size; r++)
1558 <                    if (!filter.test(e = (E) es[r]))
1559 <                        es[w++] = e;
1560 <            } catch (Throwable ex) {
1561 <                // copy remaining elements
1562 <                System.arraycopy(es, r, es, w, size - r);
1563 <                w += size - r;
1564 <                throw ex;
1565 <            } finally {
1566 <                Arrays.fill(es, (this.size = w), size, null);
1567 <            }
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 >            int w = beg;
1564 >            for (i = beg; i < end; i++)
1565 >                if (isClear(deathRow, i - beg))
1566 >                    es[w++] = es[i];
1567 >            final int oldSize = size;
1568 >            System.arraycopy(es, end, es, w, oldSize - end);
1569 >            Arrays.fill(es, size -= (end - w), oldSize, null);
1570          }
1571          if (modCount != expectedModCount)
1572              throw new ConcurrentModificationException();
1573 +        // checkInvariants();
1574          return modified;
1575      }
1576  
1577      @Override
1530    @SuppressWarnings("unchecked")
1578      public void replaceAll(UnaryOperator<E> operator) {
1579          Objects.requireNonNull(operator);
1580          final int expectedModCount = modCount;
1581 +        final Object[] es = elementData;
1582          final int size = this.size;
1583 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1584 <            elementData[i] = operator.apply((E) elementData[i]);
1585 <        }
1538 <        if (modCount != expectedModCount) {
1583 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1584 >            es[i] = operator.apply(elementAt(es, i));
1585 >        if (modCount != expectedModCount)
1586              throw new ConcurrentModificationException();
1540        }
1587          modCount++;
1588 +        // checkInvariants();
1589      }
1590  
1591      @Override
# Line 1546 | Line 1593 | public class ArrayList<E> extends Abstra
1593      public void sort(Comparator<? super E> c) {
1594          final int expectedModCount = modCount;
1595          Arrays.sort((E[]) elementData, 0, size, c);
1596 <        if (modCount != expectedModCount) {
1596 >        if (modCount != expectedModCount)
1597              throw new ConcurrentModificationException();
1551        }
1598          modCount++;
1599 +        // checkInvariants();
1600 +    }
1601 +
1602 +    void checkInvariants() {
1603 +        // assert size >= 0;
1604 +        // assert size == elementData.length || elementData[size] == null;
1605      }
1606   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines