CircularArray


public final class CircularArray<E extends Object>


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 extends Object> CircularArray(int minCapacity)

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

Public methods

final void
addFirst(@NonNull E element)

Add an element in front of the CircularArray.

final void
addLast(@NonNull E element)

Add an element at end of the CircularArray.

final void

Remove all elements from the CircularArray.

final @NonNull E
get(int index)

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

final @NonNull E

Get first element of the CircularArray.

final @NonNull E

Get last element of the CircularArray.

final boolean

Return true if size is 0.

final @NonNull E

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

final @NonNull E

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

final void
removeFromEnd(int count)

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

final void
removeFromStart(int count)

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

final int

Get number of elements in the CircularArray.

Public constructors

CircularArray

public <E extends Object> CircularArray(int minCapacity)

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

Parameters
int minCapacity

the minimum capacity, between 1 and 2^30 inclusive

Public methods

addFirst

Added in 1.0.0
public final void addFirst(@NonNull E element)

Add an element in front of the CircularArray.

Parameters
@NonNull E element

Element to add.

addLast

Added in 1.0.0
public final void addLast(@NonNull E element)

Add an element at end of the CircularArray.

Parameters
@NonNull E element

Element to add.

clear

Added in 1.0.0
public final void clear()

Remove all elements from the CircularArray.

get

Added in 1.0.0
public final @NonNullget(int index)

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

Parameters
int index

The zero based element index in the CircularArray.

Returns
@NonNull E

The nth element.

Throws
kotlin.IndexOutOfBoundsException

if n < 0 or n >= size()

getFirst

Added in 1.0.0
public final @NonNullgetFirst()

Get first element of the CircularArray.

Returns
@NonNull E

The first element.

getLast

Added in 1.0.0
public final @NonNullgetLast()

Get last element of the CircularArray.

Returns
@NonNull E

The last element.

isEmpty

Added in 1.0.0
public final boolean isEmpty()

Return true if size is 0.

Returns
boolean

true if size is 0.

popFirst

Added in 1.0.0
public final @NonNullpopFirst()

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

Returns
@NonNull E

The element removed.

Throws
kotlin.IndexOutOfBoundsException

if CircularArray is empty (on jvm)

popLast

Added in 1.0.0
public final @NonNullpopLast()

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

Returns
@NonNull E

The element removed.

removeFromEnd

Added in 1.0.0
public final void removeFromEnd(int count)

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

Parameters
int count

Number of elements to remove.

Throws
kotlin.IndexOutOfBoundsException

if count is larger than size

removeFromStart

Added in 1.0.0
public final void removeFromStart(int count)

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

Parameters
int count

Number of elements to remove.

Throws
kotlin.IndexOutOfBoundsException

if count is larger than size

size

Added in 1.0.0
public final int size()

Get number of elements in the CircularArray.

Returns
int

Number of elements in the CircularArray.