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.23 by dl, Sat Sep 17 12:50:34 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 +        checkInvariants();
502          final E[] elements = this.elements;
503          final int mask = elements.length - 1;
504          final int h = head;
# Line 527 | Line 536 | public class ArrayDeque<E> extends Abstr
536          }
537      }
538  
530    private boolean xdelete(int i) {
531        int mask = elements.length - 1;
532        int front = (i - head) & mask;
533        int back  = (tail - i) & mask;
534
535        // Invariant: head <= i < tail mod circularity
536        if (front >= ((tail - head) & mask))
537            throw new ConcurrentModificationException();
538
539        // Optimize for least element motion
540        if (front < back) {
541            if (head <= i) {
542                System.arraycopy(elements, head, elements, head + 1, front);
543            } else { // Wrap around
544                System.arraycopy(elements, 0, elements, 1, i);
545                elements[0] = elements[mask];
546                System.arraycopy(elements, head, elements, head + 1, mask - head);
547            }
548            elements[head] = null;
549            head = (head + 1) & mask;
550            return false;
551        } else {
552            int t = tail;
553            tail = (tail - 1) & mask;
554            if (i < t) { // Copy the null tail as well
555                System.arraycopy(elements, i + 1, elements, i, back);
556            } else {     // Wrap around
557                System.arraycopy(elements, i + 1, elements, i, mask - i);
558                elements[mask] = elements[0];
559                System.arraycopy(elements, 1, elements, 0, t);
560            }
561            return true;
562        }
563    }
564
539      // *** Collection Methods ***
540  
541      /**
# Line 643 | Line 617 | public class ArrayDeque<E> extends Abstr
617          }
618      }
619  
646
620      private class DescendingIterator implements Iterator<E> {
621          /*
622           * This class is nearly a mirror-image of DeqIterator, using
# Line 864 | 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();
867
840      }
841   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines