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() - 1</code>. |
516 |
> |
* {@code size() - 1} |
517 |
|
* @throws NoSuchElementException if this vector is empty |
518 |
|
*/ |
519 |
|
public synchronized E lastElement() { |
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 |
|
* |
860 |
|
* Shifts any subsequent elements to the left (subtracts one from their |
861 |
|
* indices). Returns the element that was removed from the Vector. |
862 |
|
* |
858 |
– |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
859 |
– |
* ({@code index < 0 || index >= size()}) |
863 |
|
* @param index the index of the element to be removed |
864 |
|
* @return element that was removed |
865 |
+ |
* @throws ArrayIndexOutOfBoundsException if the index is out of range |
866 |
+ |
* ({@code index < 0 || index >= size()}) |
867 |
|
* @since 1.2 |
868 |
|
*/ |
869 |
|
public synchronized E remove(int index) { |
954 |
|
* or if the specified collection is null |
955 |
|
* @since 1.2 |
956 |
|
*/ |
957 |
< |
public synchronized boolean removeAll(Collection<?> c) { |
958 |
< |
return super.removeAll(c); |
957 |
> |
public boolean removeAll(Collection<?> c) { |
958 |
> |
Objects.requireNonNull(c); |
959 |
> |
return bulkRemove(e -> c.contains(e)); |
960 |
|
} |
961 |
|
|
962 |
|
/** |
978 |
|
* or if the specified collection is null |
979 |
|
* @since 1.2 |
980 |
|
*/ |
981 |
< |
public synchronized boolean retainAll(Collection<?> c) { |
982 |
< |
return super.retainAll(c); |
981 |
> |
public boolean retainAll(Collection<?> c) { |
982 |
> |
Objects.requireNonNull(c); |
983 |
> |
return bulkRemove(e -> !c.contains(e)); |
984 |
> |
} |
985 |
> |
|
986 |
> |
@Override |
987 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
988 |
> |
Objects.requireNonNull(filter); |
989 |
> |
return bulkRemove(filter); |
990 |
> |
} |
991 |
> |
|
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 end = elementCount; |
1008 |
> |
final boolean modified; |
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 (modified = (i < end)) { |
1017 |
> |
expectedModCount++; |
1018 |
> |
modCount++; |
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(); |
1033 |
> |
return modified; |
1034 |
|
} |
1035 |
|
|
1036 |
|
/** |
1360 |
|
|
1361 |
|
@Override |
1362 |
|
@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; |
1312 |
– |
final int size = elementCount; |
1313 |
– |
final BitSet removeSet = new BitSet(size); |
1314 |
– |
final int expectedModCount = modCount; |
1315 |
– |
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) { |
1324 |
– |
throw new ConcurrentModificationException(); |
1325 |
– |
} |
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; |
1346 |
– |
} |
1347 |
– |
|
1348 |
– |
@Override |
1349 |
– |
@SuppressWarnings("unchecked") |
1363 |
|
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1364 |
|
Objects.requireNonNull(operator); |
1365 |
|
final int expectedModCount = modCount; |
1423 |
|
private int getFence() { // initialize on first use |
1424 |
|
int hi; |
1425 |
|
if ((hi = fence) < 0) { |
1426 |
< |
synchronized(list) { |
1426 |
> |
synchronized (list) { |
1427 |
|
array = list.elementData; |
1428 |
|
expectedModCount = list.modCount; |
1429 |
|
hi = fence = list.elementCount; |
1462 |
|
throw new NullPointerException(); |
1463 |
|
if ((lst = list) != null) { |
1464 |
|
if ((hi = fence) < 0) { |
1465 |
< |
synchronized(lst) { |
1465 |
> |
synchronized (lst) { |
1466 |
|
expectedModCount = lst.modCount; |
1467 |
|
a = array = lst.elementData; |
1468 |
|
hi = fence = lst.elementCount; |