1 |
|
/* |
2 |
< |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. |
3 |
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
|
* This code is free software; you can redistribute it and/or modify it |
28 |
|
import java.util.function.Consumer; |
29 |
|
import java.util.function.Predicate; |
30 |
|
import java.util.function.UnaryOperator; |
31 |
+ |
import jdk.internal.misc.SharedSecrets; |
32 |
|
|
33 |
|
/** |
34 |
|
* Resizable-array implementation of the {@code List} interface. Implements |
92 |
|
* should be used only to detect bugs.</i> |
93 |
|
* |
94 |
|
* <p>This class is a member of the |
95 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
95 |
> |
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework"> |
96 |
|
* Java Collections Framework</a>. |
97 |
|
* |
98 |
|
* @param <E> the type of elements in this list |
424 |
|
return (E) elementData[index]; |
425 |
|
} |
426 |
|
|
427 |
+ |
@SuppressWarnings("unchecked") |
428 |
+ |
static <E> E elementAt(Object[] es, int index) { |
429 |
+ |
return (E) es[index]; |
430 |
+ |
} |
431 |
+ |
|
432 |
|
/** |
433 |
|
* Returns the element at the specified position in this list. |
434 |
|
* |
502 |
|
s - index); |
503 |
|
elementData[index] = element; |
504 |
|
size = s + 1; |
505 |
+ |
// checkInvariants(); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
516 |
|
*/ |
517 |
|
public E remove(int index) { |
518 |
|
Objects.checkIndex(index, size); |
519 |
+ |
final Object[] es = elementData; |
520 |
|
|
521 |
< |
modCount++; |
522 |
< |
E oldValue = elementData(index); |
515 |
< |
|
516 |
< |
int numMoved = size - index - 1; |
517 |
< |
if (numMoved > 0) |
518 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
519 |
< |
numMoved); |
520 |
< |
elementData[--size] = null; // clear to let GC do its work |
521 |
> |
@SuppressWarnings("unchecked") E oldValue = (E) es[index]; |
522 |
> |
fastRemove(es, index); |
523 |
|
|
524 |
+ |
// checkInvariants(); |
525 |
|
return oldValue; |
526 |
|
} |
527 |
|
|
539 |
|
* @return {@code true} if this list contained the specified element |
540 |
|
*/ |
541 |
|
public boolean remove(Object o) { |
542 |
< |
if (o == null) { |
543 |
< |
for (int index = 0; index < size; index++) |
544 |
< |
if (elementData[index] == null) { |
545 |
< |
fastRemove(index); |
546 |
< |
return true; |
547 |
< |
} |
548 |
< |
} else { |
549 |
< |
for (int index = 0; index < size; index++) |
550 |
< |
if (o.equals(elementData[index])) { |
551 |
< |
fastRemove(index); |
552 |
< |
return true; |
553 |
< |
} |
542 |
> |
final Object[] es = elementData; |
543 |
> |
final int size = this.size; |
544 |
> |
int i = 0; |
545 |
> |
found: { |
546 |
> |
if (o == null) { |
547 |
> |
for (; i < size; i++) |
548 |
> |
if (es[i] == null) |
549 |
> |
break found; |
550 |
> |
} else { |
551 |
> |
for (; i < size; i++) |
552 |
> |
if (o.equals(es[i])) |
553 |
> |
break found; |
554 |
> |
} |
555 |
> |
return false; |
556 |
|
} |
557 |
< |
return false; |
557 |
> |
fastRemove(es, i); |
558 |
> |
return true; |
559 |
|
} |
560 |
|
|
561 |
< |
/* |
561 |
> |
/** |
562 |
|
* Private remove method that skips bounds checking and does not |
563 |
|
* return the value removed. |
564 |
|
*/ |
565 |
< |
private void fastRemove(int index) { |
565 |
> |
private void fastRemove(Object[] es, int i) { |
566 |
|
modCount++; |
567 |
< |
int numMoved = size - index - 1; |
568 |
< |
if (numMoved > 0) |
569 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
570 |
< |
numMoved); |
565 |
< |
elementData[--size] = null; // clear to let GC do its work |
567 |
> |
final int newSize; |
568 |
> |
if ((newSize = size - 1) > i) |
569 |
> |
System.arraycopy(es, i + 1, es, i, newSize - i); |
570 |
> |
es[size = newSize] = null; |
571 |
|
} |
572 |
|
|
573 |
|
/** |
576 |
|
*/ |
577 |
|
public void clear() { |
578 |
|
modCount++; |
579 |
< |
|
580 |
< |
// clear to let GC do its work |
581 |
< |
for (int i = 0; i < size; i++) |
577 |
< |
elementData[i] = null; |
578 |
< |
|
579 |
< |
size = 0; |
579 |
> |
final Object[] es = elementData; |
580 |
> |
for (int to = size, i = size = 0; i < to; i++) |
581 |
> |
es[i] = null; |
582 |
|
} |
583 |
|
|
584 |
|
/** |
606 |
|
elementData = grow(s + numNew); |
607 |
|
System.arraycopy(a, 0, elementData, s, numNew); |
608 |
|
size = s + numNew; |
609 |
+ |
// checkInvariants(); |
610 |
|
return true; |
611 |
|
} |
612 |
|
|
645 |
|
numMoved); |
646 |
|
System.arraycopy(a, 0, elementData, index, numNew); |
647 |
|
size = s + numNew; |
648 |
+ |
// checkInvariants(); |
649 |
|
return true; |
650 |
|
} |
651 |
|
|
668 |
|
outOfBoundsMsg(fromIndex, toIndex)); |
669 |
|
} |
670 |
|
modCount++; |
671 |
< |
int numMoved = size - toIndex; |
672 |
< |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
673 |
< |
numMoved); |
674 |
< |
|
675 |
< |
// clear to let GC do its work |
676 |
< |
int newSize = size - (toIndex-fromIndex); |
677 |
< |
for (int i = newSize; i < size; i++) { |
678 |
< |
elementData[i] = null; |
679 |
< |
} |
676 |
< |
size = newSize; |
671 |
> |
shiftTailOverGap(elementData, fromIndex, toIndex); |
672 |
> |
// checkInvariants(); |
673 |
> |
} |
674 |
> |
|
675 |
> |
/** Erases the gap from lo to hi, by sliding down following elements. */ |
676 |
> |
private void shiftTailOverGap(Object[] es, int lo, int hi) { |
677 |
> |
System.arraycopy(es, hi, es, lo, size - hi); |
678 |
> |
for (int to = size, i = (size -= hi - lo); i < to; i++) |
679 |
> |
es[i] = null; |
680 |
|
} |
681 |
|
|
682 |
|
/** |
719 |
|
* @see Collection#contains(Object) |
720 |
|
*/ |
721 |
|
public boolean removeAll(Collection<?> c) { |
722 |
< |
return batchRemove(c, false); |
722 |
> |
return batchRemove(c, false, 0, size); |
723 |
|
} |
724 |
|
|
725 |
|
/** |
739 |
|
* @see Collection#contains(Object) |
740 |
|
*/ |
741 |
|
public boolean retainAll(Collection<?> c) { |
742 |
< |
return batchRemove(c, true); |
742 |
> |
return batchRemove(c, true, 0, size); |
743 |
|
} |
744 |
|
|
745 |
< |
private boolean batchRemove(Collection<?> c, boolean complement) { |
745 |
> |
boolean batchRemove(Collection<?> c, boolean complement, |
746 |
> |
final int from, final int end) { |
747 |
|
Objects.requireNonNull(c); |
748 |
|
final Object[] es = elementData; |
745 |
– |
final int size = this.size; |
746 |
– |
final boolean modified; |
749 |
|
int r; |
750 |
|
// Optimize for initial run of survivors |
751 |
< |
for (r = 0; r < size; r++) |
751 |
> |
for (r = from;; r++) { |
752 |
> |
if (r == end) |
753 |
> |
return false; |
754 |
|
if (c.contains(es[r]) != complement) |
755 |
|
break; |
752 |
– |
if (modified = (r < size)) { |
753 |
– |
int w = r++; |
754 |
– |
try { |
755 |
– |
for (Object e; r < size; r++) |
756 |
– |
if (c.contains(e = es[r]) == complement) |
757 |
– |
es[w++] = e; |
758 |
– |
} catch (Throwable ex) { |
759 |
– |
// Preserve behavioral compatibility with AbstractCollection, |
760 |
– |
// even if c.contains() throws. |
761 |
– |
System.arraycopy(es, r, es, w, size - r); |
762 |
– |
w += size - r; |
763 |
– |
throw ex; |
764 |
– |
} finally { |
765 |
– |
modCount += size - w; |
766 |
– |
Arrays.fill(es, (this.size = w), size, null); |
767 |
– |
} |
756 |
|
} |
757 |
< |
return modified; |
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 |
> |
// checkInvariants(); |
773 |
> |
return true; |
774 |
|
} |
775 |
|
|
776 |
|
/** |
777 |
< |
* Save the state of the {@code ArrayList} instance to a stream (that |
778 |
< |
* is, serialize it). |
777 |
> |
* Saves the state of the {@code ArrayList} instance to a stream |
778 |
> |
* (that is, serializes it). |
779 |
|
* |
780 |
+ |
* @param s the stream |
781 |
+ |
* @throws java.io.IOException if an I/O error occurs |
782 |
|
* @serialData The length of the array backing the {@code ArrayList} |
783 |
|
* instance is emitted (int), followed by all of its elements |
784 |
|
* (each an {@code Object}) in the proper order. |
785 |
|
*/ |
786 |
|
private void writeObject(java.io.ObjectOutputStream s) |
787 |
< |
throws java.io.IOException{ |
787 |
> |
throws java.io.IOException { |
788 |
|
// Write out element count, and any hidden stuff |
789 |
|
int expectedModCount = modCount; |
790 |
|
s.defaultWriteObject(); |
791 |
|
|
792 |
< |
// Write out size as capacity for behavioural compatibility with clone() |
792 |
> |
// Write out size as capacity for behavioral compatibility with clone() |
793 |
|
s.writeInt(size); |
794 |
|
|
795 |
|
// Write out all elements in the proper order. |
803 |
|
} |
804 |
|
|
805 |
|
/** |
806 |
< |
* Reconstitute the {@code ArrayList} instance from a stream (that is, |
807 |
< |
* deserialize it). |
806 |
> |
* Reconstitutes the {@code ArrayList} instance from a stream (that is, |
807 |
> |
* deserializes it). |
808 |
> |
* @param s the stream |
809 |
> |
* @throws ClassNotFoundException if the class of a serialized object |
810 |
> |
* could not be found |
811 |
> |
* @throws java.io.IOException if an I/O error occurs |
812 |
|
*/ |
813 |
|
private void readObject(java.io.ObjectInputStream s) |
814 |
|
throws java.io.IOException, ClassNotFoundException { |
821 |
|
|
822 |
|
if (size > 0) { |
823 |
|
// like clone(), allocate array based upon size not capacity |
824 |
+ |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size); |
825 |
|
Object[] elements = new Object[size]; |
826 |
|
|
827 |
|
// Read in all elements in the proper order. |
921 |
|
} |
922 |
|
|
923 |
|
@Override |
924 |
< |
@SuppressWarnings("unchecked") |
925 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
915 |
< |
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 |
|
} |
928 |
– |
// update once at end of iteration to reduce heap write traffic |
929 |
– |
cursor = i; |
930 |
– |
lastRet = i - 1; |
931 |
– |
checkForComodification(); |
939 |
|
} |
940 |
|
|
941 |
|
final void checkForComodification() { |
1122 |
|
return true; |
1123 |
|
} |
1124 |
|
|
1125 |
+ |
public void replaceAll(UnaryOperator<E> operator) { |
1126 |
+ |
root.replaceAllRange(operator, offset, offset + size); |
1127 |
+ |
} |
1128 |
+ |
|
1129 |
+ |
public boolean removeAll(Collection<?> c) { |
1130 |
+ |
return batchRemove(c, false); |
1131 |
+ |
} |
1132 |
+ |
|
1133 |
+ |
public boolean retainAll(Collection<?> c) { |
1134 |
+ |
return batchRemove(c, true); |
1135 |
+ |
} |
1136 |
+ |
|
1137 |
+ |
private boolean batchRemove(Collection<?> c, boolean complement) { |
1138 |
+ |
checkForComodification(); |
1139 |
+ |
int oldSize = root.size; |
1140 |
+ |
boolean modified = |
1141 |
+ |
root.batchRemove(c, complement, offset, offset + size); |
1142 |
+ |
if (modified) |
1143 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1144 |
+ |
return modified; |
1145 |
+ |
} |
1146 |
+ |
|
1147 |
+ |
public boolean removeIf(Predicate<? super E> filter) { |
1148 |
+ |
checkForComodification(); |
1149 |
+ |
int oldSize = root.size; |
1150 |
+ |
boolean modified = root.removeIf(filter, offset, offset + size); |
1151 |
+ |
if (modified) |
1152 |
+ |
updateSizeAndModCount(root.size - oldSize); |
1153 |
+ |
return modified; |
1154 |
+ |
} |
1155 |
+ |
|
1156 |
+ |
public Object[] toArray() { |
1157 |
+ |
checkForComodification(); |
1158 |
+ |
return Arrays.copyOfRange(root.elementData, offset, offset + size); |
1159 |
+ |
} |
1160 |
+ |
|
1161 |
+ |
@SuppressWarnings("unchecked") |
1162 |
+ |
public <T> T[] toArray(T[] a) { |
1163 |
+ |
checkForComodification(); |
1164 |
+ |
if (a.length < size) |
1165 |
+ |
return (T[]) Arrays.copyOfRange( |
1166 |
+ |
root.elementData, offset, offset + size, a.getClass()); |
1167 |
+ |
System.arraycopy(root.elementData, offset, a, 0, size); |
1168 |
+ |
if (a.length > size) |
1169 |
+ |
a[size] = null; |
1170 |
+ |
return a; |
1171 |
+ |
} |
1172 |
+ |
|
1173 |
|
public Iterator<E> iterator() { |
1174 |
|
return listIterator(); |
1175 |
|
} |
1217 |
|
return (E) elementData[offset + (lastRet = i)]; |
1218 |
|
} |
1219 |
|
|
1220 |
< |
@SuppressWarnings("unchecked") |
1221 |
< |
public void forEachRemaining(Consumer<? super E> consumer) { |
1167 |
< |
Objects.requireNonNull(consumer); |
1220 |
> |
public void forEachRemaining(Consumer<? super E> action) { |
1221 |
> |
Objects.requireNonNull(action); |
1222 |
|
final int size = SubList.this.size; |
1223 |
|
int i = cursor; |
1224 |
< |
if (i >= size) { |
1225 |
< |
return; |
1226 |
< |
} |
1227 |
< |
final Object[] elementData = root.elementData; |
1228 |
< |
if (offset + i >= elementData.length) { |
1229 |
< |
throw new ConcurrentModificationException(); |
1230 |
< |
} |
1231 |
< |
while (i != size && modCount == expectedModCount) { |
1232 |
< |
consumer.accept((E) elementData[offset + (i++)]); |
1224 |
> |
if (i < size) { |
1225 |
> |
final Object[] es = root.elementData; |
1226 |
> |
if (offset + i >= es.length) |
1227 |
> |
throw new ConcurrentModificationException(); |
1228 |
> |
for (; i < size && modCount == expectedModCount; i++) |
1229 |
> |
action.accept(elementAt(es, offset + i)); |
1230 |
> |
// update once at end to reduce heap write traffic |
1231 |
> |
cursor = i; |
1232 |
> |
lastRet = i - 1; |
1233 |
> |
checkForComodification(); |
1234 |
|
} |
1180 |
– |
// update once at end of iteration to reduce heap write traffic |
1181 |
– |
lastRet = cursor = i; |
1182 |
– |
checkForComodification(); |
1235 |
|
} |
1236 |
|
|
1237 |
|
public int nextIndex() { |
1321 |
|
public Spliterator<E> spliterator() { |
1322 |
|
checkForComodification(); |
1323 |
|
|
1324 |
< |
// ArrayListSpliterator is not used because late-binding logic |
1325 |
< |
// is different here |
1274 |
< |
return new Spliterator<>() { |
1324 |
> |
// ArrayListSpliterator not used here due to late-binding |
1325 |
> |
return new Spliterator<E>() { |
1326 |
|
private int index = offset; // current index, modified on advance/split |
1327 |
|
private int fence = -1; // -1 until used; then one past last index |
1328 |
|
private int expectedModCount; // initialized when fence set |
1336 |
|
return hi; |
1337 |
|
} |
1338 |
|
|
1339 |
< |
public ArrayListSpliterator<E> trySplit() { |
1339 |
> |
public ArrayList<E>.ArrayListSpliterator trySplit() { |
1340 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1341 |
< |
// ArrayListSpliterator could be used here as the source is already bound |
1341 |
> |
// ArrayListSpliterator can be used here as the source is already bound |
1342 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1343 |
< |
new ArrayListSpliterator<>(root, lo, index = mid, |
1293 |
< |
expectedModCount); |
1343 |
> |
root.new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1344 |
|
} |
1345 |
|
|
1346 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1382 |
|
} |
1383 |
|
|
1384 |
|
public long estimateSize() { |
1385 |
< |
return (long) (getFence() - index); |
1385 |
> |
return getFence() - index; |
1386 |
|
} |
1387 |
|
|
1388 |
|
public int characteristics() { |
1392 |
|
} |
1393 |
|
} |
1394 |
|
|
1395 |
+ |
/** |
1396 |
+ |
* @throws NullPointerException {@inheritDoc} |
1397 |
+ |
*/ |
1398 |
|
@Override |
1399 |
|
public void forEach(Consumer<? super E> action) { |
1400 |
|
Objects.requireNonNull(action); |
1401 |
|
final int expectedModCount = modCount; |
1402 |
< |
@SuppressWarnings("unchecked") |
1350 |
< |
final E[] elementData = (E[]) this.elementData; |
1402 |
> |
final Object[] es = elementData; |
1403 |
|
final int size = this.size; |
1404 |
< |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1405 |
< |
action.accept(elementData[i]); |
1406 |
< |
} |
1355 |
< |
if (modCount != expectedModCount) { |
1404 |
> |
for (int i = 0; modCount == expectedModCount && i < size; i++) |
1405 |
> |
action.accept(elementAt(es, i)); |
1406 |
> |
if (modCount != expectedModCount) |
1407 |
|
throw new ConcurrentModificationException(); |
1357 |
– |
} |
1408 |
|
} |
1409 |
|
|
1410 |
|
/** |
1422 |
|
*/ |
1423 |
|
@Override |
1424 |
|
public Spliterator<E> spliterator() { |
1425 |
< |
return new ArrayListSpliterator<>(this, 0, -1, 0); |
1425 |
> |
return new ArrayListSpliterator(0, -1, 0); |
1426 |
|
} |
1427 |
|
|
1428 |
|
/** Index-based split-by-two, lazily initialized Spliterator */ |
1429 |
< |
static final class ArrayListSpliterator<E> implements Spliterator<E> { |
1429 |
> |
final class ArrayListSpliterator implements Spliterator<E> { |
1430 |
|
|
1431 |
|
/* |
1432 |
|
* If ArrayLists were immutable, or structurally immutable (no |
1460 |
|
* these streamlinings. |
1461 |
|
*/ |
1462 |
|
|
1413 |
– |
private final ArrayList<E> list; |
1463 |
|
private int index; // current index, modified on advance/split |
1464 |
|
private int fence; // -1 until used; then one past last index |
1465 |
|
private int expectedModCount; // initialized when fence set |
1466 |
|
|
1467 |
< |
/** Create new spliterator covering the given range */ |
1468 |
< |
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, |
1420 |
< |
int expectedModCount) { |
1421 |
< |
this.list = list; // OK if null unless traversed |
1467 |
> |
/** Creates new spliterator covering the given range. */ |
1468 |
> |
ArrayListSpliterator(int origin, int fence, int expectedModCount) { |
1469 |
|
this.index = origin; |
1470 |
|
this.fence = fence; |
1471 |
|
this.expectedModCount = expectedModCount; |
1473 |
|
|
1474 |
|
private int getFence() { // initialize fence to size on first use |
1475 |
|
int hi; // (a specialized variant appears in method forEach) |
1429 |
– |
ArrayList<E> lst; |
1476 |
|
if ((hi = fence) < 0) { |
1477 |
< |
if ((lst = list) == null) |
1478 |
< |
hi = fence = 0; |
1433 |
< |
else { |
1434 |
< |
expectedModCount = lst.modCount; |
1435 |
< |
hi = fence = lst.size; |
1436 |
< |
} |
1477 |
> |
expectedModCount = modCount; |
1478 |
> |
hi = fence = size; |
1479 |
|
} |
1480 |
|
return hi; |
1481 |
|
} |
1482 |
|
|
1483 |
< |
public ArrayListSpliterator<E> trySplit() { |
1483 |
> |
public ArrayListSpliterator trySplit() { |
1484 |
|
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
1485 |
|
return (lo >= mid) ? null : // divide range in half unless too small |
1486 |
< |
new ArrayListSpliterator<>(list, lo, index = mid, |
1445 |
< |
expectedModCount); |
1486 |
> |
new ArrayListSpliterator(lo, index = mid, expectedModCount); |
1487 |
|
} |
1488 |
|
|
1489 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1492 |
|
int hi = getFence(), i = index; |
1493 |
|
if (i < hi) { |
1494 |
|
index = i + 1; |
1495 |
< |
@SuppressWarnings("unchecked") E e = (E)list.elementData[i]; |
1495 |
> |
@SuppressWarnings("unchecked") E e = (E)elementData[i]; |
1496 |
|
action.accept(e); |
1497 |
< |
if (list.modCount != expectedModCount) |
1497 |
> |
if (modCount != expectedModCount) |
1498 |
|
throw new ConcurrentModificationException(); |
1499 |
|
return true; |
1500 |
|
} |
1503 |
|
|
1504 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1505 |
|
int i, hi, mc; // hoist accesses and checks from loop |
1506 |
< |
ArrayList<E> lst; Object[] a; |
1506 |
> |
Object[] a; |
1507 |
|
if (action == null) |
1508 |
|
throw new NullPointerException(); |
1509 |
< |
if ((lst = list) != null && (a = lst.elementData) != null) { |
1509 |
> |
if ((a = elementData) != null) { |
1510 |
|
if ((hi = fence) < 0) { |
1511 |
< |
mc = lst.modCount; |
1512 |
< |
hi = lst.size; |
1511 |
> |
mc = modCount; |
1512 |
> |
hi = size; |
1513 |
|
} |
1514 |
|
else |
1515 |
|
mc = expectedModCount; |
1518 |
|
@SuppressWarnings("unchecked") E e = (E) a[i]; |
1519 |
|
action.accept(e); |
1520 |
|
} |
1521 |
< |
if (lst.modCount == mc) |
1521 |
> |
if (modCount == mc) |
1522 |
|
return; |
1523 |
|
} |
1524 |
|
} |
1526 |
|
} |
1527 |
|
|
1528 |
|
public long estimateSize() { |
1529 |
< |
return (long) (getFence() - index); |
1529 |
> |
return getFence() - index; |
1530 |
|
} |
1531 |
|
|
1532 |
|
public int characteristics() { |
1534 |
|
} |
1535 |
|
} |
1536 |
|
|
1537 |
< |
@SuppressWarnings("unchecked") |
1537 |
> |
// A tiny bit set implementation |
1538 |
> |
|
1539 |
> |
private static long[] nBits(int n) { |
1540 |
> |
return new long[((n - 1) >> 6) + 1]; |
1541 |
> |
} |
1542 |
> |
private static void setBit(long[] bits, int i) { |
1543 |
> |
bits[i >> 6] |= 1L << i; |
1544 |
> |
} |
1545 |
> |
private static boolean isClear(long[] bits, int i) { |
1546 |
> |
return (bits[i >> 6] & (1L << i)) == 0; |
1547 |
> |
} |
1548 |
> |
|
1549 |
> |
/** |
1550 |
> |
* @throws NullPointerException {@inheritDoc} |
1551 |
> |
*/ |
1552 |
|
@Override |
1553 |
|
public boolean removeIf(Predicate<? super E> filter) { |
1554 |
+ |
return removeIf(filter, 0, size); |
1555 |
+ |
} |
1556 |
+ |
|
1557 |
+ |
/** |
1558 |
+ |
* Removes all elements satisfying the given predicate, from index |
1559 |
+ |
* i (inclusive) to index end (exclusive). |
1560 |
+ |
*/ |
1561 |
+ |
boolean removeIf(Predicate<? super E> filter, int i, final int end) { |
1562 |
|
Objects.requireNonNull(filter); |
1563 |
|
int expectedModCount = modCount; |
1564 |
|
final Object[] es = elementData; |
1502 |
– |
final int size = this.size; |
1503 |
– |
final boolean modified; |
1504 |
– |
int r; |
1565 |
|
// Optimize for initial run of survivors |
1566 |
< |
for (r = 0; r < size; r++) |
1567 |
< |
if (filter.test((E) es[r])) |
1568 |
< |
break; |
1569 |
< |
if (modified = (r < size)) { |
1570 |
< |
expectedModCount++; |
1566 |
> |
for (; i < end && !filter.test(elementAt(es, i)); i++) |
1567 |
> |
; |
1568 |
> |
// Tolerate predicates that reentrantly access the collection for |
1569 |
> |
// read (but writers still get CME), so traverse once to find |
1570 |
> |
// elements to delete, a second pass to physically expunge. |
1571 |
> |
if (i < end) { |
1572 |
> |
final int beg = i; |
1573 |
> |
final long[] deathRow = nBits(end - beg); |
1574 |
> |
deathRow[0] = 1L; // set bit 0 |
1575 |
> |
for (i = beg + 1; i < end; i++) |
1576 |
> |
if (filter.test(elementAt(es, i))) |
1577 |
> |
setBit(deathRow, i - beg); |
1578 |
> |
if (modCount != expectedModCount) |
1579 |
> |
throw new ConcurrentModificationException(); |
1580 |
|
modCount++; |
1581 |
< |
int w = r++; |
1582 |
< |
try { |
1583 |
< |
for (E e; r < size; r++) |
1584 |
< |
if (!filter.test(e = (E) es[r])) |
1585 |
< |
es[w++] = e; |
1586 |
< |
} catch (Throwable ex) { |
1587 |
< |
// copy remaining elements |
1588 |
< |
System.arraycopy(es, r, es, w, size - r); |
1589 |
< |
w += size - r; |
1590 |
< |
throw ex; |
1591 |
< |
} finally { |
1592 |
< |
Arrays.fill(es, (this.size = w), size, null); |
1524 |
< |
} |
1581 |
> |
int w = beg; |
1582 |
> |
for (i = beg; i < end; i++) |
1583 |
> |
if (isClear(deathRow, i - beg)) |
1584 |
> |
es[w++] = es[i]; |
1585 |
> |
shiftTailOverGap(es, w, end); |
1586 |
> |
// checkInvariants(); |
1587 |
> |
return true; |
1588 |
> |
} else { |
1589 |
> |
if (modCount != expectedModCount) |
1590 |
> |
throw new ConcurrentModificationException(); |
1591 |
> |
// checkInvariants(); |
1592 |
> |
return false; |
1593 |
|
} |
1526 |
– |
if (modCount != expectedModCount) |
1527 |
– |
throw new ConcurrentModificationException(); |
1528 |
– |
return modified; |
1594 |
|
} |
1595 |
|
|
1596 |
|
@Override |
1532 |
– |
@SuppressWarnings("unchecked") |
1597 |
|
public void replaceAll(UnaryOperator<E> operator) { |
1598 |
+ |
replaceAllRange(operator, 0, size); |
1599 |
+ |
} |
1600 |
+ |
|
1601 |
+ |
private void replaceAllRange(UnaryOperator<E> operator, int from, int to) { |
1602 |
|
Objects.requireNonNull(operator); |
1603 |
|
final int expectedModCount = modCount; |
1604 |
< |
final int size = this.size; |
1605 |
< |
for (int i=0; modCount == expectedModCount && i < size; i++) { |
1606 |
< |
elementData[i] = operator.apply((E) elementData[i]); |
1607 |
< |
} |
1540 |
< |
if (modCount != expectedModCount) { |
1604 |
> |
final Object[] es = elementData; |
1605 |
> |
for (int i = from; modCount == expectedModCount && i < to; i++) |
1606 |
> |
es[i] = operator.apply(elementAt(es, i)); |
1607 |
> |
if (modCount != expectedModCount) |
1608 |
|
throw new ConcurrentModificationException(); |
1609 |
< |
} |
1543 |
< |
modCount++; |
1609 |
> |
// checkInvariants(); |
1610 |
|
} |
1611 |
|
|
1612 |
|
@Override |
1614 |
|
public void sort(Comparator<? super E> c) { |
1615 |
|
final int expectedModCount = modCount; |
1616 |
|
Arrays.sort((E[]) elementData, 0, size, c); |
1617 |
< |
if (modCount != expectedModCount) { |
1617 |
> |
if (modCount != expectedModCount) |
1618 |
|
throw new ConcurrentModificationException(); |
1553 |
– |
} |
1619 |
|
modCount++; |
1620 |
+ |
// checkInvariants(); |
1621 |
+ |
} |
1622 |
+ |
|
1623 |
+ |
void checkInvariants() { |
1624 |
+ |
// assert size >= 0; |
1625 |
+ |
// assert size == elementData.length || elementData[size] == null; |
1626 |
|
} |
1627 |
|
} |