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() { |
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; |
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 |
|
/** |
1269 |
|
if (i >= size) { |
1270 |
|
return; |
1271 |
|
} |
1272 |
< |
@SuppressWarnings("unchecked") |
1273 |
< |
final E[] elementData = (E[]) Vector.this.elementData; |
1217 |
< |
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) { |
1221 |
< |
action.accept(elementData[i++]); |
1222 |
< |
} |
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; |
1344 |
|
public synchronized void forEach(Consumer<? super E> action) { |
1345 |
|
Objects.requireNonNull(action); |
1346 |
|
final int expectedModCount = modCount; |
1347 |
< |
@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; |
1347 |
> |
final Object[] es = elementData; |
1348 |
|
final int size = elementCount; |
1349 |
< |
final BitSet removeSet = new BitSet(size); |
1350 |
< |
final int expectedModCount = modCount; |
1351 |
< |
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) { |
1349 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1350 |
> |
action.accept(elementAt(es, i)); |
1351 |
> |
if (modCount != expectedModCount) |
1352 |
|
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; |
1353 |
|
} |
1354 |
|
|
1355 |
|
@Override |
1349 |
– |
@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 |
< |
} |
1357 |
< |
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(); |
1359 |
– |
} |
1365 |
|
modCount++; |
1366 |
|
} |
1367 |
|
|
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; |
1415 |
|
private int getFence() { // initialize on first use |
1416 |
|
int hi; |
1417 |
|
if ((hi = fence) < 0) { |
1418 |
< |
synchronized(list) { |
1418 |
> |
synchronized (list) { |
1419 |
|
array = list.elementData; |
1420 |
|
expectedModCount = list.modCount; |
1421 |
|
hi = fence = list.elementCount; |
1454 |
|
throw new NullPointerException(); |
1455 |
|
if ((lst = list) != null) { |
1456 |
|
if ((hi = fence) < 0) { |
1457 |
< |
synchronized(lst) { |
1457 |
> |
synchronized (lst) { |
1458 |
|
expectedModCount = lst.modCount; |
1459 |
|
a = array = lst.elementData; |
1460 |
|
hi = fence = lst.elementCount; |