91 |
|
* should be used only to detect bugs.</i> |
92 |
|
* |
93 |
|
* <p>This class is a member of the |
94 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
94 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
95 |
|
* Java Collections Framework</a>. |
96 |
|
* |
97 |
|
* @param <E> the type of elements in this list |
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 |
|
/** |
515 |
|
*/ |
516 |
|
public E remove(int index) { |
517 |
|
Objects.checkIndex(index, size); |
518 |
+ |
final Object[] es = elementData; |
519 |
|
|
520 |
< |
modCount++; |
521 |
< |
E oldValue = elementData(index); |
516 |
< |
|
517 |
< |
int numMoved = size - index - 1; |
518 |
< |
if (numMoved > 0) |
519 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
520 |
< |
numMoved); |
521 |
< |
elementData[--size] = null; // clear to let GC do its work |
520 |
> |
@SuppressWarnings("unchecked") E oldValue = (E) es[index]; |
521 |
> |
fastRemove(es, index); |
522 |
|
|
523 |
+ |
// checkInvariants(); |
524 |
|
return oldValue; |
525 |
|
} |
526 |
|
|
538 |
|
* @return {@code true} if this list contained the specified element |
539 |
|
*/ |
540 |
|
public boolean remove(Object o) { |
541 |
< |
if (o == null) { |
542 |
< |
for (int index = 0; index < size; index++) |
543 |
< |
if (elementData[index] == null) { |
544 |
< |
fastRemove(index); |
545 |
< |
return true; |
546 |
< |
} |
547 |
< |
} else { |
548 |
< |
for (int index = 0; index < size; index++) |
549 |
< |
if (o.equals(elementData[index])) { |
550 |
< |
fastRemove(index); |
551 |
< |
return true; |
552 |
< |
} |
541 |
> |
final Object[] es = elementData; |
542 |
> |
final int size = this.size; |
543 |
> |
int i = 0; |
544 |
> |
found: { |
545 |
> |
if (o == null) { |
546 |
> |
for (; i < size; i++) |
547 |
> |
if (es[i] == null) |
548 |
> |
break found; |
549 |
> |
} else { |
550 |
> |
for (; i < size; i++) |
551 |
> |
if (o.equals(es[i])) |
552 |
> |
break found; |
553 |
> |
} |
554 |
> |
return false; |
555 |
|
} |
556 |
< |
return false; |
556 |
> |
fastRemove(es, i); |
557 |
> |
return true; |
558 |
|
} |
559 |
|
|
560 |
< |
/* |
560 |
> |
/** |
561 |
|
* Private remove method that skips bounds checking and does not |
562 |
|
* return the value removed. |
563 |
|
*/ |
564 |
< |
private void fastRemove(int index) { |
564 |
> |
private void fastRemove(Object[] es, int i) { |
565 |
|
modCount++; |
566 |
< |
int numMoved = size - index - 1; |
567 |
< |
if (numMoved > 0) |
568 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
569 |
< |
numMoved); |
566 |
< |
elementData[--size] = null; // clear to let GC do its work |
566 |
> |
final int newSize; |
567 |
> |
if ((newSize = size - 1) > i) |
568 |
> |
System.arraycopy(es, i + 1, es, i, newSize - i); |
569 |
> |
es[size = newSize] = null; |
570 |
|
} |
571 |
|
|
572 |
|
/** |
575 |
|
*/ |
576 |
|
public void clear() { |
577 |
|
modCount++; |
578 |
< |
|
579 |
< |
// clear to let GC do its work |
580 |
< |
for (int i = 0; i < size; i++) |
578 |
< |
elementData[i] = null; |
579 |
< |
|
580 |
< |
size = 0; |
578 |
> |
final Object[] es = elementData; |
579 |
> |
for (int to = size, i = size = 0; i < to; i++) |
580 |
> |
es[i] = null; |
581 |
|
} |
582 |
|
|
583 |
|
/** |
605 |
|
elementData = grow(s + numNew); |
606 |
|
System.arraycopy(a, 0, elementData, s, numNew); |
607 |
|
size = s + numNew; |
608 |
+ |
// checkInvariants(); |
609 |
|
return true; |
610 |
|
} |
611 |
|
|
644 |
|
numMoved); |
645 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
646 |
|
size = s + numNew; |
647 |
+ |
// checkInvariants(); |
648 |
|
return true; |
649 |
|
} |
650 |
|
|
667 |
|
outOfBoundsMsg(fromIndex, toIndex)); |
668 |
|
} |
669 |
|
modCount++; |
670 |
< |
int numMoved = size - toIndex; |
671 |
< |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
672 |
< |
numMoved); |
673 |
< |
|
674 |
< |
// clear to let GC do its work |
675 |
< |
int newSize = size - (toIndex-fromIndex); |
676 |
< |
for (int i = newSize; i < size; i++) { |
677 |
< |
elementData[i] = null; |
678 |
< |
} |
677 |
< |
size = newSize; |
670 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
671 |
> |
// checkInvariants(); |
672 |
> |
} |
673 |
> |
|
674 |
> |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
675 |
> |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
676 |
> |
System.arraycopy(es, hi, es, lo, size - hi); |
677 |
> |
for (int to = size, i = (size -= hi - lo); i < to; i++) |
678 |
> |
es[i] = null; |
679 |
|
} |
680 |
|
|
681 |
|
/** |
718 |
|
* @see Collection#contains(Object) |
719 |
|
*/ |
720 |
|
public boolean removeAll(Collection<?> c) { |
721 |
< |
Objects.requireNonNull(c); |
721 |
< |
return batchRemove(c, false); |
721 |
> |
return batchRemove(c, false, 0, size); |
722 |
|
} |
723 |
|
|
724 |
|
/** |
738 |
|
* @see Collection#contains(Object) |
739 |
|
*/ |
740 |
|
public boolean retainAll(Collection<?> c) { |
741 |
< |
Objects.requireNonNull(c); |
742 |
< |
return batchRemove(c, true); |
741 |
> |
return batchRemove(c, true, 0, size); |
742 |
|
} |
743 |
|
|
744 |
< |
private boolean batchRemove(Collection<?> c, boolean complement) { |
745 |
< |
final Object[] elementData = this.elementData; |
746 |
< |
int r = 0, w = 0; |
747 |
< |
boolean modified = false; |
748 |
< |
try { |
749 |
< |
for (; r < size; r++) |
750 |
< |
if (c.contains(elementData[r]) == complement) |
751 |
< |
elementData[w++] = elementData[r]; |
752 |
< |
} finally { |
753 |
< |
// Preserve behavioral compatibility with AbstractCollection, |
754 |
< |
// even if c.contains() throws. |
755 |
< |
if (r != size) { |
756 |
< |
System.arraycopy(elementData, r, |
757 |
< |
elementData, w, |
758 |
< |
size - r); |
759 |
< |
w += size - r; |
760 |
< |
} |
761 |
< |
if (w != size) { |
762 |
< |
// clear to let GC do its work |
763 |
< |
for (int i = w; i < size; i++) |
764 |
< |
elementData[i] = null; |
765 |
< |
modCount += size - w; |
766 |
< |
size = w; |
767 |
< |
modified = true; |
744 |
> |
boolean batchRemove(Collection<?> c, boolean complement, |
745 |
> |
final int from, final int end) { |
746 |
> |
Objects.requireNonNull(c); |
747 |
> |
final Object[] es = elementData; |
748 |
> |
final boolean modified; |
749 |
> |
int r; |
750 |
> |
// Optimize for initial run of survivors |
751 |
> |
for (r = from; r < end && c.contains(es[r]) == complement; r++) |
752 |
> |
; |
753 |
> |
if (modified = (r < end)) { |
754 |
> |
int w = r++; |
755 |
> |
try { |
756 |
> |
for (Object e; r < end; r++) |
757 |
> |
if (c.contains(e = es[r]) == complement) |
758 |
> |
es[w++] = e; |
759 |
> |
} catch (Throwable ex) { |
760 |
> |
// Preserve behavioral compatibility with AbstractCollection, |
761 |
> |
// even if c.contains() throws. |
762 |
> |
System.arraycopy(es, r, es, w, end - r); |
763 |
> |
w += end - r; |
764 |
> |
throw ex; |
765 |
> |
} finally { |
766 |
> |
modCount += end - w; |
767 |
> |
shiftTailOverGap(es, w, end); |
768 |
|
} |
769 |
|
} |
770 |
+ |
// checkInvariants(); |
771 |
|
return modified; |
772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
< |
* Save the state of the {@code ArrayList} instance to a stream (that |
776 |
< |
* is, serialize it). |
775 |
> |
* Saves the state of the {@code ArrayList} instance to a stream |
776 |
> |
* (that is, serializes it). |
777 |
|
* |
778 |
+ |
* @param s the stream |
779 |
+ |
* @throws java.io.IOException if an I/O error occurs |
780 |
|
* @serialData The length of the array backing the {@code ArrayList} |
781 |
|
* instance is emitted (int), followed by all of its elements |
782 |
|
* (each an {@code Object}) in the proper order. |
783 |
|
*/ |
784 |
|
private void writeObject(java.io.ObjectOutputStream s) |
785 |
< |
throws java.io.IOException{ |
785 |
> |
throws java.io.IOException { |
786 |
|
// Write out element count, and any hidden stuff |
787 |
|
int expectedModCount = modCount; |
788 |
|
s.defaultWriteObject(); |
789 |
|
|
790 |
< |
// Write out size as capacity for behavioural compatibility with clone() |
790 |
> |
// Write out size as capacity for behavioral compatibility with clone() |
791 |
|
s.writeInt(size); |
792 |
|
|
793 |
|
// Write out all elements in the proper order. |
801 |
|
} |
802 |
|
|
803 |
|
/** |
804 |
< |
* Reconstitute the {@code ArrayList} instance from a stream (that is, |
805 |
< |
* deserialize it). |
804 |
> |
* Reconstitutes the {@code ArrayList} instance from a stream (that is, |
805 |
> |
* deserializes it). |
806 |
> |
* @param s the stream |
807 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
808 |
> |
* could not be found |
809 |
> |
* @throws java.io.IOException if an I/O error occurs |
810 |
|
*/ |
811 |
|
private void readObject(java.io.ObjectInputStream s) |
812 |
|
throws java.io.IOException, ClassNotFoundException { |
918 |
|
} |
919 |
|
|
920 |
|
@Override |
921 |
< |
@SuppressWarnings("unchecked") |
922 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
917 |
< |
Objects.requireNonNull(consumer); |
921 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
922 |
> |
Objects.requireNonNull(action); |
923 |
|
final int size = ArrayList.this.size; |
924 |
|
int i = cursor; |
925 |
< |
if (i >= size) { |
926 |
< |
return; |
927 |
< |
} |
928 |
< |
final Object[] elementData = ArrayList.this.elementData; |
929 |
< |
if (i >= elementData.length) { |
930 |
< |
throw new ConcurrentModificationException(); |
931 |
< |
} |
932 |
< |
while (i != size && modCount == expectedModCount) { |
933 |
< |
consumer.accept((E) elementData[i++]); |
925 |
> |
if (i < size) { |
926 |
> |
final Object[] es = elementData; |
927 |
> |
if (i >= es.length) |
928 |
> |
throw new ConcurrentModificationException(); |
929 |
> |
for (; i < size && modCount == expectedModCount; i++) |
930 |
> |
action.accept(elementAt(es, i)); |
931 |
> |
// update once at end to reduce heap write traffic |
932 |
> |
cursor = i; |
933 |
> |
lastRet = i - 1; |
934 |
> |
checkForComodification(); |
935 |
|
} |
930 |
– |
// update once at end of iteration to reduce heap write traffic |
931 |
– |
cursor = i; |
932 |
– |
lastRet = i - 1; |
933 |
– |
checkForComodification(); |
936 |
|
} |
937 |
|
|
938 |
|
final void checkForComodification() { |
1119 |
|
return true; |
1120 |
|
} |
1121 |
|
|
1122 |
+ |
public boolean removeAll(Collection<?> c) { |
1123 |
+ |
return batchRemove(c, false); |
1124 |
+ |
} |
1125 |
+ |
|
1126 |
+ |
public boolean retainAll(Collection<?> c) { |
1127 |
+ |
return batchRemove(c, true); |
1128 |
+ |
} |
1129 |
+ |
|
1130 |
+ |
private boolean batchRemove(Collection<?> c, boolean complement) { |
1131 |
+ |
checkForComodification(); |
1132 |
+ |
int oldSize = root.size; |
1133 |
+ |
boolean modified = |
1134 |
+ |
root.batchRemove(c, complement, offset, offset + size); |
1135 |
+ |
if (modified) |
1136 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1137 |
+ |
return modified; |
1138 |
+ |
} |
1139 |
+ |
|
1140 |
+ |
public boolean removeIf(Predicate<? super E> filter) { |
1141 |
+ |
checkForComodification(); |
1142 |
+ |
int oldSize = root.size; |
1143 |
+ |
boolean modified = root.removeIf(filter, offset, offset + size); |
1144 |
+ |
if (modified) |
1145 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1146 |
+ |
return modified; |
1147 |
+ |
} |
1148 |
+ |
|
1149 |
|
public Iterator<E> iterator() { |
1150 |
|
return listIterator(); |
1151 |
|
} |
1193 |
|
return (E) elementData[offset + (lastRet = i)]; |
1194 |
|
} |
1195 |
|
|
1196 |
< |
@SuppressWarnings("unchecked") |
1197 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
1169 |
< |
Objects.requireNonNull(consumer); |
1196 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1197 |
> |
Objects.requireNonNull(action); |
1198 |
|
final int size = SubList.this.size; |
1199 |
|
int i = cursor; |
1200 |
< |
if (i >= size) { |
1201 |
< |
return; |
1202 |
< |
} |
1203 |
< |
final Object[] elementData = root.elementData; |
1204 |
< |
if (offset + i >= elementData.length) { |
1205 |
< |
throw new ConcurrentModificationException(); |
1206 |
< |
} |
1207 |
< |
while (i != size && modCount == expectedModCount) { |
1208 |
< |
consumer.accept((E) elementData[offset + (i++)]); |
1200 |
> |
if (i < size) { |
1201 |
> |
final Object[] es = root.elementData; |
1202 |
> |
if (offset + i >= es.length) |
1203 |
> |
throw new ConcurrentModificationException(); |
1204 |
> |
for (; i < size && modCount == expectedModCount; i++) |
1205 |
> |
action.accept(elementAt(es, offset + i)); |
1206 |
> |
// update once at end to reduce heap write traffic |
1207 |
> |
cursor = i; |
1208 |
> |
lastRet = i - 1; |
1209 |
> |
checkForComodification(); |
1210 |
|
} |
1182 |
– |
// update once at end of iteration to reduce heap write traffic |
1183 |
– |
lastRet = cursor = i; |
1184 |
– |
checkForComodification(); |
1211 |
|
} |
1212 |
|
|
1213 |
|
public int nextIndex() { |
1297 |
|
public Spliterator<E> spliterator() { |
1298 |
|
checkForComodification(); |
1299 |
|
|
1300 |
< |
// ArrayListSpliterator is not used because late-binding logic |
1301 |
< |
// is different here |
1276 |
< |
return new Spliterator<>() { |
1300 |
> |
// ArrayListSpliterator not used here due to late-binding |
1301 |
> |
return new Spliterator<E>() { |
1302 |
|
private int index = offset; // current index, modified on advance/split |
1303 |
|
private int fence = -1; // -1 until used; then one past last index |
1304 |
|
private int expectedModCount; // initialized when fence set |
1312 |
|
return hi; |
1313 |
|
} |
1314 |
|
|
1315 |
< |
public ArrayListSpliterator<E> trySplit() { |
1315 |
> |
public ArrayList<E>.ArrayListSpliterator trySplit() { |
1316 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1317 |
< |
// ArrayListSpliterator could be used here as the source is already bound |
1317 |
> |
// ArrayListSpliterator can be used here as the source is already bound |
1318 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1319 |
< |
new ArrayListSpliterator<>(root, lo, index = mid, |
1295 |
< |
expectedModCount); |
1319 |
> |
root.new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1320 |
|
} |
1321 |
|
|
1322 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1358 |
|
} |
1359 |
|
|
1360 |
|
public long estimateSize() { |
1361 |
< |
return (long) (getFence() - index); |
1361 |
> |
return getFence() - index; |
1362 |
|
} |
1363 |
|
|
1364 |
|
public int characteristics() { |
1368 |
|
} |
1369 |
|
} |
1370 |
|
|
1371 |
+ |
/** |
1372 |
+ |
* @throws NullPointerException {@inheritDoc} |
1373 |
+ |
*/ |
1374 |
|
@Override |
1375 |
|
public void forEach(Consumer<? super E> action) { |
1376 |
|
Objects.requireNonNull(action); |
1377 |
|
final int expectedModCount = modCount; |
1378 |
< |
@SuppressWarnings("unchecked") |
1352 |
< |
final E[] elementData = (E[]) this.elementData; |
1378 |
> |
final Object[] es = elementData; |
1379 |
|
final int size = this.size; |
1380 |
< |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1381 |
< |
action.accept(elementData[i]); |
1382 |
< |
} |
1357 |
< |
if (modCount != expectedModCount) { |
1380 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1381 |
> |
action.accept(elementAt(es, i)); |
1382 |
> |
if (modCount != expectedModCount) |
1383 |
|
throw new ConcurrentModificationException(); |
1359 |
– |
} |
1384 |
|
} |
1385 |
|
|
1386 |
|
/** |
1398 |
|
*/ |
1399 |
|
@Override |
1400 |
|
public Spliterator<E> spliterator() { |
1401 |
< |
return new ArrayListSpliterator<>(this, 0, -1, 0); |
1401 |
> |
return new ArrayListSpliterator(0, -1, 0); |
1402 |
|
} |
1403 |
|
|
1404 |
|
/** Index-based split-by-two, lazily initialized Spliterator */ |
1405 |
< |
static final class ArrayListSpliterator<E> implements Spliterator<E> { |
1405 |
> |
final class ArrayListSpliterator implements Spliterator<E> { |
1406 |
|
|
1407 |
|
/* |
1408 |
|
* If ArrayLists were immutable, or structurally immutable (no |
1436 |
|
* these streamlinings. |
1437 |
|
*/ |
1438 |
|
|
1415 |
– |
private final ArrayList<E> list; |
1439 |
|
private int index; // current index, modified on advance/split |
1440 |
|
private int fence; // -1 until used; then one past last index |
1441 |
|
private int expectedModCount; // initialized when fence set |
1442 |
|
|
1443 |
< |
/** Create new spliterator covering the given range */ |
1444 |
< |
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, |
1422 |
< |
int expectedModCount) { |
1423 |
< |
this.list = list; // OK if null unless traversed |
1443 |
> |
/** Creates new spliterator covering the given range. */ |
1444 |
> |
ArrayListSpliterator(int origin, int fence, int expectedModCount) { |
1445 |
|
this.index = origin; |
1446 |
|
this.fence = fence; |
1447 |
|
this.expectedModCount = expectedModCount; |
1449 |
|
|
1450 |
|
private int getFence() { // initialize fence to size on first use |
1451 |
|
int hi; // (a specialized variant appears in method forEach) |
1431 |
– |
ArrayList<E> lst; |
1452 |
|
if ((hi = fence) < 0) { |
1453 |
< |
if ((lst = list) == null) |
1454 |
< |
hi = fence = 0; |
1435 |
< |
else { |
1436 |
< |
expectedModCount = lst.modCount; |
1437 |
< |
hi = fence = lst.size; |
1438 |
< |
} |
1453 |
> |
expectedModCount = modCount; |
1454 |
> |
hi = fence = size; |
1455 |
|
} |
1456 |
|
return hi; |
1457 |
|
} |
1458 |
|
|
1459 |
< |
public ArrayListSpliterator<E> trySplit() { |
1459 |
> |
public ArrayListSpliterator trySplit() { |
1460 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1461 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1462 |
< |
new ArrayListSpliterator<>(list, lo, index = mid, |
1447 |
< |
expectedModCount); |
1462 |
> |
new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1463 |
|
} |
1464 |
|
|
1465 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1468 |
|
int hi = getFence(), i = index; |
1469 |
|
if (i < hi) { |
1470 |
|
index = i + 1; |
1471 |
< |
@SuppressWarnings("unchecked") E e = (E)list.elementData[i]; |
1471 |
> |
@SuppressWarnings("unchecked") E e = (E)elementData[i]; |
1472 |
|
action.accept(e); |
1473 |
< |
if (list.modCount != expectedModCount) |
1473 |
> |
if (modCount != expectedModCount) |
1474 |
|
throw new ConcurrentModificationException(); |
1475 |
|
return true; |
1476 |
|
} |
1479 |
|
|
1480 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1481 |
|
int i, hi, mc; // hoist accesses and checks from loop |
1482 |
< |
ArrayList<E> lst; Object[] a; |
1482 |
> |
Object[] a; |
1483 |
|
if (action == null) |
1484 |
|
throw new NullPointerException(); |
1485 |
< |
if ((lst = list) != null && (a = lst.elementData) != null) { |
1485 |
> |
if ((a = elementData) != null) { |
1486 |
|
if ((hi = fence) < 0) { |
1487 |
< |
mc = lst.modCount; |
1488 |
< |
hi = lst.size; |
1487 |
> |
mc = modCount; |
1488 |
> |
hi = size; |
1489 |
|
} |
1490 |
|
else |
1491 |
|
mc = expectedModCount; |
1494 |
|
@SuppressWarnings("unchecked") E e = (E) a[i]; |
1495 |
|
action.accept(e); |
1496 |
|
} |
1497 |
< |
if (lst.modCount == mc) |
1497 |
> |
if (modCount == mc) |
1498 |
|
return; |
1499 |
|
} |
1500 |
|
} |
1502 |
|
} |
1503 |
|
|
1504 |
|
public long estimateSize() { |
1505 |
< |
return (long) (getFence() - index); |
1505 |
> |
return getFence() - index; |
1506 |
|
} |
1507 |
|
|
1508 |
|
public int characteristics() { |
1510 |
|
} |
1511 |
|
} |
1512 |
|
|
1513 |
+ |
// A tiny bit set implementation |
1514 |
+ |
|
1515 |
+ |
private static long[] nBits(int n) { |
1516 |
+ |
return new long[((n - 1) >> 6) + 1]; |
1517 |
+ |
} |
1518 |
+ |
private static void setBit(long[] bits, int i) { |
1519 |
+ |
bits[i >> 6] |= 1L << i; |
1520 |
+ |
} |
1521 |
+ |
private static boolean isClear(long[] bits, int i) { |
1522 |
+ |
return (bits[i >> 6] & (1L << i)) == 0; |
1523 |
+ |
} |
1524 |
+ |
|
1525 |
+ |
/** |
1526 |
+ |
* @throws NullPointerException {@inheritDoc} |
1527 |
+ |
*/ |
1528 |
|
@Override |
1529 |
|
public boolean removeIf(Predicate<? super E> filter) { |
1530 |
+ |
return removeIf(filter, 0, size); |
1531 |
+ |
} |
1532 |
+ |
|
1533 |
+ |
/** |
1534 |
+ |
* Removes all elements satisfying the given predicate, from index |
1535 |
+ |
* i (inclusive) to index end (exclusive). |
1536 |
+ |
*/ |
1537 |
+ |
boolean removeIf(Predicate<? super E> filter, int i, final int end) { |
1538 |
|
Objects.requireNonNull(filter); |
1539 |
< |
final int expectedModCount = modCount; |
1540 |
< |
final Object[] elementData = this.elementData; |
1541 |
< |
int r = 0, w = 0, remaining = size, deleted = 0; |
1542 |
< |
try { |
1543 |
< |
for (; remaining > 0; remaining--, r++) { |
1544 |
< |
@SuppressWarnings("unchecked") E e = (E) elementData[r]; |
1545 |
< |
if (filter.test(e)) |
1546 |
< |
deleted++; |
1547 |
< |
else { |
1548 |
< |
if (r != w) |
1549 |
< |
elementData[w] = e; |
1550 |
< |
w++; |
1551 |
< |
} |
1552 |
< |
} |
1539 |
> |
int expectedModCount = modCount; |
1540 |
> |
final Object[] es = elementData; |
1541 |
> |
// Optimize for initial run of survivors |
1542 |
> |
for (; i < end && !filter.test(elementAt(es, i)); i++) |
1543 |
> |
; |
1544 |
> |
// Tolerate predicates that reentrantly access the collection for |
1545 |
> |
// read (but writers still get CME), so traverse once to find |
1546 |
> |
// elements to delete, a second pass to physically expunge. |
1547 |
> |
if (i < end) { |
1548 |
> |
final int beg = i; |
1549 |
> |
final long[] deathRow = nBits(end - beg); |
1550 |
> |
deathRow[0] = 1L; // set bit 0 |
1551 |
> |
for (i = beg + 1; i < end; i++) |
1552 |
> |
if (filter.test(elementAt(es, i))) |
1553 |
> |
setBit(deathRow, i - beg); |
1554 |
|
if (modCount != expectedModCount) |
1555 |
|
throw new ConcurrentModificationException(); |
1556 |
< |
return deleted > 0; |
1557 |
< |
} catch (Throwable ex) { |
1558 |
< |
for (; remaining > 0; remaining--, r++, w++) |
1559 |
< |
elementData[w] = elementData[r]; |
1560 |
< |
throw ex; |
1561 |
< |
} finally { |
1562 |
< |
if (deleted > 0) { |
1563 |
< |
modCount++; |
1564 |
< |
size -= deleted; |
1565 |
< |
while (--deleted >= 0) |
1566 |
< |
elementData[w++] = null; |
1567 |
< |
} |
1556 |
> |
expectedModCount++; |
1557 |
> |
modCount++; |
1558 |
> |
int w = beg; |
1559 |
> |
for (i = beg; i < end; i++) |
1560 |
> |
if (isClear(deathRow, i - beg)) |
1561 |
> |
es[w++] = es[i]; |
1562 |
> |
shiftTailOverGap(es, w, end); |
1563 |
> |
// checkInvariants(); |
1564 |
> |
return true; |
1565 |
> |
} else { |
1566 |
> |
if (modCount != expectedModCount) |
1567 |
> |
throw new ConcurrentModificationException(); |
1568 |
> |
// checkInvariants(); |
1569 |
> |
return false; |
1570 |
|
} |
1571 |
|
} |
1572 |
|
|
1573 |
|
@Override |
1533 |
– |
@SuppressWarnings("unchecked") |
1574 |
|
public void replaceAll(UnaryOperator<E> operator) { |
1575 |
|
Objects.requireNonNull(operator); |
1576 |
|
final int expectedModCount = modCount; |
1577 |
+ |
final Object[] es = elementData; |
1578 |
|
final int size = this.size; |
1579 |
< |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1580 |
< |
elementData[i] = operator.apply((E) elementData[i]); |
1581 |
< |
} |
1541 |
< |
if (modCount != expectedModCount) { |
1579 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1580 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1581 |
> |
if (modCount != expectedModCount) |
1582 |
|
throw new ConcurrentModificationException(); |
1543 |
– |
} |
1583 |
|
modCount++; |
1584 |
+ |
// checkInvariants(); |
1585 |
|
} |
1586 |
|
|
1587 |
|
@Override |
1589 |
|
public void sort(Comparator<? super E> c) { |
1590 |
|
final int expectedModCount = modCount; |
1591 |
|
Arrays.sort((E[]) elementData, 0, size, c); |
1592 |
< |
if (modCount != expectedModCount) { |
1592 |
> |
if (modCount != expectedModCount) |
1593 |
|
throw new ConcurrentModificationException(); |
1554 |
– |
} |
1594 |
|
modCount++; |
1595 |
+ |
// checkInvariants(); |
1596 |
+ |
} |
1597 |
+ |
|
1598 |
+ |
void checkInvariants() { |
1599 |
+ |
// assert size >= 0; |
1600 |
+ |
// assert size == elementData.length || elementData[size] == null; |
1601 |
|
} |
1602 |
|
} |