java.lang.Object
com.carrotsearch.hppc.ShortArrayDeque
- All Implemented Interfaces:
Accountable,Preallocable,ShortCollection,ShortContainer,ShortDeque,Cloneable,Iterable<ShortCursor>
@Generated(date="2024-06-04T15:20:17+0200",
value="KTypeArrayDeque.java")
public class ShortArrayDeque
extends Object
implements ShortDeque, Preallocable, Cloneable, Accountable
An array-backed
ShortDeque.-
Field Summary
FieldsModifier and TypeFieldDescriptionshort[]Internal array for storing elements of the deque.intThe index of the element at the head of the deque or an arbitrary number equal to tail if the deque is empty.protected final ArraySizingStrategyBuffer resizing strategy.intThe index at which the next element would be added to the tail of the deque. -
Constructor Summary
ConstructorsConstructorDescriptionNew instance with sane defaults.ShortArrayDeque(int expectedElements) New instance with sane defaults.ShortArrayDeque(int expectedElements, ArraySizingStrategy resizer) New instance with sane defaults.ShortArrayDeque(ShortContainer container) Creates a new deque from elements of another container, appending elements at the end of the deque in the iteration order. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFirst(short e1) Inserts the specified element at the front of this deque.final voidaddFirst(short... elements) Vararg-signature method for adding elements at the front of this deque.intaddFirst(ShortContainer container) Inserts all elements from the given container to the front of this deque.intaddFirst(Iterable<? extends ShortCursor> iterable) Inserts all elements from the given iterable to the front of this deque.voidaddLast(short e1) Inserts the specified element at the end of this deque.final voidaddLast(short... elements) Vararg-signature method for adding elements at the end of this deque.intaddLast(ShortContainer container) Inserts all elements from the given container to the end of this deque.intaddLast(Iterable<? extends ShortCursor> iterable) Inserts all elements from the given iterable to the end of this deque.intbufferIndexOf(short e1) Return the index of the first (counting from head) element equal toe1.voidclear()Removes all elements from this collection.clone()Clone this object.booleancontains(short e) Lookup a given element in the container.<T extends ShortProcedure>
TdescendingForEach(T procedure) Appliesprocedureto all elements of this deque, tail to head.Returns a cursor over the values of this deque (in tail to head order).protected voidensureBufferSpace(int expectedAdditions) Ensures the internal buffer has enough free slots to storeexpectedAdditions.voidensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.protected booleanequalElements(ShortArrayDeque other) Compare order-aligned elements against anotherShortDeque.booleanReturnstrueonly if the other object is an instance of the same class and with the same elements.<T extends ShortProcedure>
TforEach(T procedure) Applies aprocedureto all container elements.static ShortArrayDequefrom(short... elements) Create a new deque by pushing a variable number of arguments to the end of it.shortgetFirst()Retrieves the first element of this deque but does not remove it.shortgetLast()Retrieves the last element of this deque but does not remove it.inthashCode()booleanisEmpty()Shortcut forsize() == 0.iterator()Returns a cursor over the values of this deque (in head to tail order).intlastBufferIndexOf(short e1) Return the index of the last (counting from tail) element equal toe1.protected static intoneLeft(int index, int modulus) Move one index to the left, wrapping around buffer.protected static intoneRight(int index, int modulus) Move one index to the right, wrapping around buffer.longAllocated memory estimationlongBytes that is actually been usedvoidrelease()Release internal buffers of this deque and reallocate with the default buffer.intremoveAll(short e1) Removes all occurrences ofefrom this collection.intremoveAll(ShortPredicate predicate) Removes all elements in this collection for which the given predicate returnstrue.intDefault implementation uses a predicate for removal.voidremoveAtBufferIndex(int index) Removes the element atindexin the internal {#linkbufferarray, returning its value.shortRetrieves and removes the first element of this deque.intremoveFirst(short e1) Removes the first element that equalse.shortRetrieves and removes the last element of this deque.intremoveLast(short e1) Removes the last element that equalse.intretainAll(ShortPredicate predicate) Default implementation redirects toShortCollection.removeAll(ShortPredicate)and negates the predicate.intDefault implementation uses a predicate for retaining.intsize()Return the current number of elements in this container.short[]toArray()Default implementation of copying to an array.short[]toArray(short[] target) Copies elements of this deque to an array.toString()Convert the contents of this container to a human-friendly string.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface com.carrotsearch.hppc.ShortCollection
removeAll, retainAll, retainAll
-
Field Details
-
buffer
public short[] bufferInternal array for storing elements of the deque. -
head
public int headThe index of the element at the head of the deque or an arbitrary number equal to tail if the deque is empty. -
tail
public int tailThe index at which the next element would be added to the tail of the deque. -
resizer
Buffer resizing strategy.
-
-
Constructor Details
-
ShortArrayDeque
public ShortArrayDeque()New instance with sane defaults. -
ShortArrayDeque
public ShortArrayDeque(int expectedElements) New instance with sane defaults.- Parameters:
expectedElements- The expected number of elements guaranteed not to cause buffer expansion (inclusive).
-
ShortArrayDeque
New instance with sane defaults.- Parameters:
expectedElements- The expected number of elements guaranteed not to cause buffer expansion (inclusive).resizer- Underlying buffer sizing strategy.
-
ShortArrayDeque
Creates a new deque from elements of another container, appending elements at the end of the deque in the iteration order.
-
-
Method Details
-
addFirst
public void addFirst(short e1) Inserts the specified element at the front of this deque.- Specified by:
addFirstin interfaceShortDeque
-
addFirst
public final void addFirst(short... elements) Vararg-signature method for adding elements at the front of this deque.This method is handy, but costly if used in tight loops (anonymous array passing)
- Parameters:
elements- The elements to add.
-
addFirst
Inserts all elements from the given container to the front of this deque.- Parameters:
container- The container to iterate over.- Returns:
- Returns the number of elements actually added as a result of this call.
-
addFirst
Inserts all elements from the given iterable to the front of this deque.- Parameters:
iterable- The iterable to iterate over.- Returns:
- Returns the number of elements actually added as a result of this call.
-
addLast
public void addLast(short e1) Inserts the specified element at the end of this deque.- Specified by:
addLastin interfaceShortDeque
-
addLast
public final void addLast(short... elements) Vararg-signature method for adding elements at the end of this deque.This method is handy, but costly if used in tight loops (anonymous array passing)
- Parameters:
elements- The elements to iterate over.
-
addLast
Inserts all elements from the given container to the end of this deque.- Parameters:
container- The container to iterate over.- Returns:
- Returns the number of elements actually added as a result of this call.
-
addLast
Inserts all elements from the given iterable to the end of this deque.- Parameters:
iterable- The iterable to iterate over.- Returns:
- Returns the number of elements actually added as a result of this call.
-
removeFirst
public short removeFirst()Retrieves and removes the first element of this deque.- Specified by:
removeFirstin interfaceShortDeque- Returns:
- the head (first) element of this deque.
-
removeLast
public short removeLast()Retrieves and removes the last element of this deque.- Specified by:
removeLastin interfaceShortDeque- Returns:
- the tail of this deque.
-
getFirst
public short getFirst()Retrieves the first element of this deque but does not remove it.- Specified by:
getFirstin interfaceShortDeque- Returns:
- the head of this deque.
-
getLast
public short getLast()Retrieves the last element of this deque but does not remove it.- Specified by:
getLastin interfaceShortDeque- Returns:
- the head of this deque.
-
removeFirst
public int removeFirst(short e1) Removes the first element that equalse.- Specified by:
removeFirstin interfaceShortDeque- Returns:
- The deleted element's index or
-1if the element was not found.
-
bufferIndexOf
public int bufferIndexOf(short e1) Return the index of the first (counting from head) element equal toe1. The index points to thebufferarray.- Parameters:
e1- The element to look for.- Returns:
- Returns the index of the first element equal to
e1or-1if not found.
-
removeLast
public int removeLast(short e1) Removes the last element that equalse.- Specified by:
removeLastin interfaceShortDeque- Returns:
- The deleted element's index or
-1if the element was not found.
-
lastBufferIndexOf
public int lastBufferIndexOf(short e1) Return the index of the last (counting from tail) element equal toe1. The index points to thebufferarray.- Parameters:
e1- The element to look for.- Returns:
- Returns the index of the first element equal to
e1or-1if not found.
-
removeAll
public int removeAll(short e1) Removes all occurrences ofefrom this collection.- Specified by:
removeAllin interfaceShortCollection- Parameters:
e1- Element to be removed from this collection, if present.- Returns:
- The number of removed elements as a result of this call.
-
removeAtBufferIndex
public void removeAtBufferIndex(int index) Removes the element atindexin the internal {#linkbufferarray, returning its value. -
isEmpty
public boolean isEmpty()Shortcut forsize() == 0.- Specified by:
isEmptyin interfaceShortContainer
-
size
public int size()Return the current number of elements in this container. The time for calculating the container's size may takeO(n)time, although implementing classes should try to maintain the current size and return in constant time.- Specified by:
sizein interfaceShortContainer
-
clear
public void clear()Removes all elements from this collection.The internal array buffers are not released as a result of this call.
- Specified by:
clearin interfaceShortCollection- See Also:
-
release
public void release()Release internal buffers of this deque and reallocate with the default buffer.- Specified by:
releasein interfaceShortCollection- See Also:
-
ensureCapacity
public void ensureCapacity(int expectedElements) Ensure this container can hold at least the given number of elements without resizing its buffers.- Specified by:
ensureCapacityin interfacePreallocable- Parameters:
expectedElements- The total number of elements, inclusive.
-
ensureBufferSpace
protected void ensureBufferSpace(int expectedAdditions) Ensures the internal buffer has enough free slots to storeexpectedAdditions. Increases internal buffer size if needed. -
toArray
public short[] toArray()Default implementation of copying to an array.- Specified by:
toArrayin interfaceShortContainer
-
toArray
public short[] toArray(short[] target) Copies elements of this deque to an array. The content of thetargetarray is filled from index 0 (head of the queue) to indexsize() - 1(tail of the queue).- Parameters:
target- The target array must be large enough to hold all elements.- Returns:
- Returns the target argument for chaining.
-
clone
Clone this object. The returned clone will reuse the same hash function and array resizing strategy. -
oneLeft
protected static int oneLeft(int index, int modulus) Move one index to the left, wrapping around buffer. -
oneRight
protected static int oneRight(int index, int modulus) Move one index to the right, wrapping around buffer. -
ramBytesAllocated
public long ramBytesAllocated()Description copied from interface:AccountableAllocated memory estimation- Specified by:
ramBytesAllocatedin interfaceAccountable- Returns:
- Ram allocated in bytes
-
ramBytesUsed
public long ramBytesUsed()Description copied from interface:AccountableBytes that is actually been used- Specified by:
ramBytesUsedin interfaceAccountable- Returns:
- Ram used in bytes
-
iterator
Returns a cursor over the values of this deque (in head to tail order). The iterator is implemented as a cursor and it returns the same cursor instance on every call toIterator.next()(to avoid boxing of primitive types). To read the current value (or index in the deque's buffer) use the cursor's public fields. An example is shown below.for (IntValueCursor c : intDeque) { System.out.println("buffer index=" + c.index + " value=" + c.value); }- Specified by:
iteratorin interfaceIterable<ShortCursor>- Specified by:
iteratorin interfaceShortContainer
-
descendingIterator
Returns a cursor over the values of this deque (in tail to head order). The iterator is implemented as a cursor and it returns the same cursor instance on every call toIterator.next()(to avoid boxing of primitive types). To read the current value (or index in the deque's buffer) use the cursor's public fields. An example is shown below.for (Iterator<IntCursor> i = intDeque.descendingIterator(); i.hasNext();) { final IntCursor c = i.next(); System.out.println("buffer index=" + c.index + " value=" + c.value); }- Specified by:
descendingIteratorin interfaceShortDeque- Returns:
- An iterator over elements in this deque in tail-to-head order.
-
forEach
Applies aprocedureto all container elements. Returns the argument (any subclass ofShortProcedure. This lets the caller to call methods of the argument by chaining the call (even if the argument is an anonymous type) to retrieve computed values, for example (IntContainer):int count = container.forEach(new IntProcedure() { int count; // this is a field declaration in an anonymous class. public void apply(int value) { count++; } }).count;- Specified by:
forEachin interfaceShortContainer
-
forEach
Applies apredicateto container elements as long, as the predicate returnstrue. The iteration is interrupted otherwise.- Specified by:
forEachin interfaceShortContainer
-
descendingForEach
Appliesprocedureto all elements of this deque, tail to head.- Specified by:
descendingForEachin interfaceShortDeque
-
descendingForEach
Applies apredicateto container elements as long, as the predicate returnstrue. The iteration is interrupted otherwise.- Specified by:
descendingForEachin interfaceShortDeque
-
removeAll
Removes all elements in this collection for which the given predicate returnstrue.- Specified by:
removeAllin interfaceShortCollection- Returns:
- Returns the number of removed elements.
-
contains
public boolean contains(short e) Lookup a given element in the container. This operation has no speed guarantees (may be linear with respect to the size of this container).- Specified by:
containsin interfaceShortContainer- Returns:
- Returns
trueif this container has an element equal toe.
-
hashCode
public int hashCode() -
equals
Returnstrueonly if the other object is an instance of the same class and with the same elements. -
equalElements
Compare order-aligned elements against anotherShortDeque. -
from
Create a new deque by pushing a variable number of arguments to the end of it. -
removeAll
Default implementation uses a predicate for removal.- Specified by:
removeAllin interfaceShortCollection- Returns:
- Returns the number of removed elements.
-
retainAll
Default implementation uses a predicate for retaining.- Specified by:
retainAllin interfaceShortCollection- Returns:
- Returns the number of removed elements.
-
retainAll
Default implementation redirects toShortCollection.removeAll(ShortPredicate)and negates the predicate.- Specified by:
retainAllin interfaceShortCollection- Returns:
- Returns the number of removed elements.
-
toString
Convert the contents of this container to a human-friendly string.
-