APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.utils

public Class LongQueue

extends Object
Class Inheritance
Imports
java.util.NoSuchElementException

A resizable, ordered array of longs with efficient add and remove at the beginning and end. Values in the backing array may wrap back to the beginning, making add and remove at the beginning and end O(1) (unless the backing array needs to resize when adding). Deque functionality is provided via removeLast() and addFirst(long).

Field Summary

Modifier and TypeField and Description
protected int
head

Index of first element.

public int
size

Number of elements in the queue.

protected int
tail

Index of last element.

protected long[]
values

Contains the values in the queue.

Constructor Summary

AccessConstructor and Description
public
LongQueue()

Creates a new LongQueue which can hold 16 values without needing to resize backing array.

public
LongQueue(int initialSize)

Creates a new LongQueue which can hold the specified number of values without needing to resize backing array.

Method Summary

Modifier and TypeMethod and Description
public void
addFirst(long value)

Prepend given value to the head.

public void
addLast(long value)

Append given value to the tail.

public void
clear()

Removes all values from this queue.

public void
ensureCapacity(int additional)

Increases the size of the backing array to accommodate the specified number of additional items.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public long
first()

Returns the first (head) item in the queue (without removing it).

public long
get(int index)

Retrieves the value in queue without removing it.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public int

Returns:

An index of first occurrence of value in queue or -1 if no such value exists
indexOf
(long value)

Returns the index of first occurrence of value in the queue, or -1 if no such value exists.

public boolean
isEmpty()

Returns true if the queue is empty.

public long
last()

Returns the last (tail) item in the queue (without removing it).

public boolean
notEmpty()

Returns true if the queue has one or more items.

public long

Returns:

removed value
removeFirst
()

Remove the first item from the queue.

public long
removeIndex(int index)

Removes and returns the item at the specified index.

public long

Returns:

removed
removeLast
()

Remove the last item from the queue.

public boolean

Returns:

true if value was found and removed, false otherwise
removeValue
(long value)

Removes the first instance of the specified value in the queue.

protected void
resize(int newSize)

Resize backing array.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public String
toString(String separator)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

sizeback to summary
public int size

Number of elements in the queue.

tailback to summary
protected int tail

Index of last element. Logically bigger than head. Usually points to an empty position, but points to the head when full (size == values.length).

valuesback to summary
protected long[] values

Contains the values in the queue. Head and tail indices go in a circle around this array, wrapping at the end.

Constructor Detail

LongQueueback to summary
public LongQueue()

Creates a new LongQueue which can hold 16 values without needing to resize backing array.

LongQueueback to summary
public LongQueue(int initialSize)

Creates a new LongQueue which can hold the specified number of values without needing to resize backing array.

Method Detail

addFirstback to summary
public void addFirst(long value)

Prepend given value to the head. (enqueue to head) Unless backing array needs resizing, operates in O(1) time.

See Also
addLast(long)
addLastback to summary
public void addLast(long value)

Append given value to the tail. (enqueue to tail) Unless backing array needs resizing, operates in O(1) time.

clearback to summary
public void clear()

Removes all values from this queue.

ensureCapacityback to summary
public void ensureCapacity(int additional)

Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

firstback to summary
public long first()

Returns the first (head) item in the queue (without removing it).

Exceptions
NoSuchElementException:
when queue is empty
See Also
addFirst(long), removeFirst()
getback to summary
public long get(int index)

Retrieves the value in queue without removing it. Indexing is from the front to back, zero based. Therefore get(0) is the same as first().

Exceptions
IndexOutOfBoundsException:
when the index is negative or >= size
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
indexOfback to summary
public int indexOf(long value)

Returns the index of first occurrence of value in the queue, or -1 if no such value exists.

Returns:int

An index of first occurrence of value in queue or -1 if no such value exists

isEmptyback to summary
public boolean isEmpty()

Returns true if the queue is empty.

lastback to summary
public long last()

Returns the last (tail) item in the queue (without removing it).

Exceptions
NoSuchElementException:
when queue is empty
See Also
addLast(long), removeLast()
notEmptyback to summary
public boolean notEmpty()

Returns true if the queue has one or more items.

removeFirstback to summary
public long removeFirst()

Remove the first item from the queue. (dequeue from head) Always O(1).

Returns:long

removed value

Exceptions
NoSuchElementException:
when queue is empty
removeIndexback to summary
public long removeIndex(int index)

Removes and returns the item at the specified index.

removeLastback to summary
public long removeLast()

Remove the last item from the queue. (dequeue from tail) Always O(1).

Returns:long

removed

Exceptions
NoSuchElementException:
when queue is empty
See Also
removeFirst()
removeValueback to summary
public boolean removeValue(long value)

Removes the first instance of the specified value in the queue.

Returns:boolean

true if value was found and removed, false otherwise

resizeback to summary
protected void resize(int newSize)

Resize backing array. newSize must be bigger than current size.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

toStringback to summary
public String toString(String separator)