--- jsr166/src/main/java/util/ArrayDeque.java 2016/10/20 01:43:31 1.80 +++ jsr166/src/main/java/util/ArrayDeque.java 2016/10/31 21:15:35 1.104 @@ -93,7 +93,7 @@ public class ArrayDeque extends Abstr private void grow(int needed) { // overflow-conscious code // checkInvariants(); - int oldCapacity = elements.length; + final int oldCapacity = elements.length; int newCapacity; // Double size if small; else grow by 50% int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1); @@ -115,8 +115,7 @@ public class ArrayDeque extends Abstr /** Capacity calculation for edge conditions, especially overflow. */ private int newCapacity(int needed, int jump) { - int oldCapacity = elements.length; - int minCapacity; + final int oldCapacity = elements.length, minCapacity; if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) { if (minCapacity < 0) throw new IllegalStateException("Sorry, deque too big"); @@ -184,15 +183,15 @@ public class ArrayDeque extends Abstr * @throws NullPointerException if the specified collection is null */ public ArrayDeque(Collection c) { - Object[] elements = c.toArray(); + Object[] es = c.toArray(); // defend against c.toArray (incorrectly) not returning Object[] // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) - if (elements.getClass() != Object[].class) - elements = Arrays.copyOf(elements, size, Object[].class); - for (Object obj : elements) + if (es.getClass() != Object[].class) + es = Arrays.copyOf(es, es.length, Object[].class); + for (Object obj : es) Objects.requireNonNull(obj); - size = elements.length; - this.elements = elements; + this.elements = es; + this.size = es.length; } /** @@ -200,7 +199,7 @@ public class ArrayDeque extends Abstr * Precondition and postcondition: 0 <= i < modulus. */ static final int inc(int i, int modulus) { - if (++i == modulus) i = 0; + if (++i >= modulus) i = 0; return i; } @@ -209,7 +208,7 @@ public class ArrayDeque extends Abstr * Precondition and postcondition: 0 <= i < modulus. */ static final int dec(int i, int modulus) { - if (--i < 0) i += modulus; + if (--i < 0) i = modulus - 1; return i; } @@ -234,17 +233,18 @@ public class ArrayDeque extends Abstr * Returns element at array index i. */ @SuppressWarnings("unchecked") - final E elementAt(int i) { + private E elementAt(int i) { return (E) elements[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. + * but does catch ones that corrupt traversal. It's a little + * surprising that javac allows this abuse of generics. */ - E checkedElementAt(Object[] elements, int i) { - @SuppressWarnings("unchecked") E e = (E) elements[i]; + static final E nonNullElementAt(Object[] es, int i) { + @SuppressWarnings("unchecked") E e = (E) es[i]; if (e == null) throw new ConcurrentModificationException(); return e; @@ -263,12 +263,17 @@ public class ArrayDeque extends Abstr public void addFirst(E e) { // checkInvariants(); Objects.requireNonNull(e); - Object[] elements; - int capacity, s = size; - while (s == (capacity = (elements = this.elements).length)) + Object[] es; + int capacity, h; + final int s; + if ((s = size) == (capacity = (es = elements).length)) { grow(1); - elements[head = dec(head, capacity)] = e; + capacity = (es = elements).length; + } + if ((h = head - 1) < 0) h = capacity - 1; + es[head = h] = e; size = s + 1; + // checkInvariants(); } /** @@ -282,12 +287,16 @@ public class ArrayDeque extends Abstr public void addLast(E e) { // checkInvariants(); Objects.requireNonNull(e); - Object[] elements; - int capacity, s = size; - while (s == (capacity = (elements = this.elements).length)) + Object[] es; + int capacity; + final int s; + if ((s = size) == (capacity = (es = elements).length)) { grow(1); - elements[add(head, s, capacity)] = e; + capacity = (es = elements).length; + } + es[add(head, s, capacity)] = e; size = s + 1; + // checkInvariants(); } /** @@ -301,23 +310,13 @@ public class ArrayDeque extends Abstr * @throws NullPointerException if the specified collection or any * of its elements are null */ - @Override public boolean addAll(Collection c) { + final int s = size, needed = c.size() - (elements.length - s); + if (needed > 0) + grow(needed); + c.forEach((e) -> addLast(e)); // checkInvariants(); - Object[] a, elements; - int newcomers, capacity, s = size; - if ((newcomers = (a = c.toArray()).length) == 0) - return false; - while ((capacity = (elements = this.elements).length) - s < newcomers) - grow(newcomers - (capacity - s)); - int i = add(head, s, capacity); - for (Object x : a) { - Objects.requireNonNull(x); - elements[i] = x; - i = inc(i, capacity); - size++; - } - return true; + return size > s; } /** @@ -349,10 +348,10 @@ public class ArrayDeque extends Abstr */ public E removeFirst() { // checkInvariants(); - E x = pollFirst(); - if (x == null) + E e = pollFirst(); + if (e == null) throw new NoSuchElementException(); - return x; + return e; } /** @@ -360,21 +359,22 @@ public class ArrayDeque extends Abstr */ public E removeLast() { // checkInvariants(); - E x = pollLast(); - if (x == null) + E e = pollLast(); + if (e == null) throw new NoSuchElementException(); - return x; + return e; } public E pollFirst() { // checkInvariants(); - final int s, h; - if ((s = size) == 0) + int s, h; + if ((s = size) <= 0) return null; - final Object[] elements = this.elements; - @SuppressWarnings("unchecked") E e = (E) elements[h = head]; - elements[h] = null; - head = inc(h, elements.length); + 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; } @@ -382,12 +382,12 @@ public class ArrayDeque extends Abstr public E pollLast() { // checkInvariants(); final int s, tail; - if ((s = size) == 0) + if ((s = size) <= 0) return null; - final Object[] elements = this.elements; + final Object[] es = elements; @SuppressWarnings("unchecked") - E e = (E) elements[tail = add(head, s - 1, elements.length)]; - elements[tail] = null; + E e = (E) es[tail = add(head, s - 1, es.length)]; + es[tail] = null; size = s - 1; return e; } @@ -397,27 +397,34 @@ public class ArrayDeque extends Abstr */ public E getFirst() { // checkInvariants(); - if (size == 0) throw new NoSuchElementException(); + if (size <= 0) throw new NoSuchElementException(); return elementAt(head); } /** * @throws NoSuchElementException {@inheritDoc} */ + @SuppressWarnings("unchecked") public E getLast() { // checkInvariants(); - if (size == 0) throw new NoSuchElementException(); - return elementAt(tail()); + final int s; + if ((s = size) <= 0) throw new NoSuchElementException(); + final Object[] es = elements; + return (E) es[add(head, s - 1, es.length)]; } public E peekFirst() { // checkInvariants(); - return (size == 0) ? null : elementAt(head); + return (size <= 0) ? null : elementAt(head); } + @SuppressWarnings("unchecked") public E peekLast() { // checkInvariants(); - return (size == 0) ? null : elementAt(tail()); + final int s; + if ((s = size) <= 0) return null; + final Object[] es = elements; + return (E) es[add(head, s - 1, es.length)]; } /** @@ -433,15 +440,18 @@ public class ArrayDeque extends Abstr * @return {@code true} if the deque contained the specified element */ public boolean removeFirstOccurrence(Object o) { - // checkInvariants(); if (o != null) { - final Object[] elements = this.elements; - final int capacity = elements.length; - for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) { - if (o.equals(elements[i])) { - delete(i); - return true; - } + 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 (; i < to; i++) + if (o.equals(es[i])) { + delete(i); + return true; + } + if (todo == 0) break; } } return false; @@ -461,14 +471,16 @@ public class ArrayDeque extends Abstr */ public boolean removeLastOccurrence(Object o) { if (o != null) { - final Object[] elements = this.elements; - final int capacity = elements.length; - for (int k = size, i = add(head, k - 1, capacity); - --k >= 0; i = dec(i, capacity)) { - if (o.equals(elements[i])) { - delete(i); - return true; - } + 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--) + if (o.equals(es[i])) { + delete(i); + return true; + } + if (todo == 0) break; } } return false; @@ -600,8 +612,8 @@ public class ArrayDeque extends Abstr */ boolean delete(int i) { // checkInvariants(); - final Object[] elements = this.elements; - final int capacity = elements.length; + 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; @@ -609,14 +621,14 @@ public class ArrayDeque extends Abstr if (front < back) { // move front elements forwards if (h <= i) { - System.arraycopy(elements, h, elements, h + 1, front); + System.arraycopy(es, h, es, h + 1, front); } else { // Wrap around - System.arraycopy(elements, 0, elements, 1, i); - elements[0] = elements[capacity - 1]; - System.arraycopy(elements, h, elements, h + 1, front - (i + 1)); + System.arraycopy(es, 0, es, 1, i); + es[0] = es[capacity - 1]; + System.arraycopy(es, h, es, h + 1, front - (i + 1)); } - elements[h] = null; - head = inc(h, capacity); + es[h] = null; + if ((head = (h + 1)) >= capacity) head = 0; size--; // checkInvariants(); return false; @@ -624,14 +636,14 @@ public class ArrayDeque extends Abstr // move back elements backwards int tail = tail(); if (i <= tail) { - System.arraycopy(elements, i + 1, elements, i, back); + System.arraycopy(es, i + 1, es, i, back); } else { // Wrap around int firstLeg = capacity - (i + 1); - System.arraycopy(elements, i + 1, elements, i, firstLeg); - elements[capacity - 1] = elements[0]; - System.arraycopy(elements, 1, elements, 0, back - firstLeg - 1); + System.arraycopy(es, i + 1, es, i, firstLeg); + es[capacity - 1] = es[0]; + System.arraycopy(es, 1, es, 0, back - firstLeg - 1); } - elements[tail] = null; + es[tail] = null; size--; // checkInvariants(); return true; @@ -689,59 +701,80 @@ public class ArrayDeque extends Abstr DeqIterator() { cursor = head; } - int advance(int i, int modulus) { - return inc(i, modulus); - } - - void doRemove() { - if (delete(lastRet)) - // if left-shifted, undo advance in next() - cursor = dec(cursor, elements.length); - } - public final boolean hasNext() { return remaining > 0; } - public final E next() { - if (remaining == 0) + public E next() { + if (remaining <= 0) throw new NoSuchElementException(); - E e = checkedElementAt(elements, cursor); + final Object[] es = elements; + E e = nonNullElementAt(es, cursor); lastRet = cursor; - cursor = advance(cursor, elements.length); + if (++cursor >= es.length) cursor = 0; remaining--; return e; } + void postDelete(boolean leftShifted) { + if (leftShifted) + if (--cursor < 0) cursor = elements.length - 1; + } + public final void remove() { if (lastRet < 0) throw new IllegalStateException(); - doRemove(); + postDelete(delete(lastRet)); lastRet = -1; } - public final void forEachRemaining(Consumer action) { + public void forEachRemaining(Consumer action) { Objects.requireNonNull(action); - final Object[] elements = ArrayDeque.this.elements; - final int capacity = elements.length; - int k = remaining; - remaining = 0; - for (int i = cursor; --k >= 0; i = advance(i, capacity)) - action.accept(checkedElementAt(elements, i)); + 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; + } } } private class DescendingIterator extends DeqIterator { DescendingIterator() { cursor = tail(); } - @Override int advance(int i, int modulus) { - return dec(i, modulus); + 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; + remaining--; + return e; } - @Override void doRemove() { - if (!delete(lastRet)) - // if right-shifted, undo advance in next - cursor = inc(cursor, elements.length); + void postDelete(boolean leftShifted) { + if (!leftShifted) + if (++cursor >= elements.length) cursor = 0; + } + + public final void forEachRemaining(Consumer 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; + } + if ((lastRet = cursor - (k - 1)) < 0) + lastRet += es.length; + } } } @@ -799,21 +832,19 @@ public class ArrayDeque extends Abstr public void forEachRemaining(Consumer action) { Objects.requireNonNull(action); - final Object[] elements = ArrayDeque.this.elements; - final int capacity = elements.length; - int k = remaining(); + final int k = remaining(); // side effect! remaining = 0; - for (int i = cursor; --k >= 0; i = inc(i, capacity)) - action.accept(checkedElementAt(elements, i)); + ArrayDeque.forEachRemaining(action, elements, cursor, k); } public boolean tryAdvance(Consumer action) { Objects.requireNonNull(action); - if (remaining() == 0) + final int k; + if ((k = remaining()) <= 0) return false; - action.accept(checkedElementAt(elements, cursor)); - cursor = inc(cursor, elements.length); - remaining--; + action.accept(nonNullElementAt(elements, cursor)); + if (++cursor >= elements.length) cursor = 0; + remaining = k - 1; return true; } @@ -829,37 +860,63 @@ public class ArrayDeque extends Abstr } } - @Override + @SuppressWarnings("unchecked") public void forEach(Consumer action) { - // checkInvariants(); Objects.requireNonNull(action); - final Object[] elements = this.elements; - final int capacity = elements.length; - for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) - action.accept(elementAt(i)); + 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 (; i < to; i++) + action.accept((E) es[i]); + if (todo == 0) 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 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[] elements = this.elements; - final int capacity = elements.length; - for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) - elements[i] = operator.apply(elementAt(i)); + 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 (; i < to; i++) + es[i] = operator.apply((E) es[i]); + if (todo == 0) break; + } // checkInvariants(); } /** * @throws NullPointerException {@inheritDoc} */ - @Override public boolean removeIf(Predicate filter) { Objects.requireNonNull(filter); return bulkRemove(filter); @@ -868,7 +925,6 @@ public class ArrayDeque extends Abstr /** * @throws NullPointerException {@inheritDoc} */ - @Override public boolean removeAll(Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> c.contains(e)); @@ -877,45 +933,83 @@ public class ArrayDeque extends Abstr /** * @throws NullPointerException {@inheritDoc} */ - @Override public boolean retainAll(Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); } /** Implementation of bulk remove methods. */ + @SuppressWarnings("unchecked") private boolean bulkRemove(Predicate filter) { // checkInvariants(); - final Object[] elements = this.elements; - final int capacity = elements.length; - int i = head, j = i, remaining = size, deleted = 0; + final Object[] es = elements; + int i, end, to, todo; + todo = (end = (i = head) + size) + - (to = (es.length - end >= 0) ? end : es.length); + // Optimize for initial run of non-removed elements + findFirstRemoved: + for (;; to = todo, todo = 0, i = 0) { + for (; i < to; i++) + if (filter.test((E) es[i])) + break findFirstRemoved; + if (todo == 0) return false; + } + bulkRemoveModified(filter, i, to, todo); + return true; + } + + /** + * Helper for bulkRemove, in case of at least one deletion. + * @param i valid index of first element to be deleted + */ + @SuppressWarnings("unchecked") + private void bulkRemoveModified( + Predicate filter, int i, int to, int todo) { + final Object[] es = elements; + final int capacity = es.length; + // a two-finger algorithm, with hare i and tortoise j + int j = i++; try { - for (; remaining > 0; remaining--, i = inc(i, capacity)) { - @SuppressWarnings("unchecked") E e = (E) elements[i]; - if (filter.test(e)) - deleted++; - else { - if (j != i) - elements[j] = e; - j = inc(j, capacity); - } + for (;;) { + E e; + // In this loop, i and j are on the same leg, with i > j + for (; i < to; i++) + if (!filter.test(e = (E) es[i])) + es[j++] = e; + if (todo == 0) break; + // In this loop, j is on the first leg, i on the second + for (to = todo, todo = 0, i = 0; i < to && j < capacity; i++) + if (!filter.test(e = (E) es[i])) + es[j++] = e; + if (i >= to) break; + j = 0; // j rejoins i on second leg } - return deleted > 0; + bulkRemoveClear(es, j, i); + // checkInvariants(); } catch (Throwable ex) { - if (deleted > 0) - for (; remaining > 0; - remaining--, i = inc(i, capacity), j = inc(j, capacity)) - elements[j] = elements[i]; - throw ex; - } finally { - size -= deleted; - for (; --deleted >= 0; j = inc(j, capacity)) - elements[j] = null; + // copy remaining elements + for (int remaining = (to - i) + todo; --remaining >= 0;) { + es[j] = es[i]; + if (++i >= capacity) i = 0; + if (++j >= capacity) j = 0; + } + bulkRemoveClear(es, j, i); // checkInvariants(); + throw ex; } } /** + * Nulls out all elements from index j upto index i. + */ + private void bulkRemoveClear(Object[] es, int j, int i) { + int deleted; + if ((deleted = i - j) <= 0) deleted += es.length; + size -= deleted; + circularClear(es, j, deleted); + } + + /** * Returns {@code true} if this deque contains the specified element. * More formally, returns {@code true} if and only if this deque contains * at least one element {@code e} such that {@code o.equals(e)}. @@ -925,11 +1019,16 @@ public class ArrayDeque extends Abstr */ public boolean contains(Object o) { if (o != null) { - final Object[] elements = this.elements; - final int capacity = elements.length; - for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) - if (o.equals(elements[i])) - return true; + 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 (; i < to; i++) + if (o.equals(es[i])) + return true; + if (todo == 0) break; + } } return false; } @@ -956,21 +1055,26 @@ public class ArrayDeque extends Abstr * The deque will be empty after this call returns. */ public void clear() { - final Object[] elements = this.elements; - final int capacity = elements.length; - final int h = this.head; - final int s = size; - if (capacity - h >= s) - Arrays.fill(elements, h, h + s, null); - else { - Arrays.fill(elements, h, capacity, null); - Arrays.fill(elements, 0, s - capacity + h, null); - } + circularClear(elements, head, size); size = head = 0; // checkInvariants(); } /** + * Nulls out count elements, starting at array index from. + * Special case (from == es.length) is treated the same as (from == 0). + */ + 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; + } + } + + /** * Returns an array containing all of the elements in this deque * in proper sequence (from first to last element). * @@ -984,11 +1088,23 @@ public class ArrayDeque extends Abstr * @return an array containing all of the elements in this deque */ public Object[] toArray() { - final int head = this.head; - final int firstLeg; - Object[] a = Arrays.copyOfRange(elements, head, head + size); - if ((firstLeg = elements.length - head) < size) - System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); + return toArray(Object[].class); + } + + 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) { + a = Arrays.copyOfRange(es, head, end, klazz); + } else { + // integer overflow! + a = Arrays.copyOfRange(es, 0, size, klazz); + System.arraycopy(es, head, a, 0, len); + } + if (todo > 0) + System.arraycopy(es, 0, a, len, todo); return a; } @@ -1030,20 +1146,18 @@ public class ArrayDeque extends Abstr */ @SuppressWarnings("unchecked") public T[] toArray(T[] a) { - final Object[] elements = this.elements; - final int head = this.head; - final int firstLeg; - boolean wrap = (firstLeg = elements.length - head) < size; - if (size > a.length) { - a = (T[]) Arrays.copyOfRange(elements, head, head + size, - a.getClass()); - } else { - System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size); - if (size < a.length) - a[size] = null; + final int size; + if ((size = this.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) { + System.arraycopy(es, i, a, j, len); + if (todo == 0) break; } - if (wrap) - System.arraycopy(elements, 0, a, firstLeg, size - firstLeg); + if (size < a.length) + a[size] = null; return a; } @@ -1084,10 +1198,15 @@ public class ArrayDeque extends Abstr s.writeInt(size); // Write out elements in order. - final Object[] elements = this.elements; - final int capacity = elements.length; - for (int k = size, i = head; --k >= 0; i = inc(i, capacity)) - s.writeObject(elements[i]); + 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 (; i < to; i++) + s.writeObject(es[i]); + if (todo == 0) break; + } } /** @@ -1110,17 +1229,16 @@ public class ArrayDeque extends Abstr } /** debugging */ - private void checkInvariants() { + void checkInvariants() { try { int capacity = elements.length; - assert size >= 0 && size <= capacity; - assert head >= 0 && ((capacity == 0 && head == 0 && size == 0) - || head < capacity); - assert size == 0 - || (elements[head] != null && elements[tail()] != null); - assert size == capacity - || (elements[dec(head, capacity)] == null - && elements[inc(tail(), capacity)] == null); + // 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; } catch (Throwable t) { System.err.printf("head=%d size=%d capacity=%d%n", head, size, elements.length);