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