--- jsr166/src/main/java/util/ArrayDeque.java 2016/10/23 00:28:41 1.83 +++ jsr166/src/main/java/util/ArrayDeque.java 2016/10/30 19:34:36 1.101 @@ -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,22 +263,19 @@ public class ArrayDeque extends Abstr public void addFirst(E e) { // checkInvariants(); Objects.requireNonNull(e); - final Object[] elements; - final int capacity, s; - if ((s = size) == (capacity = (elements = this.elements).length)) - addFirstSlowPath(e); - else - elements[head = dec(head, capacity)] = e; + Object[] es; + int capacity, h; + final int s; + if ((s = size) == (capacity = (es = elements).length)) { + grow(1); + capacity = (es = elements).length; + } + if ((h = head - 1) < 0) h = capacity - 1; + es[head = h] = e; size = s + 1; // checkInvariants(); } - private void addFirstSlowPath(E e) { - grow(1); - final Object[] elements = this.elements; - elements[head = dec(head, elements.length)] = e; - } - /** * Inserts the specified element at the end of this deque. * @@ -290,22 +287,18 @@ public class ArrayDeque extends Abstr public void addLast(E e) { // checkInvariants(); Objects.requireNonNull(e); - final Object[] elements; - final int capacity, s; - if ((s = size) == (capacity = (elements = this.elements).length)) - addLastSlowPath(e); - else - elements[add(head, s, capacity)] = e; + Object[] es; + int capacity; + final int s; + if ((s = size) == (capacity = (es = elements).length)) { + grow(1); + capacity = (es = elements).length; + } + es[add(head, s, capacity)] = e; size = s + 1; // checkInvariants(); } - private void addLastSlowPath(E e) { - grow(1); - final Object[] elements = this.elements; - elements[add(head, size, elements.length)] = e; - } - /** * Adds all of the elements in the specified collection at the end * of this deque, as if by calling {@link #addLast} on each one, @@ -317,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; } /** @@ -365,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; } /** @@ -376,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; } @@ -398,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; } @@ -413,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)]; } /** @@ -449,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, i = 0, todo = 0) { + for (; i < to; i++) + if (o.equals(es[i])) { + delete(i); + return true; + } + if (todo == 0) break; } } return false; @@ -477,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; @@ -616,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; @@ -625,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; @@ -640,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; @@ -710,18 +706,19 @@ public class ArrayDeque extends Abstr } public E next() { - if (remaining == 0) + if (remaining <= 0) throw new NoSuchElementException(); - E e = checkedElementAt(elements, cursor); + final Object[] es = elements; + E e = nonNullElementAt(es, cursor); lastRet = cursor; - cursor = inc(cursor, elements.length); + if (++cursor >= es.length) cursor = 0; remaining--; return e; } void postDelete(boolean leftShifted) { if (leftShifted) - cursor = dec(cursor, elements.length); // undo inc in next + if (--cursor < 0) cursor = elements.length - 1; } public final void remove() { @@ -733,12 +730,13 @@ 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; - remaining = 0; - for (int i = cursor; --k >= 0; i = inc(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; + } } } @@ -746,28 +744,37 @@ public class ArrayDeque extends Abstr DescendingIterator() { cursor = tail(); } public final E next() { - if (remaining == 0) + if (remaining <= 0) throw new NoSuchElementException(); - E e = checkedElementAt(elements, cursor); + final Object[] es = elements; + E e = nonNullElementAt(es, cursor); lastRet = cursor; - cursor = dec(cursor, elements.length); + if (--cursor < 0) cursor = es.length - 1; remaining--; return e; } void postDelete(boolean leftShifted) { if (!leftShifted) - cursor = inc(cursor, elements.length); // undo dec in next + if (++cursor >= elements.length) cursor = 0; } public final 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 = dec(i, capacity)) - action.accept(checkedElementAt(elements, i)); + 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; + } } } @@ -825,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; } @@ -855,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, i = 0, todo = 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, i = 0, todo = 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, i = 0, todo = 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); @@ -894,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)); @@ -903,7 +933,6 @@ public class ArrayDeque extends Abstr /** * @throws NullPointerException {@inheritDoc} */ - @Override public boolean retainAll(Collection c) { Objects.requireNonNull(c); return bulkRemove(e -> !c.contains(e)); @@ -912,31 +941,33 @@ public class ArrayDeque extends Abstr /** Implementation of bulk remove methods. */ private boolean bulkRemove(Predicate filter) { // checkInvariants(); - final Object[] elements = this.elements; - final int capacity = elements.length; + final Object[] es = elements; + final int capacity = es.length; int i = head, j = i, remaining = size, deleted = 0; try { - for (; remaining > 0; remaining--, i = inc(i, capacity)) { - @SuppressWarnings("unchecked") E e = (E) elements[i]; + for (; remaining > 0; remaining--) { + @SuppressWarnings("unchecked") E e = (E) es[i]; if (filter.test(e)) deleted++; else { if (j != i) - elements[j] = e; - j = inc(j, capacity); + es[j] = e; + if (++j >= capacity) j = 0; } + if (++i >= capacity) i = 0; } return deleted > 0; } catch (Throwable ex) { if (deleted > 0) - for (; remaining > 0; - remaining--, i = inc(i, capacity), j = inc(j, capacity)) - elements[j] = elements[i]; + for (; remaining > 0; remaining--) { + es[j] = es[i]; + if (++i >= capacity) i = 0; + if (++j >= capacity) j = 0; + } throw ex; } finally { size -= deleted; - for (; --deleted >= 0; j = inc(j, capacity)) - elements[j] = null; + circularClear(es, j, deleted); // checkInvariants(); } } @@ -951,11 +982,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, i = 0, todo = 0) { + for (; i < to; i++) + if (o.equals(es[i])) + return true; + if (todo == 0) break; + } } return false; } @@ -982,21 +1018,25 @@ 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. + */ + 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, from = 0, todo = 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). * @@ -1010,11 +1050,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 int capacity = es.length; + final int head = this.head, end = head + size; + final T[] a; + if (end >= 0) { + a = Arrays.copyOfRange(es, head, end, klazz); + } else { + // integer overflow! + a = Arrays.copyOfRange(es, 0, size, klazz); + System.arraycopy(es, head, a, 0, capacity - head); + } + if (end - capacity > 0) + System.arraycopy(es, 0, a, capacity - head, end - capacity); return a; } @@ -1056,20 +1108,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; } @@ -1110,10 +1160,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, i = 0, todo = 0) { + for (; i < to; i++) + s.writeObject(es[i]); + if (todo == 0) break; + } } /** @@ -1136,17 +1191,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);