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.33 by jsr166, Sun Nov 13 02:10:09 2016 UTC vs.
Revision 1.38 by jsr166, Thu Dec 1 00:35:21 2016 UTC

# Line 585 | Line 585 | public class Vector<E>
585          modCount++;
586          elementCount--;
587          elementData[elementCount] = null; /* to let gc do its work */
588 +        // checkInvariants();
589      }
590  
591      /**
# Line 675 | Line 676 | public class Vector<E>
676       * method (which is part of the {@link List} interface).
677       */
678      public synchronized void removeAllElements() {
679 <        // Let gc do its work
679 <        for (int i = 0; i < elementCount; i++)
680 <            elementData[i] = null;
681 <
679 >        Arrays.fill(elementData, 0, elementCount, null);
680          modCount++;
681          elementCount = 0;
682      }
# Line 693 | Line 691 | public class Vector<E>
691      public synchronized Object clone() {
692          try {
693              @SuppressWarnings("unchecked")
694 <                Vector<E> v = (Vector<E>) super.clone();
694 >            Vector<E> v = (Vector<E>) super.clone();
695              v.elementData = Arrays.copyOf(elementData, elementCount);
696              v.modCount = 0;
697              return v;
# Line 810 | Line 808 | public class Vector<E>
808              elementData = grow();
809          elementData[s] = e;
810          elementCount = s + 1;
811 +        // checkInvariants();
812      }
813  
814      /**
# Line 878 | Line 877 | public class Vector<E>
877                               numMoved);
878          elementData[--elementCount] = null; // Let gc do its work
879  
880 +        // checkInvariants();
881          return oldValue;
882      }
883  
# Line 933 | Line 933 | public class Vector<E>
933                  elementData = grow(s + numNew);
934              System.arraycopy(a, 0, elementData, s, numNew);
935              elementCount = s + numNew;
936 +            // checkInvariants();
937              return true;
938          }
939      }
# Line 1005 | Line 1006 | public class Vector<E>
1006          int expectedModCount = modCount;
1007          final Object[] es = elementData;
1008          final int end = elementCount;
1008        final boolean modified;
1009          int i;
1010          // Optimize for initial run of survivors
1011          for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
# Line 1013 | Line 1013 | public class Vector<E>
1013          // Tolerate predicates that reentrantly access the collection for
1014          // read (but writers still get CME), so traverse once to find
1015          // elements to delete, a second pass to physically expunge.
1016 <        if (modified = (i < end)) {
1017 <            expectedModCount++;
1018 <            modCount++;
1016 >        if (i < end) {
1017              final int beg = i;
1018              final long[] deathRow = nBits(end - beg);
1019              deathRow[0] = 1L;   // set bit 0
1020              for (i = beg + 1; i < end; i++)
1021                  if (filter.test(elementAt(es, i)))
1022                      setBit(deathRow, i - beg);
1023 +            if (modCount != expectedModCount)
1024 +                throw new ConcurrentModificationException();
1025 +            expectedModCount++;
1026 +            modCount++;
1027              int w = beg;
1028              for (i = beg; i < end; i++)
1029                  if (isClear(deathRow, i - beg))
1030                      es[w++] = es[i];
1031              Arrays.fill(es, elementCount = w, end, null);
1032 +            // checkInvariants();
1033 +            return true;
1034 +        } else {
1035 +            if (modCount != expectedModCount)
1036 +                throw new ConcurrentModificationException();
1037 +            // checkInvariants();
1038 +            return false;
1039          }
1031        if (modCount != expectedModCount)
1032            throw new ConcurrentModificationException();
1033        return modified;
1040      }
1041  
1042      /**
# Line 1071 | Line 1077 | public class Vector<E>
1077                               numMoved);
1078          System.arraycopy(a, 0, elementData, index, numNew);
1079          elementCount = s + numNew;
1080 +        // checkInvariants();
1081          return true;
1082      }
1083  
# Line 1152 | Line 1159 | public class Vector<E>
1159       * (If {@code toIndex==fromIndex}, this operation has no effect.)
1160       */
1161      protected synchronized void removeRange(int fromIndex, int toIndex) {
1162 <        int numMoved = elementCount - toIndex;
1163 <        System.arraycopy(elementData, toIndex, elementData, fromIndex,
1164 <                         numMoved);
1165 <
1166 <        // Let gc do its work
1167 <        modCount++;
1168 <        int newElementCount = elementCount - (toIndex-fromIndex);
1162 <        while (elementCount != newElementCount)
1163 <            elementData[--elementCount] = null;
1162 >        final Object[] es = elementData;
1163 >        final int oldSize = elementCount;
1164 >        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
1165 >
1166 >        modCount++;
1167 >        Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null);
1168 >        // checkInvariants();
1169      }
1170  
1171      /**
# Line 1269 | Line 1274 | public class Vector<E>
1274                  if (i >= size) {
1275                      return;
1276                  }
1277 <        @SuppressWarnings("unchecked")
1278 <                final E[] elementData = (E[]) Vector.this.elementData;
1274 <                if (i >= elementData.length) {
1277 >                final Object[] es = elementData;
1278 >                if (i >= es.length)
1279                      throw new ConcurrentModificationException();
1280 <                }
1281 <                while (i != size && modCount == expectedModCount) {
1278 <                    action.accept(elementData[i++]);
1279 <                }
1280 >                while (i < size && modCount == expectedModCount)
1281 >                    action.accept(elementAt(es, i++));
1282                  // update once at end of iteration to reduce heap write traffic
1283                  cursor = i;
1284                  lastRet = i - 1;
# Line 1347 | Line 1349 | public class Vector<E>
1349      public synchronized void forEach(Consumer<? super E> action) {
1350          Objects.requireNonNull(action);
1351          final int expectedModCount = modCount;
1352 <        @SuppressWarnings("unchecked")
1353 <        final E[] elementData = (E[]) this.elementData;
1354 <        final int elementCount = this.elementCount;
1355 <        for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1356 <            action.accept(elementData[i]);
1355 <        }
1356 <        if (modCount != expectedModCount) {
1352 >        final Object[] es = elementData;
1353 >        final int size = elementCount;
1354 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1355 >            action.accept(elementAt(es, i));
1356 >        if (modCount != expectedModCount)
1357              throw new ConcurrentModificationException();
1358 <        }
1358 >        // checkInvariants();
1359      }
1360  
1361      @Override
1362    @SuppressWarnings("unchecked")
1362      public synchronized void replaceAll(UnaryOperator<E> operator) {
1363          Objects.requireNonNull(operator);
1364          final int expectedModCount = modCount;
1365 +        final Object[] es = elementData;
1366          final int size = elementCount;
1367 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1368 <            elementData[i] = operator.apply((E) elementData[i]);
1369 <        }
1370 <        if (modCount != expectedModCount) {
1367 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1368 >            es[i] = operator.apply(elementAt(es, i));
1369 >        if (modCount != expectedModCount)
1370              throw new ConcurrentModificationException();
1372        }
1371          modCount++;
1372 +        // checkInvariants();
1373      }
1374  
1375      @SuppressWarnings("unchecked")
# Line 1378 | Line 1377 | public class Vector<E>
1377      public synchronized void sort(Comparator<? super E> c) {
1378          final int expectedModCount = modCount;
1379          Arrays.sort((E[]) elementData, 0, elementCount, c);
1380 <        if (modCount != expectedModCount) {
1380 >        if (modCount != expectedModCount)
1381              throw new ConcurrentModificationException();
1383        }
1382          modCount++;
1383 +        // checkInvariants();
1384      }
1385  
1386      /**
# Line 1399 | Line 1398 | public class Vector<E>
1398       */
1399      @Override
1400      public Spliterator<E> spliterator() {
1401 <        return new VectorSpliterator<>(this, null, 0, -1, 0);
1401 >        return new VectorSpliterator(null, 0, -1, 0);
1402      }
1403  
1404      /** Similar to ArrayList Spliterator */
1405 <    static final class VectorSpliterator<E> implements Spliterator<E> {
1407 <        private final Vector<E> list;
1405 >    final class VectorSpliterator implements Spliterator<E> {
1406          private Object[] array;
1407          private int index; // current index, modified on advance/split
1408          private int fence; // -1 until used; then one past last index
1409          private int expectedModCount; // initialized when fence set
1410  
1411 <        /** Create new spliterator covering the given  range */
1412 <        VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1411 >        /** Create new spliterator covering the given range */
1412 >        VectorSpliterator(Object[] array, int origin, int fence,
1413                            int expectedModCount) {
1416            this.list = list;
1414              this.array = array;
1415              this.index = origin;
1416              this.fence = fence;
# Line 1423 | Line 1420 | public class Vector<E>
1420          private int getFence() { // initialize on first use
1421              int hi;
1422              if ((hi = fence) < 0) {
1423 <                synchronized (list) {
1424 <                    array = list.elementData;
1425 <                    expectedModCount = list.modCount;
1426 <                    hi = fence = list.elementCount;
1423 >                synchronized (Vector.this) {
1424 >                    array = elementData;
1425 >                    expectedModCount = modCount;
1426 >                    hi = fence = elementCount;
1427                  }
1428              }
1429              return hi;
# Line 1435 | Line 1432 | public class Vector<E>
1432          public Spliterator<E> trySplit() {
1433              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1434              return (lo >= mid) ? null :
1435 <                new VectorSpliterator<>(list, array, lo, index = mid,
1439 <                                        expectedModCount);
1435 >                new VectorSpliterator(array, lo, index = mid, expectedModCount);
1436          }
1437  
1438          @SuppressWarnings("unchecked")
# Line 1447 | Line 1443 | public class Vector<E>
1443              if (getFence() > (i = index)) {
1444                  index = i + 1;
1445                  action.accept((E)array[i]);
1446 <                if (list.modCount != expectedModCount)
1446 >                if (modCount != expectedModCount)
1447                      throw new ConcurrentModificationException();
1448                  return true;
1449              }
# Line 1456 | Line 1452 | public class Vector<E>
1452  
1453          @SuppressWarnings("unchecked")
1454          public void forEachRemaining(Consumer<? super E> action) {
1459            int i, hi; // hoist accesses and checks from loop
1460            Vector<E> lst; Object[] a;
1455              if (action == null)
1456                  throw new NullPointerException();
1457 <            if ((lst = list) != null) {
1458 <                if ((hi = fence) < 0) {
1459 <                    synchronized (lst) {
1460 <                        expectedModCount = lst.modCount;
1461 <                        a = array = lst.elementData;
1462 <                        hi = fence = lst.elementCount;
1463 <                    }
1470 <                }
1471 <                else
1472 <                    a = array;
1473 <                if (a != null && (i = index) >= 0 && (index = hi) <= a.length) {
1474 <                    while (i < hi)
1475 <                        action.accept((E) a[i++]);
1476 <                    if (lst.modCount == expectedModCount)
1477 <                        return;
1478 <                }
1479 <            }
1480 <            throw new ConcurrentModificationException();
1457 >            final int hi = getFence();
1458 >            final Object[] a = array;
1459 >            int i;
1460 >            for (i = index, index = hi; i < hi; i++)
1461 >                action.accept((E) a[i]);
1462 >            if (modCount != expectedModCount)
1463 >                throw new ConcurrentModificationException();
1464          }
1465  
1466          public long estimateSize() {
# Line 1488 | Line 1471 | public class Vector<E>
1471              return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1472          }
1473      }
1474 +
1475 +    void checkInvariants() {
1476 +        // assert elementCount >= 0;
1477 +        // assert elementCount == elementData.length || elementData[elementCount] == null;
1478 +    }
1479   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines