|
|
|
|
@ -25,8 +25,8 @@
|
|
|
|
|
|
|
|
|
|
package cc.maxmc.blastingcrisis.misc;
|
|
|
|
|
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Doubly-linked list implementation of the {@code List} and {@code Deque}
|
|
|
|
|
@ -73,26 +73,24 @@ import java.util.*;
|
|
|
|
|
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
|
|
|
|
* Java Collections Framework</a>.
|
|
|
|
|
*
|
|
|
|
|
* @param <E> the type of elements held in this collection
|
|
|
|
|
* @author Josh Bloch
|
|
|
|
|
* @see List
|
|
|
|
|
* @see ArrayList
|
|
|
|
|
* @since 1.2
|
|
|
|
|
* @param <E> the type of elements held in this collection
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class EnhancedLinkedList<E>
|
|
|
|
|
extends AbstractSequentialList<E>
|
|
|
|
|
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
|
|
|
|
|
{
|
|
|
|
|
implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
|
|
|
|
|
private static final long serialVersionUID = 876323262645176354L;
|
|
|
|
|
transient int size = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pointer to first node.
|
|
|
|
|
* Invariant: (first == null && last == null) ||
|
|
|
|
|
* (first.prev == null && first.item != null)
|
|
|
|
|
*/
|
|
|
|
|
transient Node<E> first;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pointer to last node.
|
|
|
|
|
* Invariant: (first == null && last == null) ||
|
|
|
|
|
@ -441,6 +439,9 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Positional Access Operations
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes all of the elements from this list.
|
|
|
|
|
* The list will be empty after this call returns.
|
|
|
|
|
@ -462,9 +463,6 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
modCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Positional Access Operations
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the element at the specified position in this list.
|
|
|
|
|
*
|
|
|
|
|
@ -578,6 +576,8 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search Operations
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the (non-null) Node at the specified element index.
|
|
|
|
|
*/
|
|
|
|
|
@ -597,8 +597,6 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search Operations
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the index of the first occurrence of the specified element
|
|
|
|
|
* in this list, or -1 if this list does not contain the element.
|
|
|
|
|
@ -643,6 +641,8 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Queue operations.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the index of the last occurrence of the specified element
|
|
|
|
|
* in this list, or -1 if this list does not contain the element.
|
|
|
|
|
@ -672,8 +672,6 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Queue operations.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves, but does not remove, the head (first element) of this list.
|
|
|
|
|
*
|
|
|
|
|
@ -718,6 +716,8 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
return removeFirst();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deque operations
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds the specified element as the tail (last element) of this list.
|
|
|
|
|
*
|
|
|
|
|
@ -729,7 +729,6 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
return add(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deque operations
|
|
|
|
|
/**
|
|
|
|
|
* Inserts the specified element at the front of this list.
|
|
|
|
|
*
|
|
|
|
|
@ -879,7 +878,7 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
* Returns a list-iterator of the elements in this list (in proper
|
|
|
|
|
* sequence), starting at the specified position in the list.
|
|
|
|
|
* Obeys the general contract of {@code List.listIterator(int)}.<p>
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* The list-iterator is <i>fail-fast</i>: if the list is structurally
|
|
|
|
|
* modified at any time after the Iterator is created, in any way except
|
|
|
|
|
* through the list-iterator's own {@code remove} or {@code add}
|
|
|
|
|
@ -901,6 +900,286 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
return new ListItr(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.6
|
|
|
|
|
*/
|
|
|
|
|
public Iterator<E> descendingIterator() {
|
|
|
|
|
return new DescendingIterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private EnhancedLinkedList<E> superClone() {
|
|
|
|
|
try {
|
|
|
|
|
return (EnhancedLinkedList<E>) super.clone();
|
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
|
throw new InternalError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a shallow copy of this {@code LinkedList}. (The elements
|
|
|
|
|
* themselves are not cloned.)
|
|
|
|
|
*
|
|
|
|
|
* @return a shallow copy of this {@code LinkedList} instance
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object clone() {
|
|
|
|
|
EnhancedLinkedList<E> clone = superClone();
|
|
|
|
|
|
|
|
|
|
// Put clone into "virgin" state
|
|
|
|
|
clone.first = clone.last = null;
|
|
|
|
|
clone.size = 0;
|
|
|
|
|
clone.modCount = 0;
|
|
|
|
|
|
|
|
|
|
// Initialize clone with our elements
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
clone.add(x.item);
|
|
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array containing all of the elements in this list
|
|
|
|
|
* in proper sequence (from first to last element).
|
|
|
|
|
*
|
|
|
|
|
* <p>The returned array will be "safe" in that no references to it are
|
|
|
|
|
* maintained by this list. (In other words, this method must allocate
|
|
|
|
|
* a new array). The caller is thus free to modify the returned array.
|
|
|
|
|
*
|
|
|
|
|
* <p>This method acts as bridge between array-based and collection-based
|
|
|
|
|
* APIs.
|
|
|
|
|
*
|
|
|
|
|
* @return an array containing all of the elements in this list
|
|
|
|
|
* in proper sequence
|
|
|
|
|
*/
|
|
|
|
|
public Object[] toArray() {
|
|
|
|
|
Object[] result = new Object[size];
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
result[i++] = x.item;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array containing all of the elements in this list in
|
|
|
|
|
* proper sequence (from first to last element); the runtime type of
|
|
|
|
|
* the returned array is that of the specified array. If the list fits
|
|
|
|
|
* in the specified array, it is returned therein. Otherwise, a new
|
|
|
|
|
* array is allocated with the runtime type of the specified array and
|
|
|
|
|
* the size of this list.
|
|
|
|
|
*
|
|
|
|
|
* <p>If the list fits in the specified array with room to spare (i.e.,
|
|
|
|
|
* the array has more elements than the list), the element in the array
|
|
|
|
|
* immediately following the end of the list is set to {@code null}.
|
|
|
|
|
* (This is useful in determining the length of the list <i>only</i> if
|
|
|
|
|
* the caller knows that the list does not contain any null elements.)
|
|
|
|
|
*
|
|
|
|
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between
|
|
|
|
|
* array-based and collection-based APIs. Further, this method allows
|
|
|
|
|
* precise control over the runtime type of the output array, and may,
|
|
|
|
|
* under certain circumstances, be used to save allocation costs.
|
|
|
|
|
*
|
|
|
|
|
* <p>Suppose {@code x} is a list known to contain only strings.
|
|
|
|
|
* The following code can be used to dump the list into a newly
|
|
|
|
|
* allocated array of {@code String}:
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* String[] y = x.toArray(new String[0]);</pre>
|
|
|
|
|
* <p>
|
|
|
|
|
* Note that {@code toArray(new Object[0])} is identical in function to
|
|
|
|
|
* {@code toArray()}.
|
|
|
|
|
*
|
|
|
|
|
* @param a the array into which the elements of the list are to
|
|
|
|
|
* be stored, if it is big enough; otherwise, a new array of the
|
|
|
|
|
* same runtime type is allocated for this purpose.
|
|
|
|
|
* @return an array containing the elements of the list
|
|
|
|
|
* @throws ArrayStoreException if the runtime type of the specified array
|
|
|
|
|
* is not a supertype of the runtime type of every element in
|
|
|
|
|
* this list
|
|
|
|
|
* @throws NullPointerException if the specified array is null
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> T[] toArray(T[] a) {
|
|
|
|
|
if (a.length < size)
|
|
|
|
|
a = (T[]) java.lang.reflect.Array.newInstance(
|
|
|
|
|
a.getClass().getComponentType(), size);
|
|
|
|
|
int i = 0;
|
|
|
|
|
Object[] result = a;
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
result[i++] = x.item;
|
|
|
|
|
|
|
|
|
|
if (a.length > size)
|
|
|
|
|
a[size] = null;
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Saves the state of this {@code LinkedList} instance to a stream
|
|
|
|
|
* (that is, serializes it).
|
|
|
|
|
*
|
|
|
|
|
* @serialData The size of the list (the number of elements it
|
|
|
|
|
* contains) is emitted (int), followed by all of its
|
|
|
|
|
* elements (each an Object) in the proper order.
|
|
|
|
|
*/
|
|
|
|
|
private void writeObject(java.io.ObjectOutputStream s)
|
|
|
|
|
throws java.io.IOException {
|
|
|
|
|
// Write out any hidden serialization magic
|
|
|
|
|
s.defaultWriteObject();
|
|
|
|
|
|
|
|
|
|
// Write out size
|
|
|
|
|
s.writeInt(size);
|
|
|
|
|
|
|
|
|
|
// Write out all elements in the proper order.
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
s.writeObject(x.item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reconstitutes this {@code LinkedList} instance from a stream
|
|
|
|
|
* (that is, deserializes it).
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private void readObject(java.io.ObjectInputStream s)
|
|
|
|
|
throws java.io.IOException, ClassNotFoundException {
|
|
|
|
|
// Read in any hidden serialization magic
|
|
|
|
|
s.defaultReadObject();
|
|
|
|
|
|
|
|
|
|
// Read in size
|
|
|
|
|
int size = s.readInt();
|
|
|
|
|
|
|
|
|
|
// Read in all elements in the proper order.
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
linkLast((E) s.readObject());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
|
|
|
|
|
* and <em>fail-fast</em> {@link Spliterator} over the elements in this
|
|
|
|
|
* list.
|
|
|
|
|
*
|
|
|
|
|
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
|
|
|
|
|
* {@link Spliterator#ORDERED}. Overriding implementations should document
|
|
|
|
|
* the reporting of additional characteristic values.
|
|
|
|
|
*
|
|
|
|
|
* @return a {@code Spliterator} over the elements in this list
|
|
|
|
|
* @implNote The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED}
|
|
|
|
|
* and implements {@code trySplit} to permit limited parallelism..
|
|
|
|
|
* @since 1.8
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Spliterator<E> spliterator() {
|
|
|
|
|
return new LLSpliterator<>(this, -1, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class Node<E> {
|
|
|
|
|
E item;
|
|
|
|
|
Node<E> next;
|
|
|
|
|
Node<E> prev;
|
|
|
|
|
|
|
|
|
|
Node(Node<E> prev, E element, Node<E> next) {
|
|
|
|
|
this.item = element;
|
|
|
|
|
this.next = next;
|
|
|
|
|
this.prev = prev;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A customized variant of Spliterators.IteratorSpliterator
|
|
|
|
|
*/
|
|
|
|
|
static final class LLSpliterator<E> implements Spliterator<E> {
|
|
|
|
|
static final int BATCH_UNIT = 1 << 10; // batch array size increment
|
|
|
|
|
static final int MAX_BATCH = 1 << 25; // max batch array size;
|
|
|
|
|
final EnhancedLinkedList<E> list; // null OK unless traversed
|
|
|
|
|
Node<E> current; // current node; null until initialized
|
|
|
|
|
int est; // size estimate; -1 until first needed
|
|
|
|
|
int expectedModCount; // initialized when est set
|
|
|
|
|
int batch; // batch size for splits
|
|
|
|
|
|
|
|
|
|
LLSpliterator(EnhancedLinkedList<E> list, int est, int expectedModCount) {
|
|
|
|
|
this.list = list;
|
|
|
|
|
this.est = est;
|
|
|
|
|
this.expectedModCount = expectedModCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getEst() {
|
|
|
|
|
int s; // force initialization
|
|
|
|
|
final EnhancedLinkedList<E> lst;
|
|
|
|
|
if ((s = est) < 0) {
|
|
|
|
|
if ((lst = list) == null)
|
|
|
|
|
s = est = 0;
|
|
|
|
|
else {
|
|
|
|
|
expectedModCount = lst.modCount;
|
|
|
|
|
current = lst.first;
|
|
|
|
|
s = est = lst.size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long estimateSize() {
|
|
|
|
|
return getEst();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Spliterator<E> trySplit() {
|
|
|
|
|
Node<E> p;
|
|
|
|
|
int s = getEst();
|
|
|
|
|
if (s > 1 && (p = current) != null) {
|
|
|
|
|
int n = batch + BATCH_UNIT;
|
|
|
|
|
if (n > s)
|
|
|
|
|
n = s;
|
|
|
|
|
if (n > MAX_BATCH)
|
|
|
|
|
n = MAX_BATCH;
|
|
|
|
|
Object[] a = new Object[n];
|
|
|
|
|
int j = 0;
|
|
|
|
|
do {
|
|
|
|
|
a[j++] = p.item;
|
|
|
|
|
} while ((p = p.next) != null && j < n);
|
|
|
|
|
current = p;
|
|
|
|
|
batch = j;
|
|
|
|
|
est = s - j;
|
|
|
|
|
return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void forEachRemaining(Consumer<? super E> action) {
|
|
|
|
|
Node<E> p;
|
|
|
|
|
int n;
|
|
|
|
|
if (action == null) throw new NullPointerException();
|
|
|
|
|
if ((n = getEst()) > 0 && (p = current) != null) {
|
|
|
|
|
current = null;
|
|
|
|
|
est = 0;
|
|
|
|
|
do {
|
|
|
|
|
E e = p.item;
|
|
|
|
|
p = p.next;
|
|
|
|
|
action.accept(e);
|
|
|
|
|
} while (p != null && --n > 0);
|
|
|
|
|
}
|
|
|
|
|
if (list.modCount != expectedModCount)
|
|
|
|
|
throw new ConcurrentModificationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean tryAdvance(Consumer<? super E> action) {
|
|
|
|
|
Node<E> p;
|
|
|
|
|
if (action == null) throw new NullPointerException();
|
|
|
|
|
if (getEst() > 0 && (p = current) != null) {
|
|
|
|
|
--est;
|
|
|
|
|
E e = p.item;
|
|
|
|
|
current = p.next;
|
|
|
|
|
action.accept(e);
|
|
|
|
|
if (list.modCount != expectedModCount)
|
|
|
|
|
throw new ConcurrentModificationException();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int characteristics() {
|
|
|
|
|
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class ListItr implements ListIterator<E> {
|
|
|
|
|
private Node<E> lastReturned;
|
|
|
|
|
private Node<E> next;
|
|
|
|
|
@ -1000,297 +1279,23 @@ public class EnhancedLinkedList<E>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class Node<E> {
|
|
|
|
|
E item;
|
|
|
|
|
Node<E> next;
|
|
|
|
|
Node<E> prev;
|
|
|
|
|
|
|
|
|
|
Node(Node<E> prev, E element, Node<E> next) {
|
|
|
|
|
this.item = element;
|
|
|
|
|
this.next = next;
|
|
|
|
|
this.prev = prev;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.6
|
|
|
|
|
*/
|
|
|
|
|
public Iterator<E> descendingIterator() {
|
|
|
|
|
return new DescendingIterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adapter to provide descending iterators via ListItr.previous
|
|
|
|
|
*/
|
|
|
|
|
private class DescendingIterator implements Iterator<E> {
|
|
|
|
|
private final ListItr itr = new ListItr(size());
|
|
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return itr.hasPrevious();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public E next() {
|
|
|
|
|
return itr.previous();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void remove() {
|
|
|
|
|
itr.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private EnhancedLinkedList<E> superClone() {
|
|
|
|
|
try {
|
|
|
|
|
return (EnhancedLinkedList<E>) super.clone();
|
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
|
throw new InternalError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a shallow copy of this {@code LinkedList}. (The elements
|
|
|
|
|
* themselves are not cloned.)
|
|
|
|
|
*
|
|
|
|
|
* @return a shallow copy of this {@code LinkedList} instance
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Object clone() {
|
|
|
|
|
EnhancedLinkedList<E> clone = superClone();
|
|
|
|
|
|
|
|
|
|
// Put clone into "virgin" state
|
|
|
|
|
clone.first = clone.last = null;
|
|
|
|
|
clone.size = 0;
|
|
|
|
|
clone.modCount = 0;
|
|
|
|
|
|
|
|
|
|
// Initialize clone with our elements
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
clone.add(x.item);
|
|
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array containing all of the elements in this list
|
|
|
|
|
* in proper sequence (from first to last element).
|
|
|
|
|
*
|
|
|
|
|
* <p>The returned array will be "safe" in that no references to it are
|
|
|
|
|
* maintained by this list. (In other words, this method must allocate
|
|
|
|
|
* a new array). The caller is thus free to modify the returned array.
|
|
|
|
|
*
|
|
|
|
|
* <p>This method acts as bridge between array-based and collection-based
|
|
|
|
|
* APIs.
|
|
|
|
|
*
|
|
|
|
|
* @return an array containing all of the elements in this list
|
|
|
|
|
* in proper sequence
|
|
|
|
|
*/
|
|
|
|
|
public Object[] toArray() {
|
|
|
|
|
Object[] result = new Object[size];
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
result[i++] = x.item;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array containing all of the elements in this list in
|
|
|
|
|
* proper sequence (from first to last element); the runtime type of
|
|
|
|
|
* the returned array is that of the specified array. If the list fits
|
|
|
|
|
* in the specified array, it is returned therein. Otherwise, a new
|
|
|
|
|
* array is allocated with the runtime type of the specified array and
|
|
|
|
|
* the size of this list.
|
|
|
|
|
*
|
|
|
|
|
* <p>If the list fits in the specified array with room to spare (i.e.,
|
|
|
|
|
* the array has more elements than the list), the element in the array
|
|
|
|
|
* immediately following the end of the list is set to {@code null}.
|
|
|
|
|
* (This is useful in determining the length of the list <i>only</i> if
|
|
|
|
|
* the caller knows that the list does not contain any null elements.)
|
|
|
|
|
*
|
|
|
|
|
* <p>Like the {@link #toArray()} method, this method acts as bridge between
|
|
|
|
|
* array-based and collection-based APIs. Further, this method allows
|
|
|
|
|
* precise control over the runtime type of the output array, and may,
|
|
|
|
|
* under certain circumstances, be used to save allocation costs.
|
|
|
|
|
*
|
|
|
|
|
* <p>Suppose {@code x} is a list known to contain only strings.
|
|
|
|
|
* The following code can be used to dump the list into a newly
|
|
|
|
|
* allocated array of {@code String}:
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* String[] y = x.toArray(new String[0]);</pre>
|
|
|
|
|
*
|
|
|
|
|
* Note that {@code toArray(new Object[0])} is identical in function to
|
|
|
|
|
* {@code toArray()}.
|
|
|
|
|
*
|
|
|
|
|
* @param a the array into which the elements of the list are to
|
|
|
|
|
* be stored, if it is big enough; otherwise, a new array of the
|
|
|
|
|
* same runtime type is allocated for this purpose.
|
|
|
|
|
* @return an array containing the elements of the list
|
|
|
|
|
* @throws ArrayStoreException if the runtime type of the specified array
|
|
|
|
|
* is not a supertype of the runtime type of every element in
|
|
|
|
|
* this list
|
|
|
|
|
* @throws NullPointerException if the specified array is null
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> T[] toArray(T[] a) {
|
|
|
|
|
if (a.length < size)
|
|
|
|
|
a = (T[])java.lang.reflect.Array.newInstance(
|
|
|
|
|
a.getClass().getComponentType(), size);
|
|
|
|
|
int i = 0;
|
|
|
|
|
Object[] result = a;
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
result[i++] = x.item;
|
|
|
|
|
|
|
|
|
|
if (a.length > size)
|
|
|
|
|
a[size] = null;
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 876323262645176354L;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Saves the state of this {@code LinkedList} instance to a stream
|
|
|
|
|
* (that is, serializes it).
|
|
|
|
|
*
|
|
|
|
|
* @serialData The size of the list (the number of elements it
|
|
|
|
|
* contains) is emitted (int), followed by all of its
|
|
|
|
|
* elements (each an Object) in the proper order.
|
|
|
|
|
*/
|
|
|
|
|
private void writeObject(java.io.ObjectOutputStream s)
|
|
|
|
|
throws java.io.IOException {
|
|
|
|
|
// Write out any hidden serialization magic
|
|
|
|
|
s.defaultWriteObject();
|
|
|
|
|
|
|
|
|
|
// Write out size
|
|
|
|
|
s.writeInt(size);
|
|
|
|
|
|
|
|
|
|
// Write out all elements in the proper order.
|
|
|
|
|
for (Node<E> x = first; x != null; x = x.next)
|
|
|
|
|
s.writeObject(x.item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reconstitutes this {@code LinkedList} instance from a stream
|
|
|
|
|
* (that is, deserializes it).
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private void readObject(java.io.ObjectInputStream s)
|
|
|
|
|
throws java.io.IOException, ClassNotFoundException {
|
|
|
|
|
// Read in any hidden serialization magic
|
|
|
|
|
s.defaultReadObject();
|
|
|
|
|
|
|
|
|
|
// Read in size
|
|
|
|
|
int size = s.readInt();
|
|
|
|
|
|
|
|
|
|
// Read in all elements in the proper order.
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
linkLast((E)s.readObject());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
|
|
|
|
|
* and <em>fail-fast</em> {@link Spliterator} over the elements in this
|
|
|
|
|
* list.
|
|
|
|
|
*
|
|
|
|
|
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
|
|
|
|
|
* {@link Spliterator#ORDERED}. Overriding implementations should document
|
|
|
|
|
* the reporting of additional characteristic values.
|
|
|
|
|
*
|
|
|
|
|
* @implNote
|
|
|
|
|
* The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED}
|
|
|
|
|
* and implements {@code trySplit} to permit limited parallelism..
|
|
|
|
|
*
|
|
|
|
|
* @return a {@code Spliterator} over the elements in this list
|
|
|
|
|
* @since 1.8
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Spliterator<E> spliterator() {
|
|
|
|
|
return new LLSpliterator<>(this, -1, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A customized variant of Spliterators.IteratorSpliterator */
|
|
|
|
|
static final class LLSpliterator<E> implements Spliterator<E> {
|
|
|
|
|
static final int BATCH_UNIT = 1 << 10; // batch array size increment
|
|
|
|
|
static final int MAX_BATCH = 1 << 25; // max batch array size;
|
|
|
|
|
final EnhancedLinkedList<E> list; // null OK unless traversed
|
|
|
|
|
Node<E> current; // current node; null until initialized
|
|
|
|
|
int est; // size estimate; -1 until first needed
|
|
|
|
|
int expectedModCount; // initialized when est set
|
|
|
|
|
int batch; // batch size for splits
|
|
|
|
|
|
|
|
|
|
LLSpliterator(EnhancedLinkedList<E> list, int est, int expectedModCount) {
|
|
|
|
|
this.list = list;
|
|
|
|
|
this.est = est;
|
|
|
|
|
this.expectedModCount = expectedModCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getEst() {
|
|
|
|
|
int s; // force initialization
|
|
|
|
|
final EnhancedLinkedList<E> lst;
|
|
|
|
|
if ((s = est) < 0) {
|
|
|
|
|
if ((lst = list) == null)
|
|
|
|
|
s = est = 0;
|
|
|
|
|
else {
|
|
|
|
|
expectedModCount = lst.modCount;
|
|
|
|
|
current = lst.first;
|
|
|
|
|
s = est = lst.size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long estimateSize() { return getEst(); }
|
|
|
|
|
|
|
|
|
|
public Spliterator<E> trySplit() {
|
|
|
|
|
Node<E> p;
|
|
|
|
|
int s = getEst();
|
|
|
|
|
if (s > 1 && (p = current) != null) {
|
|
|
|
|
int n = batch + BATCH_UNIT;
|
|
|
|
|
if (n > s)
|
|
|
|
|
n = s;
|
|
|
|
|
if (n > MAX_BATCH)
|
|
|
|
|
n = MAX_BATCH;
|
|
|
|
|
Object[] a = new Object[n];
|
|
|
|
|
int j = 0;
|
|
|
|
|
do { a[j++] = p.item; } while ((p = p.next) != null && j < n);
|
|
|
|
|
current = p;
|
|
|
|
|
batch = j;
|
|
|
|
|
est = s - j;
|
|
|
|
|
return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void forEachRemaining(Consumer<? super E> action) {
|
|
|
|
|
Node<E> p; int n;
|
|
|
|
|
if (action == null) throw new NullPointerException();
|
|
|
|
|
if ((n = getEst()) > 0 && (p = current) != null) {
|
|
|
|
|
current = null;
|
|
|
|
|
est = 0;
|
|
|
|
|
do {
|
|
|
|
|
E e = p.item;
|
|
|
|
|
p = p.next;
|
|
|
|
|
action.accept(e);
|
|
|
|
|
} while (p != null && --n > 0);
|
|
|
|
|
}
|
|
|
|
|
if (list.modCount != expectedModCount)
|
|
|
|
|
throw new ConcurrentModificationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean tryAdvance(Consumer<? super E> action) {
|
|
|
|
|
Node<E> p;
|
|
|
|
|
if (action == null) throw new NullPointerException();
|
|
|
|
|
if (getEst() > 0 && (p = current) != null) {
|
|
|
|
|
--est;
|
|
|
|
|
E e = p.item;
|
|
|
|
|
current = p.next;
|
|
|
|
|
action.accept(e);
|
|
|
|
|
if (list.modCount != expectedModCount)
|
|
|
|
|
throw new ConcurrentModificationException();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int characteristics() {
|
|
|
|
|
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|