104 |
|
* @see Vector |
105 |
|
* @since 1.2 |
106 |
|
*/ |
107 |
– |
|
107 |
|
public class ArrayList<E> extends AbstractList<E> |
108 |
|
implements List<E>, RandomAccess, Cloneable, java.io.Serializable |
109 |
|
{ |
423 |
|
return (E) elementData[index]; |
424 |
|
} |
425 |
|
|
426 |
+ |
@SuppressWarnings("unchecked") |
427 |
+ |
static <E> E elementAt(Object[] es, int index) { |
428 |
+ |
return (E) es[index]; |
429 |
+ |
} |
430 |
+ |
|
431 |
|
/** |
432 |
|
* Returns the element at the specified position in this list. |
433 |
|
* |
501 |
|
s - index); |
502 |
|
elementData[index] = element; |
503 |
|
size = s + 1; |
504 |
+ |
// checkInvariants(); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
525 |
|
numMoved); |
526 |
|
elementData[--size] = null; // clear to let GC do its work |
527 |
|
|
528 |
+ |
// checkInvariants(); |
529 |
|
return oldValue; |
530 |
|
} |
531 |
|
|
559 |
|
return false; |
560 |
|
} |
561 |
|
|
562 |
< |
/* |
562 |
> |
/** |
563 |
|
* Private remove method that skips bounds checking and does not |
564 |
|
* return the value removed. |
565 |
|
*/ |
578 |
|
*/ |
579 |
|
public void clear() { |
580 |
|
modCount++; |
581 |
< |
|
582 |
< |
// clear to let GC do its work |
583 |
< |
for (int i = 0; i < size; i++) |
578 |
< |
elementData[i] = null; |
579 |
< |
|
580 |
< |
size = 0; |
581 |
> |
final Object[] es = elementData; |
582 |
> |
for (int to = size, i = size = 0; i < to; i++) |
583 |
> |
es[i] = null; |
584 |
|
} |
585 |
|
|
586 |
|
/** |
608 |
|
elementData = grow(s + numNew); |
609 |
|
System.arraycopy(a, 0, elementData, s, numNew); |
610 |
|
size = s + numNew; |
611 |
+ |
// checkInvariants(); |
612 |
|
return true; |
613 |
|
} |
614 |
|
|
647 |
|
numMoved); |
648 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
649 |
|
size = s + numNew; |
650 |
+ |
// checkInvariants(); |
651 |
|
return true; |
652 |
|
} |
653 |
|
|
670 |
|
outOfBoundsMsg(fromIndex, toIndex)); |
671 |
|
} |
672 |
|
modCount++; |
673 |
< |
int numMoved = size - toIndex; |
674 |
< |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
675 |
< |
numMoved); |
676 |
< |
|
677 |
< |
// clear to let GC do its work |
678 |
< |
int newSize = size - (toIndex-fromIndex); |
679 |
< |
for (int i = newSize; i < size; i++) { |
680 |
< |
elementData[i] = null; |
681 |
< |
} |
677 |
< |
size = newSize; |
673 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
674 |
> |
// checkInvariants(); |
675 |
> |
} |
676 |
> |
|
677 |
> |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
678 |
> |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
679 |
> |
System.arraycopy(es, hi, es, lo, size - hi); |
680 |
> |
for (int to = size, i = (size -= hi - lo); i < to; i++) |
681 |
> |
es[i] = null; |
682 |
|
} |
683 |
|
|
684 |
|
/** |
721 |
|
* @see Collection#contains(Object) |
722 |
|
*/ |
723 |
|
public boolean removeAll(Collection<?> c) { |
724 |
< |
Objects.requireNonNull(c); |
721 |
< |
return batchRemove(c, false); |
724 |
> |
return batchRemove(c, false, 0, size); |
725 |
|
} |
726 |
|
|
727 |
|
/** |
741 |
|
* @see Collection#contains(Object) |
742 |
|
*/ |
743 |
|
public boolean retainAll(Collection<?> c) { |
744 |
< |
Objects.requireNonNull(c); |
742 |
< |
return batchRemove(c, true); |
744 |
> |
return batchRemove(c, true, 0, size); |
745 |
|
} |
746 |
|
|
747 |
< |
private boolean batchRemove(Collection<?> c, boolean complement) { |
748 |
< |
final Object[] elementData = this.elementData; |
749 |
< |
int r = 0, w = 0; |
750 |
< |
boolean modified = false; |
751 |
< |
try { |
752 |
< |
for (; r < size; r++) |
753 |
< |
if (c.contains(elementData[r]) == complement) |
754 |
< |
elementData[w++] = elementData[r]; |
755 |
< |
} finally { |
756 |
< |
// Preserve behavioral compatibility with AbstractCollection, |
757 |
< |
// even if c.contains() throws. |
758 |
< |
if (r != size) { |
759 |
< |
System.arraycopy(elementData, r, |
760 |
< |
elementData, w, |
761 |
< |
size - r); |
762 |
< |
w += size - r; |
763 |
< |
} |
764 |
< |
if (w != size) { |
765 |
< |
// clear to let GC do its work |
766 |
< |
for (int i = w; i < size; i++) |
767 |
< |
elementData[i] = null; |
768 |
< |
modCount += size - w; |
769 |
< |
size = w; |
770 |
< |
modified = true; |
747 |
> |
boolean batchRemove(Collection<?> c, boolean complement, |
748 |
> |
final int from, final int end) { |
749 |
> |
Objects.requireNonNull(c); |
750 |
> |
final Object[] es = elementData; |
751 |
> |
final boolean modified; |
752 |
> |
int r; |
753 |
> |
// Optimize for initial run of survivors |
754 |
> |
for (r = from; r < end && c.contains(es[r]) == complement; r++) |
755 |
> |
; |
756 |
> |
if (modified = (r < end)) { |
757 |
> |
int w = r++; |
758 |
> |
try { |
759 |
> |
for (Object e; r < end; r++) |
760 |
> |
if (c.contains(e = es[r]) == complement) |
761 |
> |
es[w++] = e; |
762 |
> |
} catch (Throwable ex) { |
763 |
> |
// Preserve behavioral compatibility with AbstractCollection, |
764 |
> |
// even if c.contains() throws. |
765 |
> |
System.arraycopy(es, r, es, w, end - r); |
766 |
> |
w += end - r; |
767 |
> |
throw ex; |
768 |
> |
} finally { |
769 |
> |
modCount += end - w; |
770 |
> |
shiftTailOverGap(es, w, end); |
771 |
|
} |
772 |
|
} |
773 |
+ |
// checkInvariants(); |
774 |
|
return modified; |
775 |
|
} |
776 |
|
|
777 |
|
/** |
778 |
< |
* Save the state of the {@code ArrayList} instance to a stream (that |
779 |
< |
* is, serialize it). |
778 |
> |
* Saves the state of the {@code ArrayList} instance to a stream |
779 |
> |
* (that is, serializes it). |
780 |
|
* |
781 |
+ |
* @param s the stream |
782 |
+ |
* @throws java.io.IOException if an I/O error occurs |
783 |
|
* @serialData The length of the array backing the {@code ArrayList} |
784 |
|
* instance is emitted (int), followed by all of its elements |
785 |
|
* (each an {@code Object}) in the proper order. |
786 |
|
*/ |
787 |
|
private void writeObject(java.io.ObjectOutputStream s) |
788 |
< |
throws java.io.IOException{ |
788 |
> |
throws java.io.IOException { |
789 |
|
// Write out element count, and any hidden stuff |
790 |
|
int expectedModCount = modCount; |
791 |
|
s.defaultWriteObject(); |
804 |
|
} |
805 |
|
|
806 |
|
/** |
807 |
< |
* Reconstitute the {@code ArrayList} instance from a stream (that is, |
808 |
< |
* deserialize it). |
807 |
> |
* Reconstitutes the {@code ArrayList} instance from a stream (that is, |
808 |
> |
* deserializes it). |
809 |
> |
* @param s the stream |
810 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
811 |
> |
* could not be found |
812 |
> |
* @throws java.io.IOException if an I/O error occurs |
813 |
|
*/ |
814 |
|
private void readObject(java.io.ObjectInputStream s) |
815 |
|
throws java.io.IOException, ClassNotFoundException { |
921 |
|
} |
922 |
|
|
923 |
|
@Override |
924 |
< |
@SuppressWarnings("unchecked") |
925 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
917 |
< |
Objects.requireNonNull(consumer); |
924 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
925 |
> |
Objects.requireNonNull(action); |
926 |
|
final int size = ArrayList.this.size; |
927 |
|
int i = cursor; |
928 |
< |
if (i >= size) { |
929 |
< |
return; |
930 |
< |
} |
931 |
< |
final Object[] elementData = ArrayList.this.elementData; |
932 |
< |
if (i >= elementData.length) { |
933 |
< |
throw new ConcurrentModificationException(); |
934 |
< |
} |
935 |
< |
while (i != size && modCount == expectedModCount) { |
936 |
< |
consumer.accept((E) elementData[i++]); |
928 |
> |
if (i < size) { |
929 |
> |
final Object[] es = elementData; |
930 |
> |
if (i >= es.length) |
931 |
> |
throw new ConcurrentModificationException(); |
932 |
> |
for (; i < size && modCount == expectedModCount; i++) |
933 |
> |
action.accept(elementAt(es, i)); |
934 |
> |
// update once at end to reduce heap write traffic |
935 |
> |
cursor = i; |
936 |
> |
lastRet = i - 1; |
937 |
> |
checkForComodification(); |
938 |
|
} |
930 |
– |
// update once at end of iteration to reduce heap write traffic |
931 |
– |
cursor = i; |
932 |
– |
lastRet = i - 1; |
933 |
– |
checkForComodification(); |
939 |
|
} |
940 |
|
|
941 |
|
final void checkForComodification() { |
1122 |
|
return true; |
1123 |
|
} |
1124 |
|
|
1125 |
+ |
public boolean removeAll(Collection<?> c) { |
1126 |
+ |
return batchRemove(c, false); |
1127 |
+ |
} |
1128 |
+ |
|
1129 |
+ |
public boolean retainAll(Collection<?> c) { |
1130 |
+ |
return batchRemove(c, true); |
1131 |
+ |
} |
1132 |
+ |
|
1133 |
+ |
private boolean batchRemove(Collection<?> c, boolean complement) { |
1134 |
+ |
checkForComodification(); |
1135 |
+ |
int oldSize = root.size; |
1136 |
+ |
boolean modified = |
1137 |
+ |
root.batchRemove(c, complement, offset, offset + size); |
1138 |
+ |
if (modified) |
1139 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1140 |
+ |
return modified; |
1141 |
+ |
} |
1142 |
+ |
|
1143 |
+ |
public boolean removeIf(Predicate<? super E> filter) { |
1144 |
+ |
checkForComodification(); |
1145 |
+ |
int oldSize = root.size; |
1146 |
+ |
boolean modified = root.removeIf(filter, offset, offset + size); |
1147 |
+ |
if (modified) |
1148 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1149 |
+ |
return modified; |
1150 |
+ |
} |
1151 |
+ |
|
1152 |
|
public Iterator<E> iterator() { |
1153 |
|
return listIterator(); |
1154 |
|
} |
1196 |
|
return (E) elementData[offset + (lastRet = i)]; |
1197 |
|
} |
1198 |
|
|
1199 |
< |
@SuppressWarnings("unchecked") |
1200 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
1169 |
< |
Objects.requireNonNull(consumer); |
1199 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1200 |
> |
Objects.requireNonNull(action); |
1201 |
|
final int size = SubList.this.size; |
1202 |
|
int i = cursor; |
1203 |
< |
if (i >= size) { |
1204 |
< |
return; |
1205 |
< |
} |
1206 |
< |
final Object[] elementData = root.elementData; |
1207 |
< |
if (offset + i >= elementData.length) { |
1208 |
< |
throw new ConcurrentModificationException(); |
1209 |
< |
} |
1210 |
< |
while (i != size && modCount == expectedModCount) { |
1211 |
< |
consumer.accept((E) elementData[offset + (i++)]); |
1203 |
> |
if (i < size) { |
1204 |
> |
final Object[] es = root.elementData; |
1205 |
> |
if (offset + i >= es.length) |
1206 |
> |
throw new ConcurrentModificationException(); |
1207 |
> |
for (; i < size && modCount == expectedModCount; i++) |
1208 |
> |
action.accept(elementAt(es, offset + i)); |
1209 |
> |
// update once at end to reduce heap write traffic |
1210 |
> |
cursor = i; |
1211 |
> |
lastRet = i - 1; |
1212 |
> |
checkForComodification(); |
1213 |
|
} |
1182 |
– |
// update once at end of iteration to reduce heap write traffic |
1183 |
– |
lastRet = cursor = i; |
1184 |
– |
checkForComodification(); |
1214 |
|
} |
1215 |
|
|
1216 |
|
public int nextIndex() { |
1300 |
|
public Spliterator<E> spliterator() { |
1301 |
|
checkForComodification(); |
1302 |
|
|
1303 |
< |
// ArrayListSpliterator is not used because late-binding logic |
1304 |
< |
// is different here |
1276 |
< |
return new Spliterator<>() { |
1303 |
> |
// ArrayListSpliterator not used here due to late-binding |
1304 |
> |
return new Spliterator<E>() { |
1305 |
|
private int index = offset; // current index, modified on advance/split |
1306 |
|
private int fence = -1; // -1 until used; then one past last index |
1307 |
|
private int expectedModCount; // initialized when fence set |
1315 |
|
return hi; |
1316 |
|
} |
1317 |
|
|
1318 |
< |
public ArrayListSpliterator<E> trySplit() { |
1318 |
> |
public ArrayList<E>.ArrayListSpliterator trySplit() { |
1319 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1320 |
< |
// ArrayListSpliterator could be used here as the source is already bound |
1320 |
> |
// ArrayListSpliterator can be used here as the source is already bound |
1321 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1322 |
< |
new ArrayListSpliterator<>(root, lo, index = mid, |
1295 |
< |
expectedModCount); |
1322 |
> |
root.new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1323 |
|
} |
1324 |
|
|
1325 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1361 |
|
} |
1362 |
|
|
1363 |
|
public long estimateSize() { |
1364 |
< |
return (long) (getFence() - index); |
1364 |
> |
return getFence() - index; |
1365 |
|
} |
1366 |
|
|
1367 |
|
public int characteristics() { |
1371 |
|
} |
1372 |
|
} |
1373 |
|
|
1374 |
+ |
/** |
1375 |
+ |
* @throws NullPointerException {@inheritDoc} |
1376 |
+ |
*/ |
1377 |
|
@Override |
1378 |
|
public void forEach(Consumer<? super E> action) { |
1379 |
|
Objects.requireNonNull(action); |
1380 |
|
final int expectedModCount = modCount; |
1381 |
< |
@SuppressWarnings("unchecked") |
1352 |
< |
final E[] elementData = (E[]) this.elementData; |
1381 |
> |
final Object[] es = elementData; |
1382 |
|
final int size = this.size; |
1383 |
< |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1384 |
< |
action.accept(elementData[i]); |
1385 |
< |
} |
1357 |
< |
if (modCount != expectedModCount) { |
1383 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1384 |
> |
action.accept(elementAt(es, i)); |
1385 |
> |
if (modCount != expectedModCount) |
1386 |
|
throw new ConcurrentModificationException(); |
1359 |
– |
} |
1387 |
|
} |
1388 |
|
|
1389 |
|
/** |
1401 |
|
*/ |
1402 |
|
@Override |
1403 |
|
public Spliterator<E> spliterator() { |
1404 |
< |
return new ArrayListSpliterator<>(this, 0, -1, 0); |
1404 |
> |
return new ArrayListSpliterator(0, -1, 0); |
1405 |
|
} |
1406 |
|
|
1407 |
|
/** Index-based split-by-two, lazily initialized Spliterator */ |
1408 |
< |
static final class ArrayListSpliterator<E> implements Spliterator<E> { |
1408 |
> |
final class ArrayListSpliterator implements Spliterator<E> { |
1409 |
|
|
1410 |
|
/* |
1411 |
|
* If ArrayLists were immutable, or structurally immutable (no |
1439 |
|
* these streamlinings. |
1440 |
|
*/ |
1441 |
|
|
1415 |
– |
private final ArrayList<E> list; |
1442 |
|
private int index; // current index, modified on advance/split |
1443 |
|
private int fence; // -1 until used; then one past last index |
1444 |
|
private int expectedModCount; // initialized when fence set |
1445 |
|
|
1446 |
< |
/** Create new spliterator covering the given range */ |
1447 |
< |
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, |
1422 |
< |
int expectedModCount) { |
1423 |
< |
this.list = list; // OK if null unless traversed |
1446 |
> |
/** Creates new spliterator covering the given range. */ |
1447 |
> |
ArrayListSpliterator(int origin, int fence, int expectedModCount) { |
1448 |
|
this.index = origin; |
1449 |
|
this.fence = fence; |
1450 |
|
this.expectedModCount = expectedModCount; |
1452 |
|
|
1453 |
|
private int getFence() { // initialize fence to size on first use |
1454 |
|
int hi; // (a specialized variant appears in method forEach) |
1431 |
– |
ArrayList<E> lst; |
1455 |
|
if ((hi = fence) < 0) { |
1456 |
< |
if ((lst = list) == null) |
1457 |
< |
hi = fence = 0; |
1435 |
< |
else { |
1436 |
< |
expectedModCount = lst.modCount; |
1437 |
< |
hi = fence = lst.size; |
1438 |
< |
} |
1456 |
> |
expectedModCount = modCount; |
1457 |
> |
hi = fence = size; |
1458 |
|
} |
1459 |
|
return hi; |
1460 |
|
} |
1461 |
|
|
1462 |
< |
public ArrayListSpliterator<E> trySplit() { |
1462 |
> |
public ArrayListSpliterator trySplit() { |
1463 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1464 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1465 |
< |
new ArrayListSpliterator<>(list, lo, index = mid, |
1447 |
< |
expectedModCount); |
1465 |
> |
new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1466 |
|
} |
1467 |
|
|
1468 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1471 |
|
int hi = getFence(), i = index; |
1472 |
|
if (i < hi) { |
1473 |
|
index = i + 1; |
1474 |
< |
@SuppressWarnings("unchecked") E e = (E)list.elementData[i]; |
1474 |
> |
@SuppressWarnings("unchecked") E e = (E)elementData[i]; |
1475 |
|
action.accept(e); |
1476 |
< |
if (list.modCount != expectedModCount) |
1476 |
> |
if (modCount != expectedModCount) |
1477 |
|
throw new ConcurrentModificationException(); |
1478 |
|
return true; |
1479 |
|
} |
1482 |
|
|
1483 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1484 |
|
int i, hi, mc; // hoist accesses and checks from loop |
1485 |
< |
ArrayList<E> lst; Object[] a; |
1485 |
> |
Object[] a; |
1486 |
|
if (action == null) |
1487 |
|
throw new NullPointerException(); |
1488 |
< |
if ((lst = list) != null && (a = lst.elementData) != null) { |
1488 |
> |
if ((a = elementData) != null) { |
1489 |
|
if ((hi = fence) < 0) { |
1490 |
< |
mc = lst.modCount; |
1491 |
< |
hi = lst.size; |
1490 |
> |
mc = modCount; |
1491 |
> |
hi = size; |
1492 |
|
} |
1493 |
|
else |
1494 |
|
mc = expectedModCount; |
1497 |
|
@SuppressWarnings("unchecked") E e = (E) a[i]; |
1498 |
|
action.accept(e); |
1499 |
|
} |
1500 |
< |
if (lst.modCount == mc) |
1500 |
> |
if (modCount == mc) |
1501 |
|
return; |
1502 |
|
} |
1503 |
|
} |
1505 |
|
} |
1506 |
|
|
1507 |
|
public long estimateSize() { |
1508 |
< |
return (long) (getFence() - index); |
1508 |
> |
return getFence() - index; |
1509 |
|
} |
1510 |
|
|
1511 |
|
public int characteristics() { |
1513 |
|
} |
1514 |
|
} |
1515 |
|
|
1516 |
+ |
// A tiny bit set implementation |
1517 |
+ |
|
1518 |
+ |
private static long[] nBits(int n) { |
1519 |
+ |
return new long[((n - 1) >> 6) + 1]; |
1520 |
+ |
} |
1521 |
+ |
private static void setBit(long[] bits, int i) { |
1522 |
+ |
bits[i >> 6] |= 1L << i; |
1523 |
+ |
} |
1524 |
+ |
private static boolean isClear(long[] bits, int i) { |
1525 |
+ |
return (bits[i >> 6] & (1L << i)) == 0; |
1526 |
+ |
} |
1527 |
+ |
|
1528 |
+ |
/** |
1529 |
+ |
* @throws NullPointerException {@inheritDoc} |
1530 |
+ |
*/ |
1531 |
|
@Override |
1532 |
|
public boolean removeIf(Predicate<? super E> filter) { |
1533 |
+ |
return removeIf(filter, 0, size); |
1534 |
+ |
} |
1535 |
+ |
|
1536 |
+ |
/** |
1537 |
+ |
* Removes all elements satisfying the given predicate, from index |
1538 |
+ |
* i (inclusive) to index end (exclusive). |
1539 |
+ |
*/ |
1540 |
+ |
boolean removeIf(Predicate<? super E> filter, int i, final int end) { |
1541 |
|
Objects.requireNonNull(filter); |
1542 |
< |
final int expectedModCount = modCount; |
1543 |
< |
final Object[] elementData = this.elementData; |
1544 |
< |
int r = 0, w = 0, remaining = size, deleted = 0; |
1545 |
< |
try { |
1546 |
< |
for (; remaining > 0; remaining--, r++) { |
1547 |
< |
@SuppressWarnings("unchecked") E e = (E) elementData[r]; |
1548 |
< |
if (filter.test(e)) |
1549 |
< |
deleted++; |
1550 |
< |
else { |
1551 |
< |
if (r != w) |
1552 |
< |
elementData[w] = e; |
1553 |
< |
w++; |
1554 |
< |
} |
1555 |
< |
} |
1542 |
> |
int expectedModCount = modCount; |
1543 |
> |
final Object[] es = elementData; |
1544 |
> |
// Optimize for initial run of survivors |
1545 |
> |
for (; i < end && !filter.test(elementAt(es, i)); i++) |
1546 |
> |
; |
1547 |
> |
// Tolerate predicates that reentrantly access the collection for |
1548 |
> |
// read (but writers still get CME), so traverse once to find |
1549 |
> |
// elements to delete, a second pass to physically expunge. |
1550 |
> |
if (i < end) { |
1551 |
> |
final int beg = i; |
1552 |
> |
final long[] deathRow = nBits(end - beg); |
1553 |
> |
deathRow[0] = 1L; // set bit 0 |
1554 |
> |
for (i = beg + 1; i < end; i++) |
1555 |
> |
if (filter.test(elementAt(es, i))) |
1556 |
> |
setBit(deathRow, i - beg); |
1557 |
|
if (modCount != expectedModCount) |
1558 |
|
throw new ConcurrentModificationException(); |
1559 |
< |
return deleted > 0; |
1560 |
< |
} catch (Throwable ex) { |
1561 |
< |
if (deleted > 0) |
1562 |
< |
for (; remaining > 0; remaining--, r++, w++) |
1563 |
< |
elementData[w] = elementData[r]; |
1564 |
< |
throw ex; |
1565 |
< |
} finally { |
1566 |
< |
if (deleted > 0) { |
1567 |
< |
modCount++; |
1568 |
< |
size -= deleted; |
1569 |
< |
while (--deleted >= 0) |
1570 |
< |
elementData[w++] = null; |
1571 |
< |
} |
1559 |
> |
expectedModCount++; |
1560 |
> |
modCount++; |
1561 |
> |
int w = beg; |
1562 |
> |
for (i = beg; i < end; i++) |
1563 |
> |
if (isClear(deathRow, i - beg)) |
1564 |
> |
es[w++] = es[i]; |
1565 |
> |
shiftTailOverGap(es, w, end); |
1566 |
> |
// checkInvariants(); |
1567 |
> |
return true; |
1568 |
> |
} else { |
1569 |
> |
if (modCount != expectedModCount) |
1570 |
> |
throw new ConcurrentModificationException(); |
1571 |
> |
// checkInvariants(); |
1572 |
> |
return false; |
1573 |
|
} |
1574 |
|
} |
1575 |
|
|
1576 |
|
@Override |
1534 |
– |
@SuppressWarnings("unchecked") |
1577 |
|
public void replaceAll(UnaryOperator<E> operator) { |
1578 |
|
Objects.requireNonNull(operator); |
1579 |
|
final int expectedModCount = modCount; |
1580 |
+ |
final Object[] es = elementData; |
1581 |
|
final int size = this.size; |
1582 |
< |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1583 |
< |
elementData[i] = operator.apply((E) elementData[i]); |
1584 |
< |
} |
1542 |
< |
if (modCount != expectedModCount) { |
1582 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1583 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1584 |
> |
if (modCount != expectedModCount) |
1585 |
|
throw new ConcurrentModificationException(); |
1544 |
– |
} |
1586 |
|
modCount++; |
1587 |
+ |
// checkInvariants(); |
1588 |
|
} |
1589 |
|
|
1590 |
|
@Override |
1592 |
|
public void sort(Comparator<? super E> c) { |
1593 |
|
final int expectedModCount = modCount; |
1594 |
|
Arrays.sort((E[]) elementData, 0, size, c); |
1595 |
< |
if (modCount != expectedModCount) { |
1595 |
> |
if (modCount != expectedModCount) |
1596 |
|
throw new ConcurrentModificationException(); |
1555 |
– |
} |
1597 |
|
modCount++; |
1598 |
+ |
// checkInvariants(); |
1599 |
+ |
} |
1600 |
+ |
|
1601 |
+ |
void checkInvariants() { |
1602 |
+ |
// assert size >= 0; |
1603 |
+ |
// assert size == elementData.length || elementData[size] == null; |
1604 |
|
} |
1605 |
|
} |