--- jsr166/src/main/java/util/ArrayDeque.java 2016/11/21 15:30:44 1.121
+++ jsr166/src/main/java/util/ArrayDeque.java 2018/10/01 00:10:52 1.135
@@ -9,6 +9,7 @@ import java.io.Serializable;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
+import jdk.internal.misc.SharedSecrets;
/**
* Resizable-array implementation of the {@link Deque} interface. Array
@@ -50,7 +51,7 @@ import java.util.function.UnaryOperator;
* Iterator} interfaces.
*
*
This class is a member of the
- *
+ *
* Java Collections Framework.
*
* @author Josh Bloch and Doug Lea
@@ -117,16 +118,16 @@ public class ArrayDeque extends Abstr
if (jump < needed
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0)
newCapacity = newCapacity(needed, jump);
- elements = Arrays.copyOf(elements, newCapacity);
+ final Object[] es = elements = Arrays.copyOf(elements, newCapacity);
// Exceptionally, here tail == head needs to be disambiguated
- if (tail < head || (tail == head && elements[head] != null)) {
+ if (tail < head || (tail == head && es[head] != null)) {
// wrap around; slide first leg forward to end of array
int newSpace = newCapacity - oldCapacity;
- System.arraycopy(elements, head,
- elements, head + newSpace,
+ System.arraycopy(es, head,
+ es, head + newSpace,
oldCapacity - head);
- Arrays.fill(elements, head, head + newSpace, null);
- head += newSpace;
+ for (int i = head, to = (head += newSpace); i < to; i++)
+ es[i] = null;
}
// checkInvariants();
}
@@ -180,7 +181,7 @@ public class ArrayDeque extends Abstr
* sufficient to hold 16 elements.
*/
public ArrayDeque() {
- elements = new Object[16];
+ elements = new Object[16 + 1];
}
/**
@@ -208,11 +209,11 @@ public class ArrayDeque extends Abstr
*/
public ArrayDeque(Collection extends E> c) {
this(c.size());
- addAll(c);
+ copyElements(c);
}
/**
- * Increments i, mod modulus.
+ * Circularly increments i, mod modulus.
* Precondition and postcondition: 0 <= i < modulus.
*/
static final int inc(int i, int modulus) {
@@ -221,7 +222,7 @@ public class ArrayDeque extends Abstr
}
/**
- * Decrements i, mod modulus.
+ * Circularly decrements i, mod modulus.
* Precondition and postcondition: 0 <= i < modulus.
*/
static final int dec(int i, int modulus) {
@@ -234,8 +235,8 @@ public class ArrayDeque extends Abstr
* Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
* @return index 0 <= i < modulus
*/
- static final int add(int i, int distance, int modulus) {
- if ((i += distance) - modulus >= 0) distance -= modulus;
+ static final int inc(int i, int distance, int modulus) {
+ if ((i += distance) - modulus >= 0) i -= modulus;
return i;
}
@@ -244,7 +245,7 @@ public class ArrayDeque extends Abstr
* Index i must be logically ahead of index j.
* Precondition: 0 <= i < modulus, 0 <= j < modulus.
* @return the "circular distance" from j to i; corner case i == j
- * is diambiguated to "empty", returning 0.
+ * is disambiguated to "empty", returning 0.
*/
static final int sub(int i, int j, int modulus) {
if ((i -= j) < 0) i += modulus;
@@ -313,8 +314,7 @@ public class ArrayDeque extends Abstr
/**
* Adds all of the elements in the specified collection at the end
* of this deque, as if by calling {@link #addLast} on each one,
- * in the order that they are returned by the collection's
- * iterator.
+ * in the order that they are returned by the collection's iterator.
*
* @param c the elements to be inserted into this deque
* @return {@code true} if this deque changed as a result of the call
@@ -325,11 +325,15 @@ public class ArrayDeque extends Abstr
final int s, needed;
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
grow(needed);
- c.forEach(this::addLast);
+ copyElements(c);
// checkInvariants();
return size() > s;
}
+ private void copyElements(Collection extends E> c) {
+ c.forEach(this::addLast);
+ }
+
/**
* Inserts the specified element at the front of this deque.
*
@@ -520,8 +524,8 @@ public class ArrayDeque extends Abstr
/**
* Retrieves and removes the head of the queue represented by this deque.
*
- * This method differs from {@link #poll poll} only in that it throws an
- * exception if this deque is empty.
+ * This method differs from {@link #poll() poll()} only in that it
+ * throws an exception if this deque is empty.
*
* This method is equivalent to {@link #removeFirst}.
*
@@ -710,8 +714,7 @@ public class ArrayDeque extends Abstr
throw new NoSuchElementException();
final Object[] es = elements;
E e = nonNullElementAt(es, cursor);
- lastRet = cursor;
- cursor = inc(cursor, es.length);
+ cursor = inc(lastRet = cursor, es.length);
remaining--;
return e;
}
@@ -759,8 +762,7 @@ public class ArrayDeque extends Abstr
throw new NoSuchElementException();
final Object[] es = elements;
E e = nonNullElementAt(es, cursor);
- lastRet = cursor;
- cursor = dec(cursor, es.length);
+ cursor = dec(lastRet = cursor, es.length);
remaining--;
return e;
}
@@ -822,6 +824,8 @@ public class ArrayDeque extends Abstr
/** Constructs spliterator over the given range. */
DeqSpliterator(int origin, int fence) {
+ // assert 0 <= origin && origin < elements.length;
+ // assert 0 <= fence && fence < elements.length;
this.cursor = origin;
this.fence = fence;
}
@@ -841,7 +845,7 @@ public class ArrayDeque extends Abstr
final int i, n;
return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
? null
- : new DeqSpliterator(i, cursor = add(i, n, es.length));
+ : new DeqSpliterator(i, cursor = inc(i, n, es.length));
}
public void forEachRemaining(Consumer super E> action) {
@@ -888,6 +892,9 @@ public class ArrayDeque extends Abstr
}
}
+ /**
+ * @throws NullPointerException {@inheritDoc}
+ */
public void forEach(Consumer super E> action) {
Objects.requireNonNull(action);
final Object[] es = elements;
@@ -1076,11 +1083,14 @@ public class ArrayDeque extends Abstr
/**
* Nulls out slots starting at array index i, upto index end.
+ * Condition i == end means "empty" - nothing to do.
*/
private static void circularClear(Object[] es, int i, int end) {
+ // assert 0 <= i && i < es.length;
+ // assert 0 <= end && end < es.length;
for (int to = (i <= end) ? end : es.length;
; i = 0, to = end) {
- Arrays.fill(es, i, to, null);
+ for (; i < to; i++) es[i] = null;
if (to == end) break;
}
}
@@ -1230,6 +1240,7 @@ public class ArrayDeque extends Abstr
// Read in size and allocate array
int size = s.readInt();
+ SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1);
elements = new Object[size + 1];
this.tail = size;
@@ -1244,8 +1255,8 @@ public class ArrayDeque extends Abstr
// head == tail disambiguates to "empty".
try {
int capacity = elements.length;
- // assert head >= 0 && head < capacity;
- // assert tail >= 0 && tail < capacity;
+ // assert 0 <= head && head < capacity;
+ // assert 0 <= tail && tail < capacity;
// assert capacity > 0;
// assert size() < capacity;
// assert head == tail || elements[head] != null;