jdk/src/share/classes/java/util/LinkedList.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4184 66ef929d58f2
child 6124 ba148d56813d
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4184
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4184
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4184
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4184
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4184
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4184
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    29
 * Linked list implementation of the {@code List} interface.  Implements all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * optional list operations, and permits all elements (including
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    31
 * {@code null}).  In addition to implementing the {@code List} interface,
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    32
 * the {@code LinkedList} class provides uniformly named methods to
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    33
 * {@code get}, {@code remove} and {@code insert} an element at the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * beginning and end of the list.  These operations allow linked lists to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * used as a stack, {@linkplain Queue queue}, or {@linkplain Deque
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    36
 * double-ended queue}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    38
 * <p>The class implements the {@code Deque} interface, providing
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    39
 * first-in-first-out queue operations for {@code add},
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    40
 * {@code poll}, along with other stack and deque operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    42
 * <p>All of the operations perform as could be expected for a doubly-linked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * list.  Operations that index into the list will traverse the list from
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    44
 * the beginning or the end, whichever is closer to the specified index.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * If multiple threads access a linked list concurrently, and at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * one of the threads modifies the list structurally, it <i>must</i> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * that adds or deletes one or more elements; merely setting the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * an element is not a structural modification.)  This is typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * accomplished by synchronizing on some object that naturally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * encapsulates the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * If no such object exists, the list should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link Collections#synchronizedList Collections.synchronizedList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * unsynchronized access to the list:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   List list = Collections.synchronizedList(new LinkedList(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    61
 * <p>The iterators returned by this class's {@code iterator} and
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    62
 * {@code listIterator} methods are <i>fail-fast</i>: if the list is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * structurally modified at any time after the iterator is created, in
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    64
 * any way except through the Iterator's own {@code remove} or
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    65
 * {@code add} methods, the iterator will throw a {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * modification, the iterator fails quickly and cleanly, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * risking arbitrary, non-deterministic behavior at an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    74
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * exception for its correctness:   <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see     List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see     ArrayList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
public class LinkedList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    extends AbstractSequentialList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
{
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    94
    transient int size = 0;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    95
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    96
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    97
     * Pointer to first node.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    98
     * Invariant: (first == null && last == null) ||
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
    99
     *            (first.prev == null && first.item != null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   100
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   101
    transient Node<E> first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   102
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   103
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   104
     * Pointer to last node.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   105
     * Invariant: (first == null && last == null) ||
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   106
     *            (last.next == null && last.item != null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   107
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   108
    transient Node<E> last;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Constructs an empty list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public LinkedList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Constructs a list containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param  c the collection whose elements are to be placed into this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public LinkedList(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   130
     * Links e as first element.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   131
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   132
    private void linkFirst(E e) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   133
        final Node<E> f = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   134
        final Node<E> newNode = new Node<E>(null, e, f);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   135
        first = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   136
        if (f == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   137
            last = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   138
        else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   139
            f.prev = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   140
        size++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   141
        modCount++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   142
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   143
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   144
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   145
     * Links e as last element.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   146
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   147
    void linkLast(E e) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   148
        final Node<E> l = last;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   149
        final Node<E> newNode = new Node<E>(l, e, null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   150
        last = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   151
        if (l == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   152
            first = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   153
        else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   154
            l.next = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   155
        size++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   156
        modCount++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   157
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   158
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   159
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   160
     * Inserts element e before non-null Node succ.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   161
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   162
    void linkBefore(E e, Node<E> succ) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   163
        // assert succ != null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   164
        final Node<E> pred = succ.prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   165
        final Node<E> newNode = new Node<E>(pred, e, succ);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   166
        succ.prev = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   167
        if (pred == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   168
            first = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   169
        else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   170
            pred.next = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   171
        size++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   172
        modCount++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   173
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   174
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   175
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   176
     * Unlinks non-null first node f.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   177
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   178
    private E unlinkFirst(Node<E> f) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   179
        // assert f == first && f != null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   180
        final E element = f.item;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   181
        final Node<E> next = f.next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   182
        f.item = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   183
        f.next = null; // help GC
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   184
        first = next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   185
        if (next == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   186
            last = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   187
        else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   188
            next.prev = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   189
        size--;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   190
        modCount++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   191
        return element;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   192
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   193
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   194
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   195
     * Unlinks non-null last node l.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   196
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   197
    private E unlinkLast(Node<E> l) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   198
        // assert l == last && l != null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   199
        final E element = l.item;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   200
        final Node<E> prev = l.prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   201
        l.item = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   202
        l.prev = null; // help GC
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   203
        last = prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   204
        if (prev == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   205
            first = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   206
        else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   207
            prev.next = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   208
        size--;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   209
        modCount++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   210
        return element;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   211
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   212
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   213
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   214
     * Unlinks non-null node x.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   215
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   216
    E unlink(Node<E> x) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   217
        // assert x != null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   218
        final E element = x.item;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   219
        final Node<E> next = x.next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   220
        final Node<E> prev = x.prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   221
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   222
        if (prev == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   223
            first = next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   224
        } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   225
            prev.next = next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   226
            x.prev = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   227
        }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   228
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   229
        if (next == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   230
            last = prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   231
        } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   232
            next.prev = prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   233
            x.next = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   234
        }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   235
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   236
        x.item = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   237
        size--;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   238
        modCount++;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   239
        return element;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   240
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   241
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   242
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns the first element in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return the first element in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public E getFirst() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   249
        final Node<E> f = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   250
        if (f == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new NoSuchElementException();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   252
        return f.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns the last element in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return the last element in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public E getLast()  {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   262
        final Node<E> l = last;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   263
        if (l == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new NoSuchElementException();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   265
        return l.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Removes and returns the first element from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return the first element from this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public E removeFirst() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   275
        final Node<E> f = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   276
        if (f == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   277
            throw new NoSuchElementException();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   278
        return unlinkFirst(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Removes and returns the last element from this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return the last element from this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public E removeLast() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   288
        final Node<E> l = last;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   289
        if (l == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   290
            throw new NoSuchElementException();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   291
        return unlinkLast(l);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Inserts the specified element at the beginning of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public void addFirst(E e) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   300
        linkFirst(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <p>This method is equivalent to {@link #add}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public void addLast(E e) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   311
        linkLast(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   315
     * Returns {@code true} if this list contains the specified element.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   316
     * More formally, returns {@code true} if and only if this list contains
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   317
     * at least one element {@code e} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param o element whose presence in this list is to be tested
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   321
     * @return {@code true} if this list contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return indexOf(o) != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns the number of elements in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return the number of elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Appends the specified element to the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <p>This method is equivalent to {@link #addLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param e element to be appended to this list
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   342
     * @return {@code true} (as specified by {@link Collection#add})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public boolean add(E e) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   345
        linkLast(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Removes the first occurrence of the specified element from this list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * if it is present.  If this list does not contain the element, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * unchanged.  More formally, removes the element with the lowest index
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   353
     * {@code i} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   355
     * (if such an element exists).  Returns {@code true} if this list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * contained the specified element (or equivalently, if this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param o element to be removed from this list, if present
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   360
     * @return {@code true} if this list contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public boolean remove(Object o) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   363
        if (o == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   364
            for (Node<E> x = first; x != null; x = x.next) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   365
                if (x.item == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   366
                    unlink(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } else {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   371
            for (Node<E> x = first; x != null; x = x.next) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   372
                if (o.equals(x.item)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   373
                    unlink(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Appends all of the elements in the specified collection to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * this list, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * collection's iterator.  The behavior of this operation is undefined if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * the specified collection is modified while the operation is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * progress.  (Note that this will occur if the specified collection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * this list, and it's nonempty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param c collection containing elements to be added to this list
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   390
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return addAll(size, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Inserts all of the elements in the specified collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * list, starting at the specified position.  Shifts the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * currently at that position (if any) and any subsequent elements to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * the right (increases their indices).  The new elements will appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * in the list in the order that they are returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * specified collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param index index at which to insert the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *              from the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param c collection containing elements to be added to this list
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   408
     * @return {@code true} if this list changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public boolean addAll(int index, Collection<? extends E> c) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   413
        checkPositionIndex(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   414
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        Object[] a = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int numNew = a.length;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   417
        if (numNew == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return false;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   419
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   420
        Node<E> pred, succ;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   421
        if (index == size) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   422
            succ = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   423
            pred = last;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   424
        } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   425
            succ = node(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   426
            pred = succ.prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   427
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   429
        for (Object o : a) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   430
            @SuppressWarnings("unchecked") E e = (E) o;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   431
            Node<E> newNode = new Node<E>(pred, e, null);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   432
            if (pred == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   433
                first = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   434
            else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   435
                pred.next = newNode;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   436
            pred = newNode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   438
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   439
        if (succ == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   440
            last = pred;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   441
        } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   442
            pred.next = succ;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   443
            succ.prev = pred;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   444
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        size += numNew;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   447
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Removes all of the elements from this list.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   453
     * The list will be empty after this call returns.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public void clear() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   456
        // Clearing all of the links between nodes is "unnecessary", but:
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   457
        // - helps a generational GC if the discarded nodes inhabit
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   458
        //   more than one generation
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   459
        // - is sure to free memory even if there is a reachable Iterator
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   460
        for (Node<E> x = first; x != null; ) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   461
            Node<E> next = x.next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   462
            x.item = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   463
            x.next = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   464
            x.prev = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   465
            x = next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   467
        first = last = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Returns the element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param index index of the element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @return the element at the specified position in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public E get(int index) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   483
        checkElementIndex(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   484
        return node(index).item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Replaces the element at the specified position in this list with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param index index of the element to replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param element element to be stored at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @return the element previously at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public E set(int index, E element) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   497
        checkElementIndex(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   498
        Node<E> x = node(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   499
        E oldVal = x.item;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   500
        x.item = element;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return oldVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Inserts the specified element at the specified position in this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Shifts the element currently at that position (if any) and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param index index at which the specified element is to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param element element to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public void add(int index, E element) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   514
        checkPositionIndex(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   515
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   516
        if (index == size)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   517
            linkLast(element);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   518
        else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   519
            linkBefore(element, node(index));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Removes the element at the specified position in this list.  Shifts any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * subsequent elements to the left (subtracts one from their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Returns the element that was removed from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param index the index of the element to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return the element previously at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public E remove(int index) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   532
        checkElementIndex(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   533
        return unlink(node(index));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   534
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   535
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   536
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   537
     * Tells if the argument is the index of an existing element.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   538
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   539
    private boolean isElementIndex(int index) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   540
        return index >= 0 && index < size;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   541
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   542
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   543
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   544
     * Tells if the argument is the index of a valid position for an
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   545
     * iterator or an add operation.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   546
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   547
    private boolean isPositionIndex(int index) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   548
        return index >= 0 && index <= size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   552
     * Constructs an IndexOutOfBoundsException detail message.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   553
     * Of the many possible refactorings of the error handling code,
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   554
     * this "outlining" performs best with both server and client VMs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   556
    private String outOfBoundsMsg(int index) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   557
        return "Index: "+index+", Size: "+size;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   558
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   559
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   560
    private void checkElementIndex(int index) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   561
        if (!isElementIndex(index))
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   562
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   563
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   564
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   565
    private void checkPositionIndex(int index) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   566
        if (!isPositionIndex(index))
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   567
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   570
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   571
     * Returns the (non-null) Node at the specified element index.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   572
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   573
    Node<E> node(int index) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   574
        // assert isElementIndex(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   575
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   576
        if (index < (size >> 1)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   577
            Node<E> x = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   578
            for (int i = 0; i < index; i++)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   579
                x = x.next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   580
            return x;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   581
        } else {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   582
            Node<E> x = last;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   583
            for (int i = size - 1; i > index; i--)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   584
                x = x.prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   585
            return x;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   586
        }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   587
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    // Search Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Returns the index of the first occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * in this list, or -1 if this list does not contain the element.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   594
     * More formally, returns the lowest index {@code i} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @return the index of the first occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *         this list, or -1 if this list does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        int index = 0;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   604
        if (o == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   605
            for (Node<E> x = first; x != null; x = x.next) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   606
                if (x.item == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        } else {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   611
            for (Node<E> x = first; x != null; x = x.next) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   612
                if (o.equals(x.item))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Returns the index of the last occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * in this list, or -1 if this list does not contain the element.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   623
     * More formally, returns the highest index {@code i} such that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @return the index of the last occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *         this list, or -1 if this list does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        int index = size;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   633
        if (o == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   634
            for (Node<E> x = last; x != null; x = x.prev) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                index--;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   636
                if (x.item == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } else {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   640
            for (Node<E> x = last; x != null; x = x.prev) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                index--;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   642
                if (o.equals(x.item))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    // Queue operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Retrieves, but does not remove, the head (first element) of this list.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   653
     *
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   654
     * @return the head of this list, or {@code null} if this list is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public E peek() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   658
        final Node<E> f = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   659
        return (f == null) ? null : f.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Retrieves, but does not remove, the head (first element) of this list.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   664
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @return the head of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   674
     * Retrieves and removes the head (first element) of this list.
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   675
     *
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   676
     * @return the head of this list, or {@code null} if this list is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public E poll() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   680
        final Node<E> f = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   681
        return (f == null) ? null : unlinkFirst(f);
2
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
     * Retrieves and removes the head (first element) of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return the head of this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * Adds the specified element as the tail (last element) of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @param e the element to add
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   699
     * @return {@code true} (as specified by {@link Queue#offer})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        return add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    // Deque operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Inserts the specified element at the front of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @param e the element to insert
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   711
     * @return {@code true} (as specified by {@link Deque#offerFirst})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public boolean offerFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Inserts the specified element at the end of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @param e the element to insert
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   723
     * @return {@code true} (as specified by {@link Deque#offerLast})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public boolean offerLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * Retrieves, but does not remove, the first element of this list,
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   733
     * or returns {@code null} if this list is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   735
     * @return the first element of this list, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *         if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public E peekFirst() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   740
        final Node<E> f = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   741
        return (f == null) ? null : f.item;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   742
     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * Retrieves, but does not remove, the last element of this list,
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   746
     * or returns {@code null} if this list is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   748
     * @return the last element of this list, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *         if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public E peekLast() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   753
        final Node<E> l = last;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   754
        return (l == null) ? null : l.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Retrieves and removes the first element of this list,
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   759
     * or returns {@code null} if this list is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   761
     * @return the first element of this list, or {@code null} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *     this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public E pollFirst() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   766
        final Node<E> f = first;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   767
        return (f == null) ? null : unlinkFirst(f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * Retrieves and removes the last element of this list,
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   772
     * or returns {@code null} if this list is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   774
     * @return the last element of this list, or {@code null} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *     this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public E pollLast() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   779
        final Node<E> l = last;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   780
        return (l == null) ? null : unlinkLast(l);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Pushes an element onto the stack represented by this list.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * words, inserts the element at the front of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * <p>This method is equivalent to {@link #addFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @param e the element to push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public void push(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Pops an element from the stack represented by this list.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * words, removes and returns the first element of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <p>This method is equivalent to {@link #removeFirst()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @return the element at the front of this list (which is the top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *         of the stack represented by this list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @throws NoSuchElementException if this list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    public E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * Removes the first occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * list (when traversing the list from head to tail).  If the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * does not contain the element, it is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param o element to be removed from this list, if present
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   817
     * @return {@code true} if the list contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    public boolean removeFirstOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        return remove(o);
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
     * Removes the last occurrence of the specified element in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * list (when traversing the list from head to tail).  If the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * does not contain the element, it is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @param o element to be removed from this list, if present
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   830
     * @return {@code true} if the list contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    public boolean removeLastOccurrence(Object o) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   834
        if (o == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   835
            for (Node<E> x = last; x != null; x = x.prev) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   836
                if (x.item == null) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   837
                    unlink(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        } else {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   842
            for (Node<E> x = last; x != null; x = x.prev) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   843
                if (o.equals(x.item)) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   844
                    unlink(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Returns a list-iterator of the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * sequence), starting at the specified position in the list.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   855
     * Obeys the general contract of {@code List.listIterator(int)}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * The list-iterator is <i>fail-fast</i>: if the list is structurally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * modified at any time after the Iterator is created, in any way except
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   859
     * through the list-iterator's own {@code remove} or {@code add}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * methods, the list-iterator will throw a
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   861
     * {@code ConcurrentModificationException}.  Thus, in the face of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * concurrent modification, the iterator fails quickly and cleanly, rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * than risking arbitrary, non-deterministic behavior at an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @param index index of the first element to be returned from the
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   867
     *              list-iterator (by a call to {@code next})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @return a ListIterator of the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *         sequence), starting at the specified position in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @see List#listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    public ListIterator<E> listIterator(int index) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   874
        checkPositionIndex(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    private class ListItr implements ListIterator<E> {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   879
        private Node<E> lastReturned = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   880
        private Node<E> next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        private int nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        private int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        ListItr(int index) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   885
            // assert isPositionIndex(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   886
            next = (index == size) ? null : node(index);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   887
            nextIndex = index;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        public boolean hasNext() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   891
            return nextIndex < size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            checkForComodification();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   896
            if (!hasNext())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            lastReturned = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            next = next.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            nextIndex++;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   902
            return lastReturned.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        public boolean hasPrevious() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   906
            return nextIndex > 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        public E previous() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   910
            checkForComodification();
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   911
            if (!hasPrevious())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   914
            lastReturned = next = (next == null) ? last : next.prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            nextIndex--;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   916
            return lastReturned.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            return nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        public int previousIndex() {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   924
            return nextIndex - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            checkForComodification();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   929
            if (lastReturned == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                throw new IllegalStateException();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   931
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   932
            Node<E> lastNext = lastReturned.next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   933
            unlink(lastReturned);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   934
            if (next == lastReturned)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                next = lastNext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                nextIndex--;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   938
            lastReturned = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            expectedModCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        public void set(E e) {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   943
            if (lastReturned == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            checkForComodification();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   946
            lastReturned.item = e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            checkForComodification();
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   951
            lastReturned = null;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   952
            if (next == null)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   953
                linkLast(e);
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   954
            else
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   955
                linkBefore(e, next);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            nextIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            expectedModCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   966
    private static class Node<E> {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   967
        E item;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   968
        Node<E> next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   969
        Node<E> prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   971
        Node(Node<E> prev, E element, Node<E> next) {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   972
            this.item = element;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   973
            this.next = next;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   974
            this.prev = prev;
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   975
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        return new DescendingIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   985
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   986
     * Adapter to provide descending iterators via ListItr.previous
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   987
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   988
    private class DescendingIterator implements Iterator<E> {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
   989
        private final ListItr itr = new ListItr(size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            return itr.hasPrevious();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            return itr.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            itr.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1001
    @SuppressWarnings("unchecked")
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1002
    private LinkedList<E> superClone() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        try {
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1004
            return (LinkedList<E>) super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1008
    }
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1009
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1010
    /**
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1011
     * Returns a shallow copy of this {@code LinkedList}. (The elements
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1012
     * themselves are not cloned.)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1013
     *
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1014
     * @return a shallow copy of this {@code LinkedList} instance
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1015
     */
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1016
    public Object clone() {
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1017
        LinkedList<E> clone = superClone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // Put clone into "virgin" state
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1020
        clone.first = clone.last = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        clone.size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        clone.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        // Initialize clone with our elements
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1025
        for (Node<E> x = first; x != null; x = x.next)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1026
            clone.add(x.item);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * Returns an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * in proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * maintained by this list.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @return an array containing all of the elements in this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *         in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        Object[] result = new Object[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        int i = 0;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1048
        for (Node<E> x = first; x != null; x = x.next)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1049
            result[i++] = x.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Returns an array containing all of the elements in this list in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * proper sequence (from first to last element); the runtime type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * the returned array is that of the specified array.  If the list fits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * in the specified array, it is returned therein.  Otherwise, a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * array is allocated with the runtime type of the specified array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * the size of this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * <p>If the list fits in the specified array with room to spare (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * the array has more elements than the list), the element in the array
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1063
     * immediately following the end of the list is set to {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * (This is useful in determining the length of the list <i>only</i> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * the caller knows that the list does not contain any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1072
     * <p>Suppose {@code x} is a list known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * The following code can be used to dump the list into a newly
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1074
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1079
     * Note that {@code toArray(new Object[0])} is identical in function to
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1080
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @param a the array into which the elements of the list are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * @return an array containing the elements of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *         this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     */
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1091
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (a.length < size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            a = (T[])java.lang.reflect.Array.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                                a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        Object[] result = a;
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1098
        for (Node<E> x = first; x != null; x = x.next)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1099
            result[i++] = x.item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (a.length > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            a[size] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    private static final long serialVersionUID = 876323262645176354L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    /**
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1110
     * Saves the state of this {@code LinkedList} instance to a stream
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1111
     * (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @serialData The size of the list (the number of elements it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *             contains) is emitted (int), followed by all of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *             elements (each an Object) in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        // Write out any hidden serialization magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        // Write out size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        // Write out all elements in the proper order.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1126
        for (Node<E> x = first; x != null; x = x.next)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1127
            s.writeObject(x.item);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    /**
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1131
     * Reconstitutes this {@code LinkedList} instance from a stream
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1132
     * (that is, deserializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     */
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1134
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        // Read in any hidden serialization magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        // Read in size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        int size = s.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // Read in all elements in the proper order.
4184
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1144
        for (int i = 0; i < size; i++)
66ef929d58f2 6897553: LinkedList performance improvements
martin
parents: 2
diff changeset
  1145
            linkLast((E)s.readObject());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
}