E
- the type of elements in this listpublic class SpinedBuffer<E> extends Object implements Consumer<E>, Iterable<E>
One or more arrays are used to store elements. The use of a multiple
arrays has better performance characteristics than a single array used by
ArrayList
, as when the capacity of the list needs to be increased
no copying of elements is required. This is usually beneficial in the case
where the results will be traversed a small number of times.
Modifier and Type | Class and Description |
---|---|
static class |
SpinedBuffer.OfDouble
An ordered collection of
double values. |
static class |
SpinedBuffer.OfInt
An ordered collection of
int values. |
static class |
SpinedBuffer.OfLong
An ordered collection of
long values. |
static class |
SpinedBuffer.OfPrimitive<E,T_ARR,T_CONS>
An ordered collection of primitive values.
|
Modifier and Type | Field and Description |
---|---|
protected E[] |
curChunk
Chunk that we're currently writing into; may or may not be aliased with
the first element of the spine.
|
protected int |
elementIndex
Index of the *next* element to write; may point into, or just outside of,
the current chunk.
|
protected int |
initialChunkPower
log2 of the size of the first chunk.
|
static int |
MAX_CHUNK_POWER
Max power-of-two for chunks.
|
static int |
MIN_CHUNK_POWER
Minimum power-of-two for the first chunk.
|
static int |
MIN_CHUNK_SIZE
Minimum size for the first chunk.
|
static int |
MIN_SPINE_SIZE
Minimum array size for array-of-chunks.
|
protected long[] |
priorElementCount
Count of elements in all prior chunks.
|
protected E[][] |
spine
All chunks, or null if there is only one chunk.
|
protected int |
spineIndex
Index of the *current* chunk in the spine array, if the spine array is
non-null.
|
Constructor and Description |
---|
SpinedBuffer()
Constructs an empty list with an initial capacity of sixteen.
|
SpinedBuffer(int initialCapacity)
Constructs an empty list with the specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
accept(E e)
Performs this operation on the given argument.
|
E[] |
asArray(IntFunction<E[]> arrayFactory)
Create a new array using the specified array factory, and copy the
elements into it.
|
protected long |
capacity()
Returns the current capacity of the buffer
|
protected int |
chunkSize(int n)
How big should the nth chunk be?
|
void |
clear()
Remove all data from the buffer
|
void |
copyInto(E[] array,
int offset)
Copy the elements, starting at the specified offset, into the specified
array.
|
long |
count()
How many elements are currently in the buffer?
|
protected void |
ensureCapacity(long targetSize)
Ensure that the buffer has at least capacity to hold the target size
|
void |
forEach(Consumer<? super E> consumer)
Performs the given action for each element of the
Iterable
until all elements have been processed or the action throws an
exception. |
E |
get(long index)
Retrieve the element at the specified index.
|
protected void |
increaseCapacity()
Force the buffer to increase its capacity.
|
boolean |
isEmpty()
Is the buffer currently empty?
|
Iterator<E> |
iterator()
Returns an iterator over elements of type
T . |
Spliterator<E> |
spliterator()
Return a
Spliterator describing the contents of the buffer. |
String |
toString()
Returns a string representation of the object.
|
protected E[] curChunk
protected E[][] spine
public static final int MIN_CHUNK_POWER
public static final int MIN_CHUNK_SIZE
public static final int MAX_CHUNK_POWER
public static final int MIN_SPINE_SIZE
protected final int initialChunkPower
protected int elementIndex
protected int spineIndex
protected long[] priorElementCount
public SpinedBuffer(int initialCapacity)
initialCapacity
- the initial capacity of the listIllegalArgumentException
- if the specified initial capacity
is negativepublic SpinedBuffer()
protected long capacity()
protected final void ensureCapacity(long targetSize)
protected void increaseCapacity()
public E get(long index)
public void copyInto(E[] array, int offset)
public E[] asArray(IntFunction<E[]> arrayFactory)
public void clear()
public Iterator<E> iterator()
Iterable
T
.public void forEach(Consumer<? super E> consumer)
Iterable
Iterable
until all elements have been processed or the action throws an
exception. Unless otherwise specified by the implementing class,
actions are performed in the order of iteration (if an iteration order
is specified). Exceptions thrown by the action are relayed to the
caller.public void accept(E e)
Consumer
public String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public Spliterator<E> spliterator()
Spliterator
describing the contents of the buffer.spliterator
in interface Iterable<E>
Spliterator
over the elements described by this
Iterable
.public boolean isEmpty()
public long count()
protected int chunkSize(int n)