jdk/src/java.base/share/classes/java/util/ArrayDeque.java
author dl
Mon, 28 Nov 2016 23:36:11 -0800
changeset 42319 0193886267c3
parent 40817 4f5fb115676d
child 42927 1d31e540bfcb
permissions -rw-r--r--
8143577: optimize ArrayList.removeIf 8169679: ArrayList.subList().iterator().forEachRemaining() off-by-one-error 8167202: ArrayDeque improvements 8164793: new ArrayDeque(2**N) allocates backing array of size 2**(N+1) 8169739: LinkedBlockingDeque spliterator needs to support node self-linking 8169738: CopyOnWriteArrayList subList needs more synchronization Reviewed-by: martin, smarks, psandoz, forax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Josh Bloch of Google Inc. and released to the public domain,
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    32
 * as explained at http://creativecommons.org/publicdomain/zero/1.0/.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package java.util;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    36
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    37
import java.io.Serializable;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    38
import java.util.function.Consumer;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    39
import java.util.function.Predicate;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    40
import java.util.function.UnaryOperator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Resizable-array implementation of the {@link Deque} interface.  Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * deques have no capacity restrictions; they grow as necessary to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * usage.  They are not thread-safe; in the absence of external
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * synchronization, they do not support concurrent access by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Null elements are prohibited.  This class is likely to be faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@link Stack} when used as a stack, and faster than {@link LinkedList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * when used as a queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    51
 * <p>Most {@code ArrayDeque} operations run in amortized constant time.
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    52
 * Exceptions include
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    53
 * {@link #remove(Object) remove},
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    54
 * {@link #removeFirstOccurrence removeFirstOccurrence},
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    55
 * {@link #removeLastOccurrence removeLastOccurrence},
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    56
 * {@link #contains contains},
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    57
 * {@link #iterator iterator.remove()},
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    58
 * and the bulk operations, all of which run in linear time.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    60
 * <p>The iterators returned by this class's {@link #iterator() iterator}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    61
 * method are <em>fail-fast</em>: If the deque is modified at any time after
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    62
 * the iterator is created, in any way except through the iterator's own
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    63
 * {@code remove} method, the iterator will generally throw a {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * arbitrary, non-deterministic behavior at an undetermined time in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    72
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @author  Josh Bloch and Doug Lea
40817
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 32991
diff changeset
    86
 * @param <E> the type of elements held in this deque
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
public class ArrayDeque<E> extends AbstractCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                           implements Deque<E>, Cloneable, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
{
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    92
    /*
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    93
     * VMs excel at optimizing simple array loops where indices are
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    94
     * incrementing or decrementing over a valid slice, e.g.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    95
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    96
     * for (int i = start; i < end; i++) ... elements[i]
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    97
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    98
     * Because in a circular array, elements are in general stored in
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
    99
     * two disjoint such slices, we help the VM by writing unusual
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   100
     * nested loops for all traversals over the elements.  Having only
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   101
     * one hot inner loop body instead of two or three eases human
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   102
     * maintenance and encourages VM loop inlining into the caller.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   103
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The array in which the elements of the deque are stored.
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   107
     * All array cells not holding deque elements are always null.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   108
     * The array always has at least one null slot (at tail).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   110
    transient Object[] elements;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The index of the element at the head of the deque (which is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * element that would be removed by remove() or pop()); or an
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   115
     * arbitrary number 0 <= head < elements.length equal to tail if
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   116
     * the deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   118
    transient int head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The index at which the next element would be added to the tail
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   122
     * of the deque (via addLast(E), add(E), or push(E));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   123
     * elements[tail] is always null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   125
    transient int tail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   128
     * The maximum size of array to allocate.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   129
     * Some VMs reserve some header words in an array.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   130
     * Attempts to allocate larger arrays may result in
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   131
     * OutOfMemoryError: Requested array size exceeds VM limit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   133
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   136
     * Increases the capacity of this deque by at least the given amount.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   138
     * @param needed the required minimum extra capacity; must be positive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   140
    private void grow(int needed) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   141
        // overflow-conscious code
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   142
        final int oldCapacity = elements.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   143
        int newCapacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   144
        // Double capacity if small; else grow by 50%
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   145
        int jump = (oldCapacity < 64) ? (oldCapacity + 2) : (oldCapacity >> 1);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   146
        if (jump < needed
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   147
            || (newCapacity = (oldCapacity + jump)) - MAX_ARRAY_SIZE > 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   148
            newCapacity = newCapacity(needed, jump);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   149
        elements = Arrays.copyOf(elements, newCapacity);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   150
        // Exceptionally, here tail == head needs to be disambiguated
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   151
        if (tail < head || (tail == head && elements[head] != null)) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   152
            // wrap around; slide first leg forward to end of array
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   153
            int newSpace = newCapacity - oldCapacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   154
            System.arraycopy(elements, head,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   155
                             elements, head + newSpace,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   156
                             oldCapacity - head);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   157
            Arrays.fill(elements, head, head + newSpace, null);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   158
            head += newSpace;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   162
    /** Capacity calculation for edge conditions, especially overflow. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   163
    private int newCapacity(int needed, int jump) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   164
        final int oldCapacity = elements.length, minCapacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   165
        if ((minCapacity = oldCapacity + needed) - MAX_ARRAY_SIZE > 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   166
            if (minCapacity < 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   167
                throw new IllegalStateException("Sorry, deque too big");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   168
            return Integer.MAX_VALUE;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   169
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   170
        if (needed > jump)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   171
            return minCapacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   172
        return (oldCapacity + jump - MAX_ARRAY_SIZE < 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   173
            ? oldCapacity + jump
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   174
            : MAX_ARRAY_SIZE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Constructs an empty array deque with an initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * sufficient to hold 16 elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public ArrayDeque() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   182
        elements = new Object[16];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Constructs an empty array deque with an initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * sufficient to hold the specified number of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   189
     * @param numElements lower bound on initial capacity of the deque
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public ArrayDeque(int numElements) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   192
        elements =
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   193
            new Object[(numElements < 1) ? 1 :
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   194
                       (numElements == Integer.MAX_VALUE) ? Integer.MAX_VALUE :
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   195
                       numElements + 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Constructs a deque containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * iterator.  (The first element returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * iterator becomes the first element, or <i>front</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * deque.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param c the collection whose elements are to be placed into the deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public ArrayDeque(Collection<? extends E> c) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   209
        this(c.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   213
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   214
     * Increments i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   215
     * Precondition and postcondition: 0 <= i < modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   216
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   217
    static final int inc(int i, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   218
        if (++i >= modulus) i = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   219
        return i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   220
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   221
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   222
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   223
     * Decrements i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   224
     * Precondition and postcondition: 0 <= i < modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   225
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   226
    static final int dec(int i, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   227
        if (--i < 0) i = modulus - 1;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   228
        return i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   229
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   230
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   231
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   232
     * Circularly adds the given distance to index i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   233
     * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   234
     * @return index 0 <= i < modulus
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   235
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   236
    static final int add(int i, int distance, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   237
        if ((i += distance) - modulus >= 0) i -= modulus;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   238
        return i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   239
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   240
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   241
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   242
     * Subtracts j from i, mod modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   243
     * Index i must be logically ahead of index j.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   244
     * Precondition: 0 <= i < modulus, 0 <= j < modulus.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   245
     * @return the "circular distance" from j to i; corner case i == j
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   246
     * is diambiguated to "empty", returning 0.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   247
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   248
    static final int sub(int i, int j, int modulus) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   249
        if ((i -= j) < 0) i += modulus;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   250
        return i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   251
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   252
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   253
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   254
     * Returns element at array index i.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   255
     * This is a slight abuse of generics, accepted by javac.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   256
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   257
    @SuppressWarnings("unchecked")
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   258
    static final <E> E elementAt(Object[] es, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   259
        return (E) es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   260
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   261
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   262
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   263
     * A version of elementAt that checks for null elements.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   264
     * This check doesn't catch all possible comodifications,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   265
     * but does catch ones that corrupt traversal.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   266
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   267
    static final <E> E nonNullElementAt(Object[] es, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   268
        @SuppressWarnings("unchecked") E e = (E) es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   269
        if (e == null)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   270
            throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   271
        return e;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   272
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   273
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // The main insertion and extraction methods are addFirst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // addLast, pollFirst, pollLast. The other methods are defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // terms of these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Inserts the specified element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public void addFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new NullPointerException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   287
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   288
        es[head = dec(head, es.length)] = e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (head == tail)
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   290
            grow(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p>This method is equivalent to {@link #add}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public void addLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new NullPointerException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   304
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   305
        es[tail] = e;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   306
        if (head == (tail = inc(tail, es.length)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   307
            grow(1);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   308
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   309
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   310
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   311
     * Adds all of the elements in the specified collection at the end
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   312
     * of this deque, as if by calling {@link #addLast} on each one,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   313
     * in the order that they are returned by the collection's
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   314
     * iterator.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   315
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   316
     * @param c the elements to be inserted into this deque
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   317
     * @return {@code true} if this deque changed as a result of the call
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   318
     * @throws NullPointerException if the specified collection or any
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   319
     *         of its elements are null
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   320
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   321
    public boolean addAll(Collection<? extends E> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   322
        final int s, needed;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   323
        if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   324
            grow(needed);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   325
        c.forEach(this::addLast);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   326
        return size() > s;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Inserts the specified element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   333
     * @return {@code true} (as specified by {@link Deque#offerFirst})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public boolean offerFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   345
     * @return {@code true} (as specified by {@link Deque#offerLast})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public boolean offerLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public E removeFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   357
        E e = pollFirst();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   358
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   360
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public E removeLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   367
        E e = pollLast();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   368
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   370
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public E pollFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   374
        final Object[] es;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   375
        final int h;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   376
        E e = elementAt(es = elements, h = head);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   377
        if (e != null) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   378
            es[h] = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   379
            head = inc(h, es.length);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   380
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   381
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public E pollLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   385
        final Object[] es;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   386
        final int t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   387
        E e = elementAt(es = elements, t = dec(tail, es.length));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   388
        if (e != null)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   389
            es[tail = t] = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   390
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public E getFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   397
        E e = elementAt(elements, head);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   398
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   400
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public E getLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   407
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   408
        E e = elementAt(es, dec(tail, es.length));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   409
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   411
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public E peekFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   415
        return elementAt(elements, head);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public E peekLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   419
        final Object[] es;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   420
        return elementAt(es = elements, dec(tail, es.length));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Removes the first occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   427
     * More formally, removes the first element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   428
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   429
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   433
     * @return {@code true} if the deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public boolean removeFirstOccurrence(Object o) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   436
        if (o != null) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   437
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   438
            for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   439
                 ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   440
                for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   441
                    if (o.equals(es[i])) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   442
                        delete(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   443
                        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   444
                    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   445
                if (to == end) break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Removes the last occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   455
     * More formally, removes the last element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   456
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   457
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   461
     * @return {@code true} if the deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public boolean removeLastOccurrence(Object o) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   464
        if (o != null) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   465
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   466
            for (int i = tail, end = head, to = (i >= end) ? end : 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   467
                 ; i = es.length, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   468
                for (i--; i > to - 1; i--)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   469
                    if (o.equals(es[i])) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   470
                        delete(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   471
                        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   472
                    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   473
                if (to == end) break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    // *** Queue methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <p>This method is equivalent to {@link #addLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   487
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * <p>This method is equivalent to {@link #offerLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   501
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return offerLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Retrieves and removes the head of the queue represented by this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * This method differs from {@link #poll poll} only in that it throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * <p>This method is equivalent to {@link #removeFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Retrieves and removes the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * (in other words, the first element of this deque), or returns
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   526
     * {@code null} if this deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <p>This method is equivalent to {@link #pollFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @return the head of the queue represented by this deque, or
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   531
     *         {@code null} if this deque is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Retrieves, but does not remove, the head of the queue represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * this deque.  This method differs from {@link #peek peek} only in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * that it throws an exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <p>This method is equivalent to {@link #getFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Retrieves, but does not remove, the head of the queue represented by
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   553
     * this deque, or returns {@code null} if this deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * <p>This method is equivalent to {@link #peekFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @return the head of the queue represented by this deque, or
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   558
     *         {@code null} if this deque is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return peekFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    // *** Stack methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Pushes an element onto the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * words, inserts the element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p>This method is equivalent to {@link #addFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param e the element to push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    public void push(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Pops an element from the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * words, removes and returns the first element of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <p>This method is equivalent to {@link #removeFirst()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @return the element at the front of this deque (which is the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *         of the stack represented by this deque)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   594
     * Removes the element at the specified position in the elements array.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   595
     * This can result in forward or backwards motion of array elements.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   596
     * We optimize for least element motion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p>This method is called delete rather than remove to emphasize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * that its semantics differ from those of {@link List#remove(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   601
     * @return true if elements near tail moved backwards
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   603
    boolean delete(int i) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   604
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   605
        final int capacity = es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   606
        final int h, t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   607
        // number of elements before to-be-deleted elt
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   608
        final int front = sub(i, h = head, capacity);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   609
        // number of elements after to-be-deleted elt
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   610
        final int back = sub(t = tail, i, capacity) - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (front < back) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   612
            // move front elements forwards
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            if (h <= i) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   614
                System.arraycopy(es, h, es, h + 1, front);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            } else { // Wrap around
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   616
                System.arraycopy(es, 0, es, 1, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   617
                es[0] = es[capacity - 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   618
                System.arraycopy(es, h, es, h + 1, front - (i + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   620
            es[h] = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   621
            head = inc(h, capacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        } else {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   624
            // move back elements backwards
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   625
            tail = dec(t, capacity);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   626
            if (i <= tail) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   627
                System.arraycopy(es, i + 1, es, i, back);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            } else { // Wrap around
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   629
                System.arraycopy(es, i + 1, es, i, capacity - (i + 1));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   630
                es[capacity - 1] = es[0];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   631
                System.arraycopy(es, 1, es, 0, t - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   633
            es[tail] = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    // *** Collection Methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Returns the number of elements in this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @return the number of elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public int size() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   646
        return sub(tail, head, elements.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   650
     * Returns {@code true} if this deque contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   652
     * @return {@code true} if this deque contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return head == tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Returns an iterator over the elements in this deque.  The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * will be ordered from first (head) to last (tail).  This is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * order that elements would be dequeued (via successive calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * {@link #remove} or popped (via successive calls to {@link #pop}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @return an iterator over the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        return new DeqIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        return new DescendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private class DeqIterator implements Iterator<E> {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   675
        /** Index of element to be returned by subsequent call to next. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   676
        int cursor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   678
        /** Number of elements yet to be returned. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   679
        int remaining = size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * Index of element returned by most recent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         * Reset to -1 if element is deleted by a call to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   685
        int lastRet = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   687
        DeqIterator() { cursor = head; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   688
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   689
        public final boolean hasNext() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   690
            return remaining > 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        public E next() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   694
            if (remaining <= 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   696
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   697
            E e = nonNullElementAt(es, cursor);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   698
            cursor = inc(lastRet = cursor, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   699
            remaining--;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   700
            return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   703
        void postDelete(boolean leftShifted) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   704
            if (leftShifted)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   705
                cursor = dec(cursor, elements.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   706
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   707
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   708
        public final void remove() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                throw new IllegalStateException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   711
            postDelete(delete(lastRet));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   714
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   715
        public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   716
            Objects.requireNonNull(action);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   717
            int r;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   718
            if ((r = remaining) <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   719
                return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   720
            remaining = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   721
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   722
            if (es[cursor] == null || sub(tail, cursor, es.length) != r)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   723
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   724
            for (int i = cursor, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   725
                 ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   726
                for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   727
                    action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   728
                if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   729
                    if (end != tail)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   730
                        throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   731
                    lastRet = dec(end, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   732
                    break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   733
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   734
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   735
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   736
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   737
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   738
    private class DescendingIterator extends DeqIterator {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   739
        DescendingIterator() { cursor = dec(tail, elements.length); }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   740
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   741
        public final E next() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   742
            if (remaining <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   743
                throw new NoSuchElementException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   744
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   745
            E e = nonNullElementAt(es, cursor);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   746
            cursor = dec(lastRet = cursor, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   747
            remaining--;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   748
            return e;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   749
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   750
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   751
        void postDelete(boolean leftShifted) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   752
            if (!leftShifted)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   753
                cursor = inc(cursor, elements.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   754
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   755
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   756
        public final void forEachRemaining(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   757
            Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   758
            int r;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   759
            if ((r = remaining) <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   760
                return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   761
            remaining = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   762
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   763
            if (es[cursor] == null || sub(cursor, head, es.length) + 1 != r)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   764
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   765
            for (int i = cursor, end = head, to = (i >= end) ? end : 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   766
                 ; i = es.length - 1, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   767
                // hotspot generates faster code than for: i >= to !
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   768
                for (; i > to - 1; i--)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   769
                    action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   770
                if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   771
                    if (end != head)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   772
                        throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   773
                    lastRet = end;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   774
                    break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   775
                }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   776
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   777
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   780
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   781
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   782
     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   783
     * deque.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   784
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   785
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   786
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   787
     * {@link Spliterator#NONNULL}.  Overriding implementations should document
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   788
     * the reporting of additional characteristic values.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   789
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   790
     * @return a {@code Spliterator} over the elements in this deque
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   791
     * @since 1.8
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   792
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   793
    public Spliterator<E> spliterator() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   794
        return new DeqSpliterator();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   795
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   796
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   797
    final class DeqSpliterator implements Spliterator<E> {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   798
        private int fence;      // -1 until first use
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   799
        private int cursor;     // current index, modified on traverse/split
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   800
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   801
        /** Constructs late-binding spliterator over all elements. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   802
        DeqSpliterator() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   803
            this.fence = -1;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   804
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   805
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   806
        /** Constructs spliterator over the given range. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   807
        DeqSpliterator(int origin, int fence) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   808
            // assert 0 <= origin && origin < elements.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   809
            // assert 0 <= fence && fence < elements.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   810
            this.cursor = origin;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   811
            this.fence = fence;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   812
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   813
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   814
        /** Ensures late-binding initialization; then returns fence. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   815
        private int getFence() { // force initialization
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   816
            int t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   817
            if ((t = fence) < 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   818
                t = fence = tail;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   819
                cursor = head;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   820
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   821
            return t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   822
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   824
        public DeqSpliterator trySplit() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   825
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   826
            final int i, n;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   827
            return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   828
                ? null
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   829
                : new DeqSpliterator(i, cursor = add(i, n, es.length));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   830
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   831
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   832
        public void forEachRemaining(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   833
            if (action == null)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   834
                throw new NullPointerException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   835
            final int end = getFence(), cursor = this.cursor;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   836
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   837
            if (cursor != end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   838
                this.cursor = end;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   839
                // null check at both ends of range is sufficient
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   840
                if (es[cursor] == null || es[dec(end, es.length)] == null)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   841
                    throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   842
                for (int i = cursor, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   843
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   844
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   845
                        action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   846
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   847
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   848
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   849
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   850
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   851
        public boolean tryAdvance(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   852
            Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   853
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   854
            if (fence < 0) { fence = tail; cursor = head; } // late-binding
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   855
            final int i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   856
            if ((i = cursor) == fence)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   857
                return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   858
            E e = nonNullElementAt(es, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   859
            cursor = inc(i, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   860
            action.accept(e);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   861
            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   862
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   863
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   864
        public long estimateSize() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   865
            return sub(getFence(), cursor, elements.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   868
        public int characteristics() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   869
            return Spliterator.NONNULL
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   870
                | Spliterator.ORDERED
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   871
                | Spliterator.SIZED
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   872
                | Spliterator.SUBSIZED;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   873
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   874
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   875
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   876
    public void forEach(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   877
        Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   878
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   879
        for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   880
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   881
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   882
                action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   883
            if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   884
                if (end != tail) throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   885
                break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   886
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   888
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   890
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   891
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   892
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   893
    public boolean removeIf(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   894
        Objects.requireNonNull(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   895
        return bulkRemove(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   896
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   897
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   898
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   899
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   900
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   901
    public boolean removeAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   902
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   903
        return bulkRemove(e -> c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   904
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   905
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   906
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   907
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   908
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   909
    public boolean retainAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   910
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   911
        return bulkRemove(e -> !c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   912
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   913
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   914
    /** Implementation of bulk remove methods. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   915
    private boolean bulkRemove(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   916
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   917
        // Optimize for initial run of survivors
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   918
        for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   919
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   920
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   921
                if (filter.test(elementAt(es, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   922
                    return bulkRemoveModified(filter, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   923
            if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   924
                if (end != tail) throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   925
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   928
        return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   929
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   930
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   931
    // A tiny bit set implementation
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   932
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   933
    private static long[] nBits(int n) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   934
        return new long[((n - 1) >> 6) + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   935
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   936
    private static void setBit(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   937
        bits[i >> 6] |= 1L << i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   938
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   939
    private static boolean isClear(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   940
        return (bits[i >> 6] & (1L << i)) == 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   941
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   942
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   943
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   944
     * Helper for bulkRemove, in case of at least one deletion.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   945
     * Tolerate predicates that reentrantly access the collection for
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   946
     * read (but writers still get CME), so traverse once to find
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   947
     * elements to delete, a second pass to physically expunge.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   948
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   949
     * @param beg valid index of first element to be deleted
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   950
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   951
    private boolean bulkRemoveModified(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   952
        Predicate<? super E> filter, final int beg) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   953
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   954
        final int capacity = es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   955
        final int end = tail;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   956
        final long[] deathRow = nBits(sub(end, beg, capacity));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   957
        deathRow[0] = 1L;   // set bit 0
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   958
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   959
             ; i = 0, to = end, k -= capacity) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   960
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   961
                if (filter.test(elementAt(es, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   962
                    setBit(deathRow, i - k);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   963
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   964
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   965
        // a two-finger traversal, with hare i reading, tortoise w writing
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   966
        int w = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   967
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   968
             ; w = 0) { // w rejoins i on second leg
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   969
            // In this loop, i and w are on the same leg, with i > w
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   970
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   971
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   972
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   973
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   974
            // In this loop, w is on the first leg, i on the second
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   975
            for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   976
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   977
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   978
            if (i >= to) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   979
                if (w == capacity) w = 0; // "corner" case
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   980
                break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   981
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   982
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   983
        if (end != tail) throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   984
        circularClear(es, tail = w, end);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   985
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   989
     * Returns {@code true} if this deque contains the specified element.
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   990
     * More formally, returns {@code true} if and only if this deque contains
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   991
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @param o object to be checked for containment in this deque
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   994
     * @return {@code true} if this deque contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    public boolean contains(Object o) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   997
        if (o != null) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   998
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   999
            for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1000
                 ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1001
                for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1002
                    if (o.equals(es[i]))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1003
                        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1004
                if (to == end) break;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1005
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * Removes a single instance of the specified element from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1013
     * More formally, removes the first element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1014
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1015
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1018
     * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1021
     * @return {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        return removeFirstOccurrence(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * Removes all of the elements from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * The deque will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    public void clear() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1032
        circularClear(elements, head, tail);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1033
        head = tail = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1034
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1035
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1036
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1037
     * Nulls out slots starting at array index i, upto index end.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1038
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1039
    private static void circularClear(Object[] es, int i, int end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1040
        for (int to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1041
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1042
            Arrays.fill(es, i, to, null);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1043
            if (to == end) break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * Returns an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * maintained by this deque.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    public Object[] toArray() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1061
        return toArray(Object[].class);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1062
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1063
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1064
    private <T> T[] toArray(Class<T[]> klazz) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1065
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1066
        final T[] a;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1067
        final int head = this.head, tail = this.tail, end;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1068
        if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1069
            // Uses null extension feature of copyOfRange
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1070
            a = Arrays.copyOfRange(es, head, end, klazz);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1071
        } else {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1072
            // integer overflow!
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1073
            a = Arrays.copyOfRange(es, 0, end - head, klazz);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1074
            System.arraycopy(es, head, a, 0, es.length - head);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1075
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1076
        if (end != tail)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1077
            System.arraycopy(es, 0, a, es.length - head, tail);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1078
        return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * Returns an array containing all of the elements in this deque in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * proper sequence (from first to last element); the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * returned array is that of the specified array.  If the deque fits in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * the specified array, it is returned therein.  Otherwise, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * is allocated with the runtime type of the specified array and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * size of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * <p>If this deque fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * (i.e., the array has more elements than this deque), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * the array immediately following the end of the deque is set to
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1092
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1099
     * <p>Suppose {@code x} is a deque known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * The following code can be used to dump the deque into a newly
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1101
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1103
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1105
     * Note that {@code toArray(new Object[0])} is identical in function to
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1106
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @param a the array into which the elements of the deque are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *         this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1117
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public <T> T[] toArray(T[] a) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1119
        final int size;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1120
        if ((size = size()) > a.length)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1121
            return toArray((Class<T[]>) a.getClass());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1122
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1123
        for (int i = head, j = 0, len = Math.min(size, es.length - i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1124
             ; i = 0, len = tail) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1125
            System.arraycopy(es, i, a, j, len);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1126
            if ((j += len) == size) break;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1127
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1128
        if (size < a.length)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1129
            a[size] = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    // *** Object methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * Returns a copy of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @return a copy of this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    public ArrayDeque<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        try {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9242
diff changeset
  1142
            @SuppressWarnings("unchecked")
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1143
            ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            result.elements = Arrays.copyOf(elements, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    private static final long serialVersionUID = 2340985798034038923L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1154
     * Saves this deque to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1156
     * @param s the stream
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1157
     * @throws java.io.IOException if an I/O error occurs
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1158
     * @serialData The current size ({@code int}) of the deque,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * followed by all of its elements (each an object reference) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * first-to-last order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1162
    private void writeObject(java.io.ObjectOutputStream s)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1163
            throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        s.writeInt(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        // Write out elements in order.
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1170
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1171
        for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1172
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1173
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1174
                s.writeObject(es[i]);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1175
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1176
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1180
     * Reconstitutes this deque from a stream (that is, deserializes it).
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1181
     * @param s the stream
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1182
     * @throws ClassNotFoundException if the class of a serialized object
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1183
     *         could not be found
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1184
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1186
    private void readObject(java.io.ObjectInputStream s)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1187
            throws java.io.IOException, ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        // Read in size and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        int size = s.readInt();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1192
        elements = new Object[size + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1193
        this.tail = size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        for (int i = 0; i < size; i++)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1197
            elements[i] = s.readObject();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1198
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1199
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1200
    /** debugging */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1201
    void checkInvariants() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1202
        // Use head and tail fields with empty slot at tail strategy.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1203
        // head == tail disambiguates to "empty".
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1204
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1205
            int capacity = elements.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1206
            // assert 0 <= head && head < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1207
            // assert 0 <= tail && tail < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1208
            // assert capacity > 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1209
            // assert size() < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1210
            // assert head == tail || elements[head] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1211
            // assert elements[tail] == null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1212
            // assert head == tail || elements[dec(tail, capacity)] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1213
        } catch (Throwable t) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1214
            System.err.printf("head=%d tail=%d capacity=%d%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1215
                              head, tail, elements.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1216
            System.err.printf("elements=%s%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1217
                              Arrays.toString(elements));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1218
            throw t;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1219
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1220
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1221
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
}