jdk/src/share/classes/java/util/ArrayDeque.java
author mduigou
Wed, 21 Aug 2013 12:03:19 -0700
changeset 19572 e43d5c2e79ca
parent 19435 9d7530ff42cb
child 22078 bdec5d53e98c
permissions -rw-r--r--
8023306: Add replace() implementations to TreeMap Reviewed-by: psandoz, alanb, chegar, bpb
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;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Resizable-array implementation of the {@link Deque} interface.  Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * deques have no capacity restrictions; they grow as necessary to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * usage.  They are not thread-safe; in the absence of external
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * synchronization, they do not support concurrent access by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Null elements are prohibited.  This class is likely to be faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * {@link Stack} when used as a stack, and faster than {@link LinkedList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * when used as a queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    49
 * <p>Most {@code ArrayDeque} operations run in amortized constant time.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Exceptions include {@link #remove(Object) remove}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * removeLastOccurrence}, {@link #contains contains}, {@link #iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * iterator.remove()}, and the bulk operations, all of which run in linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    56
 * <p>The iterators returned by this class's {@code iterator} method are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <i>fail-fast</i>: If the deque is modified at any time after the iterator
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    58
 * is created, in any way except through the iterator's own {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * method, the iterator will generally throw a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * arbitrary, non-deterministic behavior at an undetermined time in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    68
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author  Josh Bloch and Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class ArrayDeque<E> extends AbstractCollection<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                           implements Deque<E>, Cloneable, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The array in which the elements of the deque are stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The capacity of the deque is the length of this array, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * always a power of two. The array is never allowed to become
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * full, except transiently within an addX method where it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * resized (see doubleCapacity) immediately upon becoming full,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * thus avoiding head and tail wrapping around to equal each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * other.  We also guarantee that all array cells not holding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * deque elements are always null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
    98
    transient Object[] elements; // non-private to simplify nested class access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * The index of the element at the head of the deque (which is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * element that would be removed by remove() or pop()); or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * arbitrary number equal to tail if the deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   105
    transient int head;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The index at which the next element would be added to the tail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * of the deque (via addLast(E), add(E), or push(E)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   111
    transient int tail;
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 minimum capacity that we'll use for a newly created deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Must be a power of 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static final int MIN_INITIAL_CAPACITY = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // ******  Array allocation and resizing utilities ******
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   122
     * Allocates empty array to hold the given number of elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param numElements  the number of elements to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private void allocateElements(int numElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int initialCapacity = MIN_INITIAL_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Find the best power of two to hold elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // Tests "<=" because arrays aren't kept full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (numElements >= initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            initialCapacity = numElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            initialCapacity |= (initialCapacity >>>  1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            initialCapacity |= (initialCapacity >>>  2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            initialCapacity |= (initialCapacity >>>  4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            initialCapacity |= (initialCapacity >>>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            initialCapacity |= (initialCapacity >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            initialCapacity++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (initialCapacity < 0)   // Too many elements, must back off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   142
        elements = new Object[initialCapacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   146
     * Doubles the capacity of this deque.  Call only when full, i.e.,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * when head and tail have wrapped around to become equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private void doubleCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        assert head == tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int p = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int n = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int r = n - p; // number of elements to the right of p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int newCapacity = n << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (newCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new IllegalStateException("Sorry, deque too big");
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   157
        Object[] a = new Object[newCapacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.arraycopy(elements, p, a, 0, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        System.arraycopy(elements, 0, a, r, p);
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   160
        elements = a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        head = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        tail = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Copies the elements from our element array into the specified array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * in order (from first to last element in the deque).  It is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * that the array is large enough to hold all elements in the deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return its argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private <T> T[] copyElements(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (head < tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            System.arraycopy(elements, head, a, 0, size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else if (head > tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            int headPortionLen = elements.length - head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            System.arraycopy(elements, head, a, 0, headPortionLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            System.arraycopy(elements, 0, a, headPortionLen, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Constructs an empty array deque with an initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * sufficient to hold 16 elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public ArrayDeque() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   188
        elements = new Object[16];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Constructs an empty array deque with an initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * sufficient to hold the specified number of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param numElements  lower bound on initial capacity of the deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public ArrayDeque(int numElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        allocateElements(numElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Constructs a deque containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * iterator.  (The first element returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * iterator becomes the first element, or <i>front</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * deque.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param c the collection whose elements are to be placed into the deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public ArrayDeque(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        allocateElements(c.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // The main insertion and extraction methods are addFirst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // addLast, pollFirst, pollLast. The other methods are defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // terms of these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Inserts the specified element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void addFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        elements[head = (head - 1) & (elements.length - 1)] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (head == tail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            doubleCapacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>This method is equivalent to {@link #add}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public void addLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        elements[tail] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if ( (tail = (tail + 1) & (elements.length - 1)) == head)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            doubleCapacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Inserts the specified element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   254
     * @return {@code true} (as specified by {@link Deque#offerFirst})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public boolean offerFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   266
     * @return {@code true} (as specified by {@link Deque#offerLast})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public boolean offerLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public E removeFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        E x = pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public E removeLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        E x = pollLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public E pollFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int h = head;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   296
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   297
        E result = (E) elements[h];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   298
        // Element is null if deque empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        elements[h] = null;     // Must null out slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        head = (h + 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public E pollLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int t = (tail - 1) & (elements.length - 1);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   308
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   309
        E result = (E) elements[t];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        elements[t] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        tail = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public E getFirst() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   321
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   322
        E result = (E) elements[head];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   323
        if (result == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new NoSuchElementException();
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   325
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public E getLast() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   332
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   333
        E result = (E) elements[(tail - 1) & (elements.length - 1)];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   334
        if (result == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            throw new NoSuchElementException();
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   336
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   339
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public E peekFirst() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   341
        // elements[head] is null if deque empty
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   342
        return (E) elements[head];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   345
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public E peekLast() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   347
        return (E) elements[(tail - 1) & (elements.length - 1)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Removes the first occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   354
     * More formally, removes the first element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   355
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   356
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   360
     * @return {@code true} if the deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public boolean removeFirstOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int i = head;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   367
        Object x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        while ( (x = elements[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (o.equals(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                delete(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            i = (i + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Removes the last occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   382
     * More formally, removes the last element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   383
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   384
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   388
     * @return {@code true} if the deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public boolean removeLastOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int i = (tail - 1) & mask;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   395
        Object x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        while ( (x = elements[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (o.equals(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                delete(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            i = (i - 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    // *** Queue methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>This method is equivalent to {@link #addLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   414
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <p>This method is equivalent to {@link #offerLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param e the element to add
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   428
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return offerLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Retrieves and removes the head of the queue represented by this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * This method differs from {@link #poll poll} only in that it throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <p>This method is equivalent to {@link #removeFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Retrieves and removes the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * (in other words, the first element of this deque), or returns
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   453
     * {@code null} if this deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <p>This method is equivalent to {@link #pollFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return the head of the queue represented by this deque, or
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   458
     *         {@code null} if this deque is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Retrieves, but does not remove, the head of the queue represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * this deque.  This method differs from {@link #peek peek} only in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * that it throws an exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <p>This method is equivalent to {@link #getFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * 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
   480
     * this deque, or returns {@code null} if this deque is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * <p>This method is equivalent to {@link #peekFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @return the head of the queue represented by this deque, or
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   485
     *         {@code null} if this deque is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return peekFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    // *** Stack methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Pushes an element onto the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * words, inserts the element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <p>This method is equivalent to {@link #addFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param e the element to push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public void push(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Pops an element from the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * words, removes and returns the first element of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <p>This method is equivalent to {@link #removeFirst()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return the element at the front of this deque (which is the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *         of the stack represented by this deque)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private void checkInvariants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        assert elements[tail] == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        assert head == tail ? elements[head] == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            (elements[head] != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             elements[(tail - 1) & (elements.length - 1)] != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        assert elements[(head - 1) & (elements.length - 1)] == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Removes the element at the specified position in the elements array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * adjusting head and tail as necessary.  This can result in motion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * elements backwards or forwards in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <p>This method is called delete rather than remove to emphasize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * that its semantics differ from those of {@link List#remove(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @return true if elements moved backwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    private boolean delete(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        checkInvariants();
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   540
        final Object[] elements = this.elements;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        final int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        final int h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        final int t = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        final int front = (i - h) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        final int back  = (t - i) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // Invariant: head <= i < tail mod circularity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (front >= ((t - h) & mask))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // Optimize for least element motion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (front < back) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if (h <= i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                System.arraycopy(elements, h, elements, h + 1, front);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            } else { // Wrap around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                System.arraycopy(elements, 0, elements, 1, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                elements[0] = elements[mask];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                System.arraycopy(elements, h, elements, h + 1, mask - h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            elements[h] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            head = (h + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if (i < t) { // Copy the null tail as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                System.arraycopy(elements, i + 1, elements, i, back);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                tail = t - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            } else { // Wrap around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                System.arraycopy(elements, i + 1, elements, i, mask - i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                elements[mask] = elements[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                System.arraycopy(elements, 1, elements, 0, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                tail = (t - 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    // *** Collection Methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Returns the number of elements in this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @return the number of elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        return (tail - head) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   589
     * Returns {@code true} if this deque contains no elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   591
     * @return {@code true} if this deque contains no elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return head == tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Returns an iterator over the elements in this deque.  The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * will be ordered from first (head) to last (tail).  This is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * order that elements would be dequeued (via successive calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * {@link #remove} or popped (via successive calls to {@link #pop}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @return an iterator over the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return new DeqIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return new DescendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private class DeqIterator implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * Index of element to be returned by subsequent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        private int cursor = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * Tail recorded at construction (also in remove), to stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         * iterator and also to check for comodification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        private int fence = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * Index of element returned by most recent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * Reset to -1 if element is deleted by a call to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        private int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            return cursor != fence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (cursor == fence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                throw new NoSuchElementException();
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   638
            @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   639
            E result = (E) elements[cursor];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // This check doesn't catch all possible comodifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            // but does catch the ones that corrupt traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (tail != fence || result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            lastRet = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            cursor = (cursor + 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            if (delete(lastRet)) { // if left-shifted, undo increment in next()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                cursor = (cursor - 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                fence = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   658
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   659
        public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   660
            Objects.requireNonNull(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   661
            Object[] a = elements;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   662
            int m = a.length - 1, f = fence, i = cursor;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   663
            cursor = f;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   664
            while (i != f) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   665
                @SuppressWarnings("unchecked") E e = (E)a[i];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   666
                i = (i + 1) & m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   667
                if (e == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   668
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   669
                action.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   670
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   671
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private class DescendingIterator implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * This class is nearly a mirror-image of DeqIterator, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * tail instead of head for initial cursor, and head instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * tail for fence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        private int cursor = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        private int fence = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        private int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return cursor != fence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            if (cursor == fence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            cursor = (cursor - 1) & (elements.length - 1);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   692
            @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   693
            E result = (E) elements[cursor];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            if (head != fence || result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            lastRet = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (!delete(lastRet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                cursor = (cursor + 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                fence = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   712
     * Returns {@code true} if this deque contains the specified element.
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   713
     * More formally, returns {@code true} if and only if this deque contains
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   714
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @param o object to be checked for containment in this deque
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   717
     * @return {@code true} if this deque contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        int i = head;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   724
        Object x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        while ( (x = elements[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (o.equals(x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            i = (i + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * Removes a single instance of the specified element from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * If the deque does not contain the element, it is unchanged.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   736
     * More formally, removes the first element {@code e} such that
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   737
     * {@code o.equals(e)} (if such an element exists).
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   738
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   741
     * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param o element to be removed from this deque, if present
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   744
     * @return {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return removeFirstOccurrence(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Removes all of the elements from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * The deque will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        int h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        int t = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (h != t) { // clear all cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            head = tail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            int i = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                elements[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                i = (i + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            } while (i != t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * Returns an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * maintained by this deque.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        return copyElements(new Object[size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Returns an array containing all of the elements in this deque in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * proper sequence (from first to last element); the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * returned array is that of the specified array.  If the deque fits in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * the specified array, it is returned therein.  Otherwise, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * is allocated with the runtime type of the specified array and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * size of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * <p>If this deque fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * (i.e., the array has more elements than this deque), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * 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
   796
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   803
     * <p>Suppose {@code x} is a deque known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * 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
   805
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   807
     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   809
     * Note that {@code toArray(new Object[0])} is identical in function to
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   810
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param a the array into which the elements of the deque are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *         this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   821
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            a = (T[])java.lang.reflect.Array.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        copyElements(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    // *** Object methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Returns a copy of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @return a copy of this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public ArrayDeque<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        try {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9242
diff changeset
   842
            @SuppressWarnings("unchecked")
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   843
            ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            result.elements = Arrays.copyOf(elements, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    private static final long serialVersionUID = 2340985798034038923L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   854
     * Saves this deque to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   856
     * @serialData The current size ({@code int}) of the deque,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * followed by all of its elements (each an object reference) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * first-to-last order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   860
    private void writeObject(java.io.ObjectOutputStream s)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   861
            throws java.io.IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        s.writeInt(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        // Write out elements in order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        for (int i = head; i != tail; i = (i + 1) & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            s.writeObject(elements[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   874
     * Reconstitutes this deque from a stream (that is, deserializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   876
    private void readObject(java.io.ObjectInputStream s)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   877
            throws java.io.IOException, ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        // Read in size and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        allocateElements(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        head = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        tail = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        for (int i = 0; i < size; i++)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   888
            elements[i] = s.readObject();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   889
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   890
19435
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   891
    /**
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   892
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   893
     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   894
     * deque.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   895
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   896
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   897
     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   898
     * {@link Spliterator#NONNULL}.  Overriding implementations should document
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   899
     * the reporting of additional characteristic values.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   900
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   901
     * @return a {@code Spliterator} over the elements in this deque
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   902
     * @since 1.8
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 17168
diff changeset
   903
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   904
    public Spliterator<E> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   905
        return new DeqSpliterator<E>(this, -1, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   907
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   908
    static final class DeqSpliterator<E> implements Spliterator<E> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   909
        private final ArrayDeque<E> deq;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   910
        private int fence;  // -1 until first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   911
        private int index;  // current index, modified on traverse/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   912
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   913
        /** Creates new spliterator covering the given array and range */
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   914
        DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   915
            this.deq = deq;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   916
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   917
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   918
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   919
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   920
        private int getFence() { // force initialization
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   921
            int t;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   922
            if ((t = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   923
                t = fence = deq.tail;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   924
                index = deq.head;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   925
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   926
            return t;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   927
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   928
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   929
        public DeqSpliterator<E> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   930
            int t = getFence(), h = index, n = deq.elements.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   931
            if (h != t && ((h + 1) & (n - 1)) != t) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   932
                if (h > t)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   933
                    t += n;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   934
                int m = ((h + t) >>> 1) & (n - 1);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   935
                return new DeqSpliterator<>(deq, h, index = m);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   936
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   937
            return null;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   938
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   939
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   940
        public void forEachRemaining(Consumer<? super E> consumer) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   941
            if (consumer == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   942
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   943
            Object[] a = deq.elements;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   944
            int m = a.length - 1, f = getFence(), i = index;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   945
            index = f;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   946
            while (i != f) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   947
                @SuppressWarnings("unchecked") E e = (E)a[i];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   948
                i = (i + 1) & m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   949
                if (e == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   950
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   951
                consumer.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   952
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   953
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   954
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   955
        public boolean tryAdvance(Consumer<? super E> consumer) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   956
            if (consumer == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   957
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   958
            Object[] a = deq.elements;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   959
            int m = a.length - 1, f = getFence(), i = index;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   960
            if (i != fence) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   961
                @SuppressWarnings("unchecked") E e = (E)a[i];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   962
                index = (i + 1) & m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   963
                if (e == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   964
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   965
                consumer.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   966
                return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   967
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   968
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   969
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   970
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   971
        public long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   972
            int n = getFence() - index;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   973
            if (n < 0)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   974
                n += deq.elements.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   975
            return (long) n;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   976
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   977
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   978
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   979
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   980
            return Spliterator.ORDERED | Spliterator.SIZED |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   981
                Spliterator.NONNULL | Spliterator.SUBSIZED;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   982
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   983
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 13795
diff changeset
   984
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
}