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.30 by jsr166, Thu Nov 3 20:49:07 2016 UTC vs.
Revision 1.36 by jsr166, Mon Nov 14 22:46:22 2016 UTC

# Line 513 | Line 513 | public class Vector<E>
513       * Returns the last component of the vector.
514       *
515       * @return  the last component of the vector, i.e., the component at index
516 <     *          <code>size()&nbsp;-&nbsp;1</code>.
516 >     *          {@code size() - 1}
517       * @throws NoSuchElementException if this vector is empty
518       */
519      public synchronized E lastElement() {
# 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 855 | Line 859 | public class Vector<E>
859       * Shifts any subsequent elements to the left (subtracts one from their
860       * indices).  Returns the element that was removed from the Vector.
861       *
858     * @throws ArrayIndexOutOfBoundsException if the index is out of range
859     *         ({@code index < 0 || index >= size()})
862       * @param index the index of the element to be removed
863       * @return element that was removed
864 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
865 +     *         ({@code index < 0 || index >= size()})
866       * @since 1.2
867       */
868      public synchronized E remove(int index) {
# 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 949 | Line 955 | public class Vector<E>
955       *         or if the specified collection is null
956       * @since 1.2
957       */
958 <    public synchronized boolean removeAll(Collection<?> c) {
959 <        return super.removeAll(c);
958 >    public boolean removeAll(Collection<?> c) {
959 >        Objects.requireNonNull(c);
960 >        return bulkRemove(e -> c.contains(e));
961      }
962  
963      /**
# Line 972 | Line 979 | public class Vector<E>
979       *         or if the specified collection is null
980       * @since 1.2
981       */
982 <    public synchronized boolean retainAll(Collection<?> c) {
983 <        return super.retainAll(c);
982 >    public boolean retainAll(Collection<?> c) {
983 >        Objects.requireNonNull(c);
984 >        return bulkRemove(e -> !c.contains(e));
985 >    }
986 >
987 >    @Override
988 >    public boolean removeIf(Predicate<? super E> filter) {
989 >        Objects.requireNonNull(filter);
990 >        return bulkRemove(filter);
991 >    }
992 >
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 end = elementCount;
1009 >        int i;
1010 >        // Optimize for initial run of survivors
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 = 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 >        }
1040      }
1041  
1042      /**
# Line 1014 | 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 1095 | 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);
1165 <
1166 <        // Let gc do its work
1167 <        modCount++;
1168 <        int newElementCount = elementCount - (toIndex-fromIndex);
1105 <        while (elementCount != newElementCount)
1106 <            elementData[--elementCount] = null;
1162 >        final Object[] es = elementData;
1163 >        final int oldSize = elementCount;
1164 >        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
1165 >
1166 >        modCount++;
1167 >        Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null);
1168 >        // checkInvariants();
1169      }
1170  
1171      /**
# Line 1212 | Line 1274 | public class Vector<E>
1274                  if (i >= size) {
1275                      return;
1276                  }
1277 <        @SuppressWarnings("unchecked")
1278 <                final E[] elementData = (E[]) Vector.this.elementData;
1217 <                if (i >= elementData.length) {
1277 >                final Object[] es = elementData;
1278 >                if (i >= es.length)
1279                      throw new ConcurrentModificationException();
1280 <                }
1281 <                while (i != size && modCount == expectedModCount) {
1221 <                    action.accept(elementData[i++]);
1222 <                }
1280 >                while (i < size && modCount == expectedModCount)
1281 >                    action.accept(elementAt(es, i++));
1282                  // update once at end of iteration to reduce heap write traffic
1283                  cursor = i;
1284                  lastRet = i - 1;
# Line 1290 | Line 1349 | public class Vector<E>
1349      public synchronized void forEach(Consumer<? super E> action) {
1350          Objects.requireNonNull(action);
1351          final int expectedModCount = modCount;
1352 <        @SuppressWarnings("unchecked")
1294 <        final E[] elementData = (E[]) this.elementData;
1295 <        final int elementCount = this.elementCount;
1296 <        for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1297 <            action.accept(elementData[i]);
1298 <        }
1299 <        if (modCount != expectedModCount) {
1300 <            throw new ConcurrentModificationException();
1301 <        }
1302 <    }
1303 <
1304 <    @Override
1305 <    @SuppressWarnings("unchecked")
1306 <    public synchronized boolean removeIf(Predicate<? super E> filter) {
1307 <        Objects.requireNonNull(filter);
1308 <        // figure out which elements are to be removed
1309 <        // any exception thrown from the filter predicate at this stage
1310 <        // will leave the collection unmodified
1311 <        int removeCount = 0;
1352 >        final Object[] es = elementData;
1353          final int size = elementCount;
1354 <        final BitSet removeSet = new BitSet(size);
1355 <        final int expectedModCount = modCount;
1356 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1316 <            @SuppressWarnings("unchecked")
1317 <            final E element = (E) elementData[i];
1318 <            if (filter.test(element)) {
1319 <                removeSet.set(i);
1320 <                removeCount++;
1321 <            }
1322 <        }
1323 <        if (modCount != expectedModCount) {
1354 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1355 >            action.accept(elementAt(es, i));
1356 >        if (modCount != expectedModCount)
1357              throw new ConcurrentModificationException();
1358 <        }
1326 <
1327 <        // shift surviving elements left over the spaces left by removed elements
1328 <        final boolean anyToRemove = removeCount > 0;
1329 <        if (anyToRemove) {
1330 <            final int newSize = size - removeCount;
1331 <            for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
1332 <                i = removeSet.nextClearBit(i);
1333 <                elementData[j] = elementData[i];
1334 <            }
1335 <            for (int k=newSize; k < size; k++) {
1336 <                elementData[k] = null;  // Let gc do its work
1337 <            }
1338 <            elementCount = newSize;
1339 <            if (modCount != expectedModCount) {
1340 <                throw new ConcurrentModificationException();
1341 <            }
1342 <            modCount++;
1343 <        }
1344 <
1345 <        return anyToRemove;
1358 >        // checkInvariants();
1359      }
1360  
1361      @Override
1349    @SuppressWarnings("unchecked")
1362      public synchronized void replaceAll(UnaryOperator<E> operator) {
1363          Objects.requireNonNull(operator);
1364          final int expectedModCount = modCount;
1365 +        final Object[] es = elementData;
1366          final int size = elementCount;
1367 <        for (int i=0; modCount == expectedModCount && i < size; i++) {
1368 <            elementData[i] = operator.apply((E) elementData[i]);
1369 <        }
1357 <        if (modCount != expectedModCount) {
1367 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1368 >            es[i] = operator.apply(elementAt(es, i));
1369 >        if (modCount != expectedModCount)
1370              throw new ConcurrentModificationException();
1359        }
1371          modCount++;
1372 +        // checkInvariants();
1373      }
1374  
1375      @SuppressWarnings("unchecked")
# Line 1365 | Line 1377 | public class Vector<E>
1377      public synchronized void sort(Comparator<? super E> c) {
1378          final int expectedModCount = modCount;
1379          Arrays.sort((E[]) elementData, 0, elementCount, c);
1380 <        if (modCount != expectedModCount) {
1380 >        if (modCount != expectedModCount)
1381              throw new ConcurrentModificationException();
1370        }
1382          modCount++;
1383 +        // checkInvariants();
1384      }
1385  
1386      /**
# Line 1397 | Line 1409 | public class Vector<E>
1409          private int fence; // -1 until used; then one past last index
1410          private int expectedModCount; // initialized when fence set
1411  
1412 <        /** Create new spliterator covering the given  range */
1412 >        /** Create new spliterator covering the given range */
1413          VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1414                            int expectedModCount) {
1415              this.list = list;
# Line 1410 | Line 1422 | public class Vector<E>
1422          private int getFence() { // initialize on first use
1423              int hi;
1424              if ((hi = fence) < 0) {
1425 <                synchronized(list) {
1425 >                synchronized (list) {
1426                      array = list.elementData;
1427                      expectedModCount = list.modCount;
1428                      hi = fence = list.elementCount;
# Line 1449 | Line 1461 | public class Vector<E>
1461                  throw new NullPointerException();
1462              if ((lst = list) != null) {
1463                  if ((hi = fence) < 0) {
1464 <                    synchronized(lst) {
1464 >                    synchronized (lst) {
1465                          expectedModCount = lst.modCount;
1466                          a = array = lst.elementData;
1467                          hi = fence = lst.elementCount;
# Line 1475 | 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