src/java.base/share/classes/java/util/ArrayDeque.java
author dl
Tue, 03 Oct 2017 14:00:00 -0700
changeset 47307 6864969a78ad
parent 47216 71c04702a3d5
child 47423 4fc2a4a29f3d
permissions -rw-r--r--
8186056: Miscellaneous changes imported from jsr166 CVS 2017-09 Reviewed-by: martin, psandoz
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
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 42927
diff changeset
    82
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
2
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);
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   149
        final Object[] es = elements = Arrays.copyOf(elements, newCapacity);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   150
        // Exceptionally, here tail == head needs to be disambiguated
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   151
        if (tail < head || (tail == head && es[head] != null)) {
42319
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;
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   154
            System.arraycopy(es, head,
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   155
                             es, head + newSpace,
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   156
                             oldCapacity - head);
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   157
            for (int i = head, to = (head += newSpace); i < to; i++)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   158
                es[i] = null;
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
    /**
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   214
     * Circularly increments i, mod modulus.
42319
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
    /**
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   223
     * Circularly decrements i, mod modulus.
42319
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
     */
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   236
    static final int inc(int i, int distance, int modulus) {
42319
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
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44743
diff changeset
   246
     * is disambiguated to "empty", returning 0.
42319
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,
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44743
diff changeset
   313
     * in the order that they are returned by the collection's iterator.
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   314
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   315
     * @param c the elements to be inserted into this deque
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   316
     * @return {@code true} if this deque changed as a result of the call
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   317
     * @throws NullPointerException if the specified collection or any
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   318
     *         of its elements are null
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   319
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   320
    public boolean addAll(Collection<? extends E> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   321
        final int s, needed;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   322
        if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   323
            grow(needed);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   324
        c.forEach(this::addLast);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   325
        return size() > s;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Inserts the specified element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   332
     * @return {@code true} (as specified by {@link Deque#offerFirst})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public boolean offerFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   344
     * @return {@code true} (as specified by {@link Deque#offerLast})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public boolean offerLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public E removeFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   356
        E e = pollFirst();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   357
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   359
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public E removeLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   366
        E e = pollLast();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   367
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   369
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public E pollFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   373
        final Object[] es;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   374
        final int h;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   375
        E e = elementAt(es = elements, h = head);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   376
        if (e != null) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   377
            es[h] = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   378
            head = inc(h, es.length);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   379
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   380
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public E pollLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   384
        final Object[] es;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   385
        final int t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   386
        E e = elementAt(es = elements, t = dec(tail, es.length));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   387
        if (e != null)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   388
            es[tail = t] = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   389
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public E getFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   396
        E e = elementAt(elements, head);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   397
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   399
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public E getLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   406
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   407
        E e = elementAt(es, dec(tail, es.length));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   408
        if (e == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   410
        return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public E peekFirst() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   414
        return elementAt(elements, head);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public E peekLast() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   418
        final Object[] es;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   419
        return elementAt(es = elements, dec(tail, es.length));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Removes the first occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   426
     * More formally, removes the first element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   427
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   428
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   432
     * @return {@code true} if the deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public boolean removeFirstOccurrence(Object o) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   435
        if (o != null) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   436
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   437
            for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   438
                 ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   439
                for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   440
                    if (o.equals(es[i])) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   441
                        delete(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   442
                        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   443
                    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   444
                if (to == end) break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Removes the last occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   454
     * More formally, removes the last element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   455
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   456
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   460
     * @return {@code true} if the deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public boolean removeLastOccurrence(Object o) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   463
        if (o != null) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   464
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   465
            for (int i = tail, end = head, to = (i >= end) ? end : 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   466
                 ; i = es.length, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   467
                for (i--; i > to - 1; i--)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   468
                    if (o.equals(es[i])) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   469
                        delete(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   470
                        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   471
                    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   472
                if (to == end) break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    // *** Queue methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <p>This method is equivalent to {@link #addLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   486
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <p>This method is equivalent to {@link #offerLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   500
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return offerLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Retrieves and removes the head of the queue represented by this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44743
diff changeset
   510
     * This method differs from {@link #poll() poll()} only in that it
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44743
diff changeset
   511
     * throws an exception if this deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <p>This method is equivalent to {@link #removeFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Retrieves and removes the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * (in other words, the first element of this deque), or returns
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   525
     * {@code null} if this deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <p>This method is equivalent to {@link #pollFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @return the head of the queue represented by this deque, or
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   530
     *         {@code null} if this deque is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Retrieves, but does not remove, the head of the queue represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * this deque.  This method differs from {@link #peek peek} only in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * that it throws an exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <p>This method is equivalent to {@link #getFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * 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
   552
     * this deque, or returns {@code null} if this deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <p>This method is equivalent to {@link #peekFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return the head of the queue represented by this deque, or
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   557
     *         {@code null} if this deque is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return peekFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    // *** Stack methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Pushes an element onto the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * words, inserts the element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <p>This method is equivalent to {@link #addFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param e the element to push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public void push(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Pops an element from the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * words, removes and returns the first element of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <p>This method is equivalent to {@link #removeFirst()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @return the element at the front of this deque (which is the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *         of the stack represented by this deque)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   593
     * Removes the element at the specified position in the elements array.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   594
     * This can result in forward or backwards motion of array elements.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   595
     * We optimize for least element motion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p>This method is called delete rather than remove to emphasize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * that its semantics differ from those of {@link List#remove(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   600
     * @return true if elements near tail moved backwards
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   602
    boolean delete(int i) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   603
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   604
        final int capacity = es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   605
        final int h, t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   606
        // number of elements before to-be-deleted elt
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   607
        final int front = sub(i, h = head, capacity);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   608
        // number of elements after to-be-deleted elt
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   609
        final int back = sub(t = tail, i, capacity) - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (front < back) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   611
            // move front elements forwards
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (h <= i) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   613
                System.arraycopy(es, h, es, h + 1, front);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            } else { // Wrap around
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   615
                System.arraycopy(es, 0, es, 1, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   616
                es[0] = es[capacity - 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   617
                System.arraycopy(es, h, es, h + 1, front - (i + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   619
            es[h] = null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   620
            head = inc(h, capacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } else {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   623
            // move back elements backwards
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   624
            tail = dec(t, capacity);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   625
            if (i <= tail) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   626
                System.arraycopy(es, i + 1, es, i, back);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            } else { // Wrap around
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   628
                System.arraycopy(es, i + 1, es, i, capacity - (i + 1));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   629
                es[capacity - 1] = es[0];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   630
                System.arraycopy(es, 1, es, 0, t - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   632
            es[tail] = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    // *** Collection Methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Returns the number of elements in this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @return the number of elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public int size() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   645
        return sub(tail, head, elements.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   649
     * Returns {@code true} if this deque contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   651
     * @return {@code true} if this deque contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        return head == tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Returns an iterator over the elements in this deque.  The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * will be ordered from first (head) to last (tail).  This is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * order that elements would be dequeued (via successive calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * {@link #remove} or popped (via successive calls to {@link #pop}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return an iterator over the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return new DeqIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return new DescendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    private class DeqIterator implements Iterator<E> {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   674
        /** Index of element to be returned by subsequent call to next. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   675
        int cursor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   677
        /** Number of elements yet to be returned. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   678
        int remaining = size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * Index of element returned by most recent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * Reset to -1 if element is deleted by a call to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   684
        int lastRet = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   686
        DeqIterator() { cursor = head; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   687
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   688
        public final boolean hasNext() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   689
            return remaining > 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        public E next() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   693
            if (remaining <= 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                throw new NoSuchElementException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   695
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   696
            E e = nonNullElementAt(es, cursor);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   697
            cursor = inc(lastRet = cursor, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   698
            remaining--;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   699
            return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   702
        void postDelete(boolean leftShifted) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   703
            if (leftShifted)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   704
                cursor = dec(cursor, elements.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   705
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   706
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   707
        public final void remove() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                throw new IllegalStateException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   710
            postDelete(delete(lastRet));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   713
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   714
        public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   715
            Objects.requireNonNull(action);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   716
            int r;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   717
            if ((r = remaining) <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   718
                return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   719
            remaining = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   720
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   721
            if (es[cursor] == null || sub(tail, cursor, es.length) != r)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   722
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   723
            for (int i = cursor, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   724
                 ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   725
                for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   726
                    action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   727
                if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   728
                    if (end != tail)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   729
                        throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   730
                    lastRet = dec(end, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   731
                    break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   732
                }
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
    private class DescendingIterator extends DeqIterator {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   738
        DescendingIterator() { cursor = dec(tail, elements.length); }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   739
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   740
        public final E next() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   741
            if (remaining <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   742
                throw new NoSuchElementException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   743
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   744
            E e = nonNullElementAt(es, cursor);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   745
            cursor = dec(lastRet = cursor, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   746
            remaining--;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   747
            return e;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   748
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   749
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   750
        void postDelete(boolean leftShifted) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   751
            if (!leftShifted)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   752
                cursor = inc(cursor, elements.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   753
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   754
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   755
        public final void forEachRemaining(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   756
            Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   757
            int r;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   758
            if ((r = remaining) <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   759
                return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   760
            remaining = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   761
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   762
            if (es[cursor] == null || sub(cursor, head, es.length) + 1 != r)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   763
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   764
            for (int i = cursor, end = head, to = (i >= end) ? end : 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   765
                 ; i = es.length - 1, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   766
                // hotspot generates faster code than for: i >= to !
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   767
                for (; i > to - 1; i--)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   768
                    action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   769
                if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   770
                    if (end != head)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   771
                        throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   772
                    lastRet = end;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   773
                    break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   774
                }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   775
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   776
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   779
    /**
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   780
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   781
     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   782
     * deque.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   783
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   784
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   785
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   786
     * {@link Spliterator#NONNULL}.  Overriding implementations should document
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   787
     * the reporting of additional characteristic values.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   788
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   789
     * @return a {@code Spliterator} over the elements in this deque
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   790
     * @since 1.8
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   791
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   792
    public Spliterator<E> spliterator() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   793
        return new DeqSpliterator();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   794
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   795
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   796
    final class DeqSpliterator implements Spliterator<E> {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   797
        private int fence;      // -1 until first use
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   798
        private int cursor;     // current index, modified on traverse/split
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   799
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   800
        /** Constructs late-binding spliterator over all elements. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   801
        DeqSpliterator() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   802
            this.fence = -1;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   803
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   804
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   805
        /** Constructs spliterator over the given range. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   806
        DeqSpliterator(int origin, int fence) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   807
            // assert 0 <= origin && origin < elements.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   808
            // assert 0 <= fence && fence < elements.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   809
            this.cursor = origin;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   810
            this.fence = fence;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   811
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   812
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   813
        /** Ensures late-binding initialization; then returns fence. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   814
        private int getFence() { // force initialization
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   815
            int t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   816
            if ((t = fence) < 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   817
                t = fence = tail;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   818
                cursor = head;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   819
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   820
            return t;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   821
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   823
        public DeqSpliterator trySplit() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   824
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   825
            final int i, n;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   826
            return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   827
                ? null
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   828
                : new DeqSpliterator(i, cursor = inc(i, n, es.length));
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   829
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   830
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   831
        public void forEachRemaining(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   832
            if (action == null)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   833
                throw new NullPointerException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   834
            final int end = getFence(), cursor = this.cursor;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   835
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   836
            if (cursor != end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   837
                this.cursor = end;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   838
                // null check at both ends of range is sufficient
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   839
                if (es[cursor] == null || es[dec(end, es.length)] == null)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   840
                    throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   841
                for (int i = cursor, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   842
                     ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   843
                    for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   844
                        action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   845
                    if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   846
                }
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
        public boolean tryAdvance(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   851
            Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   852
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   853
            if (fence < 0) { fence = tail; cursor = head; } // late-binding
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   854
            final int i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   855
            if ((i = cursor) == fence)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   856
                return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   857
            E e = nonNullElementAt(es, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   858
            cursor = inc(i, es.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   859
            action.accept(e);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   860
            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   861
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   862
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   863
        public long estimateSize() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   864
            return sub(getFence(), cursor, elements.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   867
        public int characteristics() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   868
            return Spliterator.NONNULL
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   869
                | Spliterator.ORDERED
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   870
                | Spliterator.SIZED
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   871
                | Spliterator.SUBSIZED;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   872
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   873
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   874
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   875
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   876
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   877
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   878
    public void forEach(Consumer<? super E> action) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   879
        Objects.requireNonNull(action);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   880
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   881
        for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   882
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   883
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   884
                action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   885
            if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   886
                if (end != tail) throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   887
                break;
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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   892
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   893
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   894
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   895
    public boolean removeIf(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   896
        Objects.requireNonNull(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   897
        return bulkRemove(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   898
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   899
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   900
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   901
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   902
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   903
    public boolean removeAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   904
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   905
        return bulkRemove(e -> c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   906
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   907
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   908
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   909
     * @throws NullPointerException {@inheritDoc}
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   910
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   911
    public boolean retainAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   912
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   913
        return bulkRemove(e -> !c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   914
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   915
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   916
    /** Implementation of bulk remove methods. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   917
    private boolean bulkRemove(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   918
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   919
        // Optimize for initial run of survivors
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   920
        for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   921
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   922
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   923
                if (filter.test(elementAt(es, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   924
                    return bulkRemoveModified(filter, i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   925
            if (to == end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   926
                if (end != tail) throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   927
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   930
        return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   931
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   932
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   933
    // A tiny bit set implementation
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   934
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   935
    private static long[] nBits(int n) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   936
        return new long[((n - 1) >> 6) + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   937
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   938
    private static void setBit(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   939
        bits[i >> 6] |= 1L << i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   940
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   941
    private static boolean isClear(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   942
        return (bits[i >> 6] & (1L << i)) == 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   943
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   944
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   945
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   946
     * Helper for bulkRemove, in case of at least one deletion.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   947
     * Tolerate predicates that reentrantly access the collection for
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   948
     * read (but writers still get CME), so traverse once to find
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   949
     * elements to delete, a second pass to physically expunge.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   950
     *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   951
     * @param beg valid index of first element to be deleted
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   952
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   953
    private boolean bulkRemoveModified(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   954
        Predicate<? super E> filter, final int beg) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   955
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   956
        final int capacity = es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   957
        final int end = tail;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   958
        final long[] deathRow = nBits(sub(end, beg, capacity));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   959
        deathRow[0] = 1L;   // set bit 0
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   960
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   961
             ; i = 0, to = end, k -= capacity) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   962
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   963
                if (filter.test(elementAt(es, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   964
                    setBit(deathRow, i - k);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   965
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   966
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   967
        // a two-finger traversal, with hare i reading, tortoise w writing
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   968
        int w = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   969
        for (int i = beg + 1, to = (i <= end) ? end : es.length, k = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   970
             ; w = 0) { // w rejoins i on second leg
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   971
            // In this loop, i and w are on the same leg, with i > w
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   972
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   973
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   974
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   975
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   976
            // In this loop, w is on the first leg, i on the second
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   977
            for (i = 0, to = end, k -= capacity; i < to && w < capacity; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   978
                if (isClear(deathRow, i - k))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   979
                    es[w++] = es[i];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   980
            if (i >= to) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   981
                if (w == capacity) w = 0; // "corner" case
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   982
                break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   983
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   984
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   985
        if (end != tail) throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   986
        circularClear(es, tail = w, end);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
   987
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   991
     * Returns {@code true} if this deque contains the specified element.
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   992
     * More formally, returns {@code true} if and only if this deque contains
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   993
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * @param o object to be checked for containment in this deque
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   996
     * @return {@code true} if this deque contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public boolean contains(Object o) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   999
        if (o != null) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1000
            final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1001
            for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1002
                 ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1003
                for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1004
                    if (o.equals(es[i]))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1005
                        return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1006
                if (to == end) break;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1007
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Removes a single instance of the specified element from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1015
     * More formally, removes the first element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1016
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1017
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1020
     * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1023
     * @return {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        return removeFirstOccurrence(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * Removes all of the elements from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * The deque will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    public void clear() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1034
        circularClear(elements, head, tail);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1035
        head = tail = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1036
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1037
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1038
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1039
     * Nulls out slots starting at array index i, upto index end.
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1040
     * Condition i == end means "empty" - nothing to do.
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1041
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1042
    private static void circularClear(Object[] es, int i, int end) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1043
        // assert 0 <= i && i < es.length;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1044
        // assert 0 <= end && end < es.length;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1045
        for (int to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1046
             ; i = 0, to = end) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1047
            for (; i < to; i++) es[i] = null;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1048
            if (to == end) break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Returns an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * maintained by this deque.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    public Object[] toArray() {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1066
        return toArray(Object[].class);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1067
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1068
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1069
    private <T> T[] toArray(Class<T[]> klazz) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1070
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1071
        final T[] a;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1072
        final int head = this.head, tail = this.tail, end;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1073
        if ((end = tail + ((head <= tail) ? 0 : es.length)) >= 0) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1074
            // Uses null extension feature of copyOfRange
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1075
            a = Arrays.copyOfRange(es, head, end, klazz);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1076
        } else {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1077
            // integer overflow!
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1078
            a = Arrays.copyOfRange(es, 0, end - head, klazz);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1079
            System.arraycopy(es, head, a, 0, es.length - head);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1080
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1081
        if (end != tail)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1082
            System.arraycopy(es, 0, a, es.length - head, tail);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1083
        return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * Returns an array containing all of the elements in this deque in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * proper sequence (from first to last element); the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * returned array is that of the specified array.  If the deque fits in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * the specified array, it is returned therein.  Otherwise, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * is allocated with the runtime type of the specified array and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * size of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * <p>If this deque fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * (i.e., the array has more elements than this deque), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * 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
  1097
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1104
     * <p>Suppose {@code x} is a deque known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * 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
  1106
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1108
     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1110
     * Note that {@code toArray(new Object[0])} is identical in function to
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1111
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @param a the array into which the elements of the deque are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     *         this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
  1122
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    public <T> T[] toArray(T[] a) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1124
        final int size;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1125
        if ((size = size()) > a.length)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1126
            return toArray((Class<T[]>) a.getClass());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1127
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1128
        for (int i = head, j = 0, len = Math.min(size, es.length - i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1129
             ; i = 0, len = tail) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1130
            System.arraycopy(es, i, a, j, len);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1131
            if ((j += len) == size) break;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1132
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1133
        if (size < a.length)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1134
            a[size] = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    // *** Object methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * Returns a copy of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @return a copy of this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    public ArrayDeque<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        try {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9242
diff changeset
  1147
            @SuppressWarnings("unchecked")
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1148
            ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            result.elements = Arrays.copyOf(elements, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    private static final long serialVersionUID = 2340985798034038923L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1159
     * Saves this deque to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1161
     * @param s the stream
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1162
     * @throws java.io.IOException if an I/O error occurs
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1163
     * @serialData The current size ({@code int}) of the deque,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * followed by all of its elements (each an object reference) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * first-to-last order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1167
    private void writeObject(java.io.ObjectOutputStream s)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1168
            throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        s.writeInt(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        // Write out elements in order.
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1175
        final Object[] es = elements;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1176
        for (int i = head, end = tail, to = (i <= end) ? end : es.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1177
             ; i = 0, to = end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1178
            for (; i < to; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1179
                s.writeObject(es[i]);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1180
            if (to == end) break;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1181
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1185
     * 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
  1186
     * @param s the stream
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1187
     * @throws ClassNotFoundException if the class of a serialized object
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1188
     *         could not be found
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1189
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1191
    private void readObject(java.io.ObjectInputStream s)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1192
            throws java.io.IOException, ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        // Read in size and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        int size = s.readInt();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1197
        elements = new Object[size + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1198
        this.tail = size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        for (int i = 0; i < size; i++)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1202
            elements[i] = s.readObject();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1203
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1204
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1205
    /** debugging */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1206
    void checkInvariants() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1207
        // Use head and tail fields with empty slot at tail strategy.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1208
        // head == tail disambiguates to "empty".
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1209
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1210
            int capacity = elements.length;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1211
            // assert 0 <= head && head < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1212
            // assert 0 <= tail && tail < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1213
            // assert capacity > 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1214
            // assert size() < capacity;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1215
            // assert head == tail || elements[head] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1216
            // assert elements[tail] == null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1217
            // assert head == tail || elements[dec(tail, capacity)] != null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1218
        } catch (Throwable t) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1219
            System.err.printf("head=%d tail=%d capacity=%d%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1220
                              head, tail, elements.length);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1221
            System.err.printf("elements=%s%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1222
                              Arrays.toString(elements));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 40817
diff changeset
  1223
            throw t;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1224
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1225
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
  1226
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
}