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.47 by dl, Sun Feb 17 23:36:16 2013 UTC vs.
Revision 1.50 by jsr166, Wed Feb 20 12:32:01 2013 UTC

# Line 135 | Line 135 | public class ArrayDeque<E> extends Abstr
135      }
136  
137      /**
138     * Copies the elements from our element array into the specified array,
139     * in order (from first to last element in the deque).  It is assumed
140     * that the array is large enough to hold all elements in the deque.
141     *
142     * @return its argument
143     */
144    private <T> T[] copyElements(T[] a) {
145        if (head < tail) {
146            System.arraycopy(elements, head, a, 0, size());
147        } else if (head > tail) {
148            int headPortionLen = elements.length - head;
149            System.arraycopy(elements, head, a, 0, headPortionLen);
150            System.arraycopy(elements, 0, a, headPortionLen, tail);
151        }
152        return a;
153    }
154
155    /**
138       * Constructs an empty array deque with an initial capacity
139       * sufficient to hold 16 elements.
140       */
# Line 737 | Line 719 | public class ArrayDeque<E> extends Abstr
719       * @return an array containing all of the elements in this deque
720       */
721      public Object[] toArray() {
722 <        return copyElements(new Object[size()]);
722 >        final int head = this.head;
723 >        final int tail = this.tail;
724 >        boolean wrap = (tail < head);
725 >        int end = wrap ? tail + elements.length : tail;
726 >        Object[] a = Arrays.copyOfRange(elements, head, end);
727 >        if (wrap)
728 >            System.arraycopy(elements, 0, a, elements.length - head, tail);
729 >        return a;
730      }
731  
732      /**
# Line 778 | Line 767 | public class ArrayDeque<E> extends Abstr
767       */
768      @SuppressWarnings("unchecked")
769      public <T> T[] toArray(T[] a) {
770 <        int size = size();
771 <        if (a.length < size)
772 <            a = (T[])java.lang.reflect.Array.newInstance(
773 <                    a.getClass().getComponentType(), size);
774 <        copyElements(a);
775 <        if (a.length > size)
776 <            a[size] = null;
770 >        final int head = this.head;
771 >        final int tail = this.tail;
772 >        boolean wrap = (tail < head);
773 >        int size = (tail - head) + (wrap ? elements.length : 0);
774 >        int firstLeg = size - (wrap ? tail : 0);
775 >        int len = a.length;
776 >        if (size > len) {
777 >            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
778 >                                         a.getClass());
779 >        } else {
780 >            System.arraycopy(elements, head, a, 0, firstLeg);
781 >            if (size < len)
782 >                a[size] = null;
783 >        }
784 >        if (wrap)
785 >            System.arraycopy(elements, 0, a, firstLeg, tail);
786          return a;
787      }
788  
# Line 862 | Line 860 | public class ArrayDeque<E> extends Abstr
860          private final ArrayDeque<E> deq;
861          private int fence;  // -1 until first use
862          private int index;  // current index, modified on traverse/split
863 <        
864 <        /** Create new spliterator covering the given array and range */
863 >
864 >        /** Creates new spliterator covering the given array and range */
865          DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) {
866              this.deq = deq;
867              this.index = origin;
# Line 930 | Line 928 | public class ArrayDeque<E> extends Abstr
928  
929          @Override
930          public int characteristics() {
931 <            return Spliterator.ORDERED | Spliterator.SIZED |
931 >            return Spliterator.ORDERED | Spliterator.SIZED |
932                  Spliterator.NONNULL | Spliterator.SUBSIZED;
933          }
934      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines