CircularArray



CircularArray is a generic circular array data structure that provides O(1) random read, O(1) prepend and O(1) append. The CircularArray automatically grows its capacity when number of added items is over its capacity.

Summary

Public constructors

<E : Any?> CircularArray(minCapacity: Int)

Creates a circular array with capacity for at least minCapacity elements.

Cmn

Public functions

Unit
addFirst(element: E)

Add an element in front of the CircularArray.

Cmn
Unit
addLast(element: E)

Add an element at end of the CircularArray.

Cmn
Unit

Remove all elements from the CircularArray.

Cmn
operator E
get(index: Int)

Get nth (0 <= n <= size()-1) element of the CircularArray.

Cmn
Boolean

Return true if size is 0.

Cmn
E

Remove first element from front of the CircularArray and return it.

Cmn
E

Remove last element from end of the CircularArray and return it.

Cmn
Unit

Remove multiple elements from end of the CircularArray, ignore when count is less than or equals to 0.

Cmn
Unit

Remove multiple elements from front of the CircularArray, ignore when count is less than or equal to 0.

Cmn
Int

Get number of elements in the CircularArray.

Cmn

Public properties

E

Get first element of the CircularArray.

Cmn
E

Get last element of the CircularArray.

Cmn

Public constructors

CircularArray

<E : Any?> CircularArray(minCapacity: Int = 8)

Creates a circular array with capacity for at least minCapacity elements.

Parameters
minCapacity: Int = 8

the minimum capacity, between 1 and 2^30 inclusive

Public functions

addFirst

fun addFirst(element: E): Unit

Add an element in front of the CircularArray.

Parameters
element: E

Element to add.

addLast

fun addLast(element: E): Unit

Add an element at end of the CircularArray.

Parameters
element: E

Element to add.

clear

fun clear(): Unit

Remove all elements from the CircularArray.

get

operator fun get(index: Int): E

Get nth (0 <= n <= size()-1) element of the CircularArray.

Parameters
index: Int

The zero based element index in the CircularArray.

Returns
E

The nth element.

Throws
kotlin.IndexOutOfBoundsException

if n < 0 or n >= size()

isEmpty

fun isEmpty(): Boolean

Return true if size is 0.

Returns
Boolean

true if size is 0.

popFirst

fun popFirst(): E

Remove first element from front of the CircularArray and return it.

Returns
E

The element removed.

Throws
kotlin.IndexOutOfBoundsException

if CircularArray is empty (on jvm)

popLast

fun popLast(): E

Remove last element from end of the CircularArray and return it.

Returns
E

The element removed.

removeFromEnd

fun removeFromEnd(count: Int): Unit

Remove multiple elements from end of the CircularArray, ignore when count is less than or equals to 0.

Parameters
count: Int

Number of elements to remove.

Throws
kotlin.IndexOutOfBoundsException

if count is larger than size

removeFromStart

fun removeFromStart(count: Int): Unit

Remove multiple elements from front of the CircularArray, ignore when count is less than or equal to 0.

Parameters
count: Int

Number of elements to remove.

Throws
kotlin.IndexOutOfBoundsException

if count is larger than size

size

fun size(): Int

Get number of elements in the CircularArray.

Returns
Int

Number of elements in the CircularArray.

Public properties

first

val first: E

Get first element of the CircularArray.

Returns
E

The first element.

last

val last: E

Get last element of the CircularArray.

Returns
E

The last element.