R
C

CircularBuffer

v0.0.8tested

A circular (ring) buffer with automatic growth, O(1) push/pop on both ends

Signature

ts
class CircularBuffer<T> implements CircularBufferLike<T>

Type Parameters

T

Properties

PropertyTypeDescription
lengthreadonlynumberGets the number of elements in the buffer
capacityreadonlynumberGets the current capacity of the buffer
isEmptyreadonlybooleanChecks if the buffer is empty
isFullreadonlybooleanChecks if the buffer is at capacity (before auto-grow)

Methods

pushBack

Adds an element to the back of the buffer

ts
pushBack(element: T)
ParameterTypeDescription
elementTThe element to add
pushFront

Adds an element to the front of the buffer

ts
pushFront(element: T)
ParameterTypeDescription
elementTThe element to add
popBack

Removes and returns the back element

ts
popBack()
ReturnsT | undefinedThe back element, or undefined if empty
popFront

Removes and returns the front element

ts
popFront()
ReturnsT | undefinedThe front element, or undefined if empty
peekBack

Returns the back element without removing it

ts
peekBack()
ReturnsT | undefined
peekFront

Returns the front element without removing it

ts
peekFront()
ReturnsT | undefined
get

Gets element at logical index (0 = front)

ts
get(index: number)
ParameterTypeDescription
indexnumberThe logical index
ReturnsT | undefined
clear

Clears the buffer

ts
clear()
Returnsthis
toArray

Converts the buffer to an array from front to back

ts
toArray()
ReturnsT[]
toString

Returns a string representation

ts
toString()
Returnsstring
[Symbol.iterator]

Returns an iterator (front to back)

ts
* [Symbol.iterator](): IterableIterator<T>
ReturnsIterableIterator<T>
[Symbol.asyncIterator]

Returns an async iterator (front to back)

ts
async* [Symbol.asyncIterator]()
ReturnsAsyncGenerator<Awaited<T>, void, unknown>