306 |
|
modCount++; |
307 |
|
if (newSize > elementData.length) |
308 |
|
grow(newSize); |
309 |
< |
for (int i = newSize; i < elementCount; i++) |
310 |
< |
elementData[i] = null; |
311 |
< |
elementCount = newSize; |
309 |
> |
final Object[] es = elementData; |
310 |
> |
for (int to = elementCount, i = elementCount = newSize; i < to; i++) |
311 |
> |
es[i] = null; |
312 |
|
} |
313 |
|
|
314 |
|
/** |
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() { |
585 |
|
modCount++; |
586 |
|
elementCount--; |
587 |
|
elementData[elementCount] = null; /* to let gc do its work */ |
588 |
+ |
// checkInvariants(); |
589 |
|
} |
590 |
|
|
591 |
|
/** |
676 |
|
* method (which is part of the {@link List} interface). |
677 |
|
*/ |
678 |
|
public synchronized void removeAllElements() { |
679 |
< |
// Let gc do its work |
680 |
< |
for (int i = 0; i < elementCount; i++) |
681 |
< |
elementData[i] = null; |
681 |
< |
|
679 |
> |
final Object[] es = elementData; |
680 |
> |
for (int to = elementCount, i = elementCount = 0; i < to; i++) |
681 |
> |
es[i] = null; |
682 |
|
modCount++; |
683 |
– |
elementCount = 0; |
683 |
|
} |
684 |
|
|
685 |
|
/** |
692 |
|
public synchronized Object clone() { |
693 |
|
try { |
694 |
|
@SuppressWarnings("unchecked") |
695 |
< |
Vector<E> v = (Vector<E>) super.clone(); |
695 |
> |
Vector<E> v = (Vector<E>) super.clone(); |
696 |
|
v.elementData = Arrays.copyOf(elementData, elementCount); |
697 |
|
v.modCount = 0; |
698 |
|
return v; |
758 |
|
return (E) elementData[index]; |
759 |
|
} |
760 |
|
|
761 |
+ |
@SuppressWarnings("unchecked") |
762 |
+ |
static <E> E elementAt(Object[] es, int index) { |
763 |
+ |
return (E) es[index]; |
764 |
+ |
} |
765 |
+ |
|
766 |
|
/** |
767 |
|
* Returns the element at the specified position in this Vector. |
768 |
|
* |
809 |
|
elementData = grow(); |
810 |
|
elementData[s] = e; |
811 |
|
elementCount = s + 1; |
812 |
+ |
// checkInvariants(); |
813 |
|
} |
814 |
|
|
815 |
|
/** |
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) { |
878 |
|
numMoved); |
879 |
|
elementData[--elementCount] = null; // Let gc do its work |
880 |
|
|
881 |
+ |
// checkInvariants(); |
882 |
|
return oldValue; |
883 |
|
} |
884 |
|
|
934 |
|
elementData = grow(s + numNew); |
935 |
|
System.arraycopy(a, 0, elementData, s, numNew); |
936 |
|
elementCount = s + numNew; |
937 |
+ |
// checkInvariants(); |
938 |
|
return true; |
939 |
|
} |
940 |
|
} |
956 |
|
* or if the specified collection is null |
957 |
|
* @since 1.2 |
958 |
|
*/ |
959 |
< |
public synchronized boolean removeAll(Collection<?> c) { |
960 |
< |
return super.removeAll(c); |
959 |
> |
public boolean removeAll(Collection<?> c) { |
960 |
> |
Objects.requireNonNull(c); |
961 |
> |
return bulkRemove(e -> c.contains(e)); |
962 |
|
} |
963 |
|
|
964 |
|
/** |
980 |
|
* or if the specified collection is null |
981 |
|
* @since 1.2 |
982 |
|
*/ |
983 |
< |
public synchronized boolean retainAll(Collection<?> c) { |
984 |
< |
return super.retainAll(c); |
983 |
> |
public boolean retainAll(Collection<?> c) { |
984 |
> |
Objects.requireNonNull(c); |
985 |
> |
return bulkRemove(e -> !c.contains(e)); |
986 |
> |
} |
987 |
> |
|
988 |
> |
@Override |
989 |
> |
public boolean removeIf(Predicate<? super E> filter) { |
990 |
> |
Objects.requireNonNull(filter); |
991 |
> |
return bulkRemove(filter); |
992 |
> |
} |
993 |
> |
|
994 |
> |
// A tiny bit set implementation |
995 |
> |
|
996 |
> |
private static long[] nBits(int n) { |
997 |
> |
return new long[((n - 1) >> 6) + 1]; |
998 |
> |
} |
999 |
> |
private static void setBit(long[] bits, int i) { |
1000 |
> |
bits[i >> 6] |= 1L << i; |
1001 |
> |
} |
1002 |
> |
private static boolean isClear(long[] bits, int i) { |
1003 |
> |
return (bits[i >> 6] & (1L << i)) == 0; |
1004 |
> |
} |
1005 |
> |
|
1006 |
> |
private synchronized boolean bulkRemove(Predicate<? super E> filter) { |
1007 |
> |
int expectedModCount = modCount; |
1008 |
> |
final Object[] es = elementData; |
1009 |
> |
final int end = elementCount; |
1010 |
> |
int i; |
1011 |
> |
// Optimize for initial run of survivors |
1012 |
> |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
1013 |
> |
; |
1014 |
> |
// Tolerate predicates that reentrantly access the collection for |
1015 |
> |
// read (but writers still get CME), so traverse once to find |
1016 |
> |
// elements to delete, a second pass to physically expunge. |
1017 |
> |
if (i < end) { |
1018 |
> |
final int beg = i; |
1019 |
> |
final long[] deathRow = nBits(end - beg); |
1020 |
> |
deathRow[0] = 1L; // set bit 0 |
1021 |
> |
for (i = beg + 1; i < end; i++) |
1022 |
> |
if (filter.test(elementAt(es, i))) |
1023 |
> |
setBit(deathRow, i - beg); |
1024 |
> |
if (modCount != expectedModCount) |
1025 |
> |
throw new ConcurrentModificationException(); |
1026 |
> |
expectedModCount++; |
1027 |
> |
modCount++; |
1028 |
> |
int w = beg; |
1029 |
> |
for (i = beg; i < end; i++) |
1030 |
> |
if (isClear(deathRow, i - beg)) |
1031 |
> |
es[w++] = es[i]; |
1032 |
> |
for (i = elementCount = w; i < end; i++) |
1033 |
> |
es[i] = null; |
1034 |
> |
// checkInvariants(); |
1035 |
> |
return true; |
1036 |
> |
} else { |
1037 |
> |
if (modCount != expectedModCount) |
1038 |
> |
throw new ConcurrentModificationException(); |
1039 |
> |
// checkInvariants(); |
1040 |
> |
return false; |
1041 |
> |
} |
1042 |
|
} |
1043 |
|
|
1044 |
|
/** |
1079 |
|
numMoved); |
1080 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
1081 |
|
elementCount = s + numNew; |
1082 |
+ |
// checkInvariants(); |
1083 |
|
return true; |
1084 |
|
} |
1085 |
|
|
1161 |
|
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
1162 |
|
*/ |
1163 |
|
protected synchronized void removeRange(int fromIndex, int toIndex) { |
1098 |
– |
int numMoved = elementCount - toIndex; |
1099 |
– |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
1100 |
– |
numMoved); |
1101 |
– |
|
1102 |
– |
// Let gc do its work |
1164 |
|
modCount++; |
1165 |
< |
int newElementCount = elementCount - (toIndex-fromIndex); |
1166 |
< |
while (elementCount != newElementCount) |
1167 |
< |
elementData[--elementCount] = null; |
1165 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
1166 |
> |
// checkInvariants(); |
1167 |
> |
} |
1168 |
> |
|
1169 |
> |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
1170 |
> |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
1171 |
> |
System.arraycopy(es, hi, es, lo, elementCount - hi); |
1172 |
> |
for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) |
1173 |
> |
es[i] = null; |
1174 |
|
} |
1175 |
|
|
1176 |
|
/** |
1177 |
< |
* Save the state of the {@code Vector} instance to a stream (that |
1178 |
< |
* is, serialize it). |
1177 |
> |
* Saves the state of the {@code Vector} instance to a stream |
1178 |
> |
* (that is, serializes it). |
1179 |
|
* This method performs synchronization to ensure the consistency |
1180 |
|
* of the serialized data. |
1181 |
+ |
* |
1182 |
+ |
* @param s the stream |
1183 |
+ |
* @throws java.io.IOException if an I/O error occurs |
1184 |
|
*/ |
1185 |
|
private void writeObject(java.io.ObjectOutputStream s) |
1186 |
|
throws java.io.IOException { |
1282 |
|
if (i >= size) { |
1283 |
|
return; |
1284 |
|
} |
1285 |
< |
@SuppressWarnings("unchecked") |
1286 |
< |
final E[] elementData = (E[]) Vector.this.elementData; |
1217 |
< |
if (i >= elementData.length) { |
1285 |
> |
final Object[] es = elementData; |
1286 |
> |
if (i >= es.length) |
1287 |
|
throw new ConcurrentModificationException(); |
1288 |
< |
} |
1289 |
< |
while (i != size && modCount == expectedModCount) { |
1221 |
< |
action.accept(elementData[i++]); |
1222 |
< |
} |
1288 |
> |
while (i < size && modCount == expectedModCount) |
1289 |
> |
action.accept(elementAt(es, i++)); |
1290 |
|
// update once at end of iteration to reduce heap write traffic |
1291 |
|
cursor = i; |
1292 |
|
lastRet = i - 1; |
1357 |
|
public synchronized void forEach(Consumer<? super E> action) { |
1358 |
|
Objects.requireNonNull(action); |
1359 |
|
final int expectedModCount = modCount; |
1360 |
< |
@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; |
1360 |
> |
final Object[] es = elementData; |
1361 |
|
final int size = elementCount; |
1362 |
< |
final BitSet removeSet = new BitSet(size); |
1363 |
< |
final int expectedModCount = modCount; |
1364 |
< |
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) { |
1362 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1363 |
> |
action.accept(elementAt(es, i)); |
1364 |
> |
if (modCount != expectedModCount) |
1365 |
|
throw new ConcurrentModificationException(); |
1366 |
< |
} |
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; |
1366 |
> |
// checkInvariants(); |
1367 |
|
} |
1368 |
|
|
1369 |
|
@Override |
1349 |
– |
@SuppressWarnings("unchecked") |
1370 |
|
public synchronized void replaceAll(UnaryOperator<E> operator) { |
1371 |
|
Objects.requireNonNull(operator); |
1372 |
|
final int expectedModCount = modCount; |
1373 |
+ |
final Object[] es = elementData; |
1374 |
|
final int size = elementCount; |
1375 |
< |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1376 |
< |
elementData[i] = operator.apply((E) elementData[i]); |
1377 |
< |
} |
1357 |
< |
if (modCount != expectedModCount) { |
1375 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1376 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1377 |
> |
if (modCount != expectedModCount) |
1378 |
|
throw new ConcurrentModificationException(); |
1359 |
– |
} |
1379 |
|
modCount++; |
1380 |
+ |
// checkInvariants(); |
1381 |
|
} |
1382 |
|
|
1383 |
|
@SuppressWarnings("unchecked") |
1385 |
|
public synchronized void sort(Comparator<? super E> c) { |
1386 |
|
final int expectedModCount = modCount; |
1387 |
|
Arrays.sort((E[]) elementData, 0, elementCount, c); |
1388 |
< |
if (modCount != expectedModCount) { |
1388 |
> |
if (modCount != expectedModCount) |
1389 |
|
throw new ConcurrentModificationException(); |
1370 |
– |
} |
1390 |
|
modCount++; |
1391 |
+ |
// checkInvariants(); |
1392 |
|
} |
1393 |
|
|
1394 |
|
/** |
1406 |
|
*/ |
1407 |
|
@Override |
1408 |
|
public Spliterator<E> spliterator() { |
1409 |
< |
return new VectorSpliterator<>(this, null, 0, -1, 0); |
1409 |
> |
return new VectorSpliterator(null, 0, -1, 0); |
1410 |
|
} |
1411 |
|
|
1412 |
|
/** Similar to ArrayList Spliterator */ |
1413 |
< |
static final class VectorSpliterator<E> implements Spliterator<E> { |
1394 |
< |
private final Vector<E> list; |
1413 |
> |
final class VectorSpliterator implements Spliterator<E> { |
1414 |
|
private Object[] array; |
1415 |
|
private int index; // current index, modified on advance/split |
1416 |
|
private int fence; // -1 until used; then one past last index |
1417 |
|
private int expectedModCount; // initialized when fence set |
1418 |
|
|
1419 |
< |
/** Create new spliterator covering the given range */ |
1420 |
< |
VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence, |
1419 |
> |
/** Create new spliterator covering the given range */ |
1420 |
> |
VectorSpliterator(Object[] array, int origin, int fence, |
1421 |
|
int expectedModCount) { |
1403 |
– |
this.list = list; |
1422 |
|
this.array = array; |
1423 |
|
this.index = origin; |
1424 |
|
this.fence = fence; |
1428 |
|
private int getFence() { // initialize on first use |
1429 |
|
int hi; |
1430 |
|
if ((hi = fence) < 0) { |
1431 |
< |
synchronized(list) { |
1432 |
< |
array = list.elementData; |
1433 |
< |
expectedModCount = list.modCount; |
1434 |
< |
hi = fence = list.elementCount; |
1431 |
> |
synchronized (Vector.this) { |
1432 |
> |
array = elementData; |
1433 |
> |
expectedModCount = modCount; |
1434 |
> |
hi = fence = elementCount; |
1435 |
|
} |
1436 |
|
} |
1437 |
|
return hi; |
1440 |
|
public Spliterator<E> trySplit() { |
1441 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1442 |
|
return (lo >= mid) ? null : |
1443 |
< |
new VectorSpliterator<>(list, array, lo, index = mid, |
1426 |
< |
expectedModCount); |
1443 |
> |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
1444 |
|
} |
1445 |
|
|
1446 |
|
@SuppressWarnings("unchecked") |
1451 |
|
if (getFence() > (i = index)) { |
1452 |
|
index = i + 1; |
1453 |
|
action.accept((E)array[i]); |
1454 |
< |
if (list.modCount != expectedModCount) |
1454 |
> |
if (modCount != expectedModCount) |
1455 |
|
throw new ConcurrentModificationException(); |
1456 |
|
return true; |
1457 |
|
} |
1460 |
|
|
1461 |
|
@SuppressWarnings("unchecked") |
1462 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1446 |
– |
int i, hi; // hoist accesses and checks from loop |
1447 |
– |
Vector<E> lst; Object[] a; |
1463 |
|
if (action == null) |
1464 |
|
throw new NullPointerException(); |
1465 |
< |
if ((lst = list) != null) { |
1466 |
< |
if ((hi = fence) < 0) { |
1467 |
< |
synchronized(lst) { |
1468 |
< |
expectedModCount = lst.modCount; |
1469 |
< |
a = array = lst.elementData; |
1470 |
< |
hi = fence = lst.elementCount; |
1471 |
< |
} |
1457 |
< |
} |
1458 |
< |
else |
1459 |
< |
a = array; |
1460 |
< |
if (a != null && (i = index) >= 0 && (index = hi) <= a.length) { |
1461 |
< |
while (i < hi) |
1462 |
< |
action.accept((E) a[i++]); |
1463 |
< |
if (lst.modCount == expectedModCount) |
1464 |
< |
return; |
1465 |
< |
} |
1466 |
< |
} |
1467 |
< |
throw new ConcurrentModificationException(); |
1465 |
> |
final int hi = getFence(); |
1466 |
> |
final Object[] a = array; |
1467 |
> |
int i; |
1468 |
> |
for (i = index, index = hi; i < hi; i++) |
1469 |
> |
action.accept((E) a[i]); |
1470 |
> |
if (modCount != expectedModCount) |
1471 |
> |
throw new ConcurrentModificationException(); |
1472 |
|
} |
1473 |
|
|
1474 |
|
public long estimateSize() { |
1479 |
|
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1480 |
|
} |
1481 |
|
} |
1482 |
+ |
|
1483 |
+ |
void checkInvariants() { |
1484 |
+ |
// assert elementCount >= 0; |
1485 |
+ |
// assert elementCount == elementData.length || elementData[elementCount] == null; |
1486 |
+ |
} |
1487 |
|
} |