--- jsr166/src/main/java/util/ArrayDeque.java 2005/09/16 23:11:13 1.20
+++ jsr166/src/main/java/util/ArrayDeque.java 2008/05/18 23:47:55 1.30
@@ -4,7 +4,6 @@
*/
package java.util;
-import java.util.*; // for javadoc (till 6280605 is fixed)
import java.io.*;
/**
@@ -45,7 +44,7 @@ import java.io.*;
* Iterator} interfaces.
*
*
This class is a member of the
- *
+ *
* Java Collections Framework.
*
* @author Josh Bloch and Doug Lea
@@ -479,6 +478,14 @@ public class ArrayDeque extends Abstr
return removeFirst();
}
+ private void checkInvariants() {
+ assert elements[tail] == null;
+ assert head == tail ? elements[head] == null :
+ (elements[head] != null &&
+ elements[(tail - 1) & (elements.length - 1)] != null);
+ assert elements[(head - 1) & (elements.length - 1)] == null;
+ }
+
/**
* Removes the element at the specified position in the elements array,
* adjusting head and tail as necessary. This can result in motion of
@@ -490,38 +497,42 @@ public class ArrayDeque extends Abstr
* @return true if elements moved backwards
*/
private boolean delete(int i) {
- int mask = elements.length - 1;
- int front = (i - head) & mask;
- int back = (tail - i) & mask;
-
- // Invariant: head <= i < tail mod circularity
- if (front >= ((tail - head) & mask))
- throw new ConcurrentModificationException();
-
- // Optimize for least element motion
- if (front < back) {
- if (head <= i) {
- System.arraycopy(elements, head, elements, head + 1, front);
- } else { // Wrap around
- elements[0] = elements[mask];
- System.arraycopy(elements, 0, elements, 1, i);
- System.arraycopy(elements, head, elements, head + 1, mask - head);
- }
- elements[head] = null;
- head = (head + 1) & mask;
+ checkInvariants();
+ final E[] elements = this.elements;
+ final int mask = elements.length - 1;
+ final int h = head;
+ final int t = tail;
+ final int front = (i - h) & mask;
+ final int back = (t - i) & mask;
+
+ // Invariant: head <= i < tail mod circularity
+ if (front >= ((t - h) & mask))
+ throw new ConcurrentModificationException();
+
+ // Optimize for least element motion
+ if (front < back) {
+ if (h <= i) {
+ System.arraycopy(elements, h, elements, h + 1, front);
+ } else { // Wrap around
+ System.arraycopy(elements, 0, elements, 1, i);
+ elements[0] = elements[mask];
+ System.arraycopy(elements, h, elements, h + 1, mask - h);
+ }
+ elements[h] = null;
+ head = (h + 1) & mask;
return false;
- } else {
- int t = tail;
- tail = (tail - 1) & mask;
- if (i < t) { // Copy the null tail as well
- System.arraycopy(elements, i + 1, elements, i, back);
- } else { // Wrap around
- elements[mask] = elements[0];
- System.arraycopy(elements, i + 1, elements, i, mask - i);
- System.arraycopy(elements, 1, elements, 0, tail);
- }
+ } else {
+ if (i < t) { // Copy the null tail as well
+ System.arraycopy(elements, i + 1, elements, i, back);
+ tail = t - 1;
+ } else { // Wrap around
+ System.arraycopy(elements, i + 1, elements, i, mask - i);
+ elements[mask] = elements[0];
+ System.arraycopy(elements, 1, elements, 0, t);
+ tail = (t - 1) & mask;
+ }
return true;
- }
+ }
}
// *** Collection Methods ***
@@ -583,12 +594,12 @@ public class ArrayDeque extends Abstr
}
public E next() {
- E result;
if (cursor == fence)
throw new NoSuchElementException();
+ E result = elements[cursor];
// This check doesn't catch all possible comodifications,
// but does catch the ones that corrupt traversal
- if (tail != fence || (result = elements[cursor]) == null)
+ if (tail != fence || result == null)
throw new ConcurrentModificationException();
lastRet = cursor;
cursor = (cursor + 1) & (elements.length - 1);
@@ -598,49 +609,47 @@ public class ArrayDeque extends Abstr
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
- if (delete(lastRet)) // if left-shifted, undo increment in next()
+ if (delete(lastRet)) { // if left-shifted, undo increment in next()
cursor = (cursor - 1) & (elements.length - 1);
+ fence = tail;
+ }
lastRet = -1;
- fence = tail;
}
}
-
private class DescendingIterator implements Iterator {
/*
* This class is nearly a mirror-image of DeqIterator, using
- * (tail-1) instead of head for initial cursor, (head-1)
- * instead of tail for fence, and elements.length instead of -1
- * for sentinel. It shares the same structure, but not many
- * actual lines of code.
+ * tail instead of head for initial cursor, and head instead of
+ * tail for fence.
*/
- private int cursor = (tail - 1) & (elements.length - 1);
- private int fence = (head - 1) & (elements.length - 1);
- private int lastRet = elements.length;
+ private int cursor = tail;
+ private int fence = head;
+ private int lastRet = -1;
public boolean hasNext() {
return cursor != fence;
}
public E next() {
- E result;
if (cursor == fence)
throw new NoSuchElementException();
- if (((head - 1) & (elements.length - 1)) != fence ||
- (result = elements[cursor]) == null)
+ cursor = (cursor - 1) & (elements.length - 1);
+ E result = elements[cursor];
+ if (head != fence || result == null)
throw new ConcurrentModificationException();
lastRet = cursor;
- cursor = (cursor - 1) & (elements.length - 1);
return result;
}
public void remove() {
- if (lastRet >= elements.length)
+ if (lastRet < 0)
throw new IllegalStateException();
- if (!delete(lastRet))
+ if (!delete(lastRet)) {
cursor = (cursor + 1) & (elements.length - 1);
- lastRet = elements.length;
- fence = (head - 1) & (elements.length - 1);
+ fence = head;
+ }
+ lastRet = -1;
}
}
@@ -715,7 +724,7 @@ public class ArrayDeque extends Abstr
* @return an array containing all of the elements in this deque
*/
public Object[] toArray() {
- return copyElements(new Object[size()]);
+ return copyElements(new Object[size()]);
}
/**
@@ -760,7 +769,7 @@ public class ArrayDeque extends Abstr
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
- copyElements(a);
+ copyElements(a);
if (a.length > size)
a[size] = null;
return a;
@@ -776,9 +785,7 @@ public class ArrayDeque extends Abstr
public ArrayDeque clone() {
try {
ArrayDeque result = (ArrayDeque) super.clone();
- // These two lines are currently faster than cloning the array:
- result.elements = (E[]) new Object[elements.length];
- System.arraycopy(elements, 0, result.elements, 0, elements.length);
+ result.elements = Arrays.copyOf(elements, elements.length);
return result;
} catch (CloneNotSupportedException e) {
@@ -826,6 +833,5 @@ public class ArrayDeque extends Abstr
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
elements[i] = (E)s.readObject();
-
}
}