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.31 by jsr166, Fri Nov 4 02:56:17 2016 UTC vs.
Revision 1.39 by jsr166, Fri Dec 2 06:38:56 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 759 | Line 757 | public class Vector<E>
757          return (E) elementData[index];
758      }
759  
760 +    @SuppressWarnings("unchecked")
761 +    static <E> E elementAt(Object[] es, int index) {
762 +        return (E) es[index];
763 +    }
764 +
765      /**
766       * Returns the element at the specified position in this Vector.
767       *
# Line 805 | Line 808 | public class Vector<E>
808              elementData = grow();
809          elementData[s] = e;
810          elementCount = s + 1;
811 +        // checkInvariants();
812      }
813  
814      /**
# Line 873 | 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 928 | 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 984 | Line 990 | public class Vector<E>
990          return bulkRemove(filter);
991      }
992  
993 <    @SuppressWarnings("unchecked")
993 >    // A tiny bit set implementation
994 >
995 >    private static long[] nBits(int n) {
996 >        return new long[((n - 1) >> 6) + 1];
997 >    }
998 >    private static void setBit(long[] bits, int i) {
999 >        bits[i >> 6] |= 1L << i;
1000 >    }
1001 >    private static boolean isClear(long[] bits, int i) {
1002 >        return (bits[i >> 6] & (1L << i)) == 0;
1003 >    }
1004 >
1005      private synchronized boolean bulkRemove(Predicate<? super E> filter) {
1006          int expectedModCount = modCount;
1007          final Object[] es = elementData;
1008 <        final int size = elementCount;
1009 <        final boolean modified;
993 <        int r;
1008 >        final int end = elementCount;
1009 >        int i;
1010          // Optimize for initial run of survivors
1011 <        for (r = 0; r < size; r++)
1012 <            if (filter.test((E) es[r]))
1013 <                break;
1014 <        if (modified = (r < size)) {
1011 >        for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
1012 >            ;
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 (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 = r++;
1028 <            try {
1029 <                for (E e; r < size; r++)
1030 <                    if (!filter.test(e = (E) es[r]))
1031 <                        es[w++] = e;
1032 <            } catch (Throwable ex) {
1033 <                // copy remaining elements
1034 <                System.arraycopy(es, r, es, w, size - r);
1035 <                w += size - r;
1036 <                throw ex;
1037 <            } finally {
1038 <                Arrays.fill(es, elementCount = w, size, null);
1013 <            }
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          }
1015        if (modCount != expectedModCount)
1016            throw new ConcurrentModificationException();
1017        return modified;
1040      }
1041  
1042      /**
# Line 1055 | 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 1136 | 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);
1162 >        final Object[] es = elementData;
1163 >        final int oldSize = elementCount;
1164 >        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
1165  
1143        // Let gc do its work
1166          modCount++;
1167 <        int newElementCount = elementCount - (toIndex-fromIndex);
1168 <        while (elementCount != newElementCount)
1147 <            elementData[--elementCount] = null;
1167 >        Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null);
1168 >        // checkInvariants();
1169      }
1170  
1171      /**
1172 <     * Save the state of the {@code Vector} instance to a stream (that
1173 <     * is, serialize it).
1172 >     * Saves the state of the {@code Vector} instance to a stream
1173 >     * (that is, serializes it).
1174       * This method performs synchronization to ensure the consistency
1175       * of the serialized data.
1176 +     *
1177 +     * @param s the stream
1178 +     * @throws java.io.IOException if an I/O error occurs
1179       */
1180      private void writeObject(java.io.ObjectOutputStream s)
1181              throws java.io.IOException {
# Line 1253 | Line 1277 | public class Vector<E>
1277                  if (i >= size) {
1278                      return;
1279                  }
1280 <        @SuppressWarnings("unchecked")
1281 <                final E[] elementData = (E[]) Vector.this.elementData;
1258 <                if (i >= elementData.length) {
1280 >                final Object[] es = elementData;
1281 >                if (i >= es.length)
1282                      throw new ConcurrentModificationException();
1283 <                }
1284 <                while (i != size && modCount == expectedModCount) {
1262 <                    action.accept(elementData[i++]);
1263 <                }
1283 >                while (i < size && modCount == expectedModCount)
1284 >                    action.accept(elementAt(es, i++));
1285                  // update once at end of iteration to reduce heap write traffic
1286                  cursor = i;
1287                  lastRet = i - 1;
# Line 1331 | Line 1352 | public class Vector<E>
1352      public synchronized void forEach(Consumer<? super E> action) {
1353          Objects.requireNonNull(action);
1354          final int expectedModCount = modCount;
1355 <        @SuppressWarnings("unchecked")
1356 <        final E[] elementData = (E[]) this.elementData;
1357 <        final int elementCount = this.elementCount;
1358 <        for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1359 <            action.accept(elementData[i]);
1339 <        }
1340 <        if (modCount != expectedModCount) {
1355 >        final Object[] es = elementData;
1356 >        final int size = elementCount;
1357 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1358 >            action.accept(elementAt(es, i));
1359 >        if (modCount != expectedModCount)
1360              throw new ConcurrentModificationException();
1361 <        }
1361 >        // checkInvariants();
1362      }
1363  
1364      @Override
1346    @SuppressWarnings("unchecked")
1365      public synchronized void replaceAll(UnaryOperator<E> operator) {
1366          Objects.requireNonNull(operator);
1367          final int expectedModCount = modCount;
1368 +        final Object[] es = elementData;
1369          final int size = elementCount;
1370 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1371 <            elementData[i] = operator.apply((E) elementData[i]);
1372 <        }
1354 <        if (modCount != expectedModCount) {
1370 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1371 >            es[i] = operator.apply(elementAt(es, i));
1372 >        if (modCount != expectedModCount)
1373              throw new ConcurrentModificationException();
1356        }
1374          modCount++;
1375 +        // checkInvariants();
1376      }
1377  
1378      @SuppressWarnings("unchecked")
# Line 1362 | Line 1380 | public class Vector<E>
1380      public synchronized void sort(Comparator<? super E> c) {
1381          final int expectedModCount = modCount;
1382          Arrays.sort((E[]) elementData, 0, elementCount, c);
1383 <        if (modCount != expectedModCount) {
1383 >        if (modCount != expectedModCount)
1384              throw new ConcurrentModificationException();
1367        }
1385          modCount++;
1386 +        // checkInvariants();
1387      }
1388  
1389      /**
# Line 1383 | Line 1401 | public class Vector<E>
1401       */
1402      @Override
1403      public Spliterator<E> spliterator() {
1404 <        return new VectorSpliterator<>(this, null, 0, -1, 0);
1404 >        return new VectorSpliterator(null, 0, -1, 0);
1405      }
1406  
1407      /** Similar to ArrayList Spliterator */
1408 <    static final class VectorSpliterator<E> implements Spliterator<E> {
1391 <        private final Vector<E> list;
1408 >    final class VectorSpliterator implements Spliterator<E> {
1409          private Object[] array;
1410          private int index; // current index, modified on advance/split
1411          private int fence; // -1 until used; then one past last index
1412          private int expectedModCount; // initialized when fence set
1413  
1414 <        /** Create new spliterator covering the given  range */
1415 <        VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1414 >        /** Create new spliterator covering the given range */
1415 >        VectorSpliterator(Object[] array, int origin, int fence,
1416                            int expectedModCount) {
1400            this.list = list;
1417              this.array = array;
1418              this.index = origin;
1419              this.fence = fence;
# Line 1407 | Line 1423 | public class Vector<E>
1423          private int getFence() { // initialize on first use
1424              int hi;
1425              if ((hi = fence) < 0) {
1426 <                synchronized (list) {
1427 <                    array = list.elementData;
1428 <                    expectedModCount = list.modCount;
1429 <                    hi = fence = list.elementCount;
1426 >                synchronized (Vector.this) {
1427 >                    array = elementData;
1428 >                    expectedModCount = modCount;
1429 >                    hi = fence = elementCount;
1430                  }
1431              }
1432              return hi;
# Line 1419 | Line 1435 | public class Vector<E>
1435          public Spliterator<E> trySplit() {
1436              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1437              return (lo >= mid) ? null :
1438 <                new VectorSpliterator<>(list, array, lo, index = mid,
1423 <                                        expectedModCount);
1438 >                new VectorSpliterator(array, lo, index = mid, expectedModCount);
1439          }
1440  
1441          @SuppressWarnings("unchecked")
# Line 1431 | Line 1446 | public class Vector<E>
1446              if (getFence() > (i = index)) {
1447                  index = i + 1;
1448                  action.accept((E)array[i]);
1449 <                if (list.modCount != expectedModCount)
1449 >                if (modCount != expectedModCount)
1450                      throw new ConcurrentModificationException();
1451                  return true;
1452              }
# Line 1440 | Line 1455 | public class Vector<E>
1455  
1456          @SuppressWarnings("unchecked")
1457          public void forEachRemaining(Consumer<? super E> action) {
1443            int i, hi; // hoist accesses and checks from loop
1444            Vector<E> lst; Object[] a;
1458              if (action == null)
1459                  throw new NullPointerException();
1460 <            if ((lst = list) != null) {
1461 <                if ((hi = fence) < 0) {
1462 <                    synchronized (lst) {
1463 <                        expectedModCount = lst.modCount;
1464 <                        a = array = lst.elementData;
1465 <                        hi = fence = lst.elementCount;
1466 <                    }
1454 <                }
1455 <                else
1456 <                    a = array;
1457 <                if (a != null && (i = index) >= 0 && (index = hi) <= a.length) {
1458 <                    while (i < hi)
1459 <                        action.accept((E) a[i++]);
1460 <                    if (lst.modCount == expectedModCount)
1461 <                        return;
1462 <                }
1463 <            }
1464 <            throw new ConcurrentModificationException();
1460 >            final int hi = getFence();
1461 >            final Object[] a = array;
1462 >            int i;
1463 >            for (i = index, index = hi; i < hi; i++)
1464 >                action.accept((E) a[i]);
1465 >            if (modCount != expectedModCount)
1466 >                throw new ConcurrentModificationException();
1467          }
1468  
1469          public long estimateSize() {
# Line 1472 | Line 1474 | public class Vector<E>
1474              return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1475          }
1476      }
1477 +
1478 +    void checkInvariants() {
1479 +        // assert elementCount >= 0;
1480 +        // assert elementCount == elementData.length || elementData[elementCount] == null;
1481 +    }
1482   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines