jdk/src/share/classes/java/util/ArrayDeque.java
author sherman
Wed, 21 Oct 2009 11:40:40 -0700
changeset 4161 679d00486dc6
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6878475: Better syntax for the named capture group in regex Summary: Updated the syntax of the newly added named capture group Reviewed-by: martin, alanb
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * as explained at http://creativecommons.org/licenses/publicdomain.
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private void allocateElements(int numElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int initialCapacity = MIN_INITIAL_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // Find the best power of two to hold elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // Tests "<=" because arrays aren't kept full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (numElements >= initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            initialCapacity = numElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            initialCapacity |= (initialCapacity >>>  1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            initialCapacity |= (initialCapacity >>>  2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            initialCapacity |= (initialCapacity >>>  4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            initialCapacity |= (initialCapacity >>>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            initialCapacity |= (initialCapacity >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            initialCapacity++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (initialCapacity < 0)   // Too many elements, must back off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        elements = (E[]) new Object[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Double the capacity of this deque.  Call only when full, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * when head and tail have wrapped around to become equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private void doubleCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        assert head == tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int p = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int n = elements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int r = n - p; // number of elements to the right of p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int newCapacity = n << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (newCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalStateException("Sorry, deque too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Object[] a = new Object[newCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        System.arraycopy(elements, p, a, 0, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.arraycopy(elements, 0, a, r, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        elements = (E[])a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        head = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        tail = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Copies the elements from our element array into the specified array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * in order (from first to last element in the deque).  It is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * that the array is large enough to hold all elements in the deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return its argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private <T> T[] copyElements(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (head < tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            System.arraycopy(elements, head, a, 0, size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else if (head > tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int headPortionLen = elements.length - head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            System.arraycopy(elements, head, a, 0, headPortionLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.arraycopy(elements, 0, a, headPortionLen, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Constructs an empty array deque with an initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * sufficient to hold 16 elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public ArrayDeque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        elements = (E[]) new Object[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Constructs an empty array deque with an initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * sufficient to hold the specified number of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param numElements  lower bound on initial capacity of the deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public ArrayDeque(int numElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        allocateElements(numElements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Constructs a deque containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * iterator.  (The first element returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * iterator becomes the first element, or <i>front</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * deque.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param c the collection whose elements are to be placed into the deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public ArrayDeque(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        allocateElements(c.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // The main insertion and extraction methods are addFirst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // addLast, pollFirst, pollLast. The other methods are defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // terms of these.
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 front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public void addFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        elements[head = (head - 1) & (elements.length - 1)] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (head == tail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            doubleCapacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p>This method is equivalent to {@link #add}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public void addLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        elements[tail] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if ( (tail = (tail + 1) & (elements.length - 1)) == head)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            doubleCapacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Inserts the specified element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return <tt>true</tt> (as specified by {@link Deque#offerFirst})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public boolean offerFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return <tt>true</tt> (as specified by {@link Deque#offerLast})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public boolean offerLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public E removeFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        E x = pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public E removeLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        E x = pollLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public E pollFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        E result = elements[h]; // Element is null if deque empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        elements[h] = null;     // Must null out slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        head = (h + 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public E pollLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int t = (tail - 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        E result = elements[t];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        elements[t] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        tail = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public E getFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        E x = elements[head];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public E getLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        E x = elements[(tail - 1) & (elements.length - 1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public E peekFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return elements[head]; // elements[head] is null if deque empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public E peekLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return elements[(tail - 1) & (elements.length - 1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Removes the first occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * If the deque does not contain the element, it is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * More formally, removes the first element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <tt>o.equals(e)</tt> (if such an element exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns <tt>true</tt> if this deque contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param o element to be removed from this deque, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return <tt>true</tt> if the deque contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean removeFirstOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int i = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        E x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        while ( (x = elements[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (o.equals(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                delete(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            i = (i + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Removes the last occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * deque (when traversing the deque from head to tail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * If the deque does not contain the element, it is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * More formally, removes the last element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <tt>o.equals(e)</tt> (if such an element exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Returns <tt>true</tt> if this deque contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param o element to be removed from this deque, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return <tt>true</tt> if the deque contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public boolean removeLastOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        int i = (tail - 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        E x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        while ( (x = elements[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (o.equals(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                delete(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            i = (i - 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    // *** Queue methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <p>This method is equivalent to {@link #addLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Inserts the specified element at the end of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>This method is equivalent to {@link #offerLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return <tt>true</tt> (as specified by {@link Queue#offer})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return offerLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Retrieves and removes the head of the queue represented by this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * This method differs from {@link #poll poll} only in that it throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <p>This method is equivalent to {@link #removeFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Retrieves and removes the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * (in other words, the first element of this deque), or returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <tt>null</tt> if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <p>This method is equivalent to {@link #pollFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return the head of the queue represented by this deque, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *         <tt>null</tt> if this deque is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Retrieves, but does not remove, the head of the queue represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * this deque.  This method differs from {@link #peek peek} only in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * that it throws an exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <p>This method is equivalent to {@link #getFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Retrieves, but does not remove, the head of the queue represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * this deque, or returns <tt>null</tt> if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <p>This method is equivalent to {@link #peekFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return the head of the queue represented by this deque, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *         <tt>null</tt> if this deque is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return peekFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    // *** Stack methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Pushes an element onto the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * words, inserts the element at the front of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <p>This method is equivalent to {@link #addFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param e the element to push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public void push(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Pops an element from the stack represented by this deque.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * words, removes and returns the first element of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <p>This method is equivalent to {@link #removeFirst()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return the element at the front of this deque (which is the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *         of the stack represented by this deque)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    private void checkInvariants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        assert elements[tail] == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        assert head == tail ? elements[head] == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            (elements[head] != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             elements[(tail - 1) & (elements.length - 1)] != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        assert elements[(head - 1) & (elements.length - 1)] == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Removes the element at the specified position in the elements array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * adjusting head and tail as necessary.  This can result in motion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * elements backwards or forwards in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * <p>This method is called delete rather than remove to emphasize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * that its semantics differ from those of {@link List#remove(int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return true if elements moved backwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    private boolean delete(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        final E[] elements = this.elements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        final int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        final int h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        final int t = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        final int front = (i - h) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        final int back  = (t - i) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // Invariant: head <= i < tail mod circularity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (front >= ((t - h) & mask))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // Optimize for least element motion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (front < back) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (h <= i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                System.arraycopy(elements, h, elements, h + 1, front);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            } else { // Wrap around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                System.arraycopy(elements, 0, elements, 1, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                elements[0] = elements[mask];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                System.arraycopy(elements, h, elements, h + 1, mask - h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            elements[h] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            head = (h + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if (i < t) { // Copy the null tail as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                System.arraycopy(elements, i + 1, elements, i, back);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                tail = t - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            } else { // Wrap around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                System.arraycopy(elements, i + 1, elements, i, mask - i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                elements[mask] = elements[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                System.arraycopy(elements, 1, elements, 0, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                tail = (t - 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    // *** Collection Methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * Returns the number of elements in this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @return the number of elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return (tail - head) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Returns <tt>true</tt> if this deque contains no elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return <tt>true</tt> if this deque contains no elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return head == tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Returns an iterator over the elements in this deque.  The elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * will be ordered from first (head) to last (tail).  This is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * order that elements would be dequeued (via successive calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * {@link #remove} or popped (via successive calls to {@link #pop}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return an iterator over the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return new DeqIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return new DescendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private class DeqIterator implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * Index of element to be returned by subsequent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        private int cursor = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * Tail recorded at construction (also in remove), to stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * iterator and also to check for comodification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        private int fence = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * Index of element returned by most recent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * Reset to -1 if element is deleted by a call to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        private int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            return cursor != fence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (cursor == fence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            E result = elements[cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            // This check doesn't catch all possible comodifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            // but does catch the ones that corrupt traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if (tail != fence || result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            lastRet = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            cursor = (cursor + 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (delete(lastRet)) { // if left-shifted, undo increment in next()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                cursor = (cursor - 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                fence = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    private class DescendingIterator implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * This class is nearly a mirror-image of DeqIterator, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         * tail instead of head for initial cursor, and head instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * tail for fence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        private int cursor = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        private int fence = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        private int lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            return cursor != fence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if (cursor == fence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            cursor = (cursor - 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            E result = elements[cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (head != fence || result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            lastRet = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            if (!delete(lastRet)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                cursor = (cursor + 1) & (elements.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                fence = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Returns <tt>true</tt> if this deque contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * More formally, returns <tt>true</tt> if and only if this deque contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param o object to be checked for containment in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @return <tt>true</tt> if this deque contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        int i = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        E x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        while ( (x = elements[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (o.equals(x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            i = (i + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Removes a single instance of the specified element from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * If the deque does not contain the element, it is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * More formally, removes the first element <tt>e</tt> such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <tt>o.equals(e)</tt> (if such an element exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Returns <tt>true</tt> if this deque contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <p>This method is equivalent to {@link #removeFirstOccurrence}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @param o element to be removed from this deque, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @return <tt>true</tt> if this deque contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        return removeFirstOccurrence(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Removes all of the elements from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * The deque will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        int h = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        int t = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (h != t) { // clear all cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            head = tail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            int i = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                elements[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                i = (i + 1) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            } while (i != t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Returns an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * maintained by this deque.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        return copyElements(new Object[size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Returns an array containing all of the elements in this deque in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * proper sequence (from first to last element); the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * returned array is that of the specified array.  If the deque fits in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * the specified array, it is returned therein.  Otherwise, a new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * is allocated with the runtime type of the specified array and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * size of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * <p>If this deque fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * (i.e., the array has more elements than this deque), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * the array immediately following the end of the deque is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <p>Suppose <tt>x</tt> is a deque known to contain only strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * The following code can be used to dump the deque into a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * allocated array of <tt>String</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * <tt>toArray()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @param a the array into which the elements of the deque are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *         this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            a = (T[])java.lang.reflect.Array.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        copyElements(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    // *** Object methods ***
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * Returns a copy of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @return a copy of this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public ArrayDeque<E> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            result.elements = Arrays.copyOf(elements, elements.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Appease the serialization gods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    private static final long serialVersionUID = 2340985798034038923L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Serialize this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @serialData The current size (<tt>int</tt>) of the deque,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * followed by all of its elements (each an object reference) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * first-to-last order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        s.writeInt(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        // Write out elements in order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        int mask = elements.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        for (int i = head; i != tail; i = (i + 1) & mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            s.writeObject(elements[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * Deserialize this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        // Read in size and allocate array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        allocateElements(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        head = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        tail = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        // Read in all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            elements[i] = (E)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
}