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.35 by jsr166, Sun Nov 13 21:07:40 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 1030 | Line 1031 | public class Vector<E>
1031          }
1032          if (modCount != expectedModCount)
1033              throw new ConcurrentModificationException();
1034 +        // checkInvariants();
1035          return modified;
1036      }
1037  
# Line 1071 | Line 1073 | public class Vector<E>
1073                               numMoved);
1074          System.arraycopy(a, 0, elementData, index, numNew);
1075          elementCount = s + numNew;
1076 +        // checkInvariants();
1077          return true;
1078      }
1079  
# Line 1152 | Line 1155 | public class Vector<E>
1155       * (If {@code toIndex==fromIndex}, this operation has no effect.)
1156       */
1157      protected synchronized void removeRange(int fromIndex, int toIndex) {
1158 <        int numMoved = elementCount - toIndex;
1159 <        System.arraycopy(elementData, toIndex, elementData, fromIndex,
1160 <                         numMoved);
1161 <
1162 <        // Let gc do its work
1163 <        modCount++;
1164 <        int newElementCount = elementCount - (toIndex-fromIndex);
1162 <        while (elementCount != newElementCount)
1163 <            elementData[--elementCount] = null;
1158 >        final Object[] es = elementData;
1159 >        final int oldSize = elementCount;
1160 >        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
1161 >
1162 >        modCount++;
1163 >        Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null);
1164 >        // checkInvariants();
1165      }
1166  
1167      /**
# Line 1269 | Line 1270 | public class Vector<E>
1270                  if (i >= size) {
1271                      return;
1272                  }
1273 <        @SuppressWarnings("unchecked")
1274 <                final E[] elementData = (E[]) Vector.this.elementData;
1274 <                if (i >= elementData.length) {
1273 >                final Object[] es = elementData;
1274 >                if (i >= es.length)
1275                      throw new ConcurrentModificationException();
1276 <                }
1277 <                while (i != size && modCount == expectedModCount) {
1278 <                    action.accept(elementData[i++]);
1279 <                }
1276 >                while (i < size && modCount == expectedModCount)
1277 >                    action.accept(elementAt(es, i++));
1278                  // update once at end of iteration to reduce heap write traffic
1279                  cursor = i;
1280                  lastRet = i - 1;
# Line 1347 | Line 1345 | public class Vector<E>
1345      public synchronized void forEach(Consumer<? super E> action) {
1346          Objects.requireNonNull(action);
1347          final int expectedModCount = modCount;
1348 <        @SuppressWarnings("unchecked")
1349 <        final E[] elementData = (E[]) this.elementData;
1350 <        final int elementCount = this.elementCount;
1351 <        for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1352 <            action.accept(elementData[i]);
1355 <        }
1356 <        if (modCount != expectedModCount) {
1348 >        final Object[] es = elementData;
1349 >        final int size = elementCount;
1350 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1351 >            action.accept(elementAt(es, i));
1352 >        if (modCount != expectedModCount)
1353              throw new ConcurrentModificationException();
1354 <        }
1354 >        // checkInvariants();
1355      }
1356  
1357      @Override
1362    @SuppressWarnings("unchecked")
1358      public synchronized void replaceAll(UnaryOperator<E> operator) {
1359          Objects.requireNonNull(operator);
1360          final int expectedModCount = modCount;
1361 +        final Object[] es = elementData;
1362          final int size = elementCount;
1363 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1364 <            elementData[i] = operator.apply((E) elementData[i]);
1365 <        }
1370 <        if (modCount != expectedModCount) {
1363 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1364 >            es[i] = operator.apply(elementAt(es, i));
1365 >        if (modCount != expectedModCount)
1366              throw new ConcurrentModificationException();
1372        }
1367          modCount++;
1368 +        // checkInvariants();
1369      }
1370  
1371      @SuppressWarnings("unchecked")
# Line 1378 | Line 1373 | public class Vector<E>
1373      public synchronized void sort(Comparator<? super E> c) {
1374          final int expectedModCount = modCount;
1375          Arrays.sort((E[]) elementData, 0, elementCount, c);
1376 <        if (modCount != expectedModCount) {
1376 >        if (modCount != expectedModCount)
1377              throw new ConcurrentModificationException();
1383        }
1378          modCount++;
1379 +        // checkInvariants();
1380      }
1381  
1382      /**
# Line 1410 | Line 1405 | public class Vector<E>
1405          private int fence; // -1 until used; then one past last index
1406          private int expectedModCount; // initialized when fence set
1407  
1408 <        /** Create new spliterator covering the given  range */
1408 >        /** Create new spliterator covering the given range */
1409          VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1410                            int expectedModCount) {
1411              this.list = list;
# Line 1488 | Line 1483 | public class Vector<E>
1483              return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1484          }
1485      }
1486 +
1487 +    void checkInvariants() {
1488 +        // assert elementCount >= 0;
1489 +        // assert elementCount == elementData.length || elementData[elementCount] == null;
1490 +    }
1491   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines