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.41 by jsr166, Sun Nov 13 21:07:40 2016 UTC vs.
Revision 1.44 by jsr166, Wed Nov 16 00:17:25 2016 UTC

# Line 912 | Line 912 | public class ArrayList<E> extends Abstra
912          }
913  
914          @Override
915 <        @SuppressWarnings("unchecked")
916 <        public void forEachRemaining(Consumer<? super E> consumer) {
917 <            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              }
930            // update once at end of iteration to reduce heap write traffic
931            cursor = i;
932            lastRet = i - 1;
933            checkForComodification();
930          }
931  
932          final void checkForComodification() {
# Line 1191 | 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) {
1196 <                    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                      }
1209                    // update once at end of iteration to reduce heap write traffic
1210                    lastRet = cursor = i;
1211                    checkForComodification();
1205                  }
1206  
1207                  public int nextIndex() {
# Line 1536 | Line 1529 | public class ArrayList<E> extends Abstra
1529          return removeIf(filter, 0, size);
1530      }
1531  
1532 <    boolean removeIf(Predicate<? super E> filter,
1533 <                     final int from, final int end) {
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;
1544        final boolean modified;
1545        int i;
1540          // Optimize for initial run of survivors
1541 <        for (i = from; 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)) {
1553 <            expectedModCount++;
1554 <            modCount++;
1546 >        if (i < end) {
1547              final int beg = i;
1548              final long[] deathRow = nBits(end - beg);
1549              deathRow[0] = 1L;   // set bit 0
# Line 1560 | Line 1552 | public class ArrayList<E> extends Abstra
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))
# Line 1567 | Line 1561 | public class ArrayList<E> extends Abstra
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          }
1571        if (modCount != expectedModCount)
1572            throw new ConcurrentModificationException();
1573        // checkInvariants();
1574        return modified;
1572      }
1573  
1574      @Override

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines