ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Vector.java
(Generate patch)

Comparing jsr166/src/main/java/util/Vector.java (file contents):
Revision 1.36 by jsr166, Mon Nov 14 22:46:22 2016 UTC vs.
Revision 1.40 by jsr166, Mon Dec 5 00:08:01 2016 UTC

# Line 306 | Line 306 | public class Vector<E>
306          modCount++;
307          if (newSize > elementData.length)
308              grow(newSize);
309 <        for (int i = newSize; i < elementCount; i++)
310 <            elementData[i] = null;
311 <        elementCount = newSize;
309 >        final Object[] es = elementData;
310 >        for (int to = elementCount, i = elementCount = newSize; i < to; i++)
311 >            es[i] = null;
312      }
313  
314      /**
# Line 676 | Line 676 | public class Vector<E>
676       * method (which is part of the {@link List} interface).
677       */
678      public synchronized void removeAllElements() {
679 <        Arrays.fill(elementData, 0, elementCount, null);
679 >        final Object[] es = elementData;
680 >        for (int to = elementCount, i = elementCount = 0; i < to; i++)
681 >            es[i] = null;
682          modCount++;
681        elementCount = 0;
683      }
684  
685      /**
# Line 1028 | Line 1029 | public class Vector<E>
1029              for (i = beg; i < end; i++)
1030                  if (isClear(deathRow, i - beg))
1031                      es[w++] = es[i];
1032 <            Arrays.fill(es, elementCount = w, end, null);
1032 >            for (i = elementCount = w; i < end; i++)
1033 >                es[i] = null;
1034              // checkInvariants();
1035              return true;
1036          } else {
# Line 1159 | Line 1161 | public class Vector<E>
1161       * (If {@code toIndex==fromIndex}, this operation has no effect.)
1162       */
1163      protected synchronized void removeRange(int fromIndex, int toIndex) {
1162        final Object[] es = elementData;
1163        final int oldSize = elementCount;
1164        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
1165
1164          modCount++;
1165 <        Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null);
1165 >        shiftTailOverGap(elementData, fromIndex, toIndex);
1166          // checkInvariants();
1167      }
1168  
1169 +    /** Erases the gap from lo to hi, by sliding down following elements. */
1170 +    private void shiftTailOverGap(Object[] es, int lo, int hi) {
1171 +        System.arraycopy(es, hi, es, lo, elementCount - hi);
1172 +        for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++)
1173 +            es[i] = null;
1174 +    }
1175 +
1176      /**
1177 <     * Save the state of the {@code Vector} instance to a stream (that
1178 <     * is, serialize it).
1177 >     * Saves the state of the {@code Vector} instance to a stream
1178 >     * (that is, serializes it).
1179       * This method performs synchronization to ensure the consistency
1180       * of the serialized data.
1181 +     *
1182 +     * @param s the stream
1183 +     * @throws java.io.IOException if an I/O error occurs
1184       */
1185      private void writeObject(java.io.ObjectOutputStream s)
1186              throws java.io.IOException {
# Line 1398 | Line 1406 | public class Vector<E>
1406       */
1407      @Override
1408      public Spliterator<E> spliterator() {
1409 <        return new VectorSpliterator<>(this, null, 0, -1, 0);
1409 >        return new VectorSpliterator(null, 0, -1, 0);
1410      }
1411  
1412      /** Similar to ArrayList Spliterator */
1413 <    static final class VectorSpliterator<E> implements Spliterator<E> {
1406 <        private final Vector<E> list;
1413 >    final class VectorSpliterator implements Spliterator<E> {
1414          private Object[] array;
1415          private int index; // current index, modified on advance/split
1416          private int fence; // -1 until used; then one past last index
1417          private int expectedModCount; // initialized when fence set
1418  
1419          /** Create new spliterator covering the given range */
1420 <        VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1420 >        VectorSpliterator(Object[] array, int origin, int fence,
1421                            int expectedModCount) {
1415            this.list = list;
1422              this.array = array;
1423              this.index = origin;
1424              this.fence = fence;
# Line 1422 | Line 1428 | public class Vector<E>
1428          private int getFence() { // initialize on first use
1429              int hi;
1430              if ((hi = fence) < 0) {
1431 <                synchronized (list) {
1432 <                    array = list.elementData;
1433 <                    expectedModCount = list.modCount;
1434 <                    hi = fence = list.elementCount;
1431 >                synchronized (Vector.this) {
1432 >                    array = elementData;
1433 >                    expectedModCount = modCount;
1434 >                    hi = fence = elementCount;
1435                  }
1436              }
1437              return hi;
# Line 1434 | Line 1440 | public class Vector<E>
1440          public Spliterator<E> trySplit() {
1441              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1442              return (lo >= mid) ? null :
1443 <                new VectorSpliterator<>(list, array, lo, index = mid,
1438 <                                        expectedModCount);
1443 >                new VectorSpliterator(array, lo, index = mid, expectedModCount);
1444          }
1445  
1446          @SuppressWarnings("unchecked")
# Line 1446 | Line 1451 | public class Vector<E>
1451              if (getFence() > (i = index)) {
1452                  index = i + 1;
1453                  action.accept((E)array[i]);
1454 <                if (list.modCount != expectedModCount)
1454 >                if (modCount != expectedModCount)
1455                      throw new ConcurrentModificationException();
1456                  return true;
1457              }
# Line 1455 | Line 1460 | public class Vector<E>
1460  
1461          @SuppressWarnings("unchecked")
1462          public void forEachRemaining(Consumer<? super E> action) {
1458            int i, hi; // hoist accesses and checks from loop
1459            Vector<E> lst; Object[] a;
1463              if (action == null)
1464                  throw new NullPointerException();
1465 <            if ((lst = list) != null) {
1466 <                if ((hi = fence) < 0) {
1467 <                    synchronized (lst) {
1468 <                        expectedModCount = lst.modCount;
1469 <                        a = array = lst.elementData;
1470 <                        hi = fence = lst.elementCount;
1471 <                    }
1469 <                }
1470 <                else
1471 <                    a = array;
1472 <                if (a != null && (i = index) >= 0 && (index = hi) <= a.length) {
1473 <                    while (i < hi)
1474 <                        action.accept((E) a[i++]);
1475 <                    if (lst.modCount == expectedModCount)
1476 <                        return;
1477 <                }
1478 <            }
1479 <            throw new ConcurrentModificationException();
1465 >            final int hi = getFence();
1466 >            final Object[] a = array;
1467 >            int i;
1468 >            for (i = index, index = hi; i < hi; i++)
1469 >                action.accept((E) a[i]);
1470 >            if (modCount != expectedModCount)
1471 >                throw new ConcurrentModificationException();
1472          }
1473  
1474          public long estimateSize() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines