912 |
|
} |
913 |
|
|
914 |
|
@Override |
915 |
< |
@SuppressWarnings("unchecked") |
916 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
917 |
< |
Objects.requireNonNull(consumer); |
915 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
916 |
> |
Objects.requireNonNull(action); |
917 |
|
final int size = ArrayList.this.size; |
918 |
|
int i = cursor; |
919 |
< |
if (i >= size) { |
920 |
< |
return; |
921 |
< |
} |
922 |
< |
final Object[] elementData = ArrayList.this.elementData; |
923 |
< |
if (i >= elementData.length) { |
924 |
< |
throw new ConcurrentModificationException(); |
925 |
< |
} |
926 |
< |
while (i != size && modCount == expectedModCount) { |
927 |
< |
consumer.accept((E) elementData[i++]); |
919 |
> |
if (i < size) { |
920 |
> |
final Object[] es = elementData; |
921 |
> |
if (i >= es.length) |
922 |
> |
throw new ConcurrentModificationException(); |
923 |
> |
for (; i < size && modCount == expectedModCount; i++) |
924 |
> |
action.accept(elementAt(es, i)); |
925 |
> |
// update once at end to reduce heap write traffic |
926 |
> |
cursor = i; |
927 |
> |
lastRet = i - 1; |
928 |
> |
checkForComodification(); |
929 |
|
} |
930 |
– |
// update once at end of iteration to reduce heap write traffic |
931 |
– |
cursor = i; |
932 |
– |
lastRet = i - 1; |
933 |
– |
checkForComodification(); |
930 |
|
} |
931 |
|
|
932 |
|
final void checkForComodification() { |
1187 |
|
return (E) elementData[offset + (lastRet = i)]; |
1188 |
|
} |
1189 |
|
|
1190 |
< |
@SuppressWarnings("unchecked") |
1191 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
1196 |
< |
Objects.requireNonNull(consumer); |
1190 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1191 |
> |
Objects.requireNonNull(action); |
1192 |
|
final int size = SubList.this.size; |
1193 |
|
int i = cursor; |
1194 |
< |
if (i >= size) { |
1195 |
< |
return; |
1196 |
< |
} |
1197 |
< |
final Object[] elementData = root.elementData; |
1198 |
< |
if (offset + i >= elementData.length) { |
1199 |
< |
throw new ConcurrentModificationException(); |
1200 |
< |
} |
1201 |
< |
while (i != size && modCount == expectedModCount) { |
1202 |
< |
consumer.accept((E) elementData[offset + (i++)]); |
1194 |
> |
if (i < size) { |
1195 |
> |
final Object[] es = root.elementData; |
1196 |
> |
if (offset + i >= es.length) |
1197 |
> |
throw new ConcurrentModificationException(); |
1198 |
> |
for (; i < size && modCount == expectedModCount; i++) |
1199 |
> |
action.accept(elementAt(es, offset + i)); |
1200 |
> |
// update once at end to reduce heap write traffic |
1201 |
> |
cursor = i; |
1202 |
> |
lastRet = i - 1; |
1203 |
> |
checkForComodification(); |
1204 |
|
} |
1209 |
– |
// update once at end of iteration to reduce heap write traffic |
1210 |
– |
lastRet = cursor = i; |
1211 |
– |
checkForComodification(); |
1205 |
|
} |
1206 |
|
|
1207 |
|
public int nextIndex() { |
1291 |
|
public Spliterator<E> spliterator() { |
1292 |
|
checkForComodification(); |
1293 |
|
|
1294 |
< |
// ArrayListSpliterator is not used because late-binding logic |
1295 |
< |
// is different here |
1303 |
< |
return new Spliterator<>() { |
1294 |
> |
// ArrayListSpliterator not used here due to late-binding |
1295 |
> |
return new Spliterator<E>() { |
1296 |
|
private int index = offset; // current index, modified on advance/split |
1297 |
|
private int fence = -1; // -1 until used; then one past last index |
1298 |
|
private int expectedModCount; // initialized when fence set |
1306 |
|
return hi; |
1307 |
|
} |
1308 |
|
|
1309 |
< |
public ArrayListSpliterator<E> trySplit() { |
1309 |
> |
public ArrayList<E>.ArrayListSpliterator trySplit() { |
1310 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1311 |
< |
// ArrayListSpliterator could be used here as the source is already bound |
1311 |
> |
// ArrayListSpliterator can be used here as the source is already bound |
1312 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1313 |
< |
new ArrayListSpliterator<>(root, lo, index = mid, |
1322 |
< |
expectedModCount); |
1313 |
> |
root.new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1314 |
|
} |
1315 |
|
|
1316 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1352 |
|
} |
1353 |
|
|
1354 |
|
public long estimateSize() { |
1355 |
< |
return (long) (getFence() - index); |
1355 |
> |
return getFence() - index; |
1356 |
|
} |
1357 |
|
|
1358 |
|
public int characteristics() { |
1389 |
|
*/ |
1390 |
|
@Override |
1391 |
|
public Spliterator<E> spliterator() { |
1392 |
< |
return new ArrayListSpliterator<>(this, 0, -1, 0); |
1392 |
> |
return new ArrayListSpliterator(0, -1, 0); |
1393 |
|
} |
1394 |
|
|
1395 |
|
/** Index-based split-by-two, lazily initialized Spliterator */ |
1396 |
< |
static final class ArrayListSpliterator<E> implements Spliterator<E> { |
1396 |
> |
final class ArrayListSpliterator implements Spliterator<E> { |
1397 |
|
|
1398 |
|
/* |
1399 |
|
* If ArrayLists were immutable, or structurally immutable (no |
1427 |
|
* these streamlinings. |
1428 |
|
*/ |
1429 |
|
|
1439 |
– |
private final ArrayList<E> list; |
1430 |
|
private int index; // current index, modified on advance/split |
1431 |
|
private int fence; // -1 until used; then one past last index |
1432 |
|
private int expectedModCount; // initialized when fence set |
1433 |
|
|
1434 |
|
/** Create new spliterator covering the given range */ |
1435 |
< |
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, |
1446 |
< |
int expectedModCount) { |
1447 |
< |
this.list = list; // OK if null unless traversed |
1435 |
> |
ArrayListSpliterator(int origin, int fence, int expectedModCount) { |
1436 |
|
this.index = origin; |
1437 |
|
this.fence = fence; |
1438 |
|
this.expectedModCount = expectedModCount; |
1440 |
|
|
1441 |
|
private int getFence() { // initialize fence to size on first use |
1442 |
|
int hi; // (a specialized variant appears in method forEach) |
1455 |
– |
ArrayList<E> lst; |
1443 |
|
if ((hi = fence) < 0) { |
1444 |
< |
if ((lst = list) == null) |
1445 |
< |
hi = fence = 0; |
1459 |
< |
else { |
1460 |
< |
expectedModCount = lst.modCount; |
1461 |
< |
hi = fence = lst.size; |
1462 |
< |
} |
1444 |
> |
expectedModCount = modCount; |
1445 |
> |
hi = fence = size; |
1446 |
|
} |
1447 |
|
return hi; |
1448 |
|
} |
1449 |
|
|
1450 |
< |
public ArrayListSpliterator<E> trySplit() { |
1450 |
> |
public ArrayListSpliterator trySplit() { |
1451 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1452 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1453 |
< |
new ArrayListSpliterator<>(list, lo, index = mid, |
1471 |
< |
expectedModCount); |
1453 |
> |
new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1454 |
|
} |
1455 |
|
|
1456 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1459 |
|
int hi = getFence(), i = index; |
1460 |
|
if (i < hi) { |
1461 |
|
index = i + 1; |
1462 |
< |
@SuppressWarnings("unchecked") E e = (E)list.elementData[i]; |
1462 |
> |
@SuppressWarnings("unchecked") E e = (E)elementData[i]; |
1463 |
|
action.accept(e); |
1464 |
< |
if (list.modCount != expectedModCount) |
1464 |
> |
if (modCount != expectedModCount) |
1465 |
|
throw new ConcurrentModificationException(); |
1466 |
|
return true; |
1467 |
|
} |
1470 |
|
|
1471 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1472 |
|
int i, hi, mc; // hoist accesses and checks from loop |
1473 |
< |
ArrayList<E> lst; Object[] a; |
1473 |
> |
Object[] a; |
1474 |
|
if (action == null) |
1475 |
|
throw new NullPointerException(); |
1476 |
< |
if ((lst = list) != null && (a = lst.elementData) != null) { |
1476 |
> |
if ((a = elementData) != null) { |
1477 |
|
if ((hi = fence) < 0) { |
1478 |
< |
mc = lst.modCount; |
1479 |
< |
hi = lst.size; |
1478 |
> |
mc = modCount; |
1479 |
> |
hi = size; |
1480 |
|
} |
1481 |
|
else |
1482 |
|
mc = expectedModCount; |
1485 |
|
@SuppressWarnings("unchecked") E e = (E) a[i]; |
1486 |
|
action.accept(e); |
1487 |
|
} |
1488 |
< |
if (lst.modCount == mc) |
1488 |
> |
if (modCount == mc) |
1489 |
|
return; |
1490 |
|
} |
1491 |
|
} |
1493 |
|
} |
1494 |
|
|
1495 |
|
public long estimateSize() { |
1496 |
< |
return (long) (getFence() - index); |
1496 |
> |
return getFence() - index; |
1497 |
|
} |
1498 |
|
|
1499 |
|
public int characteristics() { |
1518 |
|
return removeIf(filter, 0, size); |
1519 |
|
} |
1520 |
|
|
1521 |
< |
boolean removeIf(Predicate<? super E> filter, |
1522 |
< |
final int from, final int end) { |
1521 |
> |
/** |
1522 |
> |
* Removes all elements satisfying the given predicate, from index |
1523 |
> |
* i (inclusive) to index end (exclusive). |
1524 |
> |
*/ |
1525 |
> |
boolean removeIf(Predicate<? super E> filter, int i, final int end) { |
1526 |
|
Objects.requireNonNull(filter); |
1527 |
|
int expectedModCount = modCount; |
1528 |
|
final Object[] es = elementData; |
1544 |
– |
final boolean modified; |
1545 |
– |
int i; |
1529 |
|
// Optimize for initial run of survivors |
1530 |
< |
for (i = from; i < end && !filter.test(elementAt(es, i)); i++) |
1530 |
> |
for (; i < end && !filter.test(elementAt(es, i)); i++) |
1531 |
|
; |
1532 |
|
// Tolerate predicates that reentrantly access the collection for |
1533 |
|
// read (but writers still get CME), so traverse once to find |
1534 |
|
// elements to delete, a second pass to physically expunge. |
1535 |
< |
if (modified = (i < end)) { |
1553 |
< |
expectedModCount++; |
1554 |
< |
modCount++; |
1535 |
> |
if (i < end) { |
1536 |
|
final int beg = i; |
1537 |
|
final long[] deathRow = nBits(end - beg); |
1538 |
|
deathRow[0] = 1L; // set bit 0 |
1541 |
|
setBit(deathRow, i - beg); |
1542 |
|
if (modCount != expectedModCount) |
1543 |
|
throw new ConcurrentModificationException(); |
1544 |
+ |
expectedModCount++; |
1545 |
+ |
modCount++; |
1546 |
|
int w = beg; |
1547 |
|
for (i = beg; i < end; i++) |
1548 |
|
if (isClear(deathRow, i - beg)) |
1550 |
|
final int oldSize = size; |
1551 |
|
System.arraycopy(es, end, es, w, oldSize - end); |
1552 |
|
Arrays.fill(es, size -= (end - w), oldSize, null); |
1553 |
+ |
// checkInvariants(); |
1554 |
+ |
return true; |
1555 |
+ |
} else { |
1556 |
+ |
if (modCount != expectedModCount) |
1557 |
+ |
throw new ConcurrentModificationException(); |
1558 |
+ |
// checkInvariants(); |
1559 |
+ |
return false; |
1560 |
|
} |
1571 |
– |
if (modCount != expectedModCount) |
1572 |
– |
throw new ConcurrentModificationException(); |
1573 |
– |
// checkInvariants(); |
1574 |
– |
return modified; |
1561 |
|
} |
1562 |
|
|
1563 |
|
@Override |