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