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

Comparing jsr166/src/jdk7/java/util/ArrayDeque.java (file contents):
Revision 1.1 by dl, Sun Dec 16 20:55:09 2012 UTC vs.
Revision 1.4 by jsr166, Wed Nov 16 19:56:44 2016 UTC

# Line 133 | Line 133 | public class ArrayDeque<E> extends Abstr
133      }
134  
135      /**
136     * Copies the elements from our element array into the specified array,
137     * in order (from first to last element in the deque).  It is assumed
138     * that the array is large enough to hold all elements in the deque.
139     *
140     * @return its argument
141     */
142    private <T> T[] copyElements(T[] a) {
143        if (head < tail) {
144            System.arraycopy(elements, head, a, 0, size());
145        } else if (head > tail) {
146            int headPortionLen = elements.length - head;
147            System.arraycopy(elements, head, a, 0, headPortionLen);
148            System.arraycopy(elements, 0, a, headPortionLen, tail);
149        }
150        return a;
151    }
152
153    /**
136       * Constructs an empty array deque with an initial capacity
137       * sufficient to hold 16 elements.
138       */
# Line 506 | Line 488 | public class ArrayDeque<E> extends Abstr
488       * @return true if elements moved backwards
489       */
490      private boolean delete(int i) {
491 <        checkInvariants();
491 >        // checkInvariants();
492          final Object[] elements = this.elements;
493          final int mask = elements.length - 1;
494          final int h = head;
# Line 627 | Line 609 | public class ArrayDeque<E> extends Abstr
609          }
610      }
611  
612 +    /**
613 +     * This class is nearly a mirror-image of DeqIterator, using tail
614 +     * instead of head for initial cursor, and head instead of tail
615 +     * for fence.
616 +     */
617      private class DescendingIterator implements Iterator<E> {
631        /*
632         * This class is nearly a mirror-image of DeqIterator, using
633         * tail instead of head for initial cursor, and head instead of
634         * tail for fence.
635         */
618          private int cursor = tail;
619          private int fence = head;
620          private int lastRet = -1;
# Line 735 | Line 717 | public class ArrayDeque<E> extends Abstr
717       * @return an array containing all of the elements in this deque
718       */
719      public Object[] toArray() {
720 <        return copyElements(new Object[size()]);
720 >        final int head = this.head;
721 >        final int tail = this.tail;
722 >        boolean wrap = (tail < head);
723 >        int end = wrap ? tail + elements.length : tail;
724 >        Object[] a = Arrays.copyOfRange(elements, head, end);
725 >        if (wrap)
726 >            System.arraycopy(elements, 0, a, elements.length - head, tail);
727 >        return a;
728      }
729  
730      /**
# Line 776 | Line 765 | public class ArrayDeque<E> extends Abstr
765       */
766      @SuppressWarnings("unchecked")
767      public <T> T[] toArray(T[] a) {
768 <        int size = size();
769 <        if (a.length < size)
770 <            a = (T[])java.lang.reflect.Array.newInstance(
771 <                    a.getClass().getComponentType(), size);
772 <        copyElements(a);
773 <        if (a.length > size)
774 <            a[size] = null;
768 >        final int head = this.head;
769 >        final int tail = this.tail;
770 >        boolean wrap = (tail < head);
771 >        int size = (tail - head) + (wrap ? elements.length : 0);
772 >        int firstLeg = size - (wrap ? tail : 0);
773 >        int len = a.length;
774 >        if (size > len) {
775 >            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
776 >                                         a.getClass());
777 >        } else {
778 >            System.arraycopy(elements, head, a, 0, firstLeg);
779 >            if (size < len)
780 >                a[size] = null;
781 >        }
782 >        if (wrap)
783 >            System.arraycopy(elements, 0, a, firstLeg, tail);
784          return a;
785      }
786  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines