LongDeque

class LongDeque(initialCapacity: Int)(source)

LongDeque is a double-ended queue for Long values backed by a circular buffer.

  • Amortized O(1) insertion/removal at both ends.

  • Indexed access, insertion and removal are supported (may shift elements internally).

  • Not thread-safe: synchronize externally if multiple threads mutate concurrently.

Throws

if initialCapacity is negative.

Constructors

Link copied to clipboard
constructor(initialCapacity: Int)

Creates a deque with the given initialCapacity. The deque grows automatically as needed when elements are added beyond the current capacity.

Properties

Link copied to clipboard

The last valid element index, or -1 when the deque is empty.

Link copied to clipboard
val size: Int

The number of elements in this deque.

Functions

Link copied to clipboard
fun add(value: Long)

Adds value to the end of this deque. Alias for addLast.

fun add(index: Int, value: Long)

Inserts value at index. Elements may be shifted to make room for the new element.

Link copied to clipboard
fun addFirst(value: Long)

Adds value to the front (head) of this deque.

Link copied to clipboard
fun addLast(value: Long)

Adds value to the end of this deque.

Link copied to clipboard

Returns a live List view backed by this deque.

Link copied to clipboard

Returns a live MutableList view backed by this deque. Structural changes are reflected both ways.

Link copied to clipboard
fun clear()

Removes all elements from this deque. Capacity is preserved.

Link copied to clipboard
operator fun contains(value: Long): Boolean

Returns true if value exists in this deque.

Link copied to clipboard
fun dequeue(): Long

Dequeues and returns the front element (queue-style). Alias for removeFirst.

Link copied to clipboard
fun enqueue(value: Long)

Enqueues value at the end (queue-style). Alias for addLast.

Link copied to clipboard
fun first(): Long

Returns the first element, or throws if this deque is empty.

Link copied to clipboard

Returns the first element, or null if this deque is empty.

Link copied to clipboard
inline fun forEach(action: (Long) -> Unit)

Iterates elements from first to last, invoking action for each element.

Link copied to clipboard
inline fun forEachIndexed(action: (index: Int, value: Long) -> Unit)

Iterates elements from first to last, passing the logical index and value to action.

Link copied to clipboard
inline fun forEachReversed(action: (Long) -> Unit)

Iterates elements from last to first, invoking action for each element.

Link copied to clipboard
inline fun forEachReversedIndexed(action: (index: Int, value: Long) -> Unit)

Iterates elements from last to first, passing the logical index and value to action.

Link copied to clipboard
operator fun get(index: Int): Long

Returns the element at index.

Link copied to clipboard
fun indexOf(value: Long): Int

Returns the index of the first occurrence of value, or -1 if not found.

Link copied to clipboard
inline fun isEmpty(): Boolean

Returns true if this deque contains no elements.

Link copied to clipboard
inline fun isNotEmpty(): Boolean

Returns true if this deque contains one or more elements.

Link copied to clipboard
fun last(): Long

Returns the last element, or throws if this deque is empty.

Link copied to clipboard
fun lastIndexOf(value: Long): Int

Returns the index of the last occurrence of value, or -1 if not found.

Link copied to clipboard

Returns the last element, or null if this deque is empty.

Link copied to clipboard
fun pop(): Long

Pops and returns the front element (stack-style). Alias for removeFirst.

Link copied to clipboard
fun push(value: Long)

Pushes value onto the front (stack-style). Alias for addFirst.

Link copied to clipboard
fun removeAt(index: Int): Long

Removes and returns the element at index. The internal storage may shift elements towards either end to fill the gap.

Link copied to clipboard

Removes and returns the first element.

Link copied to clipboard

Removes and returns the first element, or null if the deque is empty.

Link copied to clipboard

Removes and returns the last element.

Link copied to clipboard

Removes and returns the last element, or null if the deque is empty.

Link copied to clipboard
operator fun set(index: Int, value: Long): Long

Replaces the element at index with value.