--- jsr166/src/main/java/util/ArrayDeque.java 2016/11/13 02:10:09 1.113 +++ jsr166/src/main/java/util/ArrayDeque.java 2016/11/20 08:30:56 1.119 @@ -68,13 +68,15 @@ public class ArrayDeque extends Abstr * * 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. + * 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; @@ -88,7 +90,8 @@ public class ArrayDeque extends Abstr /** * The index at which the next element would be added to the tail - * of the deque (via addLast(E), add(E), or push(E)). + * of the deque (via addLast(E), add(E), or push(E)); + * elements[tail] is always null. */ transient int tail; @@ -187,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[Math.max(1, numElements + 1)]; + elements = + new Object[(numElements < 1) ? 1 : + (numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE : + numElements + 1]; } /** @@ -201,7 +207,7 @@ public class ArrayDeque extends Abstr * @throws NullPointerException if the specified collection is null */ public ArrayDeque(Collection c) { - elements = new Object[c.size() + 1]; + this(c.size()); addAll(c); } @@ -224,19 +230,21 @@ 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) distance -= modulus; return i; } /** * Subtracts j from i, mod modulus. - * Index i must be logically ahead of j. - * Returns the "circular distance" from j to i. - * Precondition and postcondition: 0 <= i < modulus, 0 <= j < 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. */ static final int sub(int i, int j, int modulus) { if ((i -= j) < 0) i += modulus; @@ -314,10 +322,10 @@ public class ArrayDeque extends Abstr * of its elements are null */ public boolean addAll(Collection 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; } @@ -608,10 +616,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) { @@ -627,14 +636,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(); @@ -858,13 +866,12 @@ public class ArrayDeque extends Abstr public boolean tryAdvance(Consumer action) { if (action == null) throw new NullPointerException(); - int t, i; - if ((t = fence) < 0) t = getFence(); - if (t == (i = cursor)) + final int t, i; + if ((t = getFence()) == (i = cursor)) return false; - final Object[] es; - action.accept(nonNullElementAt(es = elements, i)); + final Object[] es = elements; cursor = inc(i, es.length); + action.accept(nonNullElementAt(es, i)); return true; } @@ -1097,17 +1104,16 @@ 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) { 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; } @@ -1232,6 +1238,8 @@ 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 head >= 0 && head < capacity;