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.34 by jsr166, Sun Nov 13 19:58:47 2016 UTC

# 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 984 | Line 989 | public class Vector<E>
989          return bulkRemove(filter);
990      }
991  
992 <    @SuppressWarnings("unchecked")
992 >    // A tiny bit set implementation
993 >
994 >    private static long[] nBits(int n) {
995 >        return new long[((n - 1) >> 6) + 1];
996 >    }
997 >    private static void setBit(long[] bits, int i) {
998 >        bits[i >> 6] |= 1L << i;
999 >    }
1000 >    private static boolean isClear(long[] bits, int i) {
1001 >        return (bits[i >> 6] & (1L << i)) == 0;
1002 >    }
1003 >
1004      private synchronized boolean bulkRemove(Predicate<? super E> filter) {
1005          int expectedModCount = modCount;
1006          final Object[] es = elementData;
1007 <        final int size = elementCount;
1007 >        final int end = elementCount;
1008          final boolean modified;
1009 <        int r;
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 (modified = (i < end)) {
1017              expectedModCount++;
1018              modCount++;
1019 <            int w = r++;
1020 <            try {
1021 <                for (E e; r < size; r++)
1022 <                    if (!filter.test(e = (E) es[r]))
1023 <                        es[w++] = e;
1024 <            } catch (Throwable ex) {
1025 <                // copy remaining elements
1026 <                System.arraycopy(es, r, es, w, size - r);
1027 <                w += size - r;
1028 <                throw ex;
1029 <            } finally {
1012 <                Arrays.fill(es, elementCount = w, size, null);
1013 <            }
1019 >            final int beg = i;
1020 >            final long[] deathRow = nBits(end - beg);
1021 >            deathRow[0] = 1L;   // set bit 0
1022 >            for (i = beg + 1; i < end; i++)
1023 >                if (filter.test(elementAt(es, i)))
1024 >                    setBit(deathRow, i - beg);
1025 >            int w = beg;
1026 >            for (i = beg; i < end; i++)
1027 >                if (isClear(deathRow, i - beg))
1028 >                    es[w++] = es[i];
1029 >            Arrays.fill(es, elementCount = w, end, null);
1030          }
1031          if (modCount != expectedModCount)
1032              throw new ConcurrentModificationException();
# Line 1253 | Line 1269 | public class Vector<E>
1269                  if (i >= size) {
1270                      return;
1271                  }
1272 <        @SuppressWarnings("unchecked")
1273 <                final E[] elementData = (E[]) Vector.this.elementData;
1258 <                if (i >= elementData.length) {
1272 >                final Object[] es = elementData;
1273 >                if (i >= es.length)
1274                      throw new ConcurrentModificationException();
1275 <                }
1276 <                while (i != size && modCount == expectedModCount) {
1262 <                    action.accept(elementData[i++]);
1263 <                }
1275 >                while (i < size && modCount == expectedModCount)
1276 >                    action.accept(elementAt(es, i++));
1277                  // update once at end of iteration to reduce heap write traffic
1278                  cursor = i;
1279                  lastRet = i - 1;
# Line 1331 | Line 1344 | public class Vector<E>
1344      public synchronized void forEach(Consumer<? super E> action) {
1345          Objects.requireNonNull(action);
1346          final int expectedModCount = modCount;
1347 <        @SuppressWarnings("unchecked")
1348 <        final E[] elementData = (E[]) this.elementData;
1349 <        final int elementCount = this.elementCount;
1350 <        for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1351 <            action.accept(elementData[i]);
1339 <        }
1340 <        if (modCount != expectedModCount) {
1347 >        final Object[] es = elementData;
1348 >        final int size = elementCount;
1349 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1350 >            action.accept(elementAt(es, i));
1351 >        if (modCount != expectedModCount)
1352              throw new ConcurrentModificationException();
1342        }
1353      }
1354  
1355      @Override
1346    @SuppressWarnings("unchecked")
1356      public synchronized void replaceAll(UnaryOperator<E> operator) {
1357          Objects.requireNonNull(operator);
1358          final int expectedModCount = modCount;
1359 +        final Object[] es = elementData;
1360          final int size = elementCount;
1361 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1362 <            elementData[i] = operator.apply((E) elementData[i]);
1363 <        }
1354 <        if (modCount != expectedModCount) {
1361 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1362 >            es[i] = operator.apply(elementAt(es, i));
1363 >        if (modCount != expectedModCount)
1364              throw new ConcurrentModificationException();
1356        }
1365          modCount++;
1366      }
1367  
# Line 1394 | Line 1402 | public class Vector<E>
1402          private int fence; // -1 until used; then one past last index
1403          private int expectedModCount; // initialized when fence set
1404  
1405 <        /** Create new spliterator covering the given  range */
1405 >        /** Create new spliterator covering the given range */
1406          VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1407                            int expectedModCount) {
1408              this.list = list;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines