721 |
|
* @see Collection#contains(Object) |
722 |
|
*/ |
723 |
|
public boolean removeAll(Collection<?> c) { |
724 |
< |
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 |
< |
return batchRemove(c, true); |
744 |
> |
return batchRemove(c, true, 0, size); |
745 |
|
} |
746 |
|
|
747 |
< |
private boolean batchRemove(Collection<?> c, boolean complement) { |
747 |
> |
boolean batchRemove(Collection<?> c, boolean complement, |
748 |
> |
final int from, final int end) { |
749 |
|
Objects.requireNonNull(c); |
750 |
|
final Object[] es = elementData; |
750 |
– |
final int end = size; |
751 |
|
final boolean modified; |
752 |
|
int r; |
753 |
|
// Optimize for initial run of survivors |
754 |
< |
for (r = 0; r < end && c.contains(es[r]) == complement; r++) |
754 |
> |
for (r = from; r < end && c.contains(es[r]) == complement; r++) |
755 |
|
; |
756 |
|
if (modified = (r < end)) { |
757 |
|
int w = r++; |
766 |
|
w += end - r; |
767 |
|
throw ex; |
768 |
|
} finally { |
769 |
< |
modCount += end - w; |
770 |
< |
Arrays.fill(es, size = w, end, null); |
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 |
|
} |
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 |
1537 |
|
} |
1538 |
|
|
1539 |
|
@Override |
1540 |
< |
public boolean removeIf(Predicate<? super E> filter) { |
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 |
|
int expectedModCount = modCount; |
1548 |
|
final Object[] es = elementData; |
1516 |
– |
final int end = size; |
1549 |
|
final boolean modified; |
1550 |
|
int i; |
1551 |
|
// Optimize for initial run of survivors |
1552 |
< |
for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) |
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 |
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 |
|
int w = beg; |
1569 |
|
for (i = beg; i < end; i++) |
1570 |
|
if (isClear(deathRow, i - beg)) |
1571 |
|
es[w++] = es[i]; |
1572 |
< |
Arrays.fill(es, size = w, end, null); |
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(); |