544 |
|
return new DeqIterator(); |
545 |
|
} |
546 |
|
|
547 |
+ |
/** |
548 |
+ |
* Returns an iterator over the elements in this deque in reverse |
549 |
+ |
* sequential order. The elements will be returned in order from |
550 |
+ |
* last (tail) to first (head). |
551 |
+ |
* |
552 |
+ |
* @return an iterator over the elements in this deque in reverse |
553 |
+ |
* sequence |
554 |
+ |
*/ |
555 |
+ |
public Iterator<E> descendingIterator() { |
556 |
+ |
return new DescendingIterator(); |
557 |
+ |
} |
558 |
+ |
|
559 |
|
private class DeqIterator implements Iterator<E> { |
560 |
|
/** |
561 |
|
* Index of element to be returned by subsequent call to next. |
601 |
|
} |
602 |
|
} |
603 |
|
|
604 |
+ |
|
605 |
+ |
private class DescendingIterator implements Iterator<E> { |
606 |
+ |
/* |
607 |
+ |
* This class is nearly a mirror-image of DeqIterator. It |
608 |
+ |
* shares the same structure, but not many actual lines of |
609 |
+ |
* code. The only asymmetric part is that to simplify some |
610 |
+ |
* checks, indices are anded with length mask only on array |
611 |
+ |
* access rather than on each update. |
612 |
+ |
*/ |
613 |
+ |
private int cursor = tail - 1; |
614 |
+ |
private int fence = head - 1; |
615 |
+ |
private int lastRet = elements.length; |
616 |
+ |
|
617 |
+ |
public boolean hasNext() { |
618 |
+ |
return cursor != fence; |
619 |
+ |
} |
620 |
+ |
|
621 |
+ |
public E next() { |
622 |
+ |
E result; |
623 |
+ |
if (cursor == fence) |
624 |
+ |
throw new NoSuchElementException(); |
625 |
+ |
if ((head - 1) != fence || |
626 |
+ |
(result = elements[cursor & (elements.length-1)]) == null) |
627 |
+ |
throw new ConcurrentModificationException(); |
628 |
+ |
lastRet = cursor; |
629 |
+ |
cursor--; |
630 |
+ |
return result; |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
public void remove() { |
634 |
+ |
if (lastRet >= elements.length) |
635 |
+ |
throw new IllegalStateException(); |
636 |
+ |
if (delete(lastRet & (elements.length-1))) |
637 |
+ |
cursor++; |
638 |
+ |
lastRet = elements.length; |
639 |
+ |
fence = head - 1; |
640 |
+ |
} |
641 |
+ |
} |
642 |
+ |
|
643 |
|
/** |
644 |
|
* Returns <tt>true</tt> if this deque contains the specified element. |
645 |
|
* More formally, returns <tt>true</tt> if and only if this deque contains |