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.45 by jsr166, Wed Feb 1 20:13:47 2017 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;
309 >        final Object[] es = elementData;
310 >        for (int to = elementCount, i = newSize; i < to; i++)
311 >            es[i] = null;
312          elementCount = newSize;
313      }
314  
# Line 585 | Line 586 | public class Vector<E>
586          modCount++;
587          elementCount--;
588          elementData[elementCount] = null; /* to let gc do its work */
589 +        // checkInvariants();
590      }
591  
592      /**
# Line 675 | Line 677 | public class Vector<E>
677       * method (which is part of the {@link List} interface).
678       */
679      public synchronized void removeAllElements() {
680 <        // Let gc do its work
681 <        for (int i = 0; i < elementCount; i++)
682 <            elementData[i] = null;
681 <
680 >        final Object[] es = elementData;
681 >        for (int to = elementCount, i = elementCount = 0; i < to; i++)
682 >            es[i] = null;
683          modCount++;
683        elementCount = 0;
684      }
685  
686      /**
# Line 693 | Line 693 | public class Vector<E>
693      public synchronized Object clone() {
694          try {
695              @SuppressWarnings("unchecked")
696 <                Vector<E> v = (Vector<E>) super.clone();
696 >            Vector<E> v = (Vector<E>) super.clone();
697              v.elementData = Arrays.copyOf(elementData, elementCount);
698              v.modCount = 0;
699              return v;
# Line 759 | Line 759 | public class Vector<E>
759          return (E) elementData[index];
760      }
761  
762 +    @SuppressWarnings("unchecked")
763 +    static <E> E elementAt(Object[] es, int index) {
764 +        return (E) es[index];
765 +    }
766 +
767      /**
768       * Returns the element at the specified position in this Vector.
769       *
# Line 805 | Line 810 | public class Vector<E>
810              elementData = grow();
811          elementData[s] = e;
812          elementCount = s + 1;
813 +        // checkInvariants();
814      }
815  
816      /**
# Line 873 | Line 879 | public class Vector<E>
879                               numMoved);
880          elementData[--elementCount] = null; // Let gc do its work
881  
882 +        // checkInvariants();
883          return oldValue;
884      }
885  
# Line 928 | Line 935 | public class Vector<E>
935                  elementData = grow(s + numNew);
936              System.arraycopy(a, 0, elementData, s, numNew);
937              elementCount = s + numNew;
938 +            // checkInvariants();
939              return true;
940          }
941      }
# Line 978 | Line 986 | public class Vector<E>
986          return bulkRemove(e -> !c.contains(e));
987      }
988  
989 +    /**
990 +     * @throws NullPointerException {@inheritDoc}
991 +     */
992      @Override
993      public boolean removeIf(Predicate<? super E> filter) {
994          Objects.requireNonNull(filter);
995          return bulkRemove(filter);
996      }
997  
998 <    @SuppressWarnings("unchecked")
998 >    // A tiny bit set implementation
999 >
1000 >    private static long[] nBits(int n) {
1001 >        return new long[((n - 1) >> 6) + 1];
1002 >    }
1003 >    private static void setBit(long[] bits, int i) {
1004 >        bits[i >> 6] |= 1L << i;
1005 >    }
1006 >    private static boolean isClear(long[] bits, int i) {
1007 >        return (bits[i >> 6] & (1L << i)) == 0;
1008 >    }
1009 >
1010      private synchronized boolean bulkRemove(Predicate<? super E> filter) {
1011          int expectedModCount = modCount;
1012          final Object[] es = elementData;
1013 <        final int size = elementCount;
1014 <        final boolean modified;
993 <        int r;
1013 >        final int end = elementCount;
1014 >        int i;
1015          // Optimize for initial run of survivors
1016 <        for (r = 0; r < size; r++)
1017 <            if (filter.test((E) es[r]))
1018 <                break;
1019 <        if (modified = (r < size)) {
1016 >        for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
1017 >            ;
1018 >        // Tolerate predicates that reentrantly access the collection for
1019 >        // read (but writers still get CME), so traverse once to find
1020 >        // elements to delete, a second pass to physically expunge.
1021 >        if (i < end) {
1022 >            final int beg = i;
1023 >            final long[] deathRow = nBits(end - beg);
1024 >            deathRow[0] = 1L;   // set bit 0
1025 >            for (i = beg + 1; i < end; i++)
1026 >                if (filter.test(elementAt(es, i)))
1027 >                    setBit(deathRow, i - beg);
1028 >            if (modCount != expectedModCount)
1029 >                throw new ConcurrentModificationException();
1030              expectedModCount++;
1031              modCount++;
1032 <            int w = r++;
1033 <            try {
1034 <                for (E e; r < size; r++)
1035 <                    if (!filter.test(e = (E) es[r]))
1036 <                        es[w++] = e;
1037 <            } catch (Throwable ex) {
1038 <                // copy remaining elements
1039 <                System.arraycopy(es, r, es, w, size - r);
1040 <                w += size - r;
1041 <                throw ex;
1042 <            } finally {
1043 <                Arrays.fill(es, elementCount = w, size, null);
1044 <            }
1032 >            int w = beg;
1033 >            for (i = beg; i < end; i++)
1034 >                if (isClear(deathRow, i - beg))
1035 >                    es[w++] = es[i];
1036 >            for (i = elementCount = w; i < end; i++)
1037 >                es[i] = null;
1038 >            // checkInvariants();
1039 >            return true;
1040 >        } else {
1041 >            if (modCount != expectedModCount)
1042 >                throw new ConcurrentModificationException();
1043 >            // checkInvariants();
1044 >            return false;
1045          }
1015        if (modCount != expectedModCount)
1016            throw new ConcurrentModificationException();
1017        return modified;
1046      }
1047  
1048      /**
# Line 1055 | Line 1083 | public class Vector<E>
1083                               numMoved);
1084          System.arraycopy(a, 0, elementData, index, numNew);
1085          elementCount = s + numNew;
1086 +        // checkInvariants();
1087          return true;
1088      }
1089  
# Line 1136 | Line 1165 | public class Vector<E>
1165       * (If {@code toIndex==fromIndex}, this operation has no effect.)
1166       */
1167      protected synchronized void removeRange(int fromIndex, int toIndex) {
1139        int numMoved = elementCount - toIndex;
1140        System.arraycopy(elementData, toIndex, elementData, fromIndex,
1141                         numMoved);
1142
1143        // Let gc do its work
1168          modCount++;
1169 <        int newElementCount = elementCount - (toIndex-fromIndex);
1170 <        while (elementCount != newElementCount)
1171 <            elementData[--elementCount] = null;
1169 >        shiftTailOverGap(elementData, fromIndex, toIndex);
1170 >        // checkInvariants();
1171 >    }
1172 >
1173 >    /** Erases the gap from lo to hi, by sliding down following elements. */
1174 >    private void shiftTailOverGap(Object[] es, int lo, int hi) {
1175 >        System.arraycopy(es, hi, es, lo, elementCount - hi);
1176 >        for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++)
1177 >            es[i] = null;
1178      }
1179  
1180      /**
1181 <     * Save the state of the {@code Vector} instance to a stream (that
1182 <     * is, serialize it).
1181 >     * Saves the state of the {@code Vector} instance to a stream
1182 >     * (that is, serializes it).
1183       * This method performs synchronization to ensure the consistency
1184       * of the serialized data.
1185 +     *
1186 +     * @param s the stream
1187 +     * @throws java.io.IOException if an I/O error occurs
1188       */
1189      private void writeObject(java.io.ObjectOutputStream s)
1190              throws java.io.IOException {
# Line 1253 | Line 1286 | public class Vector<E>
1286                  if (i >= size) {
1287                      return;
1288                  }
1289 <        @SuppressWarnings("unchecked")
1290 <                final E[] elementData = (E[]) Vector.this.elementData;
1258 <                if (i >= elementData.length) {
1289 >                final Object[] es = elementData;
1290 >                if (i >= es.length)
1291                      throw new ConcurrentModificationException();
1292 <                }
1293 <                while (i != size && modCount == expectedModCount) {
1262 <                    action.accept(elementData[i++]);
1263 <                }
1292 >                while (i < size && modCount == expectedModCount)
1293 >                    action.accept(elementAt(es, i++));
1294                  // update once at end of iteration to reduce heap write traffic
1295                  cursor = i;
1296                  lastRet = i - 1;
# Line 1327 | Line 1357 | public class Vector<E>
1357          }
1358      }
1359  
1360 +    /**
1361 +     * @throws NullPointerException {@inheritDoc}
1362 +     */
1363      @Override
1364      public synchronized void forEach(Consumer<? super E> action) {
1365          Objects.requireNonNull(action);
1366          final int expectedModCount = modCount;
1367 <        @SuppressWarnings("unchecked")
1368 <        final E[] elementData = (E[]) this.elementData;
1369 <        final int elementCount = this.elementCount;
1370 <        for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1371 <            action.accept(elementData[i]);
1339 <        }
1340 <        if (modCount != expectedModCount) {
1367 >        final Object[] es = elementData;
1368 >        final int size = elementCount;
1369 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1370 >            action.accept(elementAt(es, i));
1371 >        if (modCount != expectedModCount)
1372              throw new ConcurrentModificationException();
1373 <        }
1373 >        // checkInvariants();
1374      }
1375  
1376 +    /**
1377 +     * @throws NullPointerException {@inheritDoc}
1378 +     */
1379      @Override
1346    @SuppressWarnings("unchecked")
1380      public synchronized void replaceAll(UnaryOperator<E> operator) {
1381          Objects.requireNonNull(operator);
1382          final int expectedModCount = modCount;
1383 +        final Object[] es = elementData;
1384          final int size = elementCount;
1385 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1386 <            elementData[i] = operator.apply((E) elementData[i]);
1387 <        }
1354 <        if (modCount != expectedModCount) {
1385 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1386 >            es[i] = operator.apply(elementAt(es, i));
1387 >        if (modCount != expectedModCount)
1388              throw new ConcurrentModificationException();
1356        }
1389          modCount++;
1390 +        // checkInvariants();
1391      }
1392  
1393      @SuppressWarnings("unchecked")
# Line 1362 | Line 1395 | public class Vector<E>
1395      public synchronized void sort(Comparator<? super E> c) {
1396          final int expectedModCount = modCount;
1397          Arrays.sort((E[]) elementData, 0, elementCount, c);
1398 <        if (modCount != expectedModCount) {
1398 >        if (modCount != expectedModCount)
1399              throw new ConcurrentModificationException();
1367        }
1400          modCount++;
1401 +        // checkInvariants();
1402      }
1403  
1404      /**
# Line 1383 | Line 1416 | public class Vector<E>
1416       */
1417      @Override
1418      public Spliterator<E> spliterator() {
1419 <        return new VectorSpliterator<>(this, null, 0, -1, 0);
1419 >        return new VectorSpliterator(null, 0, -1, 0);
1420      }
1421  
1422      /** Similar to ArrayList Spliterator */
1423 <    static final class VectorSpliterator<E> implements Spliterator<E> {
1391 <        private final Vector<E> list;
1423 >    final class VectorSpliterator implements Spliterator<E> {
1424          private Object[] array;
1425          private int index; // current index, modified on advance/split
1426          private int fence; // -1 until used; then one past last index
1427          private int expectedModCount; // initialized when fence set
1428  
1429 <        /** Create new spliterator covering the given  range */
1430 <        VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1429 >        /** Creates new spliterator covering the given range. */
1430 >        VectorSpliterator(Object[] array, int origin, int fence,
1431                            int expectedModCount) {
1400            this.list = list;
1432              this.array = array;
1433              this.index = origin;
1434              this.fence = fence;
# Line 1407 | Line 1438 | public class Vector<E>
1438          private int getFence() { // initialize on first use
1439              int hi;
1440              if ((hi = fence) < 0) {
1441 <                synchronized (list) {
1442 <                    array = list.elementData;
1443 <                    expectedModCount = list.modCount;
1444 <                    hi = fence = list.elementCount;
1441 >                synchronized (Vector.this) {
1442 >                    array = elementData;
1443 >                    expectedModCount = modCount;
1444 >                    hi = fence = elementCount;
1445                  }
1446              }
1447              return hi;
# Line 1419 | Line 1450 | public class Vector<E>
1450          public Spliterator<E> trySplit() {
1451              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1452              return (lo >= mid) ? null :
1453 <                new VectorSpliterator<>(list, array, lo, index = mid,
1423 <                                        expectedModCount);
1453 >                new VectorSpliterator(array, lo, index = mid, expectedModCount);
1454          }
1455  
1456          @SuppressWarnings("unchecked")
1457          public boolean tryAdvance(Consumer<? super E> action) {
1458 +            Objects.requireNonNull(action);
1459              int i;
1429            if (action == null)
1430                throw new NullPointerException();
1460              if (getFence() > (i = index)) {
1461                  index = i + 1;
1462                  action.accept((E)array[i]);
1463 <                if (list.modCount != expectedModCount)
1463 >                if (modCount != expectedModCount)
1464                      throw new ConcurrentModificationException();
1465                  return true;
1466              }
# Line 1440 | Line 1469 | public class Vector<E>
1469  
1470          @SuppressWarnings("unchecked")
1471          public void forEachRemaining(Consumer<? super E> action) {
1472 <            int i, hi; // hoist accesses and checks from loop
1473 <            Vector<E> lst; Object[] a;
1474 <            if (action == null)
1475 <                throw new NullPointerException();
1476 <            if ((lst = list) != null) {
1477 <                if ((hi = fence) < 0) {
1478 <                    synchronized (lst) {
1479 <                        expectedModCount = lst.modCount;
1451 <                        a = array = lst.elementData;
1452 <                        hi = fence = lst.elementCount;
1453 <                    }
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();
1472 >            Objects.requireNonNull(action);
1473 >            final int hi = getFence();
1474 >            final Object[] a = array;
1475 >            int i;
1476 >            for (i = index, index = hi; i < hi; i++)
1477 >                action.accept((E) a[i]);
1478 >            if (modCount != expectedModCount)
1479 >                throw new ConcurrentModificationException();
1480          }
1481  
1482          public long estimateSize() {
# Line 1472 | Line 1487 | public class Vector<E>
1487              return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1488          }
1489      }
1490 +
1491 +    void checkInvariants() {
1492 +        // assert elementCount >= 0;
1493 +        // assert elementCount == elementData.length || elementData[elementCount] == null;
1494 +    }
1495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines