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