--- jsr166/src/main/java/util/ArrayDeque.java 2005/07/18 19:14:17 1.15 +++ jsr166/src/main/java/util/ArrayDeque.java 2005/09/14 23:49:59 1.16 @@ -544,6 +544,18 @@ public class ArrayDeque extends Abstr return new DeqIterator(); } + /** + * Returns an iterator over the elements in this deque in reverse + * sequential order. The elements will be returned in order from + * last (tail) to first (head). + * + * @return an iterator over the elements in this deque in reverse + * sequence + */ + public Iterator descendingIterator() { + return new DescendingIterator(); + } + private class DeqIterator implements Iterator { /** * Index of element to be returned by subsequent call to next. @@ -589,6 +601,45 @@ public class ArrayDeque extends Abstr } } + + private class DescendingIterator implements Iterator { + /* + * This class is nearly a mirror-image of DeqIterator. It + * shares the same structure, but not many actual lines of + * code. The only asymmetric part is that to simplify some + * checks, indices are anded with length mask only on array + * access rather than on each update. + */ + private int cursor = tail - 1; + private int fence = head - 1; + private int lastRet = elements.length; + + public boolean hasNext() { + return cursor != fence; + } + + public E next() { + E result; + if (cursor == fence) + throw new NoSuchElementException(); + if ((head - 1) != fence || + (result = elements[cursor & (elements.length-1)]) == null) + throw new ConcurrentModificationException(); + lastRet = cursor; + cursor--; + return result; + } + + public void remove() { + if (lastRet >= elements.length) + throw new IllegalStateException(); + if (delete(lastRet & (elements.length-1))) + cursor++; + lastRet = elements.length; + fence = head - 1; + } + } + /** * Returns true if this deque contains the specified element. * More formally, returns true if and only if this deque contains