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.17 by dl, Thu Sep 15 16:55:24 2005 UTC vs.
Revision 1.25 by jsr166, Sat Sep 17 17:22:17 2005 UTC

# Line 479 | Line 479 | public class ArrayDeque<E> extends Abstr
479          return removeFirst();
480      }
481  
482 +    private void checkInvariants() {
483 +        assert elements[tail] == null;
484 +        assert head == tail ? elements[head] == null :
485 +            (elements[head] != null &&
486 +             elements[(tail - 1) & (elements.length - 1)] != null);
487 +        assert elements[(head - 1) & (elements.length - 1)] == null;
488 +    }
489 +
490      /**
491       * Removes the element at the specified position in the elements array,
492       * adjusting head and tail as necessary.  This can result in motion of
# Line 490 | Line 498 | public class ArrayDeque<E> extends Abstr
498       * @return true if elements moved backwards
499       */
500      private boolean delete(int i) {
501 <        int mask = elements.length - 1;
501 >        checkInvariants();
502 >        final E[] elements = this.elements;
503 >        final int mask = elements.length - 1;
504 >        final int h = head;
505 >        final int t = tail;
506 >        final int front = (i - h) & mask;
507 >        final int back  = (t - i) & mask;
508  
509          // Invariant: head <= i < tail mod circularity
510 <        if (((i - head) & mask) >= ((tail - head) & mask))
510 >        if (front >= ((t - h) & mask))
511              throw new ConcurrentModificationException();
512  
513 <        // Case 1: Deque doesn't wrap
514 <        // Case 2: Deque does wrap and removed element is in the head portion
515 <        if (i >= head) {
516 <            System.arraycopy(elements, head, elements, head + 1, i - head);
517 <            elements[head] = null;
518 <            head = (head + 1) & mask;
519 <            return false;
520 <        }
521 <
522 <        // Case 3: Deque wraps and removed element is in the tail portion
523 <        tail--;
524 <        System.arraycopy(elements, i + 1, elements, i, tail - i);
525 <        elements[tail] = null;
526 <        return true;
513 >        // Optimize for least element motion
514 >        if (front < back) {
515 >            if (h <= i) {
516 >                System.arraycopy(elements, h, elements, h + 1, front);
517 >            } else { // Wrap around
518 >                System.arraycopy(elements, 0, elements, 1, i);
519 >                elements[0] = elements[mask];
520 >                System.arraycopy(elements, h, elements, h + 1, mask - h);
521 >            }
522 >            elements[h] = null;
523 >            head = (h + 1) & mask;
524 >            return false;
525 >        } else {
526 >            if (i < t) { // Copy the null tail as well
527 >                System.arraycopy(elements, i + 1, elements, i, back);
528 >                tail = t - 1;
529 >            } else { // Wrap around
530 >                System.arraycopy(elements, i + 1, elements, i, mask - i);
531 >                elements[mask] = elements[0];
532 >                System.arraycopy(elements, 1, elements, 0, t);
533 >                tail = (t - 1) & mask;
534 >            }
535 >            return true;
536 >        }
537      }
538  
539      // *** Collection Methods ***
# Line 538 | Line 562 | public class ArrayDeque<E> extends Abstr
562       * order that elements would be dequeued (via successive calls to
563       * {@link #remove} or popped (via successive calls to {@link #pop}).
564       *
565 <     * @return an <tt>Iterator</tt> over the elements in this deque
565 >     * @return an iterator over the elements in this deque
566       */
567      public Iterator<E> iterator() {
568          return new DeqIterator();
569      }
570  
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     */
571      public Iterator<E> descendingIterator() {
572          return new DescendingIterator();
573      }
# Line 601 | Line 617 | public class ArrayDeque<E> extends Abstr
617          }
618      }
619  
604
620      private class DescendingIterator implements Iterator<E> {
621 <        /*
621 >        /*
622           * This class is nearly a mirror-image of DeqIterator, using
623           * (tail-1) instead of head for initial cursor, (head-1)
624           * instead of tail for fence, and elements.length instead of -1
# Line 622 | Line 637 | public class ArrayDeque<E> extends Abstr
637              E result;
638              if (cursor == fence)
639                  throw new NoSuchElementException();
640 <            if (((head - 1) & (elements.length - 1)) != fence ||
640 >            if (((head - 1) & (elements.length - 1)) != fence ||
641                  (result = elements[cursor]) == null)
642                  throw new ConcurrentModificationException();
643              lastRet = cursor;
# Line 633 | Line 648 | public class ArrayDeque<E> extends Abstr
648          public void remove() {
649              if (lastRet >= elements.length)
650                  throw new IllegalStateException();
651 <            if (!delete(lastRet))
651 >            if (!delete(lastRet))
652                  cursor = (cursor + 1) & (elements.length - 1);
653              lastRet = elements.length;
654              fence = (head - 1) & (elements.length - 1);
# Line 798 | Line 813 | public class ArrayDeque<E> extends Abstr
813          s.defaultWriteObject();
814  
815          // Write out size
816 <        int size = size();
802 <        s.writeInt(size);
816 >        s.writeInt(size());
817  
818          // Write out elements in order.
805        int i = head;
819          int mask = elements.length - 1;
820 <        for (int j = 0; j < size; j++) {
820 >        for (int i = head; i != tail; i = (i + 1) & mask)
821              s.writeObject(elements[i]);
809            i = (i + 1) & mask;
810        }
822      }
823  
824      /**
# Line 826 | Line 837 | public class ArrayDeque<E> extends Abstr
837          // Read in all elements in the proper order.
838          for (int i = 0; i < size; i++)
839              elements[i] = (E)s.readObject();
829
840      }
841   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines