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.47 by jsr166, Mon Dec 5 00:08:01 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 <
582 <        // clear to let GC do its work
583 <        for (int i = 0; i < size; i++)
582 <            elementData[i] = null;
583 <
584 <        size = 0;
581 >        final Object[] es = elementData;
582 >        for (int to = size, i = size = 0; i < to; i++)
583 >            es[i] = null;
584      }
585  
586      /**
# Line 609 | Line 608 | public class ArrayList<E> extends Abstra
608              elementData = grow(s + numNew);
609          System.arraycopy(a, 0, elementData, s, numNew);
610          size = s + numNew;
611 +        // checkInvariants();
612          return true;
613      }
614  
# Line 647 | Line 647 | public class ArrayList<E> extends Abstra
647                               numMoved);
648          System.arraycopy(a, 0, elementData, index, numNew);
649          size = s + numNew;
650 +        // checkInvariants();
651          return true;
652      }
653  
# Line 669 | Line 670 | public class ArrayList<E> extends Abstra
670                      outOfBoundsMsg(fromIndex, toIndex));
671          }
672          modCount++;
673 <        int numMoved = size - toIndex;
674 <        System.arraycopy(elementData, toIndex, elementData, fromIndex,
675 <                         numMoved);
676 <
677 <        // clear to let GC do its work
678 <        int newSize = size - (toIndex-fromIndex);
679 <        for (int i = newSize; i < size; i++) {
680 <            elementData[i] = null;
681 <        }
681 <        size = newSize;
673 >        shiftTailOverGap(elementData, fromIndex, toIndex);
674 >        // checkInvariants();
675 >    }
676 >
677 >    /** Erases the gap from lo to hi, by sliding down following elements. */
678 >    private void shiftTailOverGap(Object[] es, int lo, int hi) {
679 >        System.arraycopy(es, hi, es, lo, size - hi);
680 >        for (int to = size, i = (size -= hi - lo); i < to; i++)
681 >            es[i] = null;
682      }
683  
684      /**
# Line 766 | Line 766 | public class ArrayList<E> extends Abstra
766                  w += end - r;
767                  throw ex;
768              } finally {
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);
769 >                modCount += end - w;
770 >                shiftTailOverGap(es, w, end);
771              }
772          }
773 +        // checkInvariants();
774          return modified;
775      }
776  
777      /**
778 <     * Save the state of the {@code ArrayList} instance to a stream (that
779 <     * is, serialize it).
778 >     * Saves the state of the {@code ArrayList} instance to a stream
779 >     * (that is, serializes it).
780       *
781 +     * @param s the stream
782 +     * @throws java.io.IOException if an I/O error occurs
783       * @serialData The length of the array backing the {@code ArrayList}
784       *             instance is emitted (int), followed by all of its elements
785       *             (each an {@code Object}) in the proper order.
786       */
787      private void writeObject(java.io.ObjectOutputStream s)
788 <        throws java.io.IOException{
788 >        throws java.io.IOException {
789          // Write out element count, and any hidden stuff
790          int expectedModCount = modCount;
791          s.defaultWriteObject();
# Line 803 | Line 804 | public class ArrayList<E> extends Abstra
804      }
805  
806      /**
807 <     * Reconstitute the {@code ArrayList} instance from a stream (that is,
808 <     * deserialize it).
807 >     * Reconstitutes the {@code ArrayList} instance from a stream (that is,
808 >     * deserializes it).
809 >     * @param s the stream
810 >     * @throws ClassNotFoundException if the class of a serialized object
811 >     *         could not be found
812 >     * @throws java.io.IOException if an I/O error occurs
813       */
814      private void readObject(java.io.ObjectInputStream s)
815          throws java.io.IOException, ClassNotFoundException {
# Line 916 | Line 921 | public class ArrayList<E> extends Abstra
921          }
922  
923          @Override
924 <        @SuppressWarnings("unchecked")
925 <        public void forEachRemaining(Consumer<? super E> consumer) {
921 <            Objects.requireNonNull(consumer);
924 >        public void forEachRemaining(Consumer<? super E> action) {
925 >            Objects.requireNonNull(action);
926              final int size = ArrayList.this.size;
927              int i = cursor;
928 <            if (i >= size) {
929 <                return;
930 <            }
931 <            final Object[] elementData = ArrayList.this.elementData;
932 <            if (i >= elementData.length) {
933 <                throw new ConcurrentModificationException();
934 <            }
935 <            while (i != size && modCount == expectedModCount) {
936 <                consumer.accept((E) elementData[i++]);
928 >            if (i < size) {
929 >                final Object[] es = elementData;
930 >                if (i >= es.length)
931 >                    throw new ConcurrentModificationException();
932 >                for (; i < size && modCount == expectedModCount; i++)
933 >                    action.accept(elementAt(es, i));
934 >                // update once at end to reduce heap write traffic
935 >                cursor = i;
936 >                lastRet = i - 1;
937 >                checkForComodification();
938              }
934            // update once at end of iteration to reduce heap write traffic
935            cursor = i;
936            lastRet = i - 1;
937            checkForComodification();
939          }
940  
941          final void checkForComodification() {
# Line 1124 | Line 1125 | public class ArrayList<E> extends Abstra
1125          public boolean removeAll(Collection<?> c) {
1126              return batchRemove(c, false);
1127          }
1128 +
1129          public boolean retainAll(Collection<?> c) {
1130              return batchRemove(c, true);
1131          }
# Line 1194 | Line 1196 | public class ArrayList<E> extends Abstra
1196                      return (E) elementData[offset + (lastRet = i)];
1197                  }
1198  
1199 <                @SuppressWarnings("unchecked")
1200 <                public void forEachRemaining(Consumer<? super E> consumer) {
1199 <                    Objects.requireNonNull(consumer);
1199 >                public void forEachRemaining(Consumer<? super E> action) {
1200 >                    Objects.requireNonNull(action);
1201                      final int size = SubList.this.size;
1202                      int i = cursor;
1203 <                    if (i >= size) {
1204 <                        return;
1205 <                    }
1206 <                    final Object[] elementData = root.elementData;
1207 <                    if (offset + i >= elementData.length) {
1208 <                        throw new ConcurrentModificationException();
1209 <                    }
1210 <                    while (i != size && modCount == expectedModCount) {
1211 <                        consumer.accept((E) elementData[offset + (i++)]);
1203 >                    if (i < size) {
1204 >                        final Object[] es = root.elementData;
1205 >                        if (offset + i >= es.length)
1206 >                            throw new ConcurrentModificationException();
1207 >                        for (; i < size && modCount == expectedModCount; i++)
1208 >                            action.accept(elementAt(es, offset + i));
1209 >                        // update once at end to reduce heap write traffic
1210 >                        cursor = i;
1211 >                        lastRet = i - 1;
1212 >                        checkForComodification();
1213                      }
1212                    // update once at end of iteration to reduce heap write traffic
1213                    lastRet = cursor = i;
1214                    checkForComodification();
1214                  }
1215  
1216                  public int nextIndex() {
# Line 1301 | Line 1300 | public class ArrayList<E> extends Abstra
1300          public Spliterator<E> spliterator() {
1301              checkForComodification();
1302  
1303 <            // ArrayListSpliterator is not used because late-binding logic
1304 <            // is different here
1306 <            return new Spliterator<>() {
1303 >            // ArrayListSpliterator not used here due to late-binding
1304 >            return new Spliterator<E>() {
1305                  private int index = offset; // current index, modified on advance/split
1306                  private int fence = -1; // -1 until used; then one past last index
1307                  private int expectedModCount; // initialized when fence set
# Line 1317 | Line 1315 | public class ArrayList<E> extends Abstra
1315                      return hi;
1316                  }
1317  
1318 <                public ArrayListSpliterator<E> trySplit() {
1318 >                public ArrayList<E>.ArrayListSpliterator trySplit() {
1319                      int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1320 <                    // ArrayListSpliterator could be used here as the source is already bound
1320 >                    // ArrayListSpliterator can be used here as the source is already bound
1321                      return (lo >= mid) ? null : // divide range in half unless too small
1322 <                        new ArrayListSpliterator<>(root, lo, index = mid,
1325 <                                                   expectedModCount);
1322 >                        root.new ArrayListSpliterator(lo, index = mid, expectedModCount);
1323                  }
1324  
1325                  public boolean tryAdvance(Consumer<? super E> action) {
# Line 1364 | Line 1361 | public class ArrayList<E> extends Abstra
1361                  }
1362  
1363                  public long estimateSize() {
1364 <                    return (long) (getFence() - index);
1364 >                    return getFence() - index;
1365                  }
1366  
1367                  public int characteristics() {
# Line 1380 | Line 1377 | public class ArrayList<E> extends Abstra
1377          final int expectedModCount = modCount;
1378          final Object[] es = elementData;
1379          final int size = this.size;
1380 <        for (int i = 0; modCount == expectedModCount && i < size; i++) {
1380 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1381              action.accept(elementAt(es, i));
1382 <        }
1386 <        if (modCount != expectedModCount) {
1382 >        if (modCount != expectedModCount)
1383              throw new ConcurrentModificationException();
1388        }
1384      }
1385  
1386      /**
# Line 1403 | Line 1398 | public class ArrayList<E> extends Abstra
1398       */
1399      @Override
1400      public Spliterator<E> spliterator() {
1401 <        return new ArrayListSpliterator<>(this, 0, -1, 0);
1401 >        return new ArrayListSpliterator(0, -1, 0);
1402      }
1403  
1404      /** Index-based split-by-two, lazily initialized Spliterator */
1405 <    static final class ArrayListSpliterator<E> implements Spliterator<E> {
1405 >    final class ArrayListSpliterator implements Spliterator<E> {
1406  
1407          /*
1408           * If ArrayLists were immutable, or structurally immutable (no
# Line 1441 | Line 1436 | public class ArrayList<E> extends Abstra
1436           * these streamlinings.
1437           */
1438  
1444        private final ArrayList<E> list;
1439          private int index; // current index, modified on advance/split
1440          private int fence; // -1 until used; then one past last index
1441          private int expectedModCount; // initialized when fence set
1442  
1443          /** Create new spliterator covering the given range */
1444 <        ArrayListSpliterator(ArrayList<E> list, int origin, int fence,
1451 <                             int expectedModCount) {
1452 <            this.list = list; // OK if null unless traversed
1444 >        ArrayListSpliterator(int origin, int fence, int expectedModCount) {
1445              this.index = origin;
1446              this.fence = fence;
1447              this.expectedModCount = expectedModCount;
# Line 1457 | Line 1449 | public class ArrayList<E> extends Abstra
1449  
1450          private int getFence() { // initialize fence to size on first use
1451              int hi; // (a specialized variant appears in method forEach)
1460            ArrayList<E> lst;
1452              if ((hi = fence) < 0) {
1453 <                if ((lst = list) == null)
1454 <                    hi = fence = 0;
1464 <                else {
1465 <                    expectedModCount = lst.modCount;
1466 <                    hi = fence = lst.size;
1467 <                }
1453 >                expectedModCount = modCount;
1454 >                hi = fence = size;
1455              }
1456              return hi;
1457          }
1458  
1459 <        public ArrayListSpliterator<E> trySplit() {
1459 >        public ArrayListSpliterator trySplit() {
1460              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1461              return (lo >= mid) ? null : // divide range in half unless too small
1462 <                new ArrayListSpliterator<>(list, lo, index = mid,
1476 <                                           expectedModCount);
1462 >                new ArrayListSpliterator(lo, index = mid, expectedModCount);
1463          }
1464  
1465          public boolean tryAdvance(Consumer<? super E> action) {
# Line 1482 | Line 1468 | public class ArrayList<E> extends Abstra
1468              int hi = getFence(), i = index;
1469              if (i < hi) {
1470                  index = i + 1;
1471 <                @SuppressWarnings("unchecked") E e = (E)list.elementData[i];
1471 >                @SuppressWarnings("unchecked") E e = (E)elementData[i];
1472                  action.accept(e);
1473 <                if (list.modCount != expectedModCount)
1473 >                if (modCount != expectedModCount)
1474                      throw new ConcurrentModificationException();
1475                  return true;
1476              }
# Line 1493 | Line 1479 | public class ArrayList<E> extends Abstra
1479  
1480          public void forEachRemaining(Consumer<? super E> action) {
1481              int i, hi, mc; // hoist accesses and checks from loop
1482 <            ArrayList<E> lst; Object[] a;
1482 >            Object[] a;
1483              if (action == null)
1484                  throw new NullPointerException();
1485 <            if ((lst = list) != null && (a = lst.elementData) != null) {
1485 >            if ((a = elementData) != null) {
1486                  if ((hi = fence) < 0) {
1487 <                    mc = lst.modCount;
1488 <                    hi = lst.size;
1487 >                    mc = modCount;
1488 >                    hi = size;
1489                  }
1490                  else
1491                      mc = expectedModCount;
# Line 1508 | Line 1494 | public class ArrayList<E> extends Abstra
1494                          @SuppressWarnings("unchecked") E e = (E) a[i];
1495                          action.accept(e);
1496                      }
1497 <                    if (lst.modCount == mc)
1497 >                    if (modCount == mc)
1498                          return;
1499                  }
1500              }
# Line 1516 | Line 1502 | public class ArrayList<E> extends Abstra
1502          }
1503  
1504          public long estimateSize() {
1505 <            return (long) (getFence() - index);
1505 >            return getFence() - index;
1506          }
1507  
1508          public int characteristics() {
# Line 1541 | Line 1527 | public class ArrayList<E> extends Abstra
1527          return removeIf(filter, 0, size);
1528      }
1529  
1530 <    boolean removeIf(Predicate<? super E> filter,
1531 <                     final int from, final int end) {
1530 >    /**
1531 >     * Removes all elements satisfying the given predicate, from index
1532 >     * i (inclusive) to index end (exclusive).
1533 >     */
1534 >    boolean removeIf(Predicate<? super E> filter, int i, final int end) {
1535          Objects.requireNonNull(filter);
1536          int expectedModCount = modCount;
1537          final Object[] es = elementData;
1549        final boolean modified;
1550        int i;
1538          // Optimize for initial run of survivors
1539 <        for (i = from; i < end && !filter.test(elementAt(es, i)); i++)
1539 >        for (; i < end && !filter.test(elementAt(es, i)); i++)
1540              ;
1541          // Tolerate predicates that reentrantly access the collection for
1542          // read (but writers still get CME), so traverse once to find
1543          // elements to delete, a second pass to physically expunge.
1544 <        if (modified = (i < end)) {
1558 <            expectedModCount++;
1559 <            modCount++;
1544 >        if (i < end) {
1545              final int beg = i;
1546              final long[] deathRow = nBits(end - beg);
1547              deathRow[0] = 1L;   // set bit 0
# Line 1565 | Line 1550 | public class ArrayList<E> extends Abstra
1550                      setBit(deathRow, i - beg);
1551              if (modCount != expectedModCount)
1552                  throw new ConcurrentModificationException();
1553 +            expectedModCount++;
1554 +            modCount++;
1555              int w = beg;
1556              for (i = beg; i < end; i++)
1557                  if (isClear(deathRow, i - beg))
1558                      es[w++] = es[i];
1559 <            final int oldSize = size;
1560 <            System.arraycopy(es, end, es, w, oldSize - end);
1561 <            Arrays.fill(es, size -= (end - w), oldSize, null);
1559 >            shiftTailOverGap(es, w, end);
1560 >            // checkInvariants();
1561 >            return true;
1562 >        } else {
1563 >            if (modCount != expectedModCount)
1564 >                throw new ConcurrentModificationException();
1565 >            // checkInvariants();
1566 >            return false;
1567          }
1576        if (modCount != expectedModCount)
1577            throw new ConcurrentModificationException();
1578        return modified;
1568      }
1569  
1570      @Override
# Line 1584 | Line 1573 | public class ArrayList<E> extends Abstra
1573          final int expectedModCount = modCount;
1574          final Object[] es = elementData;
1575          final int size = this.size;
1576 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1576 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1577              es[i] = operator.apply(elementAt(es, i));
1578 <        }
1590 <        if (modCount != expectedModCount) {
1578 >        if (modCount != expectedModCount)
1579              throw new ConcurrentModificationException();
1592        }
1580          modCount++;
1581 +        // checkInvariants();
1582      }
1583  
1584      @Override
# Line 1598 | Line 1586 | public class ArrayList<E> extends Abstra
1586      public void sort(Comparator<? super E> c) {
1587          final int expectedModCount = modCount;
1588          Arrays.sort((E[]) elementData, 0, size, c);
1589 <        if (modCount != expectedModCount) {
1589 >        if (modCount != expectedModCount)
1590              throw new ConcurrentModificationException();
1603        }
1591          modCount++;
1592 +        // checkInvariants();
1593 +    }
1594 +
1595 +    void checkInvariants() {
1596 +        // assert size >= 0;
1597 +        // assert size == elementData.length || elementData[size] == null;
1598      }
1599   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines