--- jsr166/src/main/java/util/ArrayDeque.java 2016/10/31 16:28:32 1.103
+++ 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
@@ -60,22 +60,40 @@ import java.util.function.UnaryOperator;
public class ArrayDeque extends AbstractCollection
implements Deque, Cloneable, Serializable
{
+ /*
+ * VMs excel at optimizing simple array loops where indices are
+ * incrementing or decrementing over a valid slice, e.g.
+ *
+ * for (int i = start; i < end; i++) ... elements[i]
+ *
+ * Because in a circular array, elements are in general stored in
+ * two disjoint such slices, we help the VM by writing unusual
+ * nested loops for all traversals over the elements. Having only
+ * one hot inner loop body instead of two or three eases human
+ * maintenance and encourages VM loop inlining into the caller.
+ */
+
/**
* The array in which the elements of the deque are stored.
- * We guarantee that all array cells not holding deque elements
- * are always null.
+ * All array cells not holding deque elements are always null.
+ * The array always has at least one null slot (at tail).
*/
transient Object[] elements;
/**
* The index of the element at the head of the deque (which is the
* element that would be removed by remove() or pop()); or an
- * arbitrary number 0 <= head < elements.length if the deque is empty.
+ * arbitrary number 0 <= head < elements.length equal to tail if
+ * the deque is empty.
*/
transient int head;
- /** Number of elements in this collection. */
- transient int size;
+ /**
+ * The index at which the next element would be added to the tail
+ * of the deque (via addLast(E), add(E), or push(E));
+ * elements[tail] is always null.
+ */
+ transient int tail;
/**
* The maximum size of array to allocate.
@@ -92,23 +110,23 @@ public class ArrayDeque extends Abstr
*/
private void grow(int needed) {
// overflow-conscious code
- // checkInvariants();
final int oldCapacity = elements.length;
int newCapacity;
- // Double size if small; else grow by 50%
+ // Double capacity if small; else grow by 50%
int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1);
if (jump < needed
|| (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0)
newCapacity = newCapacity(needed, jump);
- elements = Arrays.copyOf(elements, newCapacity);
- if (oldCapacity - head < size) {
+ final Object[] es = elements = Arrays.copyOf(elements, newCapacity);
+ // Exceptionally, here tail == head needs to be disambiguated
+ 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();
}
@@ -136,8 +154,9 @@ public class ArrayDeque extends Abstr
* @since TBD
*/
/* public */ void ensureCapacity(int minCapacity) {
- if (minCapacity > elements.length)
- grow(minCapacity - elements.length);
+ int needed;
+ if ((needed = (minCapacity + 1 - elements.length)) > 0)
+ grow(needed);
// checkInvariants();
}
@@ -147,9 +166,11 @@ public class ArrayDeque extends Abstr
* @since TBD
*/
/* public */ void trimToSize() {
- if (size < elements.length) {
- elements = toArray();
+ int size;
+ if ((size = size()) + 1 < elements.length) {
+ elements = toArray(new Object[size + 1]);
head = 0;
+ tail = size;
}
// checkInvariants();
}
@@ -169,7 +190,10 @@ public class ArrayDeque extends Abstr
* @param numElements lower bound on initial capacity of the deque
*/
public ArrayDeque(int numElements) {
- elements = new Object[numElements];
+ elements =
+ new Object[(numElements < 1) ? 1 :
+ (numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE :
+ numElements + 1];
}
/**
@@ -183,15 +207,8 @@ public class ArrayDeque extends Abstr
* @throws NullPointerException if the specified collection is null
*/
public ArrayDeque(Collection extends E> c) {
- Object[] es = c.toArray();
- // defend against c.toArray (incorrectly) not returning Object[]
- // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
- if (es.getClass() != Object[].class)
- es = Arrays.copyOf(es, es.length, Object[].class);
- for (Object obj : es)
- Objects.requireNonNull(obj);
- this.elements = es;
- this.size = es.length;
+ this(c.size());
+ addAll(c);
}
/**
@@ -213,35 +230,40 @@ public class ArrayDeque extends Abstr
}
/**
- * Adds i and j, mod modulus.
- * Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus.
+ * Circularly adds the given distance to index i, mod modulus.
+ * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
+ * @return index 0 <= i < modulus
*/
- static final int add(int i, int j, int modulus) {
- if ((i += j) - modulus >= 0) i -= modulus;
+ static final int add(int i, int distance, int modulus) {
+ if ((i += distance) - modulus >= 0) i -= modulus;
return i;
}
/**
- * Returns the array index of the last element.
- * May return invalid index -1 if there are no elements.
+ * Subtracts j from i, mod modulus.
+ * 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.
*/
- final int tail() {
- return add(head, size - 1, elements.length);
+ static final int sub(int i, int j, int modulus) {
+ if ((i -= j) < 0) i += modulus;
+ return i;
}
/**
* Returns element at array index i.
+ * This is a slight abuse of generics, accepted by javac.
*/
@SuppressWarnings("unchecked")
- private E elementAt(int i) {
- return (E) elements[i];
+ static final E elementAt(Object[] es, int i) {
+ return (E) es[i];
}
/**
* A version of elementAt that checks for null elements.
* This check doesn't catch all possible comodifications,
- * but does catch ones that corrupt traversal. It's a little
- * surprising that javac allows this abuse of generics.
+ * but does catch ones that corrupt traversal.
*/
static final E nonNullElementAt(Object[] es, int i) {
@SuppressWarnings("unchecked") E e = (E) es[i];
@@ -261,18 +283,12 @@ public class ArrayDeque extends Abstr
* @throws NullPointerException if the specified element is null
*/
public void addFirst(E e) {
- // checkInvariants();
- Objects.requireNonNull(e);
- Object[] es;
- int capacity, h;
- final int s;
- if ((s = size) == (capacity = (es = elements).length)) {
+ if (e == null)
+ throw new NullPointerException();
+ final Object[] es = elements;
+ es[head = dec(head, es.length)] = e;
+ if (head == tail)
grow(1);
- capacity = (es = elements).length;
- }
- if ((h = head - 1) < 0) h = capacity - 1;
- es[head = h] = e;
- size = s + 1;
// checkInvariants();
}
@@ -285,25 +301,19 @@ public class ArrayDeque extends Abstr
* @throws NullPointerException if the specified element is null
*/
public void addLast(E e) {
- // checkInvariants();
- Objects.requireNonNull(e);
- Object[] es;
- int capacity;
- final int s;
- if ((s = size) == (capacity = (es = elements).length)) {
+ if (e == null)
+ throw new NullPointerException();
+ final Object[] es = elements;
+ es[tail] = e;
+ if (head == (tail = inc(tail, es.length)))
grow(1);
- capacity = (es = elements).length;
- }
- es[add(head, s, capacity)] = e;
- size = s + 1;
// checkInvariants();
}
/**
* 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
@@ -311,12 +321,12 @@ public class ArrayDeque extends Abstr
* of its elements are null
*/
public boolean addAll(Collection extends E> c) {
- final int s = size, needed = c.size() - (elements.length - s);
- if (needed > 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;
+ return size() > s;
}
/**
@@ -347,10 +357,10 @@ public class ArrayDeque extends Abstr
* @throws NoSuchElementException {@inheritDoc}
*/
public E removeFirst() {
- // checkInvariants();
E e = pollFirst();
if (e == null)
throw new NoSuchElementException();
+ // checkInvariants();
return e;
}
@@ -358,37 +368,32 @@ public class ArrayDeque extends Abstr
* @throws NoSuchElementException {@inheritDoc}
*/
public E removeLast() {
- // checkInvariants();
E e = pollLast();
if (e == null)
throw new NoSuchElementException();
+ // checkInvariants();
return e;
}
public E pollFirst() {
+ final Object[] es;
+ final int h;
+ E e = elementAt(es = elements, h = head);
+ if (e != null) {
+ es[h] = null;
+ head = inc(h, es.length);
+ }
// checkInvariants();
- int s, h;
- if ((s = size) <= 0)
- return null;
- final Object[] es = elements;
- @SuppressWarnings("unchecked") E e = (E) es[h = head];
- es[h] = null;
- if (++h >= es.length) h = 0;
- head = h;
- size = s - 1;
return e;
}
public E pollLast() {
+ final Object[] es;
+ final int t;
+ E e = elementAt(es = elements, t = dec(tail, es.length));
+ if (e != null)
+ es[tail = t] = null;
// checkInvariants();
- final int s, tail;
- if ((s = size) <= 0)
- return null;
- final Object[] es = elements;
- @SuppressWarnings("unchecked")
- E e = (E) es[tail = add(head, s - 1, es.length)];
- es[tail] = null;
- size = s - 1;
return e;
}
@@ -396,35 +401,34 @@ public class ArrayDeque extends Abstr
* @throws NoSuchElementException {@inheritDoc}
*/
public E getFirst() {
+ E e = elementAt(elements, head);
+ if (e == null)
+ throw new NoSuchElementException();
// checkInvariants();
- if (size <= 0) throw new NoSuchElementException();
- return elementAt(head);
+ return e;
}
/**
* @throws NoSuchElementException {@inheritDoc}
*/
- @SuppressWarnings("unchecked")
public E getLast() {
- // checkInvariants();
- final int s;
- if ((s = size) <= 0) throw new NoSuchElementException();
final Object[] es = elements;
- return (E) es[add(head, s - 1, es.length)];
+ E e = elementAt(es, dec(tail, es.length));
+ if (e == null)
+ throw new NoSuchElementException();
+ // checkInvariants();
+ return e;
}
public E peekFirst() {
// checkInvariants();
- return (size <= 0) ? null : elementAt(head);
+ return elementAt(elements, head);
}
- @SuppressWarnings("unchecked")
public E peekLast() {
// checkInvariants();
- final int s;
- if ((s = size) <= 0) return null;
- final Object[] es = elements;
- return (E) es[add(head, s - 1, es.length)];
+ final Object[] es;
+ return elementAt(es = elements, dec(tail, es.length));
}
/**
@@ -442,16 +446,14 @@ public class ArrayDeque extends Abstr
public boolean removeFirstOccurrence(Object o) {
if (o != null) {
final Object[] es = elements;
- int i, end, to, todo;
- todo = (end = (i = head) + size)
- - (to = (es.length - end >= 0) ? end : es.length);
- for (;; to = todo, todo = 0, i = 0) {
+ for (int i = head, end = tail, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
for (; i < to; i++)
if (o.equals(es[i])) {
delete(i);
return true;
}
- if (todo == 0) break;
+ if (to == end) break;
}
}
return false;
@@ -472,15 +474,14 @@ public class ArrayDeque extends Abstr
public boolean removeLastOccurrence(Object o) {
if (o != null) {
final Object[] es = elements;
- int i, to, end, todo;
- todo = (to = ((end = (i = tail()) - size) >= -1) ? end : -1) - end;
- for (;; to = (i = es.length - 1) - todo, todo = 0) {
- for (; i > to; i--)
+ for (int i = tail, end = head, to = (i >= end) ? end : 0;
+ ; i = es.length, to = end) {
+ for (i--; i > to - 1; i--)
if (o.equals(es[i])) {
delete(i);
return true;
}
- if (todo == 0) break;
+ if (to == end) break;
}
}
return false;
@@ -518,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}.
*
@@ -608,16 +609,17 @@ public class ArrayDeque extends Abstr
* This method is called delete rather than remove to emphasize
* that its semantics differ from those of {@link List#remove(int)}.
*
- * @return true if elements moved backwards
+ * @return true if elements near tail moved backwards
*/
boolean delete(int i) {
// checkInvariants();
final Object[] es = elements;
final int capacity = es.length;
- final int h = head;
- int front; // number of elements before to-be-deleted elt
- if ((front = i - h) < 0) front += capacity;
- final int back = size - front - 1; // number of elements after
+ final int h, t;
+ // number of elements before to-be-deleted elt
+ 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) {
@@ -628,23 +630,20 @@ public class ArrayDeque extends Abstr
System.arraycopy(es, h, es, h + 1, front - (i + 1));
}
es[h] = null;
- if ((head = (h + 1)) >= capacity) head = 0;
- size--;
+ head = inc(h, capacity);
// checkInvariants();
return false;
} else {
// move back elements backwards
- int tail = tail();
+ 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;
- size--;
// checkInvariants();
return true;
}
@@ -658,7 +657,7 @@ public class ArrayDeque extends Abstr
* @return the number of elements in this deque
*/
public int size() {
- return size;
+ return sub(tail, head, elements.length);
}
/**
@@ -667,7 +666,7 @@ public class ArrayDeque extends Abstr
* @return {@code true} if this deque contains no elements
*/
public boolean isEmpty() {
- return size == 0;
+ return head == tail;
}
/**
@@ -691,7 +690,7 @@ public class ArrayDeque extends Abstr
int cursor;
/** Number of elements yet to be returned. */
- int remaining = size;
+ int remaining = size();
/**
* Index of element returned by most recent call to next.
@@ -710,15 +709,14 @@ public class ArrayDeque extends Abstr
throw new NoSuchElementException();
final Object[] es = elements;
E e = nonNullElementAt(es, cursor);
- lastRet = cursor;
- if (++cursor >= es.length) cursor = 0;
+ cursor = inc(lastRet = cursor, es.length);
remaining--;
return e;
}
void postDelete(boolean leftShifted) {
if (leftShifted)
- if (--cursor < 0) cursor = elements.length - 1;
+ cursor = dec(cursor, elements.length);
}
public final void remove() {
@@ -730,50 +728,65 @@ public class ArrayDeque extends Abstr
public void forEachRemaining(Consumer super E> action) {
Objects.requireNonNull(action);
- final int k;
- if ((k = remaining) > 0) {
- remaining = 0;
- ArrayDeque.forEachRemaining(action, elements, cursor, k);
- if ((lastRet = cursor + k - 1) >= elements.length)
- lastRet -= elements.length;
+ int r;
+ if ((r = remaining) <= 0)
+ return;
+ remaining = 0;
+ final Object[] es = elements;
+ if (es[cursor] == null || sub(tail, cursor, es.length) != r)
+ throw new ConcurrentModificationException();
+ for (int i = cursor, end = tail, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
+ for (; i < to; i++)
+ action.accept(elementAt(es, i));
+ if (to == end) {
+ if (end != tail)
+ throw new ConcurrentModificationException();
+ lastRet = dec(end, es.length);
+ break;
+ }
}
}
}
private class DescendingIterator extends DeqIterator {
- DescendingIterator() { cursor = tail(); }
+ DescendingIterator() { cursor = dec(tail, elements.length); }
public final E next() {
if (remaining <= 0)
throw new NoSuchElementException();
final Object[] es = elements;
E e = nonNullElementAt(es, cursor);
- lastRet = cursor;
- if (--cursor < 0) cursor = es.length - 1;
+ cursor = dec(lastRet = cursor, es.length);
remaining--;
return e;
}
void postDelete(boolean leftShifted) {
if (!leftShifted)
- if (++cursor >= elements.length) cursor = 0;
+ cursor = inc(cursor, elements.length);
}
public final void forEachRemaining(Consumer super E> action) {
Objects.requireNonNull(action);
- final int k;
- if ((k = remaining) > 0) {
- remaining = 0;
- final Object[] es = elements;
- int i, end, to, todo;
- todo = (to = ((end = (i = cursor) - k) >= -1) ? end : -1) - end;
- for (;; to = (i = es.length - 1) - todo, todo = 0) {
- for (; i > to; i--)
- action.accept(nonNullElementAt(es, i));
- if (todo == 0) break;
+ int r;
+ if ((r = remaining) <= 0)
+ return;
+ remaining = 0;
+ final Object[] es = elements;
+ if (es[cursor] == null || sub(cursor, head, es.length) + 1 != r)
+ throw new ConcurrentModificationException();
+ for (int i = cursor, end = head, to = (i >= end) ? end : 0;
+ ; i = es.length - 1, to = end) {
+ // hotspot generates faster code than for: i >= to !
+ for (; i > to - 1; i--)
+ action.accept(elementAt(es, i));
+ if (to == end) {
+ if (end != head)
+ throw new ConcurrentModificationException();
+ lastRet = end;
+ break;
}
- if ((lastRet = cursor - (k - 1)) < 0)
- lastRet += es.length;
}
}
}
@@ -792,64 +805,78 @@ public class ArrayDeque extends Abstr
* @since 1.8
*/
public Spliterator spliterator() {
- return new ArrayDequeSpliterator();
+ return new DeqSpliterator();
}
- final class ArrayDequeSpliterator implements Spliterator {
- private int cursor;
- private int remaining; // -1 until late-binding first use
+ final class DeqSpliterator implements Spliterator {
+ private int fence; // -1 until first use
+ private int cursor; // current index, modified on traverse/split
/** Constructs late-binding spliterator over all elements. */
- ArrayDequeSpliterator() {
- this.remaining = -1;
+ DeqSpliterator() {
+ this.fence = -1;
}
- /** Constructs spliterator over the given slice. */
- ArrayDequeSpliterator(int cursor, int count) {
- this.cursor = cursor;
- this.remaining = count;
- }
-
- /** Ensures late-binding initialization; then returns remaining. */
- private int remaining() {
- if (remaining < 0) {
+ /** 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;
+ }
+
+ /** Ensures late-binding initialization; then returns fence. */
+ private int getFence() { // force initialization
+ int t;
+ if ((t = fence) < 0) {
+ t = fence = tail;
cursor = head;
- remaining = size;
}
- return remaining;
+ return t;
}
- public ArrayDequeSpliterator trySplit() {
- final int mid;
- if ((mid = remaining() >> 1) > 0) {
- int oldCursor = cursor;
- cursor = add(cursor, mid, elements.length);
- remaining -= mid;
- return new ArrayDequeSpliterator(oldCursor, mid);
- }
- return null;
+ public DeqSpliterator trySplit() {
+ final Object[] es = elements;
+ final int i, n;
+ return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
+ ? null
+ : new DeqSpliterator(i, cursor = add(i, n, es.length));
}
public void forEachRemaining(Consumer super E> action) {
- Objects.requireNonNull(action);
- final int k = remaining(); // side effect!
- remaining = 0;
- ArrayDeque.forEachRemaining(action, elements, cursor, k);
+ if (action == null)
+ throw new NullPointerException();
+ final int end = getFence(), cursor = this.cursor;
+ final Object[] es = elements;
+ if (cursor != end) {
+ this.cursor = end;
+ // null check at both ends of range is sufficient
+ if (es[cursor] == null || es[dec(end, es.length)] == null)
+ throw new ConcurrentModificationException();
+ for (int i = cursor, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
+ for (; i < to; i++)
+ action.accept(elementAt(es, i));
+ if (to == end) break;
+ }
+ }
}
public boolean tryAdvance(Consumer super E> action) {
Objects.requireNonNull(action);
- final int k;
- if ((k = remaining()) <= 0)
+ final Object[] es = elements;
+ if (fence < 0) { fence = tail; cursor = head; } // late-binding
+ final int i;
+ if ((i = cursor) == fence)
return false;
- action.accept(nonNullElementAt(elements, cursor));
- if (++cursor >= elements.length) cursor = 0;
- remaining = k - 1;
+ E e = nonNullElementAt(es, i);
+ cursor = inc(i, es.length);
+ action.accept(e);
return true;
}
public long estimateSize() {
- return remaining();
+ return sub(getFence(), cursor, elements.length);
}
public int characteristics() {
@@ -860,56 +887,42 @@ public class ArrayDeque extends Abstr
}
}
- @SuppressWarnings("unchecked")
+ /**
+ * @throws NullPointerException {@inheritDoc}
+ */
public void forEach(Consumer super E> action) {
Objects.requireNonNull(action);
final Object[] es = elements;
- int i, end, to, todo;
- todo = (end = (i = head) + size)
- - (to = (es.length - end >= 0) ? end : es.length);
- for (;; to = todo, todo = 0, i = 0) {
+ for (int i = head, end = tail, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
for (; i < to; i++)
- action.accept((E) es[i]);
- if (todo == 0) break;
+ action.accept(elementAt(es, i));
+ if (to == end) {
+ if (end != tail) throw new ConcurrentModificationException();
+ break;
+ }
}
// checkInvariants();
}
/**
- * Calls action on remaining elements, starting at index i and
- * traversing in ascending order. A variant of forEach that also
- * checks for concurrent modification, for use in iterators.
- */
- static void forEachRemaining(
- Consumer super E> action, Object[] es, int i, int remaining) {
- int end, to, todo;
- todo = (end = i + remaining)
- - (to = (es.length - end >= 0) ? end : es.length);
- for (;; to = todo, todo = 0, i = 0) {
- for (; i < to; i++)
- action.accept(nonNullElementAt(es, i));
- if (todo == 0) break;
- }
- }
-
- /**
* Replaces each element of this deque with the result of applying the
* operator to that element, as specified by {@link List#replaceAll}.
*
* @param operator the operator to apply to each element
* @since TBD
*/
- @SuppressWarnings("unchecked")
/* public */ void replaceAll(UnaryOperator operator) {
Objects.requireNonNull(operator);
final Object[] es = elements;
- int i, end, to, todo;
- todo = (end = (i = head) + size)
- - (to = (es.length - end >= 0) ? end : es.length);
- for (;; to = todo, todo = 0, i = 0) {
+ for (int i = head, end = tail, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
for (; i < to; i++)
- es[i] = operator.apply((E) es[i]);
- if (todo == 0) break;
+ es[i] = operator.apply(elementAt(es, i));
+ if (to == end) {
+ if (end != tail) throw new ConcurrentModificationException();
+ break;
+ }
}
// checkInvariants();
}
@@ -942,34 +955,76 @@ public class ArrayDeque extends Abstr
private boolean bulkRemove(Predicate super E> filter) {
// checkInvariants();
final Object[] es = elements;
+ // Optimize for initial run of survivors
+ for (int i = head, end = tail, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
+ for (; i < to; i++)
+ if (filter.test(elementAt(es, i)))
+ return bulkRemoveModified(filter, i);
+ if (to == end) {
+ if (end != tail) throw new ConcurrentModificationException();
+ break;
+ }
+ }
+ return false;
+ }
+
+ // A tiny bit set implementation
+
+ private static long[] nBits(int n) {
+ return new long[((n - 1) >> 6) + 1];
+ }
+ private static void setBit(long[] bits, int i) {
+ bits[i >> 6] |= 1L << i;
+ }
+ private static boolean isClear(long[] bits, int i) {
+ return (bits[i >> 6] & (1L << i)) == 0;
+ }
+
+ /**
+ * Helper for bulkRemove, in case of at least one deletion.
+ * 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.
+ *
+ * @param beg valid index of first element to be deleted
+ */
+ private boolean bulkRemoveModified(
+ Predicate super E> filter, final int beg) {
+ final Object[] es = elements;
final int capacity = es.length;
- int i = head, j = i, remaining = size, deleted = 0;
- try {
- for (; remaining > 0; remaining--) {
- @SuppressWarnings("unchecked") E e = (E) es[i];
- if (filter.test(e))
- deleted++;
- else {
- if (j != i)
- es[j] = e;
- if (++j >= capacity) j = 0;
- }
- if (++i >= capacity) i = 0;
+ final int end = tail;
+ final long[] deathRow = nBits(sub(end, beg, capacity));
+ deathRow[0] = 1L; // set bit 0
+ for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
+ ; i = 0, to = end, k -= capacity) {
+ for (; i < to; i++)
+ if (filter.test(elementAt(es, i)))
+ setBit(deathRow, i - k);
+ if (to == end) break;
+ }
+ // a two-finger traversal, with hare i reading, tortoise w writing
+ int w = beg;
+ for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
+ ; w = 0) { // w rejoins i on second leg
+ // In this loop, i and w are on the same leg, with i > w
+ for (; i < to; i++)
+ if (isClear(deathRow, i - k))
+ es[w++] = es[i];
+ if (to == end) break;
+ // In this loop, w is on the first leg, i on the second
+ for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
+ if (isClear(deathRow, i - k))
+ es[w++] = es[i];
+ if (i >= to) {
+ if (w == capacity) w = 0; // "corner" case
+ break;
}
- return deleted > 0;
- } catch (Throwable ex) {
- if (deleted > 0)
- for (; remaining > 0; remaining--) {
- es[j] = es[i];
- if (++i >= capacity) i = 0;
- if (++j >= capacity) j = 0;
- }
- throw ex;
- } finally {
- size -= deleted;
- circularClear(es, j, deleted);
- // checkInvariants();
}
+ if (end != tail) throw new ConcurrentModificationException();
+ circularClear(es, tail = w, end);
+ // checkInvariants();
+ return true;
}
/**
@@ -983,14 +1038,12 @@ public class ArrayDeque extends Abstr
public boolean contains(Object o) {
if (o != null) {
final Object[] es = elements;
- int i, end, to, todo;
- todo = (end = (i = head) + size)
- - (to = (es.length - end >= 0) ? end : es.length);
- for (;; to = todo, todo = 0, i = 0) {
+ for (int i = head, end = tail, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
for (; i < to; i++)
if (o.equals(es[i]))
return true;
- if (todo == 0) break;
+ if (to == end) break;
}
}
return false;
@@ -1018,21 +1071,22 @@ public class ArrayDeque extends Abstr
* The deque will be empty after this call returns.
*/
public void clear() {
- circularClear(elements, head, size);
- size = head = 0;
+ circularClear(elements, head, tail);
+ head = tail = 0;
// checkInvariants();
}
/**
- * Nulls out count elements, starting at array index from.
+ * 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 from, int count) {
- int end, to, todo;
- todo = (end = from + count)
- - (to = (es.length - end >= 0) ? end : es.length);
- for (;; to = todo, todo = 0, from = 0) {
- Arrays.fill(es, from, to, null);
- if (todo == 0) break;
+ 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) {
+ for (; i < to; i++) es[i] = null;
+ if (to == end) break;
}
}
@@ -1056,17 +1110,17 @@ public class ArrayDeque extends Abstr
private T[] toArray(Class klazz) {
final Object[] es = elements;
final T[] a;
- final int head, len, end, todo;
- todo = size - (len = Math.min(size, es.length - (head = this.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 (todo > 0)
- System.arraycopy(es, 0, a, len, todo);
+ if (end != tail)
+ System.arraycopy(es, 0, a, es.length - head, tail);
return a;
}
@@ -1109,14 +1163,13 @@ public class ArrayDeque extends Abstr
@SuppressWarnings("unchecked")
public T[] toArray(T[] a) {
final int size;
- if ((size = this.size) > a.length)
+ if ((size = size()) > a.length)
return toArray((Class) a.getClass());
final Object[] es = elements;
- int i, j, len, todo;
- todo = size - (len = Math.min(size, es.length - (i = head)));
- for (j = 0;; j += len, len = todo, todo = 0, i = 0) {
+ for (int i = head, j = 0, len = Math.min(size, es.length - i);
+ ; i = 0, len = tail) {
System.arraycopy(es, i, a, j, len);
- if (todo == 0) break;
+ if ((j += len) == size) break;
}
if (size < a.length)
a[size] = null;
@@ -1157,17 +1210,15 @@ public class ArrayDeque extends Abstr
s.defaultWriteObject();
// Write out size
- s.writeInt(size);
+ s.writeInt(size());
// Write out elements in order.
final Object[] es = elements;
- int i, end, to, todo;
- todo = (end = (i = head) + size)
- - (to = (es.length - end >= 0) ? end : es.length);
- for (;; to = todo, todo = 0, i = 0) {
+ for (int i = head, end = tail, to = (i <= end) ? end : es.length;
+ ; i = 0, to = end) {
for (; i < to; i++)
s.writeObject(es[i]);
- if (todo == 0) break;
+ if (to == end) break;
}
}
@@ -1183,7 +1234,9 @@ public class ArrayDeque extends Abstr
s.defaultReadObject();
// Read in size and allocate array
- elements = new Object[size = s.readInt()];
+ int size = s.readInt();
+ elements = new Object[size + 1];
+ this.tail = size;
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
@@ -1192,18 +1245,20 @@ public class ArrayDeque extends Abstr
/** debugging */
void checkInvariants() {
+ // Use head and tail fields with empty slot at tail strategy.
+ // head == tail disambiguates to "empty".
try {
int capacity = elements.length;
- // assert size >= 0 && size <= capacity;
- // assert head >= 0;
- // assert capacity == 0 || head < capacity;
- // assert size == 0 || elements[head] != null;
- // assert size == 0 || elements[tail()] != null;
- // assert size == capacity || elements[dec(head, capacity)] == null;
- // assert size == capacity || elements[inc(tail(), capacity)] == null;
+ // assert 0 <= head && head < capacity;
+ // assert 0 <= tail && tail < capacity;
+ // assert capacity > 0;
+ // assert size() < capacity;
+ // assert head == tail || elements[head] != null;
+ // assert elements[tail] == null;
+ // assert head == tail || elements[dec(tail, capacity)] != null;
} catch (Throwable t) {
- System.err.printf("head=%d size=%d capacity=%d%n",
- head, size, elements.length);
+ System.err.printf("head=%d tail=%d capacity=%d%n",
+ head, tail, elements.length);
System.err.printf("elements=%s%n",
Arrays.toString(elements));
throw t;