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

Comparing jsr166/src/jdk8/java/util/ArrayDeque.java (file contents):
Revision 1.2 by jsr166, Mon Oct 24 23:54:10 2016 UTC vs.
Revision 1.3 by jsr166, Sun Oct 30 19:26:34 2016 UTC

# Line 93 | Line 93 | public class ArrayDeque<E> extends Abstr
93      private void grow(int needed) {
94          // overflow-conscious code
95          // checkInvariants();
96 <        int oldCapacity = elements.length;
96 >        final int oldCapacity = elements.length;
97          int newCapacity;
98          // Double size if small; else grow by 50%
99          int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1);
# Line 115 | Line 115 | public class ArrayDeque<E> extends Abstr
115  
116      /** Capacity calculation for edge conditions, especially overflow. */
117      private int newCapacity(int needed, int jump) {
118 <        int oldCapacity = elements.length;
119 <        int minCapacity;
118 >        final int oldCapacity = elements.length, minCapacity;
119          if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) {
120              if (minCapacity < 0)
121                  throw new IllegalStateException("Sorry, deque too big");
# Line 184 | Line 183 | public class ArrayDeque<E> extends Abstr
183       * @throws NullPointerException if the specified collection is null
184       */
185      public ArrayDeque(Collection<? extends E> c) {
186 <        Object[] elements = c.toArray();
186 >        Object[] es = c.toArray();
187          // defend against c.toArray (incorrectly) not returning Object[]
188          // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
189 <        size = elements.length;
190 <        if (elements.getClass() != Object[].class)
191 <            elements = Arrays.copyOf(elements, size, Object[].class);
193 <        for (Object obj : elements)
189 >        if (es.getClass() != Object[].class)
190 >            es = Arrays.copyOf(es, es.length, Object[].class);
191 >        for (Object obj : es)
192              Objects.requireNonNull(obj);
193 <        this.elements = elements;
193 >        this.elements = es;
194 >        this.size = es.length;
195      }
196  
197      /**
# Line 241 | Line 240 | public class ArrayDeque<E> extends Abstr
240      /**
241       * A version of elementAt that checks for null elements.
242       * This check doesn't catch all possible comodifications,
243 <     * but does catch ones that corrupt traversal.
243 >     * but does catch ones that corrupt traversal.  It's a little
244 >     * surprising that javac allows this abuse of generics.
245       */
246 <    E checkedElementAt(Object[] elements, int i) {
247 <        @SuppressWarnings("unchecked") E e = (E) elements[i];
246 >    static final <E> E nonNullElementAt(Object[] es, int i) {
247 >        @SuppressWarnings("unchecked") E e = (E) es[i];
248          if (e == null)
249              throw new ConcurrentModificationException();
250          return e;
# Line 263 | Line 263 | public class ArrayDeque<E> extends Abstr
263      public void addFirst(E e) {
264          // checkInvariants();
265          Objects.requireNonNull(e);
266 <        Object[] elements;
266 >        Object[] es;
267          int capacity, h;
268          final int s;
269 <        if ((s = size) == (capacity = (elements = this.elements).length)) {
269 >        if ((s = size) == (capacity = (es = elements).length)) {
270              grow(1);
271 <            capacity = (elements = this.elements).length;
271 >            capacity = (es = elements).length;
272          }
273          if ((h = head - 1) < 0) h = capacity - 1;
274 <        elements[head = h] = e;
274 >        es[head = h] = e;
275          size = s + 1;
276          // checkInvariants();
277      }
# Line 287 | Line 287 | public class ArrayDeque<E> extends Abstr
287      public void addLast(E e) {
288          // checkInvariants();
289          Objects.requireNonNull(e);
290 <        Object[] elements;
290 >        Object[] es;
291          int capacity;
292          final int s;
293 <        if ((s = size) == (capacity = (elements = this.elements).length)) {
293 >        if ((s = size) == (capacity = (es = elements).length)) {
294              grow(1);
295 <            capacity = (elements = this.elements).length;
295 >            capacity = (es = elements).length;
296          }
297 <        elements[add(head, s, capacity)] = e;
297 >        es[add(head, s, capacity)] = e;
298          size = s + 1;
299          // checkInvariants();
300      }
# Line 368 | Line 368 | public class ArrayDeque<E> extends Abstr
368      public E pollFirst() {
369          // checkInvariants();
370          int s, h;
371 <        if ((s = size) == 0)
371 >        if ((s = size) <= 0)
372              return null;
373 <        final Object[] elements = this.elements;
374 <        @SuppressWarnings("unchecked") E e = (E) elements[h = head];
375 <        elements[h] = null;
376 <        if (++h >= elements.length) h = 0;
373 >        final Object[] es = elements;
374 >        @SuppressWarnings("unchecked") E e = (E) es[h = head];
375 >        es[h] = null;
376 >        if (++h >= es.length) h = 0;
377          head = h;
378          size = s - 1;
379          return e;
# Line 382 | Line 382 | public class ArrayDeque<E> extends Abstr
382      public E pollLast() {
383          // checkInvariants();
384          final int s, tail;
385 <        if ((s = size) == 0)
385 >        if ((s = size) <= 0)
386              return null;
387 <        final Object[] elements = this.elements;
387 >        final Object[] es = elements;
388          @SuppressWarnings("unchecked")
389 <        E e = (E) elements[tail = add(head, s - 1, elements.length)];
390 <        elements[tail] = null;
389 >        E e = (E) es[tail = add(head, s - 1, es.length)];
390 >        es[tail] = null;
391          size = s - 1;
392          return e;
393      }
# Line 397 | Line 397 | public class ArrayDeque<E> extends Abstr
397       */
398      public E getFirst() {
399          // checkInvariants();
400 <        if (size == 0) throw new NoSuchElementException();
400 >        if (size <= 0) throw new NoSuchElementException();
401          return elementAt(head);
402      }
403  
404      /**
405       * @throws NoSuchElementException {@inheritDoc}
406       */
407 +    @SuppressWarnings("unchecked")
408      public E getLast() {
409          // checkInvariants();
410 <        if (size == 0) throw new NoSuchElementException();
411 <        return elementAt(tail());
410 >        final int s;
411 >        if ((s = size) <= 0) throw new NoSuchElementException();
412 >        final Object[] es = elements;
413 >        return (E) es[add(head, s - 1, es.length)];
414      }
415  
416      public E peekFirst() {
417          // checkInvariants();
418 <        return (size == 0) ? null : elementAt(head);
418 >        return (size <= 0) ? null : elementAt(head);
419      }
420  
421 +    @SuppressWarnings("unchecked")
422      public E peekLast() {
423          // checkInvariants();
424 <        return (size == 0) ? null : elementAt(tail());
424 >        final int s;
425 >        if ((s = size) <= 0) return null;
426 >        final Object[] es = elements;
427 >        return (E) es[add(head, s - 1, es.length)];
428      }
429  
430      /**
# Line 434 | Line 441 | public class ArrayDeque<E> extends Abstr
441       */
442      public boolean removeFirstOccurrence(Object o) {
443          if (o != null) {
444 <            final Object[] elements = this.elements;
445 <            final int capacity = elements.length;
446 <            int from, end, to, leftover;
447 <            leftover = (end = (from = head) + size)
448 <                - (to = (capacity - end >= 0) ? end : capacity);
449 <            for (;; from = 0, to = leftover, leftover = 0) {
450 <                for (int i = from; i < to; i++)
444 <                    if (o.equals(elements[i])) {
444 >            final Object[] es = elements;
445 >            int i, end, to, todo;
446 >            todo = (end = (i = head) + size)
447 >                - (to = (es.length - end >= 0) ? end : es.length);
448 >            for (;; to = todo, i = 0, todo = 0) {
449 >                for (; i < to; i++)
450 >                    if (o.equals(es[i])) {
451                          delete(i);
452                          return true;
453                      }
454 <                if (leftover == 0) break;
454 >                if (todo == 0) break;
455              }
456          }
457          return false;
# Line 465 | Line 471 | public class ArrayDeque<E> extends Abstr
471       */
472      public boolean removeLastOccurrence(Object o) {
473          if (o != null) {
474 <            final Object[] elements = this.elements;
475 <            final int capacity = elements.length;
476 <            int from, to, end, leftover;
477 <            leftover = (to = ((end = (from = tail()) - size) >= -1) ? end : -1) - end;
478 <            for (;; from = capacity - 1, to = capacity - 1 - leftover, leftover = 0) {
479 <                for (int i = from; i > to; i--)
474 <                    if (o.equals(elements[i])) {
474 >            final Object[] es = elements;
475 >            int i, to, end, todo;
476 >            todo = (to = ((end = (i = tail()) - size) >= -1) ? end : -1) - end;
477 >            for (;; to = (i = es.length - 1) - todo, todo = 0) {
478 >                for (; i > to; i--)
479 >                    if (o.equals(es[i])) {
480                          delete(i);
481                          return true;
482                      }
483 <                if (leftover == 0) break;
483 >                if (todo == 0) break;
484              }
485          }
486          return false;
# Line 607 | Line 612 | public class ArrayDeque<E> extends Abstr
612       */
613      boolean delete(int i) {
614          // checkInvariants();
615 <        final Object[] elements = this.elements;
616 <        final int capacity = elements.length;
615 >        final Object[] es = elements;
616 >        final int capacity = es.length;
617          final int h = head;
618          int front;              // number of elements before to-be-deleted elt
619          if ((front = i - h) < 0) front += capacity;
# Line 616 | Line 621 | public class ArrayDeque<E> extends Abstr
621          if (front < back) {
622              // move front elements forwards
623              if (h <= i) {
624 <                System.arraycopy(elements, h, elements, h + 1, front);
624 >                System.arraycopy(es, h, es, h + 1, front);
625              } else { // Wrap around
626 <                System.arraycopy(elements, 0, elements, 1, i);
627 <                elements[0] = elements[capacity - 1];
628 <                System.arraycopy(elements, h, elements, h + 1, front - (i + 1));
626 >                System.arraycopy(es, 0, es, 1, i);
627 >                es[0] = es[capacity - 1];
628 >                System.arraycopy(es, h, es, h + 1, front - (i + 1));
629              }
630 <            elements[h] = null;
630 >            es[h] = null;
631              if ((head = (h + 1)) >= capacity) head = 0;
632              size--;
633              // checkInvariants();
# Line 631 | Line 636 | public class ArrayDeque<E> extends Abstr
636              // move back elements backwards
637              int tail = tail();
638              if (i <= tail) {
639 <                System.arraycopy(elements, i + 1, elements, i, back);
639 >                System.arraycopy(es, i + 1, es, i, back);
640              } else { // Wrap around
641                  int firstLeg = capacity - (i + 1);
642 <                System.arraycopy(elements, i + 1, elements, i, firstLeg);
643 <                elements[capacity - 1] = elements[0];
644 <                System.arraycopy(elements, 1, elements, 0, back - firstLeg - 1);
642 >                System.arraycopy(es, i + 1, es, i, firstLeg);
643 >                es[capacity - 1] = es[0];
644 >                System.arraycopy(es, 1, es, 0, back - firstLeg - 1);
645              }
646 <            elements[tail] = null;
646 >            es[tail] = null;
647              size--;
648              // checkInvariants();
649              return true;
# Line 701 | Line 706 | public class ArrayDeque<E> extends Abstr
706          }
707  
708          public E next() {
709 <            if (remaining == 0)
709 >            if (remaining <= 0)
710                  throw new NoSuchElementException();
711 <            final Object[] elements = ArrayDeque.this.elements;
712 <            E e = checkedElementAt(elements, cursor);
711 >            final Object[] es = elements;
712 >            E e = nonNullElementAt(es, cursor);
713              lastRet = cursor;
714 <            if (++cursor >= elements.length) cursor = 0;
714 >            if (++cursor >= es.length) cursor = 0;
715              remaining--;
716              return e;
717          }
# Line 724 | Line 729 | public class ArrayDeque<E> extends Abstr
729          }
730  
731          public void forEachRemaining(Consumer<? super E> action) {
732 <            int k;
732 >            Objects.requireNonNull(action);
733 >            final int k;
734              if ((k = remaining) > 0) {
735                  remaining = 0;
736                  ArrayDeque.forEachRemaining(action, elements, cursor, k);
# Line 738 | Line 744 | public class ArrayDeque<E> extends Abstr
744          DescendingIterator() { cursor = tail(); }
745  
746          public final E next() {
747 <            if (remaining == 0)
747 >            if (remaining <= 0)
748                  throw new NoSuchElementException();
749 <            final Object[] elements = ArrayDeque.this.elements;
750 <            E e = checkedElementAt(elements, cursor);
749 >            final Object[] es = elements;
750 >            E e = nonNullElementAt(es, cursor);
751              lastRet = cursor;
752 <            if (--cursor < 0) cursor = elements.length - 1;
752 >            if (--cursor < 0) cursor = es.length - 1;
753              remaining--;
754              return e;
755          }
# Line 754 | Line 760 | public class ArrayDeque<E> extends Abstr
760          }
761  
762          public final void forEachRemaining(Consumer<? super E> action) {
763 <            int k;
763 >            Objects.requireNonNull(action);
764 >            final int k;
765              if ((k = remaining) > 0) {
766                  remaining = 0;
767 <                forEachRemainingDescending(action, elements, cursor, k);
767 >                final Object[] es = elements;
768 >                int i, end, to, todo;
769 >                todo = (to = ((end = (i = cursor) - k) >= -1) ? end : -1) - end;
770 >                for (;; to = (i = es.length - 1) - todo, todo = 0) {
771 >                    for (; i > to; i--)
772 >                        action.accept(nonNullElementAt(es, i));
773 >                    if (todo == 0) break;
774 >                }
775                  if ((lastRet = cursor - (k - 1)) < 0)
776 <                    lastRet += elements.length;
776 >                    lastRet += es.length;
777              }
778          }
779      }
# Line 817 | Line 831 | public class ArrayDeque<E> extends Abstr
831          }
832  
833          public void forEachRemaining(Consumer<? super E> action) {
834 <            int k = remaining(); // side effect!
834 >            Objects.requireNonNull(action);
835 >            final int k = remaining(); // side effect!
836              remaining = 0;
837              ArrayDeque.forEachRemaining(action, elements, cursor, k);
838          }
839  
840          public boolean tryAdvance(Consumer<? super E> action) {
841              Objects.requireNonNull(action);
842 <            if (remaining() == 0)
842 >            final int k;
843 >            if ((k = remaining()) <= 0)
844                  return false;
845 <            action.accept(checkedElementAt(elements, cursor));
845 >            action.accept(nonNullElementAt(elements, cursor));
846              if (++cursor >= elements.length) cursor = 0;
847 <            remaining--;
847 >            remaining = k - 1;
848              return true;
849          }
850  
# Line 847 | Line 863 | public class ArrayDeque<E> extends Abstr
863      @SuppressWarnings("unchecked")
864      public void forEach(Consumer<? super E> action) {
865          Objects.requireNonNull(action);
866 <        final Object[] elements = this.elements;
867 <        final int capacity = elements.length;
868 <        int from, end, to, leftover;
869 <        leftover = (end = (from = head) + size)
870 <            - (to = (capacity - end >= 0) ? end : capacity);
871 <        for (;; from = 0, to = leftover, leftover = 0) {
872 <            for (int i = from; i < to; i++)
873 <                action.accept((E) elements[i]);
858 <            if (leftover == 0) break;
866 >        final Object[] es = elements;
867 >        int i, end, to, todo;
868 >        todo = (end = (i = head) + size)
869 >            - (to = (es.length - end >= 0) ? end : es.length);
870 >        for (;; to = todo, i = 0, todo = 0) {
871 >            for (; i < to; i++)
872 >                action.accept((E) es[i]);
873 >            if (todo == 0) break;
874          }
875          // checkInvariants();
876      }
877  
878      /**
879 <     * A variant of forEach that also checks for concurrent
880 <     * modification, for use in iterators.
879 >     * Calls action on remaining elements, starting at index i and
880 >     * traversing in ascending order.  A variant of forEach that also
881 >     * checks for concurrent modification, for use in iterators.
882       */
883      static <E> void forEachRemaining(
884 <        Consumer<? super E> action, Object[] elements, int from, int size) {
885 <        Objects.requireNonNull(action);
886 <        final int capacity = elements.length;
887 <        int end, to, leftover;
888 <        leftover = (end = from + size)
889 <            - (to = (capacity - end >= 0) ? end : capacity);
890 <        for (;; from = 0, to = leftover, leftover = 0) {
891 <            for (int i = from; i < to; i++) {
876 <                @SuppressWarnings("unchecked") E e = (E) elements[i];
877 <                if (e == null)
878 <                    throw new ConcurrentModificationException();
879 <                action.accept(e);
880 <            }
881 <            if (leftover == 0) break;
882 <        }
883 <    }
884 <
885 <    static <E> void forEachRemainingDescending(
886 <        Consumer<? super E> action, Object[] elements, int from, int size) {
887 <        Objects.requireNonNull(action);
888 <        final int capacity = elements.length;
889 <        int end, to, leftover;
890 <        leftover = (to = ((end = from - size) >= -1) ? end : -1) - end;
891 <        for (;; from = capacity - 1, to = capacity - 1 - leftover, leftover = 0) {
892 <            for (int i = from; i > to; i--) {
893 <                @SuppressWarnings("unchecked") E e = (E) elements[i];
894 <                if (e == null)
895 <                    throw new ConcurrentModificationException();
896 <                action.accept(e);
897 <            }
898 <            if (leftover == 0) break;
884 >        Consumer<? super E> action, Object[] es, int i, int remaining) {
885 >        int end, to, todo;
886 >        todo = (end = i + remaining)
887 >            - (to = (es.length - end >= 0) ? end : es.length);
888 >        for (;; to = todo, i = 0, todo = 0) {
889 >            for (; i < to; i++)
890 >                action.accept(nonNullElementAt(es, i));
891 >            if (todo == 0) break;
892          }
893      }
894  
# Line 906 | Line 899 | public class ArrayDeque<E> extends Abstr
899       * @param operator the operator to apply to each element
900       * @since TBD
901       */
902 +    @SuppressWarnings("unchecked")
903      /* public */ void replaceAll(UnaryOperator<E> operator) {
904          Objects.requireNonNull(operator);
905 <        final Object[] elements = this.elements;
906 <        final int capacity = elements.length;
907 <        int from, end, to, leftover;
908 <        leftover = (end = (from = head) + size)
909 <            - (to = (capacity - end >= 0) ? end : capacity);
910 <        for (;; from = 0, to = leftover, leftover = 0) {
911 <            for (int i = from; i < to; i++)
912 <                elements[i] = operator.apply(elementAt(i));
919 <            if (leftover == 0) break;
905 >        final Object[] es = elements;
906 >        int i, end, to, todo;
907 >        todo = (end = (i = head) + size)
908 >            - (to = (es.length - end >= 0) ? end : es.length);
909 >        for (;; to = todo, i = 0, todo = 0) {
910 >            for (; i < to; i++)
911 >                es[i] = operator.apply((E) es[i]);
912 >            if (todo == 0) break;
913          }
914          // checkInvariants();
915      }
# Line 948 | Line 941 | public class ArrayDeque<E> extends Abstr
941      /** Implementation of bulk remove methods. */
942      private boolean bulkRemove(Predicate<? super E> filter) {
943          // checkInvariants();
944 <        final Object[] elements = this.elements;
945 <        final int capacity = elements.length;
944 >        final Object[] es = elements;
945 >        final int capacity = es.length;
946          int i = head, j = i, remaining = size, deleted = 0;
947          try {
948              for (; remaining > 0; remaining--) {
949 <                @SuppressWarnings("unchecked") E e = (E) elements[i];
949 >                @SuppressWarnings("unchecked") E e = (E) es[i];
950                  if (filter.test(e))
951                      deleted++;
952                  else {
953                      if (j != i)
954 <                        elements[j] = e;
954 >                        es[j] = e;
955                      if (++j >= capacity) j = 0;
956                  }
957                  if (++i >= capacity) i = 0;
# Line 967 | Line 960 | public class ArrayDeque<E> extends Abstr
960          } catch (Throwable ex) {
961              if (deleted > 0)
962                  for (; remaining > 0; remaining--) {
963 <                    elements[j] = elements[i];
963 >                    es[j] = es[i];
964                      if (++i >= capacity) i = 0;
965                      if (++j >= capacity) j = 0;
966                  }
967              throw ex;
968          } finally {
969              size -= deleted;
970 <            clearSlice(elements, j, deleted);
970 >            circularClear(es, j, deleted);
971              // checkInvariants();
972          }
973      }
# Line 989 | Line 982 | public class ArrayDeque<E> extends Abstr
982       */
983      public boolean contains(Object o) {
984          if (o != null) {
985 <            final Object[] elements = this.elements;
986 <            final int capacity = elements.length;
987 <            int from, end, to, leftover;
988 <            leftover = (end = (from = head) + size)
989 <                - (to = (capacity - end >= 0) ? end : capacity);
990 <            for (;; from = 0, to = leftover, leftover = 0) {
991 <                for (int i = from; i < to; i++)
999 <                    if (o.equals(elements[i]))
985 >            final Object[] es = elements;
986 >            int i, end, to, todo;
987 >            todo = (end = (i = head) + size)
988 >                - (to = (es.length - end >= 0) ? end : es.length);
989 >            for (;; to = todo, i = 0, todo = 0) {
990 >                for (; i < to; i++)
991 >                    if (o.equals(es[i]))
992                          return true;
993 <                if (leftover == 0) break;
993 >                if (todo == 0) break;
994              }
995          }
996          return false;
# Line 1026 | 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 size elements, starting at head.
1027 >     * Nulls out count elements, starting at array index from.
1028       */
1029 <    private static void clearSlice(Object[] elements, int head, int size) {
1030 <        final int capacity = elements.length, end = head + size;
1031 <        final int leg = (capacity - end >= 0) ? end : capacity;
1032 <        Arrays.fill(elements, head, leg, null);
1033 <        if (leg != end)
1034 <            Arrays.fill(elements, 0, end - capacity, null);
1029 >    private static void circularClear(Object[] es, int from, int count) {
1030 >        int end, to, todo;
1031 >        todo = (end = from + count)
1032 >            - (to = (es.length - end >= 0) ? end : es.length);
1033 >        for (;; to = todo, from = 0, todo = 0) {
1034 >            Arrays.fill(es, from, to, null);
1035 >            if (todo == 0) break;
1036 >        }
1037      }
1038  
1039      /**
# Line 1060 | Line 1054 | public class ArrayDeque<E> extends Abstr
1054      }
1055  
1056      private <T> T[] toArray(Class<T[]> klazz) {
1057 <        final Object[] elements = this.elements;
1058 <        final int capacity = elements.length;
1057 >        final Object[] es = elements;
1058 >        final int capacity = es.length;
1059          final int head = this.head, end = head + size;
1060          final T[] a;
1061          if (end >= 0) {
1062 <            a = Arrays.copyOfRange(elements, head, end, klazz);
1062 >            a = Arrays.copyOfRange(es, head, end, klazz);
1063          } else {
1064              // integer overflow!
1065 <            a = Arrays.copyOfRange(elements, 0, size, klazz);
1066 <            System.arraycopy(elements, head, a, 0, capacity - head);
1065 >            a = Arrays.copyOfRange(es, 0, size, klazz);
1066 >            System.arraycopy(es, head, a, 0, capacity - head);
1067          }
1068          if (end - capacity > 0)
1069 <            System.arraycopy(elements, 0, a, capacity - head, end - capacity);
1069 >            System.arraycopy(es, 0, a, capacity - head, end - capacity);
1070          return a;
1071      }
1072  
# Line 1114 | Line 1108 | public class ArrayDeque<E> extends Abstr
1108       */
1109      @SuppressWarnings("unchecked")
1110      public <T> T[] toArray(T[] a) {
1111 <        final int size = this.size;
1112 <        if (size > a.length)
1111 >        final int size;
1112 >        if ((size = this.size) > a.length)
1113              return toArray((Class<T[]>) a.getClass());
1114 <        final Object[] elements = this.elements;
1121 <        final int capacity = elements.length;
1114 >        final Object[] es = elements;
1115          final int head = this.head, end = head + size;
1116 <        final int front = (capacity - end >= 0) ? size : capacity - head;
1117 <        System.arraycopy(elements, head, a, 0, front);
1118 <        if (front != size)
1119 <            System.arraycopy(elements, 0, a, capacity - head, end - capacity);
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);
1120          if (size < a.length)
1121              a[size] = null;
1122          return a;
# Line 1166 | Line 1159 | public class ArrayDeque<E> extends Abstr
1159          s.writeInt(size);
1160  
1161          // Write out elements in order.
1162 <        final Object[] elements = this.elements;
1163 <        final int capacity = elements.length;
1164 <        int from, end, to, leftover;
1165 <        leftover = (end = (from = head) + size)
1166 <            - (to = (capacity - end >= 0) ? end : capacity);
1167 <        for (;; from = 0, to = leftover, leftover = 0) {
1168 <            for (int i = from; i < to; i++)
1169 <                s.writeObject(elements[i]);
1177 <            if (leftover == 0) break;
1162 >        final Object[] es = elements;
1163 >        int i, end, to, todo;
1164 >        todo = (end = (i = head) + size)
1165 >            - (to = (es.length - end >= 0) ? end : es.length);
1166 >        for (;; to = todo, i = 0, todo = 0) {
1167 >            for (; i < to; i++)
1168 >                s.writeObject(es[i]);
1169 >            if (todo == 0) break;
1170          }
1171      }
1172  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines