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.99 by jsr166, Sun Oct 30 16:32:40 2016 UTC vs.
Revision 1.102 by jsr166, Sun Oct 30 20:10:34 2016 UTC

# Line 967 | Line 967 | public class ArrayDeque<E> extends Abstr
967              throw ex;
968          } finally {
969              size -= deleted;
970 <            clearSlice(es, j, deleted);
970 >            circularClear(es, j, deleted);
971              // checkInvariants();
972          }
973      }
# Line 1018 | Line 1018 | public class ArrayDeque<E> extends Abstr
1018       * The deque will be empty after this call returns.
1019       */
1020      public void clear() {
1021 <        clearSlice(elements, head, size);
1021 >        circularClear(elements, head, size);
1022          size = head = 0;
1023          // checkInvariants();
1024      }
1025  
1026      /**
1027 <     * Nulls out count elements, starting at array index i.
1027 >     * Nulls out count elements, starting at array index from.
1028       */
1029 <    private static void clearSlice(Object[] es, int i, int count) {
1029 >    private static void circularClear(Object[] es, int from, int count) {
1030          int end, to, todo;
1031 <        todo = (end = i + count)
1031 >        todo = (end = from + count)
1032              - (to = (es.length - end >= 0) ? end : es.length);
1033 <        for (;; to = todo, i = 0, todo = 0) {
1034 <            Arrays.fill(es, i, to, null);
1033 >        for (;; to = todo, from = 0, todo = 0) {
1034 >            Arrays.fill(es, from, to, null);
1035              if (todo == 0) break;
1036          }
1037      }
# Line 1055 | Line 1055 | public class ArrayDeque<E> extends Abstr
1055  
1056      private <T> T[] toArray(Class<T[]> klazz) {
1057          final Object[] es = elements;
1058        final int capacity = es.length;
1059        final int head = this.head, end = head + size;
1058          final T[] a;
1059 <        if (end >= 0) {
1059 >        final int head, len, end, todo;
1060 >        todo = size - (len = Math.min(size, es.length - (head = this.head)));
1061 >        if ((end = head + size) >= 0) {
1062              a = Arrays.copyOfRange(es, head, end, klazz);
1063          } else {
1064              // integer overflow!
1065              a = Arrays.copyOfRange(es, 0, size, klazz);
1066 <            System.arraycopy(es, head, a, 0, capacity - head);
1066 >            System.arraycopy(es, head, a, 0, len);
1067          }
1068 <        if (end - capacity > 0)
1069 <            System.arraycopy(es, 0, a, capacity - head, end - capacity);
1068 >        if (todo > 0)
1069 >            System.arraycopy(es, 0, a, len, todo);
1070          return a;
1071      }
1072  
# Line 1112 | Line 1112 | public class ArrayDeque<E> extends Abstr
1112          if ((size = this.size) > a.length)
1113              return toArray((Class<T[]>) a.getClass());
1114          final Object[] es = elements;
1115 <        final int head = this.head, end = head + size;
1116 <        final int front = (es.length - end >= 0) ? size : es.length - head;
1117 <        System.arraycopy(es, head, a, 0, front);
1118 <        if (front < size)
1119 <            System.arraycopy(es, 0, a, front, size - front);
1115 >        int i, j, len, todo;
1116 >        todo = size - (len = Math.min(size, es.length - (i = head)));
1117 >        for (j = 0;; j += len, len = todo, todo = 0, i = 0) {
1118 >            System.arraycopy(es, i, a, j, len);
1119 >            if (todo == 0) break;
1120 >        }
1121          if (size < a.length)
1122              a[size] = null;
1123          return a;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines