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.39 by jsr166, Sun Nov 13 02:10:09 2016 UTC vs.
Revision 1.44 by jsr166, Wed Nov 16 00:17:25 2016 UTC

# Line 501 | 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 524 | 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 557 | 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 576 | Line 578 | public class ArrayList<E> extends Abstra
578       */
579      public void clear() {
580          modCount++;
581 <
580 <        // clear to let GC do its work
581 <        for (int i = 0; i < size; i++)
582 <            elementData[i] = null;
583 <
581 >        Arrays.fill(elementData, 0, size, null);
582          size = 0;
583      }
584  
# Line 609 | 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 647 | 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 669 | 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
677 <        int newSize = size - (toIndex-fromIndex);
678 <        for (int i = newSize; i < size; i++) {
679 <            elementData[i] = null;
680 <        }
681 <        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 721 | 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 741 | 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;
750        final int end = size;
746          final boolean modified;
747          int r;
748          // Optimize for initial run of survivors
749 <        for (r = 0; r < end && c.contains(es[r]) == complement; r++)
749 >        for (r = from; r < end && c.contains(es[r]) == complement; r++)
750              ;
751          if (modified = (r < end)) {
752              int w = r++;
# Line 766 | Line 761 | public class ArrayList<E> extends Abstra
761                  w += end - r;
762                  throw ex;
763              } finally {
764 <                modCount += end - w;
765 <                Arrays.fill(es, size = w, end, 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 914 | Line 912 | public class ArrayList<E> extends Abstra
912          }
913  
914          @Override
915 <        @SuppressWarnings("unchecked")
916 <        public void forEachRemaining(Consumer<? super E> consumer) {
919 <            Objects.requireNonNull(consumer);
915 >        public void forEachRemaining(Consumer<? super E> action) {
916 >            Objects.requireNonNull(action);
917              final int size = ArrayList.this.size;
918              int i = cursor;
919 <            if (i >= size) {
920 <                return;
921 <            }
922 <            final Object[] elementData = ArrayList.this.elementData;
923 <            if (i >= elementData.length) {
924 <                throw new ConcurrentModificationException();
925 <            }
926 <            while (i != size && modCount == expectedModCount) {
927 <                consumer.accept((E) elementData[i++]);
919 >            if (i < size) {
920 >                final Object[] es = elementData;
921 >                if (i >= es.length)
922 >                    throw new ConcurrentModificationException();
923 >                for (; i < size && modCount == expectedModCount; i++)
924 >                    action.accept(elementAt(es, i));
925 >                // update once at end to reduce heap write traffic
926 >                cursor = i;
927 >                lastRet = i - 1;
928 >                checkForComodification();
929              }
932            // update once at end of iteration to reduce heap write traffic
933            cursor = i;
934            lastRet = i - 1;
935            checkForComodification();
930          }
931  
932          final void checkForComodification() {
# Line 1119 | Line 1113 | public class ArrayList<E> extends Abstra
1113              return true;
1114          }
1115  
1116 +        public boolean removeAll(Collection<?> c) {
1117 +            return batchRemove(c, false);
1118 +        }
1119 +
1120 +        public boolean retainAll(Collection<?> c) {
1121 +            return batchRemove(c, true);
1122 +        }
1123 +
1124 +        private boolean batchRemove(Collection<?> c, boolean complement) {
1125 +            checkForComodification();
1126 +            int oldSize = root.size;
1127 +            boolean modified =
1128 +                root.batchRemove(c, complement, offset, offset + size);
1129 +            if (modified)
1130 +                updateSizeAndModCount(root.size - oldSize);
1131 +            return modified;
1132 +        }
1133 +
1134 +        public boolean removeIf(Predicate<? super E> filter) {
1135 +            checkForComodification();
1136 +            int oldSize = root.size;
1137 +            boolean modified = root.removeIf(filter, offset, offset + size);
1138 +            if (modified)
1139 +                updateSizeAndModCount(root.size - oldSize);
1140 +            return modified;
1141 +        }
1142 +
1143          public Iterator<E> iterator() {
1144              return listIterator();
1145          }
# Line 1166 | Line 1187 | public class ArrayList<E> extends Abstra
1187                      return (E) elementData[offset + (lastRet = i)];
1188                  }
1189  
1190 <                @SuppressWarnings("unchecked")
1191 <                public void forEachRemaining(Consumer<? super E> consumer) {
1171 <                    Objects.requireNonNull(consumer);
1190 >                public void forEachRemaining(Consumer<? super E> action) {
1191 >                    Objects.requireNonNull(action);
1192                      final int size = SubList.this.size;
1193                      int i = cursor;
1194 <                    if (i >= size) {
1195 <                        return;
1196 <                    }
1197 <                    final Object[] elementData = root.elementData;
1198 <                    if (offset + i >= elementData.length) {
1199 <                        throw new ConcurrentModificationException();
1200 <                    }
1201 <                    while (i != size && modCount == expectedModCount) {
1202 <                        consumer.accept((E) elementData[offset + (i++)]);
1194 >                    if (i < size) {
1195 >                        final Object[] es = root.elementData;
1196 >                        if (offset + i >= es.length)
1197 >                            throw new ConcurrentModificationException();
1198 >                        for (; i < size && modCount == expectedModCount; i++)
1199 >                            action.accept(elementAt(es, offset + i));
1200 >                        // update once at end to reduce heap write traffic
1201 >                        cursor = i;
1202 >                        lastRet = i - 1;
1203 >                        checkForComodification();
1204                      }
1184                    // update once at end of iteration to reduce heap write traffic
1185                    lastRet = cursor = i;
1186                    checkForComodification();
1205                  }
1206  
1207                  public int nextIndex() {
# Line 1352 | Line 1370 | public class ArrayList<E> extends Abstra
1370          final int expectedModCount = modCount;
1371          final Object[] es = elementData;
1372          final int size = this.size;
1373 <        for (int i = 0; modCount == expectedModCount && i < size; i++) {
1373 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1374              action.accept(elementAt(es, i));
1375 <        }
1358 <        if (modCount != expectedModCount) {
1375 >        if (modCount != expectedModCount)
1376              throw new ConcurrentModificationException();
1360        }
1377      }
1378  
1379      /**
# Line 1418 | Line 1434 | public class ArrayList<E> extends Abstra
1434          private int fence; // -1 until used; then one past last index
1435          private int expectedModCount; // initialized when fence set
1436  
1437 <        /** Create new spliterator covering the given  range */
1437 >        /** Create new spliterator covering the given range */
1438          ArrayListSpliterator(ArrayList<E> list, int origin, int fence,
1439                               int expectedModCount) {
1440              this.list = list; // OK if null unless traversed
# Line 1509 | Line 1525 | public class ArrayList<E> extends Abstra
1525      }
1526  
1527      @Override
1528 <        public boolean removeIf(Predicate<? super E> filter) {
1528 >    public boolean removeIf(Predicate<? super E> filter) {
1529 >        return removeIf(filter, 0, size);
1530 >    }
1531 >
1532 >    /**
1533 >     * Removes all elements satisfying the given predicate, from index
1534 >     * i (inclusive) to index end (exclusive).
1535 >     */
1536 >    boolean removeIf(Predicate<? super E> filter, int i, final int end) {
1537          Objects.requireNonNull(filter);
1538          int expectedModCount = modCount;
1539          final Object[] es = elementData;
1516        final int end = size;
1517        final boolean modified;
1518        int i;
1540          // Optimize for initial run of survivors
1541 <        for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
1541 >        for (; i < end && !filter.test(elementAt(es, i)); i++)
1542              ;
1543          // Tolerate predicates that reentrantly access the collection for
1544          // read (but writers still get CME), so traverse once to find
1545          // elements to delete, a second pass to physically expunge.
1546 <        if (modified = (i < end)) {
1526 <            expectedModCount++;
1527 <            modCount++;
1546 >        if (i < end) {
1547              final int beg = i;
1548              final long[] deathRow = nBits(end - beg);
1549              deathRow[0] = 1L;   // set bit 0
1550              for (i = beg + 1; i < end; i++)
1551                  if (filter.test(elementAt(es, i)))
1552                      setBit(deathRow, i - beg);
1553 +            if (modCount != expectedModCount)
1554 +                throw new ConcurrentModificationException();
1555 +            expectedModCount++;
1556 +            modCount++;
1557              int w = beg;
1558              for (i = beg; i < end; i++)
1559                  if (isClear(deathRow, i - beg))
1560                      es[w++] = es[i];
1561 <            Arrays.fill(es, size = w, end, null);
1561 >            final int oldSize = size;
1562 >            System.arraycopy(es, end, es, w, oldSize - end);
1563 >            Arrays.fill(es, size -= (end - w), oldSize, null);
1564 >            // checkInvariants();
1565 >            return true;
1566 >        } else {
1567 >            if (modCount != expectedModCount)
1568 >                throw new ConcurrentModificationException();
1569 >            // checkInvariants();
1570 >            return false;
1571          }
1540        if (modCount != expectedModCount)
1541            throw new ConcurrentModificationException();
1542        return modified;
1572      }
1573  
1574      @Override
# Line 1548 | Line 1577 | public class ArrayList<E> extends Abstra
1577          final int expectedModCount = modCount;
1578          final Object[] es = elementData;
1579          final int size = this.size;
1580 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1580 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1581              es[i] = operator.apply(elementAt(es, i));
1582 <        }
1554 <        if (modCount != expectedModCount) {
1582 >        if (modCount != expectedModCount)
1583              throw new ConcurrentModificationException();
1556        }
1584          modCount++;
1585 +        // checkInvariants();
1586      }
1587  
1588      @Override
# Line 1562 | Line 1590 | public class ArrayList<E> extends Abstra
1590      public void sort(Comparator<? super E> c) {
1591          final int expectedModCount = modCount;
1592          Arrays.sort((E[]) elementData, 0, size, c);
1593 <        if (modCount != expectedModCount) {
1593 >        if (modCount != expectedModCount)
1594              throw new ConcurrentModificationException();
1567        }
1595          modCount++;
1596 +        // checkInvariants();
1597 +    }
1598 +
1599 +    void checkInvariants() {
1600 +        // assert size >= 0;
1601 +        // assert size == elementData.length || elementData[size] == null;
1602      }
1603   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines