C
CircularBuffer
v0.0.8testedA circular (ring) buffer with automatic growth, O(1) push/pop on both ends
Signature
ts
class CircularBuffer<T> implements CircularBufferLike<T>Type Parameters
TProperties
| Property | Type | Description |
|---|---|---|
lengthreadonly | number | Gets the number of elements in the buffer |
capacityreadonly | number | Gets the current capacity of the buffer |
isEmptyreadonly | boolean | Checks if the buffer is empty |
isFullreadonly | boolean | Checks if the buffer is at capacity (before auto-grow) |
Methods
pushBackAdds an element to the back of the buffer
ts
pushBack(element: T)| Parameter | Type | Description |
|---|---|---|
element | T | The element to add |
pushFrontAdds an element to the front of the buffer
ts
pushFront(element: T)| Parameter | Type | Description |
|---|---|---|
element | T | The element to add |
popBackRemoves and returns the back element
ts
popBack()Returns
T | undefinedThe back element, or undefined if emptypopFrontRemoves and returns the front element
ts
popFront()Returns
T | undefinedThe front element, or undefined if emptypeekBackReturns the back element without removing it
ts
peekBack()Returns
T | undefinedpeekFrontReturns the front element without removing it
ts
peekFront()Returns
T | undefinedgetGets element at logical index (0 = front)
ts
get(index: number)| Parameter | Type | Description |
|---|---|---|
index | number | The logical index |
Returns
T | undefinedclearClears the buffer
ts
clear()Returns
thistoArrayConverts the buffer to an array from front to back
ts
toArray()Returns
T[]toStringReturns a string representation
ts
toString()Returns
string[Symbol.iterator]Returns an iterator (front to back)
ts
* [Symbol.iterator](): IterableIterator<T>Returns
IterableIterator<T>[Symbol.asyncIterator]Returns an async iterator (front to back)
ts
async* [Symbol.asyncIterator]()Returns
AsyncGenerator<Awaited<T>, void, unknown>