--- jsr166/src/main/java/util/ArrayDeque.java 2016/11/18 03:22:20 1.114
+++ jsr166/src/main/java/util/ArrayDeque.java 2017/05/31 19:01:08 1.129
@@ -50,7 +50,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 +117,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();
}
@@ -235,7 +235,7 @@ public class ArrayDeque extends Abstr
* @return index 0 <= i < modulus
*/
static final int add(int i, int distance, int modulus) {
- if ((i += distance) - modulus >= 0) distance -= modulus;
+ if ((i += distance) - modulus >= 0) i -= modulus;
return i;
}
@@ -313,8 +313,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
@@ -322,10 +321,10 @@ public class ArrayDeque extends Abstr
* of its elements are null
*/
public boolean addAll(Collection extends E> c) {
- final int s = size(), needed;
- if ((needed = s + c.size() - elements.length + 1) > 0)
+ final int s, needed;
+ if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
grow(needed);
- c.forEach(e -> addLast(e));
+ c.forEach(this::addLast);
// checkInvariants();
return size() > s;
}
@@ -520,8 +519,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}.
*
@@ -616,10 +615,11 @@ public class ArrayDeque extends Abstr
// checkInvariants();
final Object[] es = elements;
final int capacity = es.length;
- final int h = head;
+ final int h, t;
// number of elements before to-be-deleted elt
- final int front = sub(i, h, capacity);
- final int back = size() - front - 1; // number of elements after
+ final int front = sub(i, h = head, capacity);
+ // number of elements after to-be-deleted elt
+ final int back = sub(t = tail, i, capacity) - 1;
if (front < back) {
// move front elements forwards
if (h <= i) {
@@ -635,14 +635,13 @@ public class ArrayDeque extends Abstr
return false;
} else {
// move back elements backwards
- tail = dec(tail, capacity);
+ tail = dec(t, capacity);
if (i <= tail) {
System.arraycopy(es, i + 1, es, i, back);
} else { // Wrap around
- int firstLeg = capacity - (i + 1);
- System.arraycopy(es, i + 1, es, i, firstLeg);
+ System.arraycopy(es, i + 1, es, i, capacity - (i + 1));
es[capacity - 1] = es[0];
- System.arraycopy(es, 1, es, 0, back - firstLeg - 1);
+ System.arraycopy(es, 1, es, 0, t - 1);
}
es[tail] = null;
// checkInvariants();
@@ -710,8 +709,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 +757,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 +819,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;
}
@@ -864,15 +863,15 @@ public class ArrayDeque extends Abstr
}
public boolean tryAdvance(Consumer super E> action) {
- if (action == null)
- throw new NullPointerException();
- int t, i;
- if ((t = fence) < 0) t = getFence();
- if (t == (i = cursor))
- return false;
+ Objects.requireNonNull(action);
final Object[] es = elements;
+ if (fence < 0) { fence = tail; cursor = head; } // late-binding
+ final int i;
+ if ((i = cursor) == fence)
+ return false;
+ E e = nonNullElementAt(es, i);
cursor = inc(i, es.length);
- action.accept(nonNullElementAt(es, i));
+ action.accept(e);
return true;
}
@@ -888,6 +887,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 +1078,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;
}
}
@@ -1105,17 +1110,17 @@ public class ArrayDeque extends Abstr
private T[] toArray(Class klazz) {
final Object[] es = elements;
final T[] a;
- final int size = size(), head = this.head, end;
- final int len = Math.min(size, es.length - head);
- if ((end = head + size) >= 0) {
+ final int head = this.head, tail = this.tail, end;
+ if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) {
+ // Uses null extension feature of copyOfRange
a = Arrays.copyOfRange(es, head, end, klazz);
} else {
// integer overflow!
- a = Arrays.copyOfRange(es, 0, size, klazz);
- System.arraycopy(es, head, a, 0, len);
+ a = Arrays.copyOfRange(es, 0, end - head, klazz);
+ System.arraycopy(es, head, a, 0, es.length - head);
}
- if (tail < head)
- System.arraycopy(es, 0, a, len, tail);
+ if (end != tail)
+ System.arraycopy(es, 0, a, es.length - head, tail);
return a;
}
@@ -1244,8 +1249,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;