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