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.46 by jsr166, Fri Dec 2 06:41:08 2016 UTC vs.
Revision 1.52 by jsr166, Wed May 31 22:37:31 2017 UTC

# Line 91 | Line 91 | import java.util.function.UnaryOperator;
91   * should be used only to detect bugs.</i>
92   *
93   * <p>This class is a member of the
94 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
94 > * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
95   * Java Collections Framework</a>.
96   *
97   * @param <E> the type of elements in this list
# Line 515 | Line 515 | public class ArrayList<E> extends Abstra
515       */
516      public E remove(int index) {
517          Objects.checkIndex(index, size);
518 +        final Object[] es = elementData;
519  
520 <        modCount++;
521 <        E oldValue = elementData(index);
521 <
522 <        int numMoved = size - index - 1;
523 <        if (numMoved > 0)
524 <            System.arraycopy(elementData, index+1, elementData, index,
525 <                             numMoved);
526 <        elementData[--size] = null; // clear to let GC do its work
520 >        @SuppressWarnings("unchecked") E oldValue = (E) es[index];
521 >        fastRemove(es, index);
522  
523          // checkInvariants();
524          return oldValue;
# Line 543 | Line 538 | public class ArrayList<E> extends Abstra
538       * @return {@code true} if this list contained the specified element
539       */
540      public boolean remove(Object o) {
541 <        if (o == null) {
542 <            for (int index = 0; index < size; index++)
543 <                if (elementData[index] == null) {
544 <                    fastRemove(index);
545 <                    return true;
546 <                }
547 <        } else {
548 <            for (int index = 0; index < size; index++)
549 <                if (o.equals(elementData[index])) {
550 <                    fastRemove(index);
551 <                    return true;
552 <                }
541 >        final Object[] es = elementData;
542 >        final int size = this.size;
543 >        int i = 0;
544 >        found: {
545 >            if (o == null) {
546 >                for (; i < size; i++)
547 >                    if (es[i] == null)
548 >                        break found;
549 >            } else {
550 >                for (; i < size; i++)
551 >                    if (o.equals(es[i]))
552 >                        break found;
553 >            }
554 >            return false;
555          }
556 <        return false;
556 >        fastRemove(es, i);
557 >        return true;
558      }
559  
560      /**
561       * Private remove method that skips bounds checking and does not
562       * return the value removed.
563       */
564 <    private void fastRemove(int index) {
564 >    private void fastRemove(Object[] es, int i) {
565          modCount++;
566 <        int numMoved = size - index - 1;
567 <        if (numMoved > 0)
568 <            System.arraycopy(elementData, index+1, elementData, index,
569 <                             numMoved);
572 <        elementData[--size] = null; // clear to let GC do its work
566 >        final int newSize;
567 >        if ((newSize = size - 1) > i)
568 >            System.arraycopy(es, i + 1, es, i, newSize - i);
569 >        es[size = newSize] = null;
570      }
571  
572      /**
# Line 578 | Line 575 | public class ArrayList<E> extends Abstra
575       */
576      public void clear() {
577          modCount++;
578 <        Arrays.fill(elementData, 0, size, null);
579 <        size = 0;
578 >        final Object[] es = elementData;
579 >        for (int to = size, i = size = 0; i < to; i++)
580 >            es[i] = null;
581      }
582  
583      /**
# Line 669 | Line 667 | public class ArrayList<E> extends Abstra
667                      outOfBoundsMsg(fromIndex, toIndex));
668          }
669          modCount++;
670 <        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);
670 >        shiftTailOverGap(elementData, fromIndex, toIndex);
671          // checkInvariants();
672      }
673  
674 +    /** Erases the gap from lo to hi, by sliding down following elements. */
675 +    private void shiftTailOverGap(Object[] es, int lo, int hi) {
676 +        System.arraycopy(es, hi, es, lo, size - hi);
677 +        for (int to = size, i = (size -= hi - lo); i < to; i++)
678 +            es[i] = null;
679 +    }
680 +
681      /**
682       * A version of rangeCheck used by add and addAll.
683       */
# Line 761 | Line 763 | public class ArrayList<E> extends Abstra
763                  w += end - r;
764                  throw ex;
765              } finally {
766 <                final int oldSize = size, deleted = end - w;
767 <                modCount += deleted;
766 <                System.arraycopy(es, end, es, w, oldSize - end);
767 <                Arrays.fill(es, size -= deleted, oldSize, null);
766 >                modCount += end - w;
767 >                shiftTailOverGap(es, w, end);
768              }
769          }
770          // checkInvariants();
# Line 787 | Line 787 | public class ArrayList<E> extends Abstra
787          int expectedModCount = modCount;
788          s.defaultWriteObject();
789  
790 <        // Write out size as capacity for behavioural compatibility with clone()
790 >        // Write out size as capacity for behavioral compatibility with clone()
791          s.writeInt(size);
792  
793          // Write out all elements in the proper order.
# Line 1368 | Line 1368 | public class ArrayList<E> extends Abstra
1368          }
1369      }
1370  
1371 +    /**
1372 +     * @throws NullPointerException {@inheritDoc}
1373 +     */
1374      @Override
1375      public void forEach(Consumer<? super E> action) {
1376          Objects.requireNonNull(action);
# Line 1437 | Line 1440 | public class ArrayList<E> extends Abstra
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 */
1443 >        /** Creates new spliterator covering the given range. */
1444          ArrayListSpliterator(int origin, int fence, int expectedModCount) {
1445              this.index = origin;
1446              this.fence = fence;
# Line 1519 | Line 1522 | public class ArrayList<E> extends Abstra
1522          return (bits[i >> 6] & (1L << i)) == 0;
1523      }
1524  
1525 +    /**
1526 +     * @throws NullPointerException {@inheritDoc}
1527 +     */
1528      @Override
1529      public boolean removeIf(Predicate<? super E> filter) {
1530          return removeIf(filter, 0, size);
# Line 1553 | Line 1559 | public class ArrayList<E> extends Abstra
1559              for (i = beg; i < end; i++)
1560                  if (isClear(deathRow, i - beg))
1561                      es[w++] = es[i];
1562 <            final int oldSize = size;
1557 <            System.arraycopy(es, end, es, w, oldSize - end);
1558 <            Arrays.fill(es, size -= (end - w), oldSize, null);
1562 >            shiftTailOverGap(es, w, end);
1563              // checkInvariants();
1564              return true;
1565          } else {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines