ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayDeque.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayDeque.java (file contents):
Revision 1.15 by jsr166, Mon Jul 18 19:14:17 2005 UTC vs.
Revision 1.19 by dl, Fri Sep 16 11:15:41 2005 UTC

# Line 538 | Line 538 | public class ArrayDeque<E> extends Abstr
538       * order that elements would be dequeued (via successive calls to
539       * {@link #remove} or popped (via successive calls to {@link #pop}).
540       *
541 <     * @return an <tt>Iterator</tt> over the elements in this deque
541 >     * @return an iterator over the elements in this deque
542       */
543      public Iterator<E> iterator() {
544          return new DeqIterator();
545      }
546  
547 +    public Iterator<E> descendingIterator() {
548 +        return new DescendingIterator();
549 +    }
550 +
551      private class DeqIterator implements Iterator<E> {
552          /**
553           * Index of element to be returned by subsequent call to next.
# Line 582 | Line 586 | public class ArrayDeque<E> extends Abstr
586          public void remove() {
587              if (lastRet < 0)
588                  throw new IllegalStateException();
589 <            if (delete(lastRet))
590 <                cursor--;
589 >            if (delete(lastRet)) // if left-shifted, undo increment in next()
590 >                cursor = (cursor - 1) & (elements.length - 1);
591              lastRet = -1;
592              fence = tail;
593          }
594      }
595  
596 +
597 +    private class DescendingIterator implements Iterator<E> {
598 +        /*
599 +         * This class is nearly a mirror-image of DeqIterator, using
600 +         * (tail-1) instead of head for initial cursor, (head-1)
601 +         * instead of tail for fence, and elements.length instead of -1
602 +         * for sentinel. It shares the same structure, but not many
603 +         * actual lines of code.
604 +         */
605 +        private int cursor = (tail - 1) & (elements.length - 1);
606 +        private int fence =  (head - 1) & (elements.length - 1);
607 +        private int lastRet = elements.length;
608 +
609 +        public boolean hasNext() {
610 +            return cursor != fence;
611 +        }
612 +
613 +        public E next() {
614 +            E result;
615 +            if (cursor == fence)
616 +                throw new NoSuchElementException();
617 +            if (((head - 1) & (elements.length - 1)) != fence ||
618 +                (result = elements[cursor]) == null)
619 +                throw new ConcurrentModificationException();
620 +            lastRet = cursor;
621 +            cursor = (cursor - 1) & (elements.length - 1);
622 +            return result;
623 +        }
624 +
625 +        public void remove() {
626 +            if (lastRet >= elements.length)
627 +                throw new IllegalStateException();
628 +            if (!delete(lastRet))
629 +                cursor = (cursor + 1) & (elements.length - 1);
630 +            lastRet = elements.length;
631 +            fence = (head - 1) & (elements.length - 1);
632 +        }
633 +    }
634 +
635      /**
636       * Returns <tt>true</tt> if this deque contains the specified element.
637       * More formally, returns <tt>true</tt> if and only if this deque contains
# Line 747 | Line 790 | public class ArrayDeque<E> extends Abstr
790          s.defaultWriteObject();
791  
792          // Write out size
793 <        int size = size();
751 <        s.writeInt(size);
793 >        s.writeInt(size());
794  
795          // Write out elements in order.
754        int i = head;
796          int mask = elements.length - 1;
797 <        for (int j = 0; j < size; j++) {
797 >        for (int i = head; i != tail; i = (i + 1) & mask)
798              s.writeObject(elements[i]);
758            i = (i + 1) & mask;
759        }
799      }
800  
801      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines