1006 |
|
int expectedModCount = modCount; |
1007 |
|
final Object[] es = elementData; |
1008 |
|
final int end = elementCount; |
1009 |
– |
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++) |
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)) { |
1018 |
< |
expectedModCount++; |
1019 |
< |
modCount++; |
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 |
|
} |
1032 |
– |
if (modCount != expectedModCount) |
1033 |
– |
throw new ConcurrentModificationException(); |
1034 |
– |
// checkInvariants(); |
1035 |
– |
return modified; |
1040 |
|
} |
1041 |
|
|
1042 |
|
/** |
1398 |
|
*/ |
1399 |
|
@Override |
1400 |
|
public Spliterator<E> spliterator() { |
1401 |
< |
return new VectorSpliterator<>(this, null, 0, -1, 0); |
1401 |
> |
return new VectorSpliterator(null, 0, -1, 0); |
1402 |
|
} |
1403 |
|
|
1404 |
|
/** Similar to ArrayList Spliterator */ |
1405 |
< |
static final class VectorSpliterator<E> implements Spliterator<E> { |
1402 |
< |
private final Vector<E> list; |
1405 |
> |
final class VectorSpliterator implements Spliterator<E> { |
1406 |
|
private Object[] array; |
1407 |
|
private int index; // current index, modified on advance/split |
1408 |
|
private int fence; // -1 until used; then one past last index |
1409 |
|
private int expectedModCount; // initialized when fence set |
1410 |
|
|
1411 |
|
/** Create new spliterator covering the given range */ |
1412 |
< |
VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence, |
1412 |
> |
VectorSpliterator(Object[] array, int origin, int fence, |
1413 |
|
int expectedModCount) { |
1411 |
– |
this.list = list; |
1414 |
|
this.array = array; |
1415 |
|
this.index = origin; |
1416 |
|
this.fence = fence; |
1420 |
|
private int getFence() { // initialize on first use |
1421 |
|
int hi; |
1422 |
|
if ((hi = fence) < 0) { |
1423 |
< |
synchronized (list) { |
1424 |
< |
array = list.elementData; |
1425 |
< |
expectedModCount = list.modCount; |
1426 |
< |
hi = fence = list.elementCount; |
1423 |
> |
synchronized (Vector.this) { |
1424 |
> |
array = elementData; |
1425 |
> |
expectedModCount = modCount; |
1426 |
> |
hi = fence = elementCount; |
1427 |
|
} |
1428 |
|
} |
1429 |
|
return hi; |
1432 |
|
public Spliterator<E> trySplit() { |
1433 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1434 |
|
return (lo >= mid) ? null : |
1435 |
< |
new VectorSpliterator<>(list, array, lo, index = mid, |
1434 |
< |
expectedModCount); |
1435 |
> |
new VectorSpliterator(array, lo, index = mid, expectedModCount); |
1436 |
|
} |
1437 |
|
|
1438 |
|
@SuppressWarnings("unchecked") |
1443 |
|
if (getFence() > (i = index)) { |
1444 |
|
index = i + 1; |
1445 |
|
action.accept((E)array[i]); |
1446 |
< |
if (list.modCount != expectedModCount) |
1446 |
> |
if (modCount != expectedModCount) |
1447 |
|
throw new ConcurrentModificationException(); |
1448 |
|
return true; |
1449 |
|
} |
1453 |
|
@SuppressWarnings("unchecked") |
1454 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1455 |
|
int i, hi; // hoist accesses and checks from loop |
1456 |
< |
Vector<E> lst; Object[] a; |
1456 |
> |
Object[] a; |
1457 |
|
if (action == null) |
1458 |
|
throw new NullPointerException(); |
1459 |
< |
if ((lst = list) != null) { |
1460 |
< |
if ((hi = fence) < 0) { |
1461 |
< |
synchronized (lst) { |
1462 |
< |
expectedModCount = lst.modCount; |
1463 |
< |
a = array = lst.elementData; |
1463 |
< |
hi = fence = lst.elementCount; |
1464 |
< |
} |
1465 |
< |
} |
1466 |
< |
else |
1467 |
< |
a = array; |
1468 |
< |
if (a != null && (i = index) >= 0 && (index = hi) <= a.length) { |
1469 |
< |
while (i < hi) |
1470 |
< |
action.accept((E) a[i++]); |
1471 |
< |
if (lst.modCount == expectedModCount) |
1472 |
< |
return; |
1459 |
> |
if ((hi = fence) < 0) { |
1460 |
> |
synchronized (Vector.this) { |
1461 |
> |
expectedModCount = modCount; |
1462 |
> |
a = array = elementData; |
1463 |
> |
hi = fence = elementCount; |
1464 |
|
} |
1465 |
|
} |
1466 |
+ |
else |
1467 |
+ |
a = array; |
1468 |
+ |
if (a != null && (i = index) >= 0 && (index = hi) <= a.length) { |
1469 |
+ |
while (i < hi) |
1470 |
+ |
action.accept((E) a[i++]); |
1471 |
+ |
if (modCount == expectedModCount) |
1472 |
+ |
return; |
1473 |
+ |
} |
1474 |
|
throw new ConcurrentModificationException(); |
1475 |
|
} |
1476 |
|
|