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 |
|
* |
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 |
> |
final int oldSize = size, deleted = end - w; |
770 |
> |
modCount += deleted; |
771 |
> |
System.arraycopy(es, end, es, w, oldSize - end); |
772 |
> |
Arrays.fill(es, size -= deleted, oldSize, null); |
773 |
|
} |
774 |
|
} |
775 |
|
return modified; |
1121 |
|
return true; |
1122 |
|
} |
1123 |
|
|
1124 |
+ |
public boolean removeAll(Collection<?> c) { |
1125 |
+ |
return batchRemove(c, false); |
1126 |
+ |
} |
1127 |
+ |
public boolean retainAll(Collection<?> c) { |
1128 |
+ |
return batchRemove(c, true); |
1129 |
+ |
} |
1130 |
+ |
|
1131 |
+ |
private boolean batchRemove(Collection<?> c, boolean complement) { |
1132 |
+ |
checkForComodification(); |
1133 |
+ |
int oldSize = root.size; |
1134 |
+ |
boolean modified = |
1135 |
+ |
root.batchRemove(c, complement, offset, offset + size); |
1136 |
+ |
if (modified) |
1137 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1138 |
+ |
return modified; |
1139 |
+ |
} |
1140 |
+ |
|
1141 |
+ |
public boolean removeIf(Predicate<? super E> filter) { |
1142 |
+ |
checkForComodification(); |
1143 |
+ |
int oldSize = root.size; |
1144 |
+ |
boolean modified = root.removeIf(filter, offset, offset + size); |
1145 |
+ |
if (modified) |
1146 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1147 |
+ |
return modified; |
1148 |
+ |
} |
1149 |
+ |
|
1150 |
|
public Iterator<E> iterator() { |
1151 |
|
return listIterator(); |
1152 |
|
} |
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]); |
1383 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) { |
1384 |
> |
action.accept(elementAt(es, i)); |
1385 |
|
} |
1386 |
|
if (modCount != expectedModCount) { |
1387 |
|
throw new ConcurrentModificationException(); |
1446 |
|
private int fence; // -1 until used; then one past last index |
1447 |
|
private int expectedModCount; // initialized when fence set |
1448 |
|
|
1449 |
< |
/** Create new spliterator covering the given range */ |
1449 |
> |
/** Create new spliterator covering the given range */ |
1450 |
|
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, |
1451 |
|
int expectedModCount) { |
1452 |
|
this.list = list; // OK if null unless traversed |
1524 |
|
} |
1525 |
|
} |
1526 |
|
|
1527 |
+ |
// A tiny bit set implementation |
1528 |
+ |
|
1529 |
+ |
private static long[] nBits(int n) { |
1530 |
+ |
return new long[((n - 1) >> 6) + 1]; |
1531 |
+ |
} |
1532 |
+ |
private static void setBit(long[] bits, int i) { |
1533 |
+ |
bits[i >> 6] |= 1L << i; |
1534 |
+ |
} |
1535 |
+ |
private static boolean isClear(long[] bits, int i) { |
1536 |
+ |
return (bits[i >> 6] & (1L << i)) == 0; |
1537 |
+ |
} |
1538 |
+ |
|
1539 |
|
@Override |
1540 |
|
public boolean removeIf(Predicate<? super E> filter) { |
1541 |
+ |
return removeIf(filter, 0, size); |
1542 |
+ |
} |
1543 |
+ |
|
1544 |
+ |
boolean removeIf(Predicate<? super E> filter, |
1545 |
+ |
final int from, final int end) { |
1546 |
|
Objects.requireNonNull(filter); |
1547 |
< |
final int expectedModCount = modCount; |
1548 |
< |
final Object[] elementData = this.elementData; |
1549 |
< |
int r = 0, w = 0, remaining = size, deleted = 0; |
1550 |
< |
try { |
1551 |
< |
for (; remaining > 0; remaining--, r++) { |
1552 |
< |
@SuppressWarnings("unchecked") E e = (E) elementData[r]; |
1553 |
< |
if (filter.test(e)) |
1554 |
< |
deleted++; |
1555 |
< |
else { |
1556 |
< |
if (r != w) |
1557 |
< |
elementData[w] = e; |
1558 |
< |
w++; |
1559 |
< |
} |
1560 |
< |
} |
1547 |
> |
int expectedModCount = modCount; |
1548 |
> |
final Object[] es = elementData; |
1549 |
> |
final boolean modified; |
1550 |
> |
int i; |
1551 |
> |
// Optimize for initial run of survivors |
1552 |
> |
for (i = from; i < end && !filter.test(elementAt(es, i)); i++) |
1553 |
> |
; |
1554 |
> |
// Tolerate predicates that reentrantly access the collection for |
1555 |
> |
// read (but writers still get CME), so traverse once to find |
1556 |
> |
// elements to delete, a second pass to physically expunge. |
1557 |
> |
if (modified = (i < end)) { |
1558 |
> |
expectedModCount++; |
1559 |
> |
modCount++; |
1560 |
> |
final int beg = i; |
1561 |
> |
final long[] deathRow = nBits(end - beg); |
1562 |
> |
deathRow[0] = 1L; // set bit 0 |
1563 |
> |
for (i = beg + 1; i < end; i++) |
1564 |
> |
if (filter.test(elementAt(es, i))) |
1565 |
> |
setBit(deathRow, i - beg); |
1566 |
|
if (modCount != expectedModCount) |
1567 |
|
throw new ConcurrentModificationException(); |
1568 |
< |
return deleted > 0; |
1569 |
< |
} catch (Throwable ex) { |
1570 |
< |
for (; remaining > 0; remaining--, r++, w++) |
1571 |
< |
elementData[w] = elementData[r]; |
1572 |
< |
throw ex; |
1573 |
< |
} finally { |
1574 |
< |
if (deleted > 0) { |
1524 |
< |
modCount++; |
1525 |
< |
size -= deleted; |
1526 |
< |
while (--deleted >= 0) |
1527 |
< |
elementData[w++] = null; |
1528 |
< |
} |
1568 |
> |
int w = beg; |
1569 |
> |
for (i = beg; i < end; i++) |
1570 |
> |
if (isClear(deathRow, i - beg)) |
1571 |
> |
es[w++] = es[i]; |
1572 |
> |
final int oldSize = size; |
1573 |
> |
System.arraycopy(es, end, es, w, oldSize - end); |
1574 |
> |
Arrays.fill(es, size -= (end - w), oldSize, null); |
1575 |
|
} |
1576 |
+ |
if (modCount != expectedModCount) |
1577 |
+ |
throw new ConcurrentModificationException(); |
1578 |
+ |
return modified; |
1579 |
|
} |
1580 |
|
|
1581 |
|
@Override |
1533 |
– |
@SuppressWarnings("unchecked") |
1582 |
|
public void replaceAll(UnaryOperator<E> operator) { |
1583 |
|
Objects.requireNonNull(operator); |
1584 |
|
final int expectedModCount = modCount; |
1585 |
+ |
final Object[] es = elementData; |
1586 |
|
final int size = this.size; |
1587 |
|
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1588 |
< |
elementData[i] = operator.apply((E) elementData[i]); |
1588 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1589 |
|
} |
1590 |
|
if (modCount != expectedModCount) { |
1591 |
|
throw new ConcurrentModificationException(); |