--- jsr166/src/main/java/util/ArrayList.java 2016/11/14 21:16:43 1.42
+++ jsr166/src/main/java/util/ArrayList.java 2017/05/31 22:37:31 1.52
@@ -91,7 +91,7 @@ import java.util.function.UnaryOperator;
* should be used only to detect bugs.
*
*
This class is a member of the
- *
+ *
* Java Collections Framework.
*
* @param the type of elements in this list
@@ -515,15 +515,10 @@ public class ArrayList extends Abstra
*/
public E remove(int index) {
Objects.checkIndex(index, size);
+ final Object[] es = elementData;
- modCount++;
- E oldValue = elementData(index);
-
- int numMoved = size - index - 1;
- if (numMoved > 0)
- System.arraycopy(elementData, index+1, elementData, index,
- numMoved);
- elementData[--size] = null; // clear to let GC do its work
+ @SuppressWarnings("unchecked") E oldValue = (E) es[index];
+ fastRemove(es, index);
// checkInvariants();
return oldValue;
@@ -543,33 +538,35 @@ public class ArrayList extends Abstra
* @return {@code true} if this list contained the specified element
*/
public boolean remove(Object o) {
- if (o == null) {
- for (int index = 0; index < size; index++)
- if (elementData[index] == null) {
- fastRemove(index);
- return true;
- }
- } else {
- for (int index = 0; index < size; index++)
- if (o.equals(elementData[index])) {
- fastRemove(index);
- return true;
- }
+ final Object[] es = elementData;
+ final int size = this.size;
+ int i = 0;
+ found: {
+ if (o == null) {
+ for (; i < size; i++)
+ if (es[i] == null)
+ break found;
+ } else {
+ for (; i < size; i++)
+ if (o.equals(es[i]))
+ break found;
+ }
+ return false;
}
- return false;
+ fastRemove(es, i);
+ return true;
}
/**
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
- private void fastRemove(int index) {
+ private void fastRemove(Object[] es, int i) {
modCount++;
- int numMoved = size - index - 1;
- if (numMoved > 0)
- System.arraycopy(elementData, index+1, elementData, index,
- numMoved);
- elementData[--size] = null; // clear to let GC do its work
+ final int newSize;
+ if ((newSize = size - 1) > i)
+ System.arraycopy(es, i + 1, es, i, newSize - i);
+ es[size = newSize] = null;
}
/**
@@ -578,8 +575,9 @@ public class ArrayList extends Abstra
*/
public void clear() {
modCount++;
- Arrays.fill(elementData, 0, size, null);
- size = 0;
+ final Object[] es = elementData;
+ for (int to = size, i = size = 0; i < to; i++)
+ es[i] = null;
}
/**
@@ -669,13 +667,17 @@ public class ArrayList extends Abstra
outOfBoundsMsg(fromIndex, toIndex));
}
modCount++;
- final Object[] es = elementData;
- final int oldSize = size;
- System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
- Arrays.fill(es, size -= (toIndex - fromIndex), oldSize, null);
+ shiftTailOverGap(elementData, fromIndex, toIndex);
// checkInvariants();
}
+ /** Erases the gap from lo to hi, by sliding down following elements. */
+ private void shiftTailOverGap(Object[] es, int lo, int hi) {
+ System.arraycopy(es, hi, es, lo, size - hi);
+ for (int to = size, i = (size -= hi - lo); i < to; i++)
+ es[i] = null;
+ }
+
/**
* A version of rangeCheck used by add and addAll.
*/
@@ -761,10 +763,8 @@ public class ArrayList extends Abstra
w += end - r;
throw ex;
} finally {
- final int oldSize = size, deleted = end - w;
- modCount += deleted;
- System.arraycopy(es, end, es, w, oldSize - end);
- Arrays.fill(es, size -= deleted, oldSize, null);
+ modCount += end - w;
+ shiftTailOverGap(es, w, end);
}
}
// checkInvariants();
@@ -772,20 +772,22 @@ public class ArrayList extends Abstra
}
/**
- * Save the state of the {@code ArrayList} instance to a stream (that
- * is, serialize it).
+ * Saves the state of the {@code ArrayList} instance to a stream
+ * (that is, serializes it).
*
+ * @param s the stream
+ * @throws java.io.IOException if an I/O error occurs
* @serialData The length of the array backing the {@code ArrayList}
* instance is emitted (int), followed by all of its elements
* (each an {@code Object}) in the proper order.
*/
private void writeObject(java.io.ObjectOutputStream s)
- throws java.io.IOException{
+ throws java.io.IOException {
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
- // Write out size as capacity for behavioural compatibility with clone()
+ // Write out size as capacity for behavioral compatibility with clone()
s.writeInt(size);
// Write out all elements in the proper order.
@@ -799,8 +801,12 @@ public class ArrayList extends Abstra
}
/**
- * Reconstitute the {@code ArrayList} instance from a stream (that is,
- * deserialize it).
+ * Reconstitutes the {@code ArrayList} instance from a stream (that is,
+ * deserializes it).
+ * @param s the stream
+ * @throws ClassNotFoundException if the class of a serialized object
+ * could not be found
+ * @throws java.io.IOException if an I/O error occurs
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
@@ -912,25 +918,21 @@ public class ArrayList extends Abstra
}
@Override
- @SuppressWarnings("unchecked")
- public void forEachRemaining(Consumer super E> consumer) {
- Objects.requireNonNull(consumer);
+ public void forEachRemaining(Consumer super E> action) {
+ Objects.requireNonNull(action);
final int size = ArrayList.this.size;
int i = cursor;
- if (i >= size) {
- return;
- }
- final Object[] elementData = ArrayList.this.elementData;
- if (i >= elementData.length) {
- throw new ConcurrentModificationException();
- }
- while (i != size && modCount == expectedModCount) {
- consumer.accept((E) elementData[i++]);
+ if (i < size) {
+ final Object[] es = elementData;
+ if (i >= es.length)
+ throw new ConcurrentModificationException();
+ for (; i < size && modCount == expectedModCount; i++)
+ action.accept(elementAt(es, i));
+ // update once at end to reduce heap write traffic
+ cursor = i;
+ lastRet = i - 1;
+ checkForComodification();
}
- // update once at end of iteration to reduce heap write traffic
- cursor = i;
- lastRet = i - 1;
- checkForComodification();
}
final void checkForComodification() {
@@ -1191,25 +1193,21 @@ public class ArrayList extends Abstra
return (E) elementData[offset + (lastRet = i)];
}
- @SuppressWarnings("unchecked")
- public void forEachRemaining(Consumer super E> consumer) {
- Objects.requireNonNull(consumer);
+ public void forEachRemaining(Consumer super E> action) {
+ Objects.requireNonNull(action);
final int size = SubList.this.size;
int i = cursor;
- if (i >= size) {
- return;
- }
- final Object[] elementData = root.elementData;
- if (offset + i >= elementData.length) {
- throw new ConcurrentModificationException();
- }
- while (i != size && modCount == expectedModCount) {
- consumer.accept((E) elementData[offset + (i++)]);
+ if (i < size) {
+ final Object[] es = root.elementData;
+ if (offset + i >= es.length)
+ throw new ConcurrentModificationException();
+ for (; i < size && modCount == expectedModCount; i++)
+ action.accept(elementAt(es, offset + i));
+ // update once at end to reduce heap write traffic
+ cursor = i;
+ lastRet = i - 1;
+ checkForComodification();
}
- // update once at end of iteration to reduce heap write traffic
- cursor = i;
- lastRet = i - 1;
- checkForComodification();
}
public int nextIndex() {
@@ -1299,9 +1297,8 @@ public class ArrayList extends Abstra
public Spliterator spliterator() {
checkForComodification();
- // ArrayListSpliterator is not used because late-binding logic
- // is different here
- return new Spliterator<>() {
+ // ArrayListSpliterator not used here due to late-binding
+ return new Spliterator() {
private int index = offset; // current index, modified on advance/split
private int fence = -1; // -1 until used; then one past last index
private int expectedModCount; // initialized when fence set
@@ -1315,12 +1312,11 @@ public class ArrayList extends Abstra
return hi;
}
- public ArrayListSpliterator trySplit() {
+ public ArrayList.ArrayListSpliterator trySplit() {
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
- // ArrayListSpliterator could be used here as the source is already bound
+ // ArrayListSpliterator can be used here as the source is already bound
return (lo >= mid) ? null : // divide range in half unless too small
- new ArrayListSpliterator<>(root, lo, index = mid,
- expectedModCount);
+ root.new ArrayListSpliterator(lo, index = mid, expectedModCount);
}
public boolean tryAdvance(Consumer super E> action) {
@@ -1362,7 +1358,7 @@ public class ArrayList extends Abstra
}
public long estimateSize() {
- return (long) (getFence() - index);
+ return getFence() - index;
}
public int characteristics() {
@@ -1372,6 +1368,9 @@ public class ArrayList extends Abstra
}
}
+ /**
+ * @throws NullPointerException {@inheritDoc}
+ */
@Override
public void forEach(Consumer super E> action) {
Objects.requireNonNull(action);
@@ -1399,11 +1398,11 @@ public class ArrayList extends Abstra
*/
@Override
public Spliterator spliterator() {
- return new ArrayListSpliterator<>(this, 0, -1, 0);
+ return new ArrayListSpliterator(0, -1, 0);
}
/** Index-based split-by-two, lazily initialized Spliterator */
- static final class ArrayListSpliterator implements Spliterator {
+ final class ArrayListSpliterator implements Spliterator {
/*
* If ArrayLists were immutable, or structurally immutable (no
@@ -1437,15 +1436,12 @@ public class ArrayList extends Abstra
* these streamlinings.
*/
- private final ArrayList list;
private int index; // current index, modified on advance/split
private int fence; // -1 until used; then one past last index
private int expectedModCount; // initialized when fence set
- /** Create new spliterator covering the given range */
- ArrayListSpliterator(ArrayList list, int origin, int fence,
- int expectedModCount) {
- this.list = list; // OK if null unless traversed
+ /** Creates new spliterator covering the given range. */
+ ArrayListSpliterator(int origin, int fence, int expectedModCount) {
this.index = origin;
this.fence = fence;
this.expectedModCount = expectedModCount;
@@ -1453,23 +1449,17 @@ public class ArrayList extends Abstra
private int getFence() { // initialize fence to size on first use
int hi; // (a specialized variant appears in method forEach)
- ArrayList lst;
if ((hi = fence) < 0) {
- if ((lst = list) == null)
- hi = fence = 0;
- else {
- expectedModCount = lst.modCount;
- hi = fence = lst.size;
- }
+ expectedModCount = modCount;
+ hi = fence = size;
}
return hi;
}
- public ArrayListSpliterator trySplit() {
+ public ArrayListSpliterator trySplit() {
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
return (lo >= mid) ? null : // divide range in half unless too small
- new ArrayListSpliterator<>(list, lo, index = mid,
- expectedModCount);
+ new ArrayListSpliterator(lo, index = mid, expectedModCount);
}
public boolean tryAdvance(Consumer super E> action) {
@@ -1478,9 +1468,9 @@ public class ArrayList extends Abstra
int hi = getFence(), i = index;
if (i < hi) {
index = i + 1;
- @SuppressWarnings("unchecked") E e = (E)list.elementData[i];
+ @SuppressWarnings("unchecked") E e = (E)elementData[i];
action.accept(e);
- if (list.modCount != expectedModCount)
+ if (modCount != expectedModCount)
throw new ConcurrentModificationException();
return true;
}
@@ -1489,13 +1479,13 @@ public class ArrayList extends Abstra
public void forEachRemaining(Consumer super E> action) {
int i, hi, mc; // hoist accesses and checks from loop
- ArrayList lst; Object[] a;
+ Object[] a;
if (action == null)
throw new NullPointerException();
- if ((lst = list) != null && (a = lst.elementData) != null) {
+ if ((a = elementData) != null) {
if ((hi = fence) < 0) {
- mc = lst.modCount;
- hi = lst.size;
+ mc = modCount;
+ hi = size;
}
else
mc = expectedModCount;
@@ -1504,7 +1494,7 @@ public class ArrayList extends Abstra
@SuppressWarnings("unchecked") E e = (E) a[i];
action.accept(e);
}
- if (lst.modCount == mc)
+ if (modCount == mc)
return;
}
}
@@ -1512,7 +1502,7 @@ public class ArrayList extends Abstra
}
public long estimateSize() {
- return (long) (getFence() - index);
+ return getFence() - index;
}
public int characteristics() {
@@ -1532,27 +1522,29 @@ public class ArrayList extends Abstra
return (bits[i >> 6] & (1L << i)) == 0;
}
+ /**
+ * @throws NullPointerException {@inheritDoc}
+ */
@Override
public boolean removeIf(Predicate super E> filter) {
return removeIf(filter, 0, size);
}
- boolean removeIf(Predicate super E> filter,
- final int from, final int end) {
+ /**
+ * Removes all elements satisfying the given predicate, from index
+ * i (inclusive) to index end (exclusive).
+ */
+ boolean removeIf(Predicate super E> filter, int i, final int end) {
Objects.requireNonNull(filter);
int expectedModCount = modCount;
final Object[] es = elementData;
- final boolean modified;
- int i;
// Optimize for initial run of survivors
- for (i = from; i < end && !filter.test(elementAt(es, i)); i++)
+ for (; i < end && !filter.test(elementAt(es, i)); i++)
;
// Tolerate predicates that reentrantly access the collection for
// read (but writers still get CME), so traverse once to find
// elements to delete, a second pass to physically expunge.
- if (modified = (i < end)) {
- expectedModCount++;
- modCount++;
+ if (i < end) {
final int beg = i;
final long[] deathRow = nBits(end - beg);
deathRow[0] = 1L; // set bit 0
@@ -1561,18 +1553,21 @@ public class ArrayList extends Abstra
setBit(deathRow, i - beg);
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
+ expectedModCount++;
+ modCount++;
int w = beg;
for (i = beg; i < end; i++)
if (isClear(deathRow, i - beg))
es[w++] = es[i];
- final int oldSize = size;
- System.arraycopy(es, end, es, w, oldSize - end);
- Arrays.fill(es, size -= (end - w), oldSize, null);
+ shiftTailOverGap(es, w, end);
+ // checkInvariants();
+ return true;
+ } else {
+ if (modCount != expectedModCount)
+ throw new ConcurrentModificationException();
+ // checkInvariants();
+ return false;
}
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- // checkInvariants();
- return modified;
}
@Override