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.85 by jsr166, Sun Oct 23 19:30:54 2016 UTC vs.
Revision 1.86 by jsr166, Sun Oct 23 22:30:20 2016 UTC

# Line 983 | Line 983 | public class ArrayDeque<E> extends Abstr
983       */
984      public void clear() {
985          final Object[] elements = this.elements;
986 <        final int capacity = elements.length;
987 <        final int h = this.head;
988 <        final int s = size;
989 <        if (capacity - h >= s)
990 <            Arrays.fill(elements, h, h + s, null);
986 >        final int capacity = elements.length, tail = head + size;
987 >        if (capacity - tail >= 0)
988 >            Arrays.fill(elements, head, tail, null);
989          else {
990 <            Arrays.fill(elements, h, capacity, null);
991 <            Arrays.fill(elements, 0, s - capacity + h, null);
990 >            Arrays.fill(elements, head, capacity, null);
991 >            Arrays.fill(elements, 0, tail - capacity, null);
992          }
993          size = head = 0;
994          // checkInvariants();
# Line 1010 | Line 1008 | public class ArrayDeque<E> extends Abstr
1008       * @return an array containing all of the elements in this deque
1009       */
1010      public Object[] toArray() {
1011 <        final int head = this.head;
1012 <        final int firstLeg;
1013 <        Object[] a = Arrays.copyOfRange(elements, head, head + size);
1014 <        if ((firstLeg = elements.length - head) < size)
1015 <            System.arraycopy(elements, 0, a, firstLeg, size - firstLeg);
1011 >        return toArray(Object[].class);
1012 >    }
1013 >
1014 >    private <T> T[] toArray(Class<T[]> klazz) {
1015 >        final Object[] elements = this.elements;
1016 >        final int capacity = elements.length;
1017 >        final int head = this.head, tail = head + size;
1018 >        final T[] a;
1019 >        if (tail >= 0) {
1020 >            a = Arrays.copyOfRange(elements, head, tail, klazz);
1021 >        } else {
1022 >            // integer overflow!
1023 >            a = Arrays.copyOfRange(elements, 0, size, klazz);
1024 >            System.arraycopy(elements, head, a, 0, capacity - head);
1025 >        }
1026 >        if (tail - capacity > 0)
1027 >            System.arraycopy(elements, 0, a, capacity - head, tail - capacity);
1028          return a;
1029      }
1030  
# Line 1056 | Line 1066 | public class ArrayDeque<E> extends Abstr
1066       */
1067      @SuppressWarnings("unchecked")
1068      public <T> T[] toArray(T[] a) {
1069 +        final int size = this.size;
1070 +        if (size > a.length)
1071 +            return toArray((Class<T[]>) a.getClass());
1072          final Object[] elements = this.elements;
1073 <        final int head = this.head;
1074 <        final int firstLeg;
1075 <        boolean wrap = (firstLeg = elements.length - head) < size;
1076 <        if (size > a.length) {
1077 <            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
1078 <                                         a.getClass());
1079 <        } else {
1067 <            System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size);
1068 <            if (size < a.length)
1069 <                a[size] = null;
1073 >        final int capacity = elements.length;
1074 >        final int head = this.head, tail = head + size;
1075 >        if (capacity - tail >= 0)
1076 >            System.arraycopy(elements, head, a, 0, size);
1077 >        else {
1078 >            System.arraycopy(elements, head, a, 0, capacity - head);
1079 >            System.arraycopy(elements, 0, a, capacity - head, tail - capacity);
1080          }
1081 <        if (wrap)
1082 <            System.arraycopy(elements, 0, a, firstLeg, size - firstLeg);
1081 >        if (size < a.length)
1082 >            a[size] = null;
1083          return a;
1084      }
1085  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines