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.40 by jsr166, Sun Nov 13 20:03:11 2016 UTC vs.
Revision 1.43 by jsr166, Mon Nov 14 22:46:22 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 772 | Line 767 | public class ArrayList<E> extends Abstra
767                  Arrays.fill(es, size -= deleted, oldSize, null);
768              }
769          }
770 +        // checkInvariants();
771          return modified;
772      }
773  
# Line 1124 | Line 1120 | public class ArrayList<E> extends Abstra
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          }
# Line 1210 | 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 1380 | Line 1378 | public class ArrayList<E> extends Abstra
1378          final int expectedModCount = modCount;
1379          final Object[] es = elementData;
1380          final int size = this.size;
1381 <        for (int i = 0; modCount == expectedModCount && i < size; i++) {
1381 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1382              action.accept(elementAt(es, i));
1383 <        }
1386 <        if (modCount != expectedModCount) {
1383 >        if (modCount != expectedModCount)
1384              throw new ConcurrentModificationException();
1388        }
1385      }
1386  
1387      /**
# Line 1541 | Line 1537 | public class ArrayList<E> extends Abstra
1537          return removeIf(filter, 0, size);
1538      }
1539  
1540 <    boolean removeIf(Predicate<? super E> filter,
1541 <                     final int from, final int end) {
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          int expectedModCount = modCount;
1547          final Object[] es = elementData;
1549        final boolean modified;
1550        int i;
1548          // Optimize for initial run of survivors
1549 <        for (i = from; i < end && !filter.test(elementAt(es, i)); i++)
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 (modified = (i < end)) {
1558 <            expectedModCount++;
1559 <            modCount++;
1554 >        if (i < end) {
1555              final int beg = i;
1556              final long[] deathRow = nBits(end - beg);
1557              deathRow[0] = 1L;   // set bit 0
# Line 1565 | Line 1560 | public class ArrayList<E> extends Abstra
1560                      setBit(deathRow, i - beg);
1561              if (modCount != expectedModCount)
1562                  throw new ConcurrentModificationException();
1563 +            expectedModCount++;
1564 +            modCount++;
1565              int w = beg;
1566              for (i = beg; i < end; i++)
1567                  if (isClear(deathRow, i - beg))
# Line 1572 | Line 1569 | public class ArrayList<E> extends Abstra
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          }
1576        if (modCount != expectedModCount)
1577            throw new ConcurrentModificationException();
1578        return modified;
1580      }
1581  
1582      @Override
# Line 1584 | Line 1585 | public class ArrayList<E> extends Abstra
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++) {
1588 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1589              es[i] = operator.apply(elementAt(es, i));
1590 <        }
1590 <        if (modCount != expectedModCount) {
1590 >        if (modCount != expectedModCount)
1591              throw new ConcurrentModificationException();
1592        }
1592          modCount++;
1593 +        // checkInvariants();
1594      }
1595  
1596      @Override
# Line 1598 | 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();
1603        }
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