Advertisement
alex-

Untitled

Jul 29th, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 92.15 KB | None | 0 0
  1. /*
  2.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  3.  *
  4.  * This code is free software; you can redistribute it and/or modify it
  5.  * under the terms of the GNU General Public License version 2 only, as
  6.  * published by the Free Software Foundation.  Oracle designates this
  7.  * particular file as subject to the "Classpath" exception as provided
  8.  * by Oracle in the LICENSE file that accompanied this code.
  9.  *
  10.  * This code is distributed in the hope that it will be useful, but WITHOUT
  11.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13.  * version 2 for more details (a copy is included in the LICENSE file that
  14.  * accompanied this code).
  15.  *
  16.  * You should have received a copy of the GNU General Public License version
  17.  * 2 along with this work; if not, write to the Free Software Foundation,
  18.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19.  *
  20.  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21.  * or visit www.oracle.com if you need additional information or have any
  22.  * questions.
  23.  */
  24.  
  25. /*
  26.  * This file is available under and governed by the GNU General Public
  27.  * License version 2 only, as published by the Free Software Foundation.
  28.  * However, the following notice accompanied the original version of this
  29.  * file:
  30.  *
  31.  * Written by Josh Bloch of Google Inc. and released to the public domain,
  32.  * as explained at http://creativecommons.org/publicdomain/zero/1.0/.
  33.  */
  34.  
  35. package java.util;
  36.  
  37. import java.io.Serializable;
  38. import java.util.function.Consumer;
  39. import java.util.function.Predicate;
  40. import java.util.function.UnaryOperator;
  41. import jdk.internal.misc.SharedSecrets;
  42.  
  43. /**
  44.  * Resizable-array implementation of the {@link Deque} interface. This class
  45.  * also implements all of the methods of the {@link java.util.List} interface
  46.  * (except for equals, hashCode and remove(index) (use removeAt(index) instead))
  47.  * but does not actually implement java.util.List because the original
  48.  * implementation did not and doing so now would break compatibility. There is,
  49.  * however, a subclass {@link ArrayDeque.List} which implements java.util.List
  50.  * provided. Array deques have no capacity restrictions; they grow as necessary
  51.  * to support usage.  They are not thread-safe; in the absence of external
  52.  * synchronization, they do not support concurrent access by multiple threads.
  53.  * Null elements are prohibited, but a subclass {@link ArrayDeque.WithNulls} which
  54.  * allows nulls is provided. This class is likely to be faster than
  55.  * {@link Stack} when used as a stack, and faster than {@link LinkedList}
  56.  * when used as a queue.
  57.  *
  58.  * <p>Most {@code ArrayDeque} operations run in amortized constant time.
  59.  * Exceptions include
  60.  * {@link #add(int, Object)},
  61.  * {@link #remove(Object)},
  62.  * {@link #remove(int)},
  63.  * {@link #indexOf},
  64.  * {@link #lastIndexOf},
  65.  * {@link #removeFirstOccurrence removeFirstOccurrence},
  66.  * {@link #removeLastOccurrence removeLastOccurrence},
  67.  * {@link #contains contains},
  68.  * {@link #iterator iterator.remove()},
  69.  * and the bulk operations, all of which run in linear time.
  70.  *
  71.  * <p>The iterators returned by this class's {@link #iterator() iterator}
  72.  * method are <em>fail-fast</em>: If the deque is modified at any time after
  73.  * the iterator is created, in any way except through the iterator's own
  74.  * {@code remove} method, the iterator will generally throw a {@link
  75.  * ConcurrentModificationException}.  Thus, in the face of concurrent
  76.  * modification, the iterator fails quickly and cleanly, rather than risking
  77.  * arbitrary, non-deterministic behavior at an undetermined time in the
  78.  * future.
  79.  *
  80.  * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  81.  * as it is, generally speaking, impossible to make any hard guarantees in the
  82.  * presence of unsynchronized concurrent modification.  Fail-fast iterators
  83.  * throw {@code ConcurrentModificationException} on a best-effort basis.
  84.  * Therefore, it would be wrong to write a program that depended on this
  85.  * exception for its correctness: <i>the fail-fast behavior of iterators
  86.  * should be used only to detect bugs.</i>
  87.  *
  88.  * <p>This class and its iterator implement all of the
  89.  * <em>optional</em> methods of the {@link Collection} and {@link
  90.  * Iterator} interfaces.
  91.  *
  92.  * <p>This class is a member of the
  93.  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  94.  * Java Collections Framework</a>.
  95.  *
  96.  * @author  Josh Bloch and Doug Lea
  97.  * @param <E> the type of elements held in this deque
  98.  * @since   1.6
  99.  */
  100. public class ArrayDeque<E> extends AbstractCollection<E>
  101.                            implements Deque<E>, Cloneable, Serializable
  102. {
  103.     /*
  104.      * VMs excel at optimizing simple array loops where indices are
  105.      * incrementing or decrementing over a valid slice, e.g.
  106.      *
  107.      * for (int i = start; i < end; i++) ... elements[i]
  108.      *
  109.      * Because in a circular array, elements are in general stored in
  110.      * two disjoint such slices, we help the VM by writing unusual
  111.      * nested loops for all traversals over the elements.  Having only
  112.      * one hot inner loop body instead of two or three eases human
  113.      * maintenance and encourages VM loop inlining into the caller.
  114.      */
  115.  
  116.     /**
  117.      * The array in which the elements of the deque are stored.
  118.      * All array cells not holding deque elements are always null.
  119.      * The array always has at least one null slot (at tail).
  120.      */
  121.     transient Object[] elements;
  122.  
  123.     /**
  124.      * The index of the element at the head of the deque (which is the
  125.      * element that would be removed by remove() or pop()); or an
  126.      * arbitrary number 0 <= head < elements.length equal to tail if
  127.      * the deque is empty.
  128.      */
  129.     transient int head;
  130.  
  131.     /**
  132.      * The index at which the next element would be added to the tail
  133.      * of the deque (via addLast(E), add(E), or push(E));
  134.      * elements[tail] is always null.
  135.      */
  136.     transient int tail;
  137.  
  138.     /**
  139.      * The maximum size of array to allocate.
  140.      * Some VMs reserve some header words in an array.
  141.      * Attempts to allocate larger arrays may result in
  142.      * OutOfMemoryError: Requested array size exceeds VM limit
  143.      */
  144.     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  145.  
  146.     /**
  147.      * Shared arrays for empty instances
  148.      */
  149.     private static final Object[] EMPTY = new Object[1], DEFAULT = new Object[1];
  150.  
  151.     /**
  152.      * Increases the capacity of this deque.  Call only when full, i.e.,
  153.      * when head and tail have wrapped around to become equal.
  154.      */
  155.     Object[] grow() {
  156.         assert head == tail;
  157.         Object[] e = elements;
  158.         int n = e.length, p = head, r = n - p;
  159.         Object[] a = new Object[newCapacity(1)];
  160.         System.arraycopy(e, p, a, 0, r);
  161.         System.arraycopy(e, 0, a, r, p);
  162.         head = 0;
  163.         tail = n;
  164.         return elements = a;
  165.     }
  166.  
  167.     /** Capacity calculation for edge conditions, especially overflow. */
  168.     private int newCapacity(int needed) {
  169.         final int oldCapacity = elements.length, minCapacity, newCapacity;
  170.         if (oldCapacity == 1 && elements == DEFAULT && needed <= 16)
  171.             return 16 + 1;
  172.         // Double capacity if small; else grow by 50%
  173.         int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1);
  174.         if (jump >= needed && (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE <= 0)
  175.             return newCapacity;
  176.         if ((minCapacity = oldCapacity + needed) + (1 << 31) > MAX_ARRAY_SIZE + (1 << 31)) {
  177.             if (minCapacity < 0)
  178.                 throw new IllegalStateException("Sorry, deque too big");
  179.             return Integer.MAX_VALUE;
  180.         }
  181.         return jump >= needed ? MAX_ARRAY_SIZE : minCapacity;
  182.     }
  183.  
  184.     /**
  185.      * Copies the elements from our element array into the specified array,
  186.      * in order (from first to last element in the deque).  It is assumed
  187.      * that the array is large enough to hold all elements in the deque.
  188.      *
  189.      * @return its argument
  190.      */
  191.     <T> T[] copyElements(T[] a, int from, int to) {
  192.         if (from < to) {
  193.             System.arraycopy(elements, from, a, 0, to - from);
  194.         } else if (from > to) {
  195.             Object[] es = elements;
  196.             int headPortionLen = es.length - from;
  197.             System.arraycopy(es, from, a, 0, headPortionLen);
  198.             System.arraycopy(es, 0, a, headPortionLen, to);
  199.         }
  200.         return a;
  201.     }
  202.  
  203.     /**
  204.      * Attempts to reallocate the backing array so that is has space for as close as
  205.      * possible to, but not less than, minSize elements unless it already has space
  206.      * for between minSize and maxSize elements. If the size() of this ArrayDeque is
  207.      * greater than minSize then size() will be treated as minSize.
  208.      *
  209.      * <p>This method can be used to achieve the same result as {@link ArrayList#trimToSize}
  210.      * by calling {@code reallocate(0, 0)} or {@link ArrayList#ensureCapacity(n)} by calling
  211.      * {@code reallocate(n, Integer.MAX_VALUE)}.
  212.      *
  213.      * @param minSize the requested minimum capacity
  214.      * @param maxSize the requested maximum capacity
  215.      * @return the new (or unchanged) capacity
  216.      * @throws IllegalArgumentException if {@code minSize > maxSize} (size() may be greater
  217.      * than maxSize without throwing an exception)
  218.      * @throws OutOfMemoryError if there is not enough memory available to allocate
  219.      * the array or the array would be larger than an implementation defined limit
  220.      */
  221.     public int reallocate(int minSize, int maxSize) {
  222.         if (minSize > maxSize) throw new IllegalArgumentException(minSize + " > " + maxSize);
  223.         int c = elements.length - 1, s = size();
  224.         if (c < minSize) {
  225.             if (minSize + 1 < 0) throw new OutOfMemoryError();
  226.         } else {
  227.             if (c <= maxSize) return c;
  228.             if (s > minSize) {
  229.                 if (c == (minSize = s)) return c;
  230.             }
  231.         }
  232.         elements = minSize == 0 ? EMPTY : copyElements(new Object[minSize + 1], head, tail);
  233.         head = 0;
  234.         tail = s;
  235.         return minSize;
  236.     }
  237.  
  238.     /**
  239.      * Constructs an empty array deque with an initial capacity
  240.      * sufficient to hold 16 elements.
  241.      */
  242.     public ArrayDeque() {
  243.         elements = DEFAULT;
  244.     }
  245.  
  246.     /**
  247.      * Constructs an empty array deque with an initial capacity
  248.      * sufficient to hold the specified number of elements.
  249.      *
  250.      * @param numElements lower bound on initial capacity of the deque
  251.      */
  252.     public ArrayDeque(int numElements) {
  253.         elements = numElements < 1 ? EMPTY :
  254.             new Object[(numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE :
  255.                        numElements + 1];
  256.     }
  257.  
  258.     /**
  259.      * Constructs a deque containing the elements of the specified
  260.      * collection, in the order they are returned by the collection's
  261.      * iterator.  (The first element returned by the collection's
  262.      * iterator becomes the first element, or <i>front</i> of the
  263.      * deque.)
  264.      *
  265.      * @param c the collection whose elements are to be placed into the deque
  266.      * @throws NullPointerException if the specified collection is null
  267.      */
  268.     public ArrayDeque(Collection<? extends E> c) {
  269.         Object[] a = c.toArray();
  270.         if (a.length == 0) {
  271.             elements = EMPTY;
  272.             return;
  273.         }
  274.         checkArray(a);
  275.         elements = Arrays.copyOf(a, a.length + 1, Object[].class);
  276.         tail = a.length;
  277.     }
  278.  
  279.     /**
  280.      * A subclass of ArrayDeque that implements {@link java.util.List}. This
  281.      * class overrides {@link #equals} and {@link #hashCode} to provide the
  282.      * behavior specified by the List interface.
  283.      */
  284.     public static class List<E> extends ArrayDeque<E> implements java.util.List<E>, RandomAccess {
  285.  
  286.         private static final long serialVersionUID = ArrayDeque.serialVersionUID;
  287.  
  288.         /**
  289.          * Constructs an empty ArrayDeque.List with an initial capacity
  290.          * sufficient to hold 16 elements.
  291.          */
  292.         public List() {
  293.  
  294.         }
  295.  
  296.         /**
  297.          * Constructs an empty ArrayDeque.List with an initial capacity
  298.          * sufficient to hold the specified number of elements.
  299.          *
  300.          * @param numElements lower bound on initial capacity of the deque
  301.          */
  302.         public List(int numElements) {
  303.             super(numElements);
  304.         }
  305.  
  306.         /**
  307.          * Constructs an ArrayDeque.List containing the elements of the specified
  308.          * collection, in the order they are returned by the collection's
  309.          * iterator.  (The first element returned by the collection's
  310.          * iterator becomes the first element, or <i>front</i> of the
  311.          * deque.)
  312.          *
  313.          * @param c the collection whose elements are to be placed into the deque
  314.          * @throws NullPointerException if the specified collection is null
  315.          */
  316.         public List(Collection<? extends E> c) {
  317.             super(c);
  318.         }
  319.  
  320.         public boolean equals(Object o) {
  321.             return o == this || equals(o, head, tail);
  322.         }
  323.  
  324.         public int hashCode() {
  325.             return hashCode(head, tail);
  326.         }
  327.  
  328.         public E remove(int index) {
  329.             return removeAt(index);
  330.         }
  331.  
  332.         public List<E> clone() {
  333.             return (List<E>) super.clone();
  334.         }
  335.     }
  336.  
  337.     /**
  338.      * A subclass of ArrayDeque.List that accepts null elements (and all other elements).
  339.      */
  340.     public static class WithNulls<E> extends ArrayDeque.List<E> {
  341.  
  342.         private static final long serialVersionUID = ArrayDeque.serialVersionUID;
  343.  
  344.         /**
  345.          * Constructs an empty ArrayDeque.WithNulls with an initial capacity
  346.          * sufficient to hold 16 elements.
  347.          */
  348.         public WithNulls() {
  349.  
  350.         }
  351.  
  352.         /**
  353.          * Constructs an empty ArrayDeque.WithNulls with an initial capacity
  354.          * sufficient to hold the specified number of elements.
  355.          *
  356.          * @param numElements lower bound on initial capacity of the deque
  357.          */
  358.         public WithNulls(int numElements) {
  359.             super(numElements);
  360.         }
  361.  
  362.         /**
  363.          * Constructs an ArrayDeque.WithNulls containing the elements of the specified
  364.          * collection, in the order they are returned by the collection's
  365.          * iterator.  (The first element returned by the collection's
  366.          * iterator becomes the first element, or <i>front</i> of the
  367.          * deque.)
  368.          *
  369.          * @param c the collection whose elements are to be placed into the deque
  370.          * @throws NullPointerException if the specified collection is null
  371.          */
  372.         public WithNulls(Collection<? extends E> c) {
  373.             super(c);
  374.         }
  375.  
  376.         void check(E e) {
  377.  
  378.         }
  379.  
  380.         void checkArray(Object[] es) {
  381.  
  382.         }
  383.  
  384.         boolean isValid(Object e) {
  385.             return true;
  386.         }
  387.  
  388.         public WithNulls<E> clone() {
  389.             return (WithNulls<E>) super.clone();
  390.         }
  391.     }
  392.  
  393.     /**
  394.      * Circularly increments i, mod modulus.
  395.      * Precondition and postcondition: 0 <= i < modulus.
  396.      */
  397.     static final int inc(int i, int modulus) {
  398.         if (++i >= modulus) i = 0;
  399.         return i;
  400.     }
  401.  
  402.     /**
  403.      * Circularly decrements i, mod modulus.
  404.      * Precondition and postcondition: 0 <= i < modulus.
  405.      */
  406.     static final int dec(int i, int modulus) {
  407.         if (--i < 0) i = modulus - 1;
  408.         return i;
  409.     }
  410.  
  411.     /**
  412.      * Circularly adds the given distance to index i, mod modulus.
  413.      * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
  414.      * @return index 0 <= i < modulus
  415.      */
  416.     static final int inc(int i, int distance, int modulus) {
  417.         if ((i += distance) - modulus >= 0) i -= modulus;
  418.         return i;
  419.     }
  420.  
  421.     /**
  422.      * Subtracts j from i, mod modulus.
  423.      * Index i must be logically ahead of index j.
  424.      * Precondition: 0 <= i < modulus, 0 <= j < modulus.
  425.      * @return the "circular distance" from j to i; corner case i == j
  426.      * is disambiguated to "empty", returning 0.
  427.      */
  428.     static final int sub(int i, int j, int modulus) {
  429.         if ((i -= j) < 0) i += modulus;
  430.         return i;
  431.     }
  432.  
  433.     /**
  434.      * Returns element at array index i.
  435.      * This is a slight abuse of generics, accepted by javac.
  436.      */
  437.     @SuppressWarnings("unchecked")
  438.     static final <E> E elementAt(Object[] es, int i) {
  439.         return (E) es[i];
  440.     }
  441.  
  442.     /**
  443.      * A version of elementAt that checks for null elements.
  444.      * This check doesn't catch all possible comodifications,
  445.      * but does catch ones that corrupt traversal.
  446.      */
  447.     final E validElementAt(Object[] es, int i) {
  448.         @SuppressWarnings("unchecked") E e = (E) es[i];
  449.         if (!isValid(e))
  450.             throw new ConcurrentModificationException();
  451.         return e;
  452.     }
  453.  
  454.     // Methods for restricting which elements may be added
  455.  
  456.     /**
  457.      * Throws one of the specified exceptions if the element can not be
  458.      * added to this ArrayDeque.
  459.      *
  460.      * @param e the element to add
  461.      * @throws ClassCastException if the class of the specified element
  462.      *         prevents it from being added to this deque
  463.      * @throws NullPointerException if the specified element is null and this
  464.      *         deque does not permit null elements
  465.      * @throws IllegalArgumentException if some property of the specified
  466.      *         element prevents it from being added to this deque
  467.      */
  468.     void check(E e) {
  469.         if (e == null)
  470.             throw new NullPointerException();
  471.     }
  472.  
  473.     /**
  474.      * Throws one of the specified exceptions if any of the elements in the
  475.      * array can not be added to this ArrayDeque.
  476.      *
  477.      * @param a the elements to add
  478.      * @throws ClassCastException if the class of the specified element
  479.      *         prevents it from being added to this deque
  480.      * @throws NullPointerException if the specified element is null and this
  481.      *         deque does not permit null elements
  482.      * @throws IllegalArgumentException if some property of the specified
  483.      *         element prevents it from being added to this deque
  484.      */
  485.     void checkArray(Object[] a) {
  486.         for (Object o : a) {
  487.             if (o == null)
  488.                 throw new NullPointerException();
  489.         }
  490.     }
  491.  
  492.     /**
  493.      * Returns whether or not the element can be in this ArrayDeque.
  494.      *
  495.      * @param e the element to test
  496.      * @return true if the element can be in this ArrayDeque
  497.      */
  498.     boolean isValid(Object e) {
  499.         return e != null;
  500.     }
  501.  
  502.     // The main insertion and extraction methods are addFirst,
  503.     // addLast, pollFirst, pollLast. The other methods are defined in
  504.     // terms of these.
  505.  
  506.     /**
  507.      * Inserts the specified element at the front of this deque.
  508.      *
  509.      * @param e the element to add
  510.      * @throws NullPointerException if the specified element is null
  511.      */
  512.     public void addFirst(E e) {
  513.         check(e);
  514.         final Object[] es = elements;
  515.         final int h = head = dec(head, es.length);
  516.         if (h == tail) {
  517.             grow()[0] = e;
  518.         } else {
  519.             es[h] = e;
  520.         }
  521.     }
  522.  
  523.     /**
  524.      * Inserts the specified element at the end of this deque.
  525.      *
  526.      * <p>This method is equivalent to {@link #add}.
  527.      *
  528.      * @param e the element to add
  529.      * @throws NullPointerException if the specified element is null
  530.      */
  531.     public void addLast(E e) {
  532.         check(e);
  533.         final Object[] es = elements;
  534.         final int t = tail;
  535.         if (head == (tail = inc(t, es.length))) {
  536.             grow()[es.length - 1] = e;
  537.         } else {
  538.             es[t] = e;
  539.         }
  540.     }
  541.  
  542.     /**
  543.      * Adds all of the elements in the specified collection at the end
  544.      * of this deque, as if by calling {@link #addLast} on each one,
  545.      * in the order that they are returned by the collection's iterator.
  546.      *
  547.      * @param c the elements to be inserted into this deque
  548.      * @return {@code true} if this deque changed as a result of the call
  549.      * @throws NullPointerException if the specified collection or any
  550.      *         of its elements are null
  551.      */
  552.     public boolean addAll(Collection<? extends E> c) {
  553.         Object[] a = c.toArray();
  554.         if (a.length == 0) return false;
  555.         checkArray(a);
  556.         insert(size(), a, 0, a.length);
  557.         return true;
  558.     }
  559.  
  560.     /**
  561.      * Inserts the specified element at the front of this deque.
  562.      *
  563.      * @param e the element to add
  564.      * @return {@code true} (as specified by {@link Deque#offerFirst})
  565.      * @throws NullPointerException if the specified element is null
  566.      */
  567.     public boolean offerFirst(E e) {
  568.         addFirst(e);
  569.         return true;
  570.     }
  571.  
  572.     /**
  573.      * Inserts the specified element at the end of this deque.
  574.      *
  575.      * @param e the element to add
  576.      * @return {@code true} (as specified by {@link Deque#offerLast})
  577.      * @throws NullPointerException if the specified element is null
  578.      */
  579.     public boolean offerLast(E e) {
  580.         addLast(e);
  581.         return true;
  582.     }
  583.  
  584.     /**
  585.      * @throws NoSuchElementException {@inheritDoc}
  586.      */
  587.     public E removeFirst() {
  588.         final int h = head;
  589.         final Object[] es = elements;
  590.         E e = elementAt(es, h);
  591.         //optimize for non-empty, non-null case
  592.         if (e == null && h == tail) throw new NoSuchElementException();
  593.         es[h] = null;
  594.         head = inc(h, es.length);
  595.         return e;
  596.     }
  597.  
  598.     /**
  599.      * @throws NoSuchElementException {@inheritDoc}
  600.      */
  601.     public E removeLast() {
  602.         final Object[] es = elements;
  603.         final int t = tail, d = dec(t, es.length);
  604.         E e = elementAt(es, d);
  605.         if (e == null && t == head) throw new NoSuchElementException();
  606.         es[tail = d] = null;
  607.         return e;
  608.     }
  609.  
  610.     public E pollFirst() {
  611.         final int h = head;
  612.         final Object[] es = elements;
  613.         E e = elementAt(es, h);
  614.         if (e == null && h == tail) return null;
  615.         es[h] = null;
  616.         head = inc(h, es.length);
  617.         return e;
  618.     }
  619.  
  620.     public E pollLast() {
  621.         final Object[] es = elements;
  622.         final int t = tail, d = dec(t, es.length);
  623.         E e = elementAt(es, d);
  624.         if (e == null && t == head) return null;
  625.         es[tail = d] = null;
  626.         return e;
  627.     }
  628.  
  629.     /**
  630.      * @throws NoSuchElementException {@inheritDoc}
  631.      */
  632.     public E getFirst() {
  633.         final int h = head;
  634.         final Object[] es = elements;
  635.         E e = elementAt(es, h);
  636.         if (e == null && h == tail) throw new NoSuchElementException();
  637.         return e;
  638.     }
  639.  
  640.     /**
  641.      * @throws NoSuchElementException {@inheritDoc}
  642.      */
  643.     public E getLast() {
  644.         final Object[] es = elements;
  645.         final int t = tail;
  646.         E e = elementAt(es, dec(t, es.length));
  647.         if (e == null && t == head) throw new NoSuchElementException();
  648.         return e;
  649.     }
  650.  
  651.     public E peekFirst() {
  652.         return elementAt(elements, head);
  653.     }
  654.  
  655.     public E peekLast() {
  656.         final Object[] es;
  657.         return elementAt(es = elements, dec(tail, es.length));
  658.     }
  659.  
  660.     /**
  661.      * Removes the first occurrence of the specified element in this
  662.      * deque (when traversing the deque from head to tail).
  663.      * If the deque does not contain the element, it is unchanged.
  664.      * More formally, removes the first element {@code e} such that
  665.      * {@code o.equals(e)} (if such an element exists).
  666.      * Returns {@code true} if this deque contained the specified element
  667.      * (or equivalently, if this deque changed as a result of the call).
  668.      *
  669.      * @param o element to be removed from this deque, if present
  670.      * @return {@code true} if the deque contained the specified element
  671.      */
  672.     public boolean removeFirstOccurrence(Object o) {
  673.         int i = index(o, head, tail);
  674.         if (i == -1) return false;
  675.         delete(i);
  676.         return true;
  677.     }
  678.  
  679.     /**
  680.      * Removes the last occurrence of the specified element in this
  681.      * deque (when traversing the deque from head to tail).
  682.      * If the deque does not contain the element, it is unchanged.
  683.      * More formally, removes the last element {@code e} such that
  684.      * {@code o.equals(e)} (if such an element exists).
  685.      * Returns {@code true} if this deque contained the specified element
  686.      * (or equivalently, if this deque changed as a result of the call).
  687.      *
  688.      * @param o element to be removed from this deque, if present
  689.      * @return {@code true} if the deque contained the specified element
  690.      */
  691.     public boolean removeLastOccurrence(Object o) {
  692.         int i = lastIndex(o, head, tail);
  693.         if (i == -1) return false;
  694.         delete(i);
  695.         return true;
  696.     }
  697.  
  698.     // *** Queue methods ***
  699.  
  700.     /**
  701.      * Inserts the specified element at the end of this deque.
  702.      *
  703.      * <p>This method is equivalent to {@link #addLast}.
  704.      *
  705.      * @param e the element to add
  706.      * @return {@code true} (as specified by {@link Collection#add})
  707.      * @throws NullPointerException if the specified element is null
  708.      */
  709.     public boolean add(E e) {
  710.         addLast(e);
  711.         return true;
  712.     }
  713.  
  714.     /**
  715.      * Inserts the specified element at the end of this deque.
  716.      *
  717.      * <p>This method is equivalent to {@link #offerLast}.
  718.      *
  719.      * @param e the element to add
  720.      * @return {@code true} (as specified by {@link Queue#offer})
  721.      * @throws NullPointerException if the specified element is null
  722.      */
  723.     public boolean offer(E e) {
  724.         return offerLast(e);
  725.     }
  726.  
  727.     /**
  728.      * Retrieves and removes the head of the queue represented by this deque.
  729.      *
  730.      * This method differs from {@link #poll() poll()} only in that it
  731.      * throws an exception if this deque is empty.
  732.      *
  733.      * <p>This method is equivalent to {@link #removeFirst}.
  734.      *
  735.      * @return the head of the queue represented by this deque
  736.      * @throws NoSuchElementException {@inheritDoc}
  737.      */
  738.     public E remove() {
  739.         return removeFirst();
  740.     }
  741.  
  742.     /**
  743.      * Retrieves and removes the head of the queue represented by this deque
  744.      * (in other words, the first element of this deque), or returns
  745.      * {@code null} if this deque is empty.
  746.      *
  747.      * <p>This method is equivalent to {@link #pollFirst}.
  748.      *
  749.      * @return the head of the queue represented by this deque, or
  750.      *         {@code null} if this deque is empty
  751.      */
  752.     public E poll() {
  753.         return pollFirst();
  754.     }
  755.  
  756.     /**
  757.      * Retrieves, but does not remove, the head of the queue represented by
  758.      * this deque.  This method differs from {@link #peek peek} only in
  759.      * that it throws an exception if this deque is empty.
  760.      *
  761.      * <p>This method is equivalent to {@link #getFirst}.
  762.      *
  763.      * @return the head of the queue represented by this deque
  764.      * @throws NoSuchElementException {@inheritDoc}
  765.      */
  766.     public E element() {
  767.         return getFirst();
  768.     }
  769.  
  770.     /**
  771.      * Retrieves, but does not remove, the head of the queue represented by
  772.      * this deque, or returns {@code null} if this deque is empty.
  773.      *
  774.      * <p>This method is equivalent to {@link #peekFirst}.
  775.      *
  776.      * @return the head of the queue represented by this deque, or
  777.      *         {@code null} if this deque is empty
  778.      */
  779.     public E peek() {
  780.         return peekFirst();
  781.     }
  782.  
  783.     // *** Stack methods ***
  784.  
  785.     /**
  786.      * Pushes an element onto the stack represented by this deque.  In other
  787.      * words, inserts the element at the front of this deque.
  788.      *
  789.      * <p>This method is equivalent to {@link #addFirst}.
  790.      *
  791.      * @param e the element to push
  792.      * @throws NullPointerException if the specified element is null
  793.      */
  794.     public void push(E e) {
  795.         addFirst(e);
  796.     }
  797.  
  798.     /**
  799.      * Pops an element from the stack represented by this deque.  In other
  800.      * words, removes and returns the first element of this deque.
  801.      *
  802.      * <p>This method is equivalent to {@link #removeFirst()}.
  803.      *
  804.      * @return the element at the front of this deque (which is the top
  805.      *         of the stack represented by this deque)
  806.      * @throws NoSuchElementException {@inheritDoc}
  807.      */
  808.     public E pop() {
  809.         return removeFirst();
  810.     }
  811.  
  812.     // *** List Methods ***
  813.  
  814.     /**
  815.      * Returns the element at the specified position in this ArrayDeque.
  816.      *
  817.      * @param index index of the element to return
  818.      * @return the element at the specified position in this list
  819.      * @throws IndexOutOfBoundsException if the index is out of range
  820.      *         ({@code index < 0 || index >= size()})
  821.      */
  822.     public E get(int index) {
  823.         if (index < 0 || index >= size()) throw new IndexOutOfBoundsException();
  824.         final Object[] es = elements;
  825.         return elementAt(es, inc(head, index, es.length));
  826.     }
  827.  
  828.     /**
  829.      * Replaces the element at the specified position in this ArrayDeque with
  830.      * the specified element.
  831.      *
  832.      * @param index index of the element to replace
  833.      * @param element element to be stored at the specified position
  834.      * @return the element previously at the specified position
  835.      * @throws NullPointerException if the specified element is null
  836.      * @throws IndexOutOfBoundsException if the index is out of range
  837.      *         ({@code index < 0 || index >= size()})
  838.      */
  839.     public E set(int index, E element) {
  840.         if (index < 0 || index >= size()) throw new IndexOutOfBoundsException();
  841.         check(element);
  842.         final Object[] es = elements;
  843.         index = inc(head, index, es.length);
  844.         E old = elementAt(es, index);
  845.         es[index] = element;
  846.         return old;
  847.     }
  848.  
  849.     /**
  850.      * Inserts the specified element at the specified position in this
  851.      * ArrayDeque. Shifts the element currently at that position
  852.      * (if any) and any subsequent elements to the right (adds one to their
  853.      * indices).
  854.      *
  855.      * @param index index at which the specified element is to be inserted
  856.      * @param element element to be inserted
  857.      * @throws NullPointerException if the specified element is null
  858.      * @throws IndexOutOfBoundsException if the index is out of range
  859.      *         ({@code index < 0 || index > size()})
  860.      */
  861.     public void add(int index, E element) {
  862.         if (index < 0 || index > size()) throw new IndexOutOfBoundsException();
  863.         check(element);
  864.         insert(inc(head, index, elements.length), element);
  865.     }
  866.  
  867.     /**
  868.      * Removes the element at the specified position in this ArrayDeque.
  869.      * Shifts any subsequent elements to the left (subtracts one
  870.      * from their indices).  Returns the element that was removed from the
  871.      * ArrayDeque.
  872.      *
  873.      * @param index the index of the element to be removed
  874.      * @return the element previously at the specified position
  875.      * @throws IndexOutOfBoundsException if the index is out of range
  876.      *         ({@code index < 0 || index >= size()})
  877.      */
  878.     public E removeAt(int index) {
  879.         if (index < 0 || index >= size()) throw new IndexOutOfBoundsException();
  880.         final Object[] es = elements;
  881.         index = inc(head, index, es.length);
  882.         E old = elementAt(es, index);
  883.         delete(index);
  884.         return old;
  885.     }
  886.  
  887.     /**
  888.      * Inserts all of the elements in the specified collection into this
  889.      * ArrayDeque at the specified position.  Shifts the
  890.      * element currently at that position (if any) and any subsequent
  891.      * elements to the right (increases their indices).  The new elements
  892.      * will appear in this list in the order that they are returned by the
  893.      * specified collection's iterator.  The behavior of this operation is
  894.      * undefined if the specified collection is modified while the
  895.      * operation is in progress.  (Note that this will occur if the specified
  896.      * collection is this ArrayDeque, and it's nonempty.)
  897.      *
  898.      * @param index index at which to insert the first element from the
  899.      *              specified collection
  900.      * @param c collection containing elements to be added to this list
  901.      * @return {@code true} if this list changed as a result of the call
  902.      * @throws NullPointerException if the specified collection contains one
  903.      *         or more null elements or if the specified collection is null
  904.      * @throws IndexOutOfBoundsException if the index is out of range
  905.      *         ({@code index < 0 || index > size()})
  906.      */
  907.     public boolean addAll(int index, Collection<? extends E> c) {
  908.         if (index < 0 || index > size()) throw new IndexOutOfBoundsException();
  909.         Object[] a = c.toArray();
  910.         if (a.length == 0) return false;
  911.         checkArray(a);
  912.         insert(index, a, 0, a.length);
  913.         return true;
  914.     }
  915.  
  916.     /**
  917.      * Returns the index of the first occurrence of the specified element
  918.      * in this ArrayDeque, or -1 if this ArrayDeque does not contain the element.
  919.      * More formally, returns the lowest index {@code i} such that
  920.      * {@code Objects.equals(o, get(i))},
  921.      * or -1 if there is no such index.
  922.      *
  923.      * @param o element to search for
  924.      * @return the index of the first occurrence of the specified element in
  925.      *         this list, or -1 if this list does not contain the element
  926.      */
  927.     public int indexOf(Object o) {
  928.         int i = index(o, head, tail);
  929.         return i == -1 ? -1 : sub(i, head, elements.length);
  930.     }
  931.  
  932.     /**
  933.      * Returns the index of the last occurrence of the specified element
  934.      * in this ArrayDeque, or -1 if this ArrayDeque does not contain the element.
  935.      * More formally, returns the highest index {@code i} such that
  936.      * {@code Objects.equals(o, get(i))},
  937.      * or -1 if there is no such index.
  938.      *
  939.      * @param o element to search for
  940.      * @return the index of the last occurrence of the specified element in
  941.      *         this list, or -1 if this list does not contain the element
  942.      */
  943.     public int lastIndexOf(Object o) {
  944.         int i = lastIndex(o, head, tail);
  945.         return i == -1 ? -1 : sub(i, head, elements.length);
  946.     }
  947.  
  948.     int index(Object o, int from, int to) {
  949.         final Object[] es = elements;
  950.         if (o != null) {
  951.             for (int i = from, e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  952.                 for (; i < e; i++)
  953.                     if (o.equals(es[i])) return i;
  954.                 if (e == to) break;
  955.             }
  956.         } else {
  957.             for (int i = from, e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  958.                 for (; i < e; i++)
  959.                     if (es[i] == null) return i;
  960.                 if (e == to) break;
  961.             }
  962.         }
  963.         return -1;
  964.     }
  965.  
  966.     int lastIndex(Object o, int to, int from) {
  967.         final Object[] es = elements;
  968.         if (o != null) {
  969.             for (int i = from, e = (i >= to) ? to : 0; ; i = es.length, e = to) {
  970.                 for ( ; i > e; )
  971.                     if (o.equals(es[--i])) return i;
  972.                 if (e == to) break;
  973.             }
  974.         } else {
  975.             for (int i = from, e = (i >= to) ? to : 0; ; i = es.length, e = to) {
  976.                 for ( ; i > e; )
  977.                     if (es[--i] == null) return i;
  978.                 if (e == to) break;
  979.             }
  980.         }
  981.         return -1;
  982.     }
  983.  
  984.     /**
  985.      * Replaces each element of this ArrayDeque with the result of applying the
  986.      * operator to that element.  Errors or runtime exceptions thrown by
  987.      * the operator are relayed to the caller.
  988.      *
  989.      * @param operator the operator to apply to each element
  990.      * @throws NullPointerException if the specified operator is null or
  991.      *         if the operator result is a null value
  992.      */
  993.     public void replaceAll(UnaryOperator<E> operator) {
  994.         replaceAll(operator, head, tail);
  995.     }
  996.  
  997.     void replaceAll(UnaryOperator<E> operator, int from, int to) {
  998.         Objects.requireNonNull(operator);
  999.         final Object[] es = elements;
  1000.         for (int i = from, e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  1001.             for (; i < e; i++) {
  1002.                 E v = operator.apply(elementAt(es, i));
  1003.                 check(v);
  1004.                 es[i] = v;
  1005.             }
  1006.             if (e == to) break;
  1007.         }
  1008.     }
  1009.  
  1010.     /**
  1011.      * Sorts this ArrayDeque according to the order induced by the specified
  1012.      * {@link Comparator}.  The sort is <i>stable</i>: this method must not
  1013.      * reorder equal elements.
  1014.      *
  1015.      * <p>All elements in this ArrayDeque must be <i>mutually comparable</i> using the
  1016.      * specified comparator (that is, {@code c.compare(e1, e2)} must not throw
  1017.      * a {@code ClassCastException} for any elements {@code e1} and {@code e2}
  1018.      * in the ArrayDeque).
  1019.      *
  1020.      * <p>If the specified comparator is {@code null} then all elements in this
  1021.      * ArrayDeque must implement the {@link Comparable} interface and the elements'
  1022.      * {@linkplain Comparable natural ordering} should be used.
  1023.      *
  1024.      * @implNote
  1025.      * This implementation is a stable, adaptive, iterative mergesort that
  1026.      * requires far fewer than n lg(n) comparisons when the input array is
  1027.      * partially sorted, while offering the performance of a traditional
  1028.      * mergesort when the input array is randomly ordered.  If the input array
  1029.      * is nearly sorted, the implementation requires approximately n
  1030.      * comparisons.  Temporary storage requirements vary from a small constant
  1031.      * for nearly sorted input arrays to n/2 object references for randomly
  1032.      * ordered input arrays.
  1033.      *
  1034.      * <p>The implementation takes equal advantage of ascending and
  1035.      * descending order in its input array, and can take advantage of
  1036.      * ascending and descending order in different parts of the same
  1037.      * input array.  It is well-suited to merging two or more sorted arrays:
  1038.      * simply concatenate the arrays and sort the resulting array.
  1039.      *
  1040.      * <p>The implementation was adapted from Tim Peters's list sort for Python
  1041.      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
  1042.      * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
  1043.      * Sorting and Information Theoretic Complexity", in Proceedings of the
  1044.      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
  1045.      * January 1993.
  1046.      *
  1047.      * @param c the {@code Comparator} used to compare deque elements.
  1048.      *          A {@code null} value indicates that the elements'
  1049.      *          {@linkplain Comparable natural ordering} should be used
  1050.      * @throws ClassCastException if the list contains elements that are not
  1051.      *         <i>mutually comparable</i> using the specified comparator
  1052.      * @throws IllegalArgumentException
  1053.      *         if the comparator is found to violate the {@link Comparator}
  1054.      *         contract
  1055.      */
  1056.     public void sort(Comparator<? super E> c) {
  1057.         sort(c, head, tail);
  1058.     }
  1059.  
  1060.     @SuppressWarnings("unchecked")
  1061.     void sort(Comparator<? super E> c, int from, int to) {
  1062.         E[] a = (E[]) elements;
  1063.         if (from <= to) {
  1064.             Arrays.sort(a, from, to, c);
  1065.             return;
  1066.         }
  1067.         if (to == 0) {
  1068.             Arrays.sort(a, from, a.length, c);
  1069.             return;
  1070.         }
  1071.         int end = a.length - from;
  1072.         int size = end + to;
  1073.         int t = tail;
  1074.         if (t + (long)size <= head) {
  1075.             System.arraycopy(a, from, a, t, end);
  1076.             System.arraycopy(a, 0, a, t + end, to);
  1077.             try {
  1078.                 Arrays.sort(a, t, t + size, c);
  1079.             } catch(RuntimeException | Error e) {
  1080.                 Arrays.fill(a, t, t + size, null);
  1081.                 throw e;
  1082.             }
  1083.             System.arraycopy(a, t, a, from, end);
  1084.             System.arraycopy(a, t + end, a, 0, to);
  1085.             Arrays.fill(a, t, t + size, null);
  1086.             return;
  1087.         }
  1088.         E[] e = (E[]) new Object[size];
  1089.         System.arraycopy(a, from, e, 0, end);
  1090.         System.arraycopy(a, 0, e, end, to);
  1091.         Arrays.sort(e, 0, size, c);
  1092.         System.arraycopy(e, 0, a, from, end);
  1093.         System.arraycopy(e, end, a, 0, to);
  1094.     }
  1095.  
  1096.     /**
  1097.      * Returns a List view of the portion of this ArrayDeque between the specified
  1098.      * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.  (If
  1099.      * {@code fromIndex} and {@code toIndex} are equal, the returned list is
  1100.      * empty.)  The returned list is backed by this ArrayDeque, so
  1101.      * changes in the returned list are reflected in this ArrayDeque, and vice-versa.
  1102.      * The returned list supports all of the optional list operations supported
  1103.      * by this ArrayDeque.<p>
  1104.      *
  1105.      * This method eliminates the need for explicit range operations (of
  1106.      * the sort that commonly exist for arrays).  Any operation that expects
  1107.      * a list can be used as a range operation by passing a subList view
  1108.      * instead of a whole list.  For example, the following idiom
  1109.      * removes a range of elements from a list:
  1110.      * <pre>{@code
  1111.      *      list.subList(from, to).clear();
  1112.      * }</pre>
  1113.      * Similar idioms may be constructed for {@code indexOf} and
  1114.      * {@code lastIndexOf}, and all of the algorithms in the
  1115.      * {@code Collections} class can be applied to a subList.<p>
  1116.      *
  1117.      * The semantics of the list returned by this method become undefined if
  1118.      * the backing ArrayDeque (i.e., this ArrayDeque) is <i>structurally modified</i> in
  1119.      * any way other than via the returned list.  (Structural modifications are
  1120.      * those that change the size of this ArrayDeque, or otherwise perturb it in such
  1121.      * a fashion that iterations in progress may yield incorrect results.)
  1122.      *
  1123.      * @param fromIndex low endpoint (inclusive) of the subList
  1124.      * @param toIndex high endpoint (exclusive) of the subList
  1125.      * @return a list view of the specified range within this ArrayDeque
  1126.      * @throws IndexOutOfBoundsException for an illegal endpoint index value
  1127.      *         ({@code fromIndex < 0 || toIndex > size ||
  1128.      *         fromIndex > toIndex})
  1129.      */
  1130.     public java.util.List<E> subList(int fromIndex, int toIndex) {
  1131.         sublistRangeCheck(size(), fromIndex, toIndex);
  1132.         return new SubList(null, fromIndex, toIndex - fromIndex);
  1133.     }
  1134.  
  1135.     static void sublistRangeCheck(int size, int from, int to) {
  1136.         if (from < 0) throw new IndexOutOfBoundsException(from + " < 0");
  1137.         if (to > size) throw new IndexOutOfBoundsException(to + " > " + size);
  1138.         if (from > to) throw new IndexOutOfBoundsException(from + " > " + to);
  1139.     }
  1140.  
  1141.     private class SubList extends AbstractCollection<E> implements java.util.List<E>, RandomAccess {
  1142.         final SubList parent;
  1143.         final int offset;
  1144.         int size;
  1145.  
  1146.         SubList(SubList parent, int offset, int size) {
  1147.             this.parent = parent;
  1148.             this.size = size;
  1149.             this.offset = offset;
  1150.         }
  1151.         void addToSize(int s) {
  1152.             size += s;
  1153.             for (SubList p = parent; p != null; p = p.parent) {
  1154.                 p.size += s;
  1155.             }
  1156.         }
  1157.         public E get(int index) {
  1158.             if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
  1159.             final Object[] es = elements;
  1160.             return elementAt(es, inc(head, offset + index, es.length));
  1161.         }
  1162.         public E set(int index, E e) {
  1163.             if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
  1164.             check(e);
  1165.             final Object[] es = elements;
  1166.             index = inc(head, offset + index, es.length);
  1167.             E old = elementAt(es, index);
  1168.             es[index] = e;
  1169.             return old;
  1170.         }
  1171.         public void add(int index, E e) {
  1172.             if (index < 0 || index > size) throw new IndexOutOfBoundsException();
  1173.             check(e);
  1174.             insert(inc(head, offset + index, elements.length), e);
  1175.             addToSize(1);
  1176.         }
  1177.         public E remove(int index) {
  1178.             if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
  1179.             final Object[] es = elements;
  1180.             index = inc(head, offset + index, es.length);
  1181.             E old = elementAt(es, index);
  1182.             delete(index);
  1183.             addToSize(-1);
  1184.             return old;
  1185.         }
  1186.         public boolean addAll(int index, Collection<? extends E> c) {
  1187.             if (index < 0 || index > size) throw new IndexOutOfBoundsException();
  1188.             Object[] a = c.toArray();
  1189.             if (a.length == 0) return false;
  1190.             checkArray(a);
  1191.             insert(index + offset, a, 0, a.length);
  1192.             addToSize(a.length);
  1193.             return true;
  1194.         }
  1195.         public java.util.List<E> subList(int from, int to) {
  1196.             sublistRangeCheck(size, from, to);
  1197.             return new SubList(this, from + offset, to - from);
  1198.         }
  1199.         public int size() {
  1200.             return size;
  1201.         }
  1202.         public boolean isEmpty() {
  1203.             return size == 0;
  1204.         }
  1205.         public void clear() {
  1206.             int s = size;
  1207.             if (s == 0) return;
  1208.             delete(offset, s);
  1209.             addToSize(-s);
  1210.         }
  1211.         public boolean contains(Object o) {
  1212.             int l = elements.length, s = inc(head, offset, l);
  1213.             return index(o, s, inc(s, size, l)) != -1;
  1214.         }
  1215.         public Object[] toArray() {
  1216.             int l = elements.length, s = inc(head, offset, l);
  1217.             return ArrayDeque.this.toArray(Object[].class, s, inc(s, size, l));
  1218.         }
  1219.         public <T> T[] toArray(T[] a) {
  1220.             int l = elements.length, s = inc(head, offset, l);
  1221.             return ArrayDeque.this.toArray(a, s, inc(s, size, l));
  1222.         }
  1223.         public boolean add(E e) {
  1224.             check(e);
  1225.             insert(inc(head, offset + size, elements.length), e);
  1226.             addToSize(1);
  1227.             return true;
  1228.         }
  1229.         public boolean remove(Object o) {
  1230.             int l = elements.length, s = inc(head, offset, l);
  1231.             int i = index(o, s, inc(s, size, l));
  1232.             if (i == -1) return false;
  1233.             delete(i);
  1234.             addToSize(-1);
  1235.             return true;
  1236.         }
  1237.         public boolean removeIf(Predicate<? super E> filter) {
  1238.             int l = elements.length, s = inc(head, offset, l);
  1239.             int r = ArrayDeque.this.bulkRemove(filter, s, inc(s, size, l));
  1240.             if (r == 0) return false;
  1241.             addToSize(-r);
  1242.             return true;
  1243.         }
  1244.         public boolean addAll(Collection<? extends E> c) {
  1245.             return addAll(size, c);
  1246.         }
  1247.         public boolean containsAll(Collection<?> c) {
  1248.             int l = elements.length, s = inc(head, offset, l), t = inc(s, size, l);
  1249.             for (Object e : c) {
  1250.                 if (index(e, s, t) == -1) return false;
  1251.             }
  1252.             return true;
  1253.         }
  1254.         public boolean removeAll(Collection<?> c) {
  1255.             return removeIf(c::contains);
  1256.         }
  1257.         public boolean retainAll(Collection<?> c) {
  1258.             Objects.requireNonNull(c);
  1259.             return removeIf(e -> !c.contains(e));
  1260.         }
  1261.         public void replaceAll(UnaryOperator<E> operator) {
  1262.             int l = elements.length, s = inc(head, offset, l);
  1263.             ArrayDeque.this.replaceAll(operator, s, inc(s, size, l));
  1264.         }
  1265.         public void forEach(Consumer<? super E> action) {
  1266.             int l = elements.length, s = inc(head, offset, l);
  1267.             ArrayDeque.this.forEach(action, s, inc(s, size, l));
  1268.         }
  1269.         public void sort(Comparator<? super E> c) {
  1270.             int l = elements.length, s = inc(head, offset, l);
  1271.             ArrayDeque.this.sort(c, s, inc(s, size, l));
  1272.         }
  1273.         public boolean equals(Object o) {
  1274.             if (o == this) return true;
  1275.             int l = elements.length, s = inc(head, offset, l);
  1276.             return ArrayDeque.this.equals(o, s, inc(s, size, l));
  1277.         }
  1278.         public int hashCode() {
  1279.             int l = elements.length, s = inc(head, offset, l);
  1280.             return ArrayDeque.this.hashCode(s, inc(s, size, l));
  1281.         }
  1282.         public int indexOf(Object o) {
  1283.             int l = elements.length, s = inc(head, offset, l);
  1284.             int i = index(o, s, inc(s, size, l));
  1285.             return i == -1 ? -1 : sub(i, s, l);
  1286.         }
  1287.         public int lastIndexOf(Object o) {
  1288.             int l = elements.length, s = inc(head, offset, l);
  1289.             int i = lastIndex(o, s, inc(s, size, l));
  1290.             return i == -1 ? -1 : sub(i, s, l);
  1291.         }
  1292.         public Spliterator<E> spliterator() {
  1293.             int l = elements.length, s = inc(head, offset, l);
  1294.             return new DeqSpliterator(s, inc(s, size, l));
  1295.         }
  1296.         public Iterator<E> iterator() {
  1297.             return new SubListIterator(0);
  1298.         }
  1299.         public ListIterator<E> listIterator() {
  1300.             return new SubListListIterator(0);
  1301.         }
  1302.         public ListIterator<E> listIterator(final int index) {
  1303.             if (index < 0 || index > size) throw new IndexOutOfBoundsException();
  1304.             return new SubListListIterator(index);
  1305.         }
  1306.         class SubListIterator implements Iterator<E> {
  1307.             final ArrayDeque<E> d = ArrayDeque.this;
  1308.             int cursor, lastRet = -1;
  1309.  
  1310.             SubListIterator(int index) {
  1311.                 cursor = index;
  1312.             }
  1313.             public boolean hasNext() {
  1314.                 return cursor < size;
  1315.             }
  1316.             public E next() {
  1317.                 int c = cursor;
  1318.                 if (c >= size) throw new NoSuchElementException();
  1319.                 cursor = c + 1;
  1320.                 Object[] es = d.elements;
  1321.                 return elementAt(es, lastRet = inc(d.head, offset + c, es.length));
  1322.             }
  1323.             public void remove() {
  1324.                 int l = lastRet;
  1325.                 if (l < 0) throw new IllegalStateException();
  1326.                 if (l != inc(d.head, offset + cursor, d.elements.length)) {
  1327.                     cursor--;
  1328.                 }
  1329.                 d.delete(l);
  1330.                 lastRet = -1;
  1331.                 addToSize(-1);
  1332.             }
  1333.             public void forEachRemaining(Consumer<? super E> action) {
  1334.                 Objects.requireNonNull(action);
  1335.                 Object[] a = d.elements;
  1336.                 int s = offset + d.head;
  1337.                 int l = a.length, i = inc(s, cursor, l), to = inc(s, size, l);
  1338.                 if (i == to) return;
  1339.                 for (int e = (i <= to) ? to : a.length; ; i = 0, e = to) {
  1340.                     for (; i < e; i++)
  1341.                         action.accept(elementAt(a, i));
  1342.                     if (e == to) break;
  1343.                 }
  1344.                 lastRet = dec(to, l);
  1345.                 cursor = size;
  1346.             }
  1347.         }
  1348.         class SubListListIterator extends SubListIterator implements ListIterator<E> {
  1349.  
  1350.             SubListListIterator(int index) {
  1351.                 super(index);
  1352.             }
  1353.             public boolean hasPrevious() {
  1354.                 return cursor > 0;
  1355.             }
  1356.             public E previous() {
  1357.                 int c = cursor;
  1358.                 if (c <= 0) throw new NoSuchElementException();
  1359.                 Object[] es = d.elements;
  1360.                 return elementAt(es, lastRet = inc(d.head, offset + (cursor = c - 1), es.length));
  1361.             }
  1362.             public int nextIndex() {
  1363.                 return cursor;
  1364.             }
  1365.             public int previousIndex() {
  1366.                 return cursor - 1;
  1367.             }
  1368.             public void set(E e) {
  1369.                 if (lastRet < 0) throw new IllegalStateException();
  1370.                 d.check(e);
  1371.                 d.elements[lastRet] = e;
  1372.             }
  1373.             public void add(E e) {
  1374.                 d.check(e);
  1375.                 d.insert(inc(d.head, offset + cursor++, d.elements.length), e);
  1376.                 addToSize(1);
  1377.                 lastRet = -1;
  1378.             }
  1379.         }
  1380.     }
  1381.  
  1382.     /**
  1383.      * Returns a list iterator over the elements in this ArrayDeque (in proper
  1384.      * sequence).
  1385.      *
  1386.      * @return a list iterator over the elements in this ArrayDeque (in proper
  1387.      *         sequence)
  1388.      */
  1389.     public ListIterator<E> listIterator() {
  1390.         return new DeqListIterator(head);
  1391.     }
  1392.  
  1393.     /**
  1394.      * Returns a list iterator over the elements in this ArrayDeque (in proper
  1395.      * sequence), starting at the specified position in the ArrayDeque.
  1396.      * The specified index indicates the first element that would be
  1397.      * returned by an initial call to {@link ListIterator#next next}.
  1398.      * An initial call to {@link ListIterator#previous previous} would
  1399.      * return the element with the specified index minus one.
  1400.      *
  1401.      * @param index index of the first element to be returned from the
  1402.      *        list iterator (by a call to {@link ListIterator#next next})
  1403.      * @return a list iterator over the elements in this ArrayDeque (in proper
  1404.      *         sequence), starting at the specified position in the ArrayDeque
  1405.      * @throws IndexOutOfBoundsException if the index is out of range
  1406.      *         ({@code index < 0 || index > size()})
  1407.      */
  1408.     public ListIterator<E> listIterator(final int index) {
  1409.         if (index < 0 || index > size()) throw new IndexOutOfBoundsException();
  1410.         return new DeqListIterator(inc(head, index, elements.length));
  1411.     }
  1412.  
  1413.     private class DeqListIterator implements ListIterator<E> {
  1414.         /**
  1415.          * Index of element to be returned by subsequent call to next.
  1416.          */
  1417.         int cursor;
  1418.         /**
  1419.          * Tail and head recorded at construction (also in remove),
  1420.          * to stop iterator and also to check for comodification.
  1421.          */
  1422.         int end = tail, start = head;
  1423.         /**
  1424.          * Index of element returned by most recent call to next.
  1425.          * Reset to -1 if element is deleted by a call to remove.
  1426.          */
  1427.         int lastRet = -1;
  1428.  
  1429.         DeqListIterator(int index) {
  1430.             cursor = index;
  1431.         }
  1432.         public boolean hasNext() {
  1433.             return cursor != end;
  1434.         }
  1435.         public boolean hasPrevious() {
  1436.             return cursor != start;
  1437.         }
  1438.         public int nextIndex() {
  1439.             return sub(cursor, start, elements.length);
  1440.         }
  1441.         public int previousIndex() {
  1442.             return nextIndex() - 1;
  1443.         }
  1444.         final void checkMod() {
  1445.             if (tail != end || head != start) throw new ConcurrentModificationException();
  1446.         }
  1447.         public E next() {
  1448.             int c = cursor;
  1449.             if (c == end) throw new NoSuchElementException();
  1450.             checkMod();
  1451.             Object[] a = elements;
  1452.             cursor = inc(c, a.length);
  1453.             return elementAt(a, lastRet = c);
  1454.         }
  1455.         public E previous() {
  1456.             int c = cursor;
  1457.             if (c == start) throw new NoSuchElementException();
  1458.             checkMod();
  1459.             Object[] a = elements;
  1460.             return elementAt(a, lastRet = cursor = dec(c, a.length));
  1461.         }
  1462.         public void forEachRemaining(Consumer<? super E> action) {
  1463.             Objects.requireNonNull(action);
  1464.             checkMod();
  1465.             int i = cursor, to = end;
  1466.             if (i == to) return;
  1467.             cursor = to;
  1468.             Object[] a = elements;
  1469.             for (int e = (i <= to) ? to : a.length; ; i = 0, e = to) {
  1470.                 for (; i < e; i++)
  1471.                     action.accept(elementAt(a, i));
  1472.                 if (e == to) break;
  1473.             }
  1474.             lastRet = dec(to, a.length);
  1475.         }
  1476.         public void remove() {
  1477.             int l = lastRet;
  1478.             if (l < 0) throw new IllegalStateException();
  1479.             checkMod();
  1480.             int c = cursor;
  1481.             boolean prev = l == c;
  1482.             if (delete(l)) {
  1483.                 if (!prev) cursor = dec(c, elements.length);
  1484.                 end = tail;
  1485.             } else {
  1486.                 if (prev) cursor = inc(c, elements.length);
  1487.                 start = head;
  1488.             }
  1489.             lastRet = -1;
  1490.         }
  1491.         public void set(E e) {
  1492.             int l = lastRet;
  1493.             if (l < 0) throw new IllegalStateException();
  1494.             checkMod();
  1495.             check(e);
  1496.             elements[l] = e;
  1497.         }
  1498.         public void add(E e) {
  1499.             checkMod();
  1500.             check(e);
  1501.             int c = cursor, l = elements.length;
  1502.             switch(insert(c, e)) {
  1503.             case 0:
  1504.                 cursor = sub(c, start, l) + 1;
  1505.                 start = 0;
  1506.                 end = tail;
  1507.                 break;
  1508.             case 1:
  1509.                 cursor = inc(c, l);
  1510.                 end = tail;
  1511.                 break;
  1512.             case -1:
  1513.                 start = head;
  1514.                 break;
  1515.             }
  1516.             lastRet = -1;
  1517.         }
  1518.     }
  1519.  
  1520.     // *** Collection Methods ***
  1521.  
  1522.     /**
  1523.      * Returns the number of elements in this deque.
  1524.      *
  1525.      * @return the number of elements in this deque
  1526.      */
  1527.     public int size() {
  1528.         return sub(tail, head, elements.length);
  1529.     }
  1530.  
  1531.     /**
  1532.      * Returns {@code true} if this deque contains no elements.
  1533.      *
  1534.      * @return {@code true} if this deque contains no elements
  1535.      */
  1536.     public boolean isEmpty() {
  1537.         return head == tail;
  1538.     }
  1539.  
  1540.     /**
  1541.      * Returns an iterator over the elements in this deque.  The elements
  1542.      * will be ordered from first (head) to last (tail).  This is the same
  1543.      * order that elements would be dequeued (via successive calls to
  1544.      * {@link #remove} or popped (via successive calls to {@link #pop}).
  1545.      *
  1546.      * @return an iterator over the elements in this deque
  1547.      */
  1548.     public Iterator<E> iterator() {
  1549.         return new DeqIterator();
  1550.     }
  1551.  
  1552.     public Iterator<E> descendingIterator() {
  1553.         return new DescendingIterator();
  1554.     }
  1555.  
  1556.     private class DeqIterator implements Iterator<E> {
  1557.         /** Index of element to be returned by subsequent call to next. */
  1558.         int cursor;
  1559.  
  1560.         /** Number of elements yet to be returned. */
  1561.         int remaining = size();
  1562.  
  1563.         /**
  1564.          * Index of element returned by most recent call to next.
  1565.          * Reset to -1 if element is deleted by a call to remove.
  1566.          */
  1567.         int lastRet = -1;
  1568.  
  1569.         DeqIterator() { cursor = head; }
  1570.  
  1571.         public final boolean hasNext() {
  1572.             return remaining > 0;
  1573.         }
  1574.  
  1575.         public E next() {
  1576.             if (remaining <= 0)
  1577.                 throw new NoSuchElementException();
  1578.             final Object[] es = elements;
  1579.             E e = validElementAt(es, cursor);
  1580.             cursor = inc(lastRet = cursor, es.length);
  1581.             remaining--;
  1582.             return e;
  1583.         }
  1584.  
  1585.         void postDelete(boolean leftShifted) {
  1586.             if (leftShifted)
  1587.                 cursor = dec(cursor, elements.length);
  1588.         }
  1589.  
  1590.         public final void remove() {
  1591.             if (lastRet < 0)
  1592.                 throw new IllegalStateException();
  1593.             postDelete(delete(lastRet));
  1594.             lastRet = -1;
  1595.         }
  1596.  
  1597.         public void forEachRemaining(Consumer<? super E> action) {
  1598.             Objects.requireNonNull(action);
  1599.             int r;
  1600.             if ((r = remaining) <= 0)
  1601.                 return;
  1602.             remaining = 0;
  1603.             final Object[] es = elements;
  1604.             if (!isValid(es[cursor]) || sub(tail, cursor, es.length) != r)
  1605.                 throw new ConcurrentModificationException();
  1606.             for (int i = cursor, end = tail, to = (i <= end) ? end : es.length;
  1607.                  ; i = 0, to = end) {
  1608.                 for (; i < to; i++)
  1609.                     action.accept(elementAt(es, i));
  1610.                 if (to == end) {
  1611.                     if (end != tail)
  1612.                         throw new ConcurrentModificationException();
  1613.                     lastRet = dec(end, es.length);
  1614.                     break;
  1615.                 }
  1616.             }
  1617.         }
  1618.     }
  1619.  
  1620.     private class DescendingIterator extends DeqIterator {
  1621.         DescendingIterator() { cursor = dec(tail, elements.length); }
  1622.  
  1623.         public final E next() {
  1624.             if (remaining <= 0)
  1625.                 throw new NoSuchElementException();
  1626.             final Object[] es = elements;
  1627.             E e = validElementAt(es, cursor);
  1628.             cursor = dec(lastRet = cursor, es.length);
  1629.             remaining--;
  1630.             return e;
  1631.         }
  1632.  
  1633.         void postDelete(boolean leftShifted) {
  1634.             if (!leftShifted)
  1635.                 cursor = inc(cursor, elements.length);
  1636.         }
  1637.  
  1638.         public final void forEachRemaining(Consumer<? super E> action) {
  1639.             Objects.requireNonNull(action);
  1640.             int r;
  1641.             if ((r = remaining) <= 0)
  1642.                 return;
  1643.             remaining = 0;
  1644.             final Object[] es = elements;
  1645.             if (!isValid(es[cursor]) || sub(cursor, head, es.length) + 1 != r)
  1646.                 throw new ConcurrentModificationException();
  1647.             for (int i = cursor, end = head, to = (i >= end) ? end : 0;
  1648.                  ; i = es.length - 1, to = end) {
  1649.                 // hotspot generates faster code than for: i >= to !
  1650.                 for (; i > to - 1; i--)
  1651.                     action.accept(elementAt(es, i));
  1652.                 if (to == end) {
  1653.                     if (end != head)
  1654.                         throw new ConcurrentModificationException();
  1655.                     lastRet = end;
  1656.                     break;
  1657.                 }
  1658.             }
  1659.         }
  1660.     }
  1661.  
  1662.     /**
  1663.      * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
  1664.      * and <em>fail-fast</em> {@link Spliterator} over the elements in this
  1665.      * deque.
  1666.      *
  1667.      * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
  1668.      * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
  1669.      * {@link Spliterator#NONNULL}.  Overriding implementations should document
  1670.      * the reporting of additional characteristic values.
  1671.      *
  1672.      * @return a {@code Spliterator} over the elements in this deque
  1673.      * @since 1.8
  1674.      */
  1675.     public Spliterator<E> spliterator() {
  1676.         return new DeqSpliterator();
  1677.     }
  1678.  
  1679.     final class DeqSpliterator implements Spliterator<E> {
  1680.         private int fence;      // -1 until first use
  1681.         private int cursor;     // current index, modified on traverse/split
  1682.  
  1683.         /** Constructs late-binding spliterator over all elements. */
  1684.         DeqSpliterator() {
  1685.             this.fence = -1;
  1686.         }
  1687.  
  1688.         /** Constructs spliterator over the given range. */
  1689.         DeqSpliterator(int origin, int fence) {
  1690.             // assert 0 <= origin && origin < elements.length;
  1691.             // assert 0 <= fence && fence < elements.length;
  1692.             this.cursor = origin;
  1693.             this.fence = fence;
  1694.         }
  1695.  
  1696.         /** Ensures late-binding initialization; then returns fence. */
  1697.         private int getFence() { // force initialization
  1698.             int t;
  1699.             if ((t = fence) < 0) {
  1700.                 t = fence = tail;
  1701.                 cursor = head;
  1702.             }
  1703.             return t;
  1704.         }
  1705.  
  1706.         public DeqSpliterator trySplit() {
  1707.             final Object[] es = elements;
  1708.             final int i, n;
  1709.             return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
  1710.                 ? null
  1711.                 : new DeqSpliterator(i, cursor = inc(i, n, es.length));
  1712.         }
  1713.  
  1714.         public void forEachRemaining(Consumer<? super E> action) {
  1715.             if (action == null)
  1716.                 throw new NullPointerException();
  1717.             final int end = getFence(), cursor = this.cursor;
  1718.             final Object[] es = elements;
  1719.             if (cursor != end) {
  1720.                 this.cursor = end;
  1721.                 // null check at both ends of range is sufficient
  1722.                 if (!isValid(es[cursor]) || !isValid(es[dec(end, es.length)]))
  1723.                     throw new ConcurrentModificationException();
  1724.                 for (int i = cursor, to = (i <= end) ? end : es.length;
  1725.                      ; i = 0, to = end) {
  1726.                     for (; i < to; i++)
  1727.                         action.accept(elementAt(es, i));
  1728.                     if (to == end) break;
  1729.                 }
  1730.             }
  1731.         }
  1732.  
  1733.         public boolean tryAdvance(Consumer<? super E> action) {
  1734.             Objects.requireNonNull(action);
  1735.             final Object[] es = elements;
  1736.             if (fence < 0) { fence = tail; cursor = head; } // late-binding
  1737.             final int i;
  1738.             if ((i = cursor) == fence)
  1739.                 return false;
  1740.             E e = validElementAt(es, i);
  1741.             cursor = inc(i, es.length);
  1742.             action.accept(e);
  1743.             return true;
  1744.         }
  1745.  
  1746.         public long estimateSize() {
  1747.             return sub(getFence(), cursor, elements.length);
  1748.         }
  1749.  
  1750.         public int characteristics() {
  1751.             return Spliterator.ORDERED
  1752.                 | Spliterator.SIZED
  1753.                 | Spliterator.SUBSIZED
  1754.                 | (isValid(null) ? 0 : Spliterator.NONNULL);
  1755.         }
  1756.     }
  1757.  
  1758.     /**
  1759.      * @throws NullPointerException {@inheritDoc}
  1760.      */
  1761.     public void forEach(Consumer<? super E> action) {
  1762.         forEach(action, head, tail);
  1763.     }
  1764.  
  1765.     void forEach(Consumer<? super E> action, int from, int to) {
  1766.         Objects.requireNonNull(action);
  1767.         final Object[] es = elements;
  1768.         for (int i = from, e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  1769.             for (; i < e; i++)
  1770.                 action.accept(elementAt(es, i));
  1771.             if (e == to) break;
  1772.         }
  1773.     }
  1774.  
  1775.     /**
  1776.      * @throws NullPointerException {@inheritDoc}
  1777.      */
  1778.     public boolean removeIf(Predicate<? super E> filter) {
  1779.         Objects.requireNonNull(filter);
  1780.         return bulkRemove(filter, head, tail) != 0;
  1781.     }
  1782.  
  1783.     /**
  1784.      * @throws NullPointerException {@inheritDoc}
  1785.      */
  1786.     public boolean removeAll(Collection<?> c) {
  1787.         Objects.requireNonNull(c);
  1788.         return bulkRemove(e -> c.contains(e), head, tail) != 0;
  1789.     }
  1790.  
  1791.     /**
  1792.      * @throws NullPointerException {@inheritDoc}
  1793.      */
  1794.     public boolean retainAll(Collection<?> c) {
  1795.         Objects.requireNonNull(c);
  1796.         return bulkRemove(e -> !c.contains(e), head, tail) != 0;
  1797.     }
  1798.  
  1799.     /** Implementation of bulk remove methods. */
  1800.     private int bulkRemove(Predicate<? super E> filter, int from, int to) {
  1801.         final Object[] es = elements;
  1802.         // Optimize for initial run of survivors
  1803.         for (int i = from, e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  1804.             for (; i < e; i++)
  1805.                 if (filter.test(elementAt(es, i)))
  1806.                     return bulkRemoveModified(filter, i, to);
  1807.             if (e == to) break;
  1808.         }
  1809.         return 0;
  1810.     }
  1811.  
  1812.     // A tiny bit set implementation
  1813.  
  1814.     private static long[] nBits(int n) {
  1815.         return new long[((n - 1) >> 6) + 1];
  1816.     }
  1817.     private static void setBit(long[] bits, int i) {
  1818.         bits[i >> 6] |= 1L << i;
  1819.     }
  1820.     private static boolean isClear(long[] bits, int i) {
  1821.         return (bits[i >> 6] & (1L << i)) == 0;
  1822.     }
  1823.  
  1824.     /**
  1825.      * Helper for bulkRemove, in case of at least one deletion.
  1826.      * Tolerate predicates that reentrantly access the collection for
  1827.      * read (but writers still get CME), so traverse once to find
  1828.      * elements to delete, a second pass to physically expunge.
  1829.      *
  1830.      * @param beg valid index of first element to be deleted
  1831.      */
  1832.     private int bulkRemoveModified(
  1833.         Predicate<? super E> filter, final int beg, final int end) {
  1834.         final Object[] es = elements;
  1835.         final int capacity = es.length;
  1836.         final long[] deathRow = nBits(sub(end, beg, capacity));
  1837.         deathRow[0] = 1L;   // set bit 0
  1838.         for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
  1839.              ; i = 0, to = end, k -= capacity) {
  1840.             for (; i < to; i++)
  1841.                 if (filter.test(elementAt(es, i)))
  1842.                     setBit(deathRow, i - k);
  1843.             if (to == end) break;
  1844.         }
  1845.         // a two-finger traversal, with hare i reading, tortoise w writing
  1846.         int w = beg;
  1847.         for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
  1848.              ; w = 0) { // w rejoins i on second leg
  1849.             // In this loop, i and w are on the same leg, with i > w
  1850.             for (; i < to; i++)
  1851.                 if (isClear(deathRow, i - k))
  1852.                     es[w++] = es[i];
  1853.             if (to == end) break;
  1854.             // In this loop, w is on the first leg, i on the second
  1855.             for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
  1856.                 if (isClear(deathRow, i - k))
  1857.                     es[w++] = es[i];
  1858.             if (i >= to) {
  1859.                 if (w == capacity) w = 0; // "corner" case
  1860.                 break;
  1861.             }
  1862.         }
  1863.         int r = sub(end, w, capacity);
  1864.         delete(sub(w, head, capacity), r);
  1865.         return r;
  1866.     }
  1867.  
  1868.     /**
  1869.      * Returns {@code true} if this deque contains the specified element.
  1870.      * More formally, returns {@code true} if and only if this deque contains
  1871.      * at least one element {@code e} such that {@code o.equals(e)}.
  1872.      *
  1873.      * @param o object to be checked for containment in this deque
  1874.      * @return {@code true} if this deque contains the specified element
  1875.      */
  1876.     public boolean contains(Object o) {
  1877.         return index(o, head, tail) != -1;
  1878.     }
  1879.  
  1880.     public boolean containsAll(Collection<?> c) {
  1881.         int h = head, t = tail;
  1882.         for (Object e : c) {
  1883.             if (index(e, h, t) == -1) return false;
  1884.         }
  1885.         return true;
  1886.     }
  1887.  
  1888.     /**
  1889.      * Removes a single instance of the specified element from this deque.
  1890.      * If the deque does not contain the element, it is unchanged.
  1891.      * More formally, removes the first element {@code e} such that
  1892.      * {@code o.equals(e)} (if such an element exists).
  1893.      * Returns {@code true} if this deque contained the specified element
  1894.      * (or equivalently, if this deque changed as a result of the call).
  1895.      *
  1896.      * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
  1897.      *
  1898.      * @param o element to be removed from this deque, if present
  1899.      * @return {@code true} if this deque contained the specified element
  1900.      */
  1901.     public boolean remove(Object o) {
  1902.         return removeFirstOccurrence(o);
  1903.     }
  1904.  
  1905.     /**
  1906.      * Removes all of the elements from this deque.
  1907.      * The deque will be empty after this call returns.
  1908.      */
  1909.     public void clear() {
  1910.         circularClear(elements, head, tail);
  1911.         head = tail = 0;
  1912.     }
  1913.  
  1914.     /**
  1915.      * Nulls out slots starting at array index i, upto index end.
  1916.      * Condition i == end means "empty" - nothing to do.
  1917.      */
  1918.     private static void circularClear(Object[] es, int i, int end) {
  1919.         // assert 0 <= i && i < es.length;
  1920.         // assert 0 <= end && end < es.length;
  1921.         for (int to = (i <= end) ? end : es.length;
  1922.              ; i = 0, to = end) {
  1923.             for (; i < to; i++) es[i] = null;
  1924.             if (to == end) break;
  1925.         }
  1926.     }
  1927.  
  1928.     /**
  1929.      * Returns an array containing all of the elements in this deque
  1930.      * in proper sequence (from first to last element).
  1931.      *
  1932.      * <p>The returned array will be "safe" in that no references to it are
  1933.      * maintained by this deque.  (In other words, this method must allocate
  1934.      * a new array).  The caller is thus free to modify the returned array.
  1935.      *
  1936.      * <p>This method acts as bridge between array-based and collection-based
  1937.      * APIs.
  1938.      *
  1939.      * @return an array containing all of the elements in this deque
  1940.      */
  1941.     public Object[] toArray() {
  1942.         return toArray(Object[].class, head, tail);
  1943.     }
  1944.  
  1945.     private <T> T[] toArray(Class<T[]> klazz, int head, int tail) {
  1946.         final Object[] es = elements;
  1947.         final T[] a;
  1948.         final int end;
  1949.         if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) {
  1950.             // Uses null extension feature of copyOfRange
  1951.             a = Arrays.copyOfRange(es, head, end, klazz);
  1952.         } else {
  1953.             // integer overflow!
  1954.             a = Arrays.copyOfRange(es, 0, end - head, klazz);
  1955.             System.arraycopy(es, head, a, 0, es.length - head);
  1956.         }
  1957.         if (end != tail)
  1958.             System.arraycopy(es, 0, a, es.length - head, tail);
  1959.         return a;
  1960.     }
  1961.  
  1962.     /**
  1963.      * Returns an array containing all of the elements in this deque in
  1964.      * proper sequence (from first to last element); the runtime type of the
  1965.      * returned array is that of the specified array.  If the deque fits in
  1966.      * the specified array, it is returned therein.  Otherwise, a new array
  1967.      * is allocated with the runtime type of the specified array and the
  1968.      * size of this deque.
  1969.      *
  1970.      * <p>If this deque fits in the specified array with room to spare
  1971.      * (i.e., the array has more elements than this deque), the element in
  1972.      * the array immediately following the end of the deque is set to
  1973.      * {@code null}.
  1974.      *
  1975.      * <p>Like the {@link #toArray()} method, this method acts as bridge between
  1976.      * array-based and collection-based APIs.  Further, this method allows
  1977.      * precise control over the runtime type of the output array, and may,
  1978.      * under certain circumstances, be used to save allocation costs.
  1979.      *
  1980.      * <p>Suppose {@code x} is a deque known to contain only strings.
  1981.      * The following code can be used to dump the deque into a newly
  1982.      * allocated array of {@code String}:
  1983.      *
  1984.      * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
  1985.      *
  1986.      * Note that {@code toArray(new Object[0])} is identical in function to
  1987.      * {@code toArray()}.
  1988.      *
  1989.      * @param a the array into which the elements of the deque are to
  1990.      *          be stored, if it is big enough; otherwise, a new array of the
  1991.      *          same runtime type is allocated for this purpose
  1992.      * @return an array containing all of the elements in this deque
  1993.      * @throws ArrayStoreException if the runtime type of the specified array
  1994.      *         is not a supertype of the runtime type of every element in
  1995.      *         this deque
  1996.      * @throws NullPointerException if the specified array is null
  1997.      */
  1998.     public <T> T[] toArray(T[] a) {
  1999.         return toArray(a, head, tail);
  2000.     }
  2001.  
  2002.     @SuppressWarnings("unchecked")
  2003.     <T> T[] toArray(T[] a, int head, int tail) {
  2004.         final Object[] es = elements;
  2005.         final int size = sub(tail, head, es.length);
  2006.         if (size > a.length)
  2007.             return toArray((Class<T[]>) a.getClass(), head, tail);
  2008.         for (int i = head, j = 0, len = Math.min(size, es.length - i);
  2009.              ; i = 0, len = tail) {
  2010.             System.arraycopy(es, i, a, j, len);
  2011.             if ((j += len) == size) break;
  2012.         }
  2013.         if (size < a.length)
  2014.             a[size] = null;
  2015.         return a;
  2016.     }
  2017.  
  2018.     // *** Object methods ***
  2019.  
  2020.     boolean equals(Object o, int from, int to) {
  2021.         if (!(o instanceof java.util.List)) return false;
  2022.         java.util.List<?> l = (java.util.List<?>)o;
  2023.         final Object[] es = elements;
  2024.         int i = from, s = l.size();
  2025.         if (s != sub(to, from, es.length)) return false;
  2026.         if (s == 0) return true;
  2027.         if (o instanceof RandomAccess) {
  2028.             for (int j = 0, e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  2029.                 for (; i < e; i++) {
  2030.                     Object t = es[i];
  2031.                     Object c = l.get(j++);
  2032.                     if (!(t == null ? c == null : t.equals(c))) return false;
  2033.                 }
  2034.                 if (e == to) break;
  2035.             }
  2036.             return true;
  2037.         }
  2038.         Iterator<?> it = l.iterator();
  2039.         for (int e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  2040.             for (; i < e; i++) {
  2041.                 if (!it.hasNext()) return false;
  2042.                 Object t = es[i];
  2043.                 Object c = it.next();
  2044.                 if (!(t == null ? c == null : t.equals(c))) return false;
  2045.             }
  2046.             if (e == to) break;
  2047.         }
  2048.         return !it.hasNext();
  2049.     }
  2050.  
  2051.     int hashCode(int from, int to) {
  2052.         int hashCode = 1;
  2053.         final Object[] es = elements;
  2054.         for (int i = from, e = (i <= to) ? to : es.length; ; i = 0, e = to) {
  2055.             for (; i < e; i++) {
  2056.                 Object o = es[i];
  2057.                 hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode());
  2058.             }
  2059.             if (e == to) break;
  2060.         }
  2061.         return hashCode;
  2062.     }
  2063.  
  2064.     /**
  2065.      * Returns a copy of this deque.
  2066.      *
  2067.      * @return a copy of this deque
  2068.      */
  2069.     public ArrayDeque<E> clone() {
  2070.         try {
  2071.             @SuppressWarnings("unchecked")
  2072.             ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
  2073.             final Object[] es = elements;
  2074.             result.elements = es.length == 1 ? es : es.clone();
  2075.             return result;
  2076.         } catch (CloneNotSupportedException e) {
  2077.             throw new AssertionError();
  2078.         }
  2079.     }
  2080.  
  2081.     private static final long serialVersionUID = 2340985798034038923L;
  2082.  
  2083.     /**
  2084.      * Saves this deque to a stream (that is, serializes it).
  2085.      *
  2086.      * @param s the stream
  2087.      * @throws java.io.IOException if an I/O error occurs
  2088.      * @serialData The current size ({@code int}) of the deque,
  2089.      * followed by all of its elements (each an object reference) in
  2090.      * first-to-last order.
  2091.      */
  2092.     private void writeObject(java.io.ObjectOutputStream s)
  2093.             throws java.io.IOException {
  2094.         s.defaultWriteObject();
  2095.  
  2096.         // Write out size
  2097.         s.writeInt(size());
  2098.  
  2099.         // Write out elements in order.
  2100.         final Object[] es = elements;
  2101.         for (int i = head, end = tail, to = (i <= end) ? end : es.length;
  2102.              ; i = 0, to = end) {
  2103.             for (; i < to; i++)
  2104.                 s.writeObject(es[i]);
  2105.             if (to == end) break;
  2106.         }
  2107.     }
  2108.  
  2109.     /**
  2110.      * Reconstitutes this deque from a stream (that is, deserializes it).
  2111.      * @param s the stream
  2112.      * @throws ClassNotFoundException if the class of a serialized object
  2113.      *         could not be found
  2114.      * @throws java.io.IOException if an I/O error occurs
  2115.      */
  2116.     private void readObject(java.io.ObjectInputStream s)
  2117.             throws java.io.IOException, ClassNotFoundException {
  2118.         s.defaultReadObject();
  2119.  
  2120.         // Read in size and allocate array
  2121.         int size = s.readInt();
  2122.         SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1);
  2123.         elements = size == 0 ? EMPTY : new Object[size + 1];
  2124.         this.tail = size;
  2125.  
  2126.         // Read in all elements in the proper order.
  2127.         for (int i = 0; i < size; i++)
  2128.             elements[i] = s.readObject();
  2129.     }
  2130.  
  2131.     /** debugging */
  2132.     void checkInvariants() {
  2133.         // Use head and tail fields with empty slot at tail strategy.
  2134.         // head == tail disambiguates to "empty".
  2135.         try {
  2136.             int capacity = elements.length;
  2137.             // assert 0 <= head && head < capacity;
  2138.             // assert 0 <= tail && tail < capacity;
  2139.             // assert capacity > 0;
  2140.             // assert size() < capacity;
  2141.             // assert head == tail || elements[head] != null;
  2142.             // assert elements[tail] == null;
  2143.             // assert head == tail || elements[dec(tail, capacity)] != null;
  2144.         } catch (Throwable t) {
  2145.             System.err.printf("head=%d tail=%d capacity=%d%n",
  2146.                               head, tail, elements.length);
  2147.             System.err.printf("elements=%s%n",
  2148.                               Arrays.toString(elements));
  2149.             throw t;
  2150.         }
  2151.     }
  2152.  
  2153.     // *** Element insertion and deletion  ***
  2154.  
  2155.     /**
  2156.      * Removes the element at the specified position in the elements array.
  2157.      * This can result in forward or backwards motion of array elements.
  2158.      * We optimize for least element motion.
  2159.      *
  2160.      * <p>This method is called delete rather than remove to emphasize
  2161.      * that its semantics differ from those of {@link java.util.List#remove(int)}.
  2162.      *
  2163.      * @return true if elements near tail moved backwards
  2164.      */
  2165.     boolean delete(int i) {
  2166.         final Object[] es = elements;
  2167.         final int capacity = es.length;
  2168.         final int h, t;
  2169.         // number of elements before to-be-deleted elt
  2170.         final int front = sub(i, h = head, capacity);
  2171.         // number of elements after to-be-deleted elt
  2172.         final int back = sub(t = tail, i, capacity) - 1;
  2173.         if (front < back) {
  2174.             // move front elements forwards
  2175.             if (h <= i) {
  2176.                 System.arraycopy(es, h, es, h + 1, front);
  2177.             } else { // Wrap around
  2178.                 System.arraycopy(es, 0, es, 1, i);
  2179.                 es[0] = es[capacity - 1];
  2180.                 System.arraycopy(es, h, es, h + 1, front - (i + 1));
  2181.             }
  2182.             es[h] = null;
  2183.             head = inc(h, capacity);
  2184.             return false;
  2185.         } else {
  2186.             // move back elements backwards
  2187.             tail = dec(t, capacity);
  2188.             if (i <= tail) {
  2189.                 System.arraycopy(es, i + 1, es, i, back);
  2190.             } else { // Wrap around
  2191.                 System.arraycopy(es, i + 1, es, i, capacity - (i + 1));
  2192.                 es[capacity - 1] = es[0];
  2193.                 System.arraycopy(es, 1, es, 0, t - 1);
  2194.             }
  2195.             es[tail] = null;
  2196.             return true;
  2197.         }
  2198.     }
  2199.  
  2200.     /**
  2201.      * Inserts the element at the specified position in the elements array.
  2202.      * May either shift the element at the specified index and all after forward,
  2203.      * of shift all elements before the index backwards and insert the element
  2204.      * before the index, or resize the elements array.
  2205.      *
  2206.      * @return 1 if elements moved forward (and tail was incremented), -1 if
  2207.      * elements moved backward, 0 if the elements array was resized
  2208.      */
  2209.     int insert(int i, E e) {
  2210.         Object[] es = elements;
  2211.         final int capacity = es.length;
  2212.         int h = head;
  2213.         final int t = tail;
  2214.         final int front = sub(i, h, capacity);
  2215.         final int back  = sub(t, i, capacity);
  2216.  
  2217.         if (front + back + 1 == capacity) {
  2218.             Object[] a = elements = new Object[newCapacity(1)];
  2219.             if (h <= t) {
  2220.                 System.arraycopy(es, h, a, 0, front);
  2221.                 a[front] = e;
  2222.                 System.arraycopy(es, h + front, a, front + 1, back);
  2223.             } else {
  2224.                 if (h <= i) {
  2225.                     System.arraycopy(es, h, a, 0, front);
  2226.                     a[front] = e;
  2227.                     System.arraycopy(es, i, a, front + 1, capacity - i);
  2228.                     System.arraycopy(es, 0, a, front + 1 + capacity - i, t);
  2229.                 } else {
  2230.                     System.arraycopy(es, h, a, 0, capacity - h);
  2231.                     System.arraycopy(es, 0, a, capacity - h, i);
  2232.                     a[front] = e;
  2233.                     System.arraycopy(es, i, a, front + 1, back);
  2234.                 }
  2235.             }
  2236.             head = 0;
  2237.             tail = capacity;
  2238.             return 0;
  2239.         }
  2240.         // Optimize for least element motion
  2241.         if (front < back) {
  2242.             i = dec(i, capacity);
  2243.             h = dec(h, capacity);
  2244.             if (h <= i) {
  2245.                 System.arraycopy(es, h + 1, es, h, front);
  2246.             } else { // Wrap around
  2247.                 System.arraycopy(es, h + 1, es, h, capacity - 1 - h);
  2248.                 es[capacity - 1] = es[0];
  2249.                 System.arraycopy(es, 1, es, 0, i);
  2250.             }
  2251.             es[i] = e;
  2252.             head = h;
  2253.             return -1;
  2254.         } else {
  2255.             if (i <= t) {
  2256.                 System.arraycopy(es, i, es, i + 1, back);
  2257.             } else { // Wrap around
  2258.                 System.arraycopy(es, 0, es, 1, t);
  2259.                 es[0] = es[capacity - 1];
  2260.                 System.arraycopy(es, i, es, i + 1, capacity - 1 - i);
  2261.             }
  2262.             es[i] = e;
  2263.             tail = inc(t, capacity);
  2264.             return 1;
  2265.         }
  2266.     }
  2267.  
  2268.     /**
  2269.      * Removes length elements from the elements array starting at index + head (inclusive).
  2270.      *
  2271.      * @return true if elements moved backward (and tail was decreased)
  2272.      */
  2273.     boolean delete(int index, int length) {
  2274.         int size = size();
  2275.         int ahead = size - index - length;
  2276.         Object[] a = elements;
  2277.         int capacity = a.length;
  2278.         int i = inc(head, index, capacity);
  2279.         int e = inc(i, length, capacity);
  2280.         if (index <= ahead) {
  2281.             if (head <= i) {
  2282.                 if (i <= e) {
  2283.                     System.arraycopy(a, head, a, e - index, index);
  2284.                     Arrays.fill(a, head, head += length, null);
  2285.                 } else {
  2286.                     if (e >= index) {
  2287.                         System.arraycopy(a, head, a, e - index, index);
  2288.                         Arrays.fill(a, head, a.length, null);
  2289.                         Arrays.fill(a, 0, head = e - index, null);
  2290.                     } else {
  2291.                         System.arraycopy(a, i - e, a, 0, e);
  2292.                         System.arraycopy(a, head, a, head + length, index - e);
  2293.                         Arrays.fill(a, head, head += length, null);
  2294.                     }
  2295.                 }
  2296.             } else {
  2297.                 System.arraycopy(a, 0, a, length, i);
  2298.                 int r = a.length - head;
  2299.                 if (length >= r) {
  2300.                     System.arraycopy(a, head, a, length - r, r);
  2301.                     Arrays.fill(a, 0, length - r, null);
  2302.                     Arrays.fill(a, head, a.length, null);
  2303.                     head = inc(head, length, capacity);
  2304.                 } else {
  2305.                     System.arraycopy(a, a.length - length, a, 0, length);
  2306.                     System.arraycopy(a, head, a, head + length, r - length);
  2307.                     Arrays.fill(a, head, head += length, null);
  2308.                 }
  2309.             }
  2310.             return false;
  2311.         } else {
  2312.             if (tail >= e) {
  2313.                 if (i <= e) {
  2314.                     System.arraycopy(a, e, a, i, ahead);
  2315.                     Arrays.fill(a, tail - length, tail, null);
  2316.                     tail -= length;
  2317.                 } else {
  2318.                     if (a.length >= i + ahead) {
  2319.                         System.arraycopy(a, e, a, i, ahead);
  2320.                         Arrays.fill(a, i + ahead, a.length, null);
  2321.                         Arrays.fill(a, 0, tail, null);
  2322.                         tail = inc(i, ahead, capacity);
  2323.                     } else {
  2324.                         int r = a.length - i;
  2325.                         System.arraycopy(a, e, a, i, r);
  2326.                         System.arraycopy(a, e + r, a, 0, ahead - r);
  2327.                         Arrays.fill(a, ahead - r, tail, null);
  2328.                         tail = ahead - r;
  2329.                     }
  2330.                 }
  2331.             } else {
  2332.                 int f = a.length - e;
  2333.                 System.arraycopy(a, e, a, i, f);
  2334.                 if (length >= tail) {
  2335.                     System.arraycopy(a, 0, a, i + f, tail);
  2336.                     Arrays.fill(a, 0, tail, null);
  2337.                     Arrays.fill(a, i + f + tail, a.length, null);
  2338.                     tail = inc(tail, i + f, capacity);
  2339.                 } else {
  2340.                     System.arraycopy(a, 0, a, i + f, length);
  2341.                     System.arraycopy(a, length, a, 0, tail - length);
  2342.                     Arrays.fill(a, tail - length, tail, null);
  2343.                     tail -= length;
  2344.                 }
  2345.             }
  2346.             return true;
  2347.         }
  2348.     }
  2349.  
  2350.     /**
  2351.      * Inserts length elements from a starting at offset (inclusive) into the
  2352.      * elements array at index + head. May shift elements forwards or backwards
  2353.      * or resize the elements array.
  2354.      */
  2355.     void insert(int index, Object[] a, int offset, int length) {
  2356.         int size = size();
  2357.         int newSize = size + length;
  2358.         if (newSize + 1 < 0) {
  2359.             throw new IllegalStateException("Sorry, deque too big");
  2360.         }
  2361.         Object[] es = elements;
  2362.         if (newSize >= es.length) {
  2363.             Object[] n = elements = new Object[newCapacity(newSize + 1 - es.length)];
  2364.             if (tail >= head) {
  2365.                 System.arraycopy(es, head, n, 0, index);
  2366.                 System.arraycopy(a, offset, n, index, length);
  2367.                 System.arraycopy(es, head + index, n, index + length, size - index);
  2368.             } else {
  2369.                 int r = es.length - head;
  2370.                 if (r < index) {
  2371.                     System.arraycopy(es, head, n, 0, r);
  2372.                     System.arraycopy(es, 0, n, r, index - r);
  2373.                     System.arraycopy(a, offset, n, index, length);
  2374.                     System.arraycopy(es, index - r, n, index + length, size - index);
  2375.                 } else {
  2376.                     System.arraycopy(es, head, n, 0, index);
  2377.                     System.arraycopy(a, offset, n, index, length);
  2378.                     System.arraycopy(es, head + index, n, index + length, r - index);
  2379.                     System.arraycopy(es, 0, n, r + length, tail);
  2380.                 }
  2381.             }
  2382.             head = 0;
  2383.             tail = newSize;
  2384.             return;
  2385.         }
  2386.         int back = size - index;
  2387.         int capacity = es.length;
  2388.         if (index <= back) {
  2389.             int h = head - length;
  2390.             if (h < 0) {
  2391.                 int nh = h + capacity;
  2392.                 if (index >= -h) {
  2393.                     System.arraycopy(es, head, es, nh, -h);
  2394.                     System.arraycopy(es, head - h, es, 0, index + h);
  2395.                     System.arraycopy(a, offset, es, h + index, length);
  2396.                 } else {
  2397.                     System.arraycopy(es, head, es, nh, index);
  2398.                     int f = -h - index;
  2399.                     System.arraycopy(a, offset, es, nh + index, f);
  2400.                     System.arraycopy(a, offset + f, es, 0, length - f);
  2401.                 }
  2402.                 h = nh;
  2403.             } else {
  2404.                 int i = head + index - es.length;
  2405.                 if (i <= 0) {
  2406.                     System.arraycopy(es, head, es, h, index);
  2407.                     System.arraycopy(a, offset, es, h + index, length);
  2408.                 } else {
  2409.                     int r = index - i;
  2410.                     System.arraycopy(es, head, es, h, r);
  2411.                     int ni = i - length;
  2412.                     if (ni >= 0) {
  2413.                         System.arraycopy(es, 0, es, h + r, length);
  2414.                         System.arraycopy(es, length, es, 0, ni);
  2415.                         System.arraycopy(a, offset, es, ni, length);
  2416.                     } else {
  2417.                         System.arraycopy(es, 0, es, h + r, i);
  2418.                         System.arraycopy(a, offset, es, h + index, -ni);
  2419.                         System.arraycopy(a, offset - ni, es, 0, length + ni);
  2420.                     }
  2421.                 }
  2422.             }
  2423.             head = h;
  2424.         } else {
  2425.             int t = tail + length;
  2426.             if (t - capacity >= 0) {
  2427.                 t -= capacity;
  2428.                 if (t >= back) {
  2429.                     int f = t - back;
  2430.                     System.arraycopy(es, tail - back, es, f, back);
  2431.                     System.arraycopy(a, offset, es, tail - back, length - f);
  2432.                     System.arraycopy(a, offset + length - f, es, 0, f);
  2433.                 } else {
  2434.                     System.arraycopy(es, tail - t, es, 0, t);
  2435.                     System.arraycopy(es, tail - back, es, es.length - back + t, back - t);
  2436.                     System.arraycopy(a, offset, es, tail - back, length);
  2437.                 }
  2438.             } else {
  2439.                 int i = tail - back;
  2440.                 if (i >= 0) {
  2441.                     System.arraycopy(es, i, es, i + length, back);
  2442.                     System.arraycopy(a, offset, es, i, length);
  2443.                 } else {
  2444.                     System.arraycopy(es, 0, es, t - tail, tail);
  2445.                     int ni = i + length;
  2446.                     int mi = i + capacity;
  2447.                     if (ni >= 0) {
  2448.                         System.arraycopy(es, mi, es, ni, -i);
  2449.                         System.arraycopy(a, offset, es, mi, -i);
  2450.                         System.arraycopy(a, offset - i, es, 0, ni);
  2451.                     } else {
  2452.                         System.arraycopy(es, mi - ni, es, 0, -i + ni);
  2453.                         System.arraycopy(es, mi, es, ni + capacity, -ni);
  2454.                         System.arraycopy(a, offset, es, mi, length);
  2455.                     }
  2456.                 }
  2457.             }
  2458.             tail = t;
  2459.         }
  2460.     }
  2461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement