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.50 by jsr166, Sat May 6 06:49:45 2017 UTC vs.
Revision 1.53 by jsr166, Mon Jul 3 20:08:10 2017 UTC

# 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 748 | Line 745 | public class ArrayList<E> extends Abstra
745                          final int from, final int end) {
746          Objects.requireNonNull(c);
747          final Object[] es = elementData;
751        final boolean modified;
748          int r;
749          // Optimize for initial run of survivors
750 <        for (r = from; r < end && c.contains(es[r]) == complement; r++)
751 <            ;
752 <        if (modified = (r < end)) {
753 <            int w = r++;
754 <            try {
755 <                for (Object e; r < end; r++)
756 <                    if (c.contains(e = es[r]) == complement)
757 <                        es[w++] = e;
758 <            } catch (Throwable ex) {
759 <                // Preserve behavioral compatibility with AbstractCollection,
760 <                // even if c.contains() throws.
761 <                System.arraycopy(es, r, es, w, end - r);
762 <                w += end - r;
763 <                throw ex;
764 <            } finally {
765 <                modCount += end - w;
766 <                shiftTailOverGap(es, w, end);
767 <            }
750 >        for (r = from;; r++) {
751 >            if (r == end)
752 >                return false;
753 >            if (c.contains(es[r]) != complement)
754 >                break;
755 >        }
756 >        int w = r++;
757 >        try {
758 >            for (Object e; r < end; r++)
759 >                if (c.contains(e = es[r]) == complement)
760 >                    es[w++] = e;
761 >        } catch (Throwable ex) {
762 >            // Preserve behavioral compatibility with AbstractCollection,
763 >            // even if c.contains() throws.
764 >            System.arraycopy(es, r, es, w, end - r);
765 >            w += end - r;
766 >            throw ex;
767 >        } finally {
768 >            modCount += end - w;
769 >            shiftTailOverGap(es, w, end);
770          }
771          // checkInvariants();
772 <        return modified;
772 >        return true;
773      }
774  
775      /**
# Line 790 | Line 788 | public class ArrayList<E> extends Abstra
788          int expectedModCount = modCount;
789          s.defaultWriteObject();
790  
791 <        // Write out size as capacity for behavioural compatibility with clone()
791 >        // Write out size as capacity for behavioral compatibility with clone()
792          s.writeInt(size);
793  
794          // Write out all elements in the proper order.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines