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.32 by jsr166, Sat Nov 12 20:51:59 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 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;
1008 >        final int end = elementCount;
1009          final boolean modified;
1010 <        int r;
1010 >        int i;
1011          // Optimize for initial run of survivors
1012 <        for (r = 0; r < size && !filter.test((E) es[r]); r++)
1012 >        for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
1013              ;
1014 <        if (modified = (r < size)) {
1014 >        // Tolerate predicates that reentrantly access the collection for
1015 >        // read (but writers still get CME), so traverse once to find
1016 >        // elements to delete, a second pass to physically expunge.
1017 >        if (modified = (i < end)) {
1018              expectedModCount++;
1019              modCount++;
1020 <            int w = r++;
1021 <            try {
1022 <                for (E e; r < size; r++)
1023 <                    if (!filter.test(e = (E) es[r]))
1024 <                        es[w++] = e;
1025 <            } catch (Throwable ex) {
1026 <                // copy remaining elements
1027 <                System.arraycopy(es, r, es, w, size - r);
1028 <                w += size - r;
1029 <                throw ex;
1030 <            } finally {
1011 <                Arrays.fill(es, elementCount = w, size, null);
1012 <            }
1020 >            final int beg = i;
1021 >            final long[] deathRow = nBits(end - beg);
1022 >            deathRow[0] = 1L;   // set bit 0
1023 >            for (i = beg + 1; i < end; i++)
1024 >                if (filter.test(elementAt(es, i)))
1025 >                    setBit(deathRow, i - beg);
1026 >            int w = beg;
1027 >            for (i = beg; i < end; i++)
1028 >                if (isClear(deathRow, i - beg))
1029 >                    es[w++] = es[i];
1030 >            Arrays.fill(es, elementCount = w, end, null);
1031          }
1032          if (modCount != expectedModCount)
1033              throw new ConcurrentModificationException();
1034 +        // checkInvariants();
1035          return modified;
1036      }
1037  
# Line 1054 | 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 1135 | 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);
1145 <        while (elementCount != newElementCount)
1146 <            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 1252 | Line 1270 | public class Vector<E>
1270                  if (i >= size) {
1271                      return;
1272                  }
1273 <        @SuppressWarnings("unchecked")
1274 <                final E[] elementData = (E[]) Vector.this.elementData;
1257 <                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) {
1261 <                    action.accept(elementData[i++]);
1262 <                }
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 1330 | 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]);
1338 <        }
1339 <        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
1345    @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 <        }
1353 <        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();
1355        }
1367          modCount++;
1368 +        // checkInvariants();
1369      }
1370  
1371      @SuppressWarnings("unchecked")
# Line 1361 | 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();
1366        }
1378          modCount++;
1379 +        // checkInvariants();
1380      }
1381  
1382      /**
# Line 1393 | 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 1471 | 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