Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CircularBuffer<T>

A circular buffer with constant time push and pop on both ends.

Type parameters

  • T

    The type of item contained inside.

Hierarchy

  • CircularBuffer

Index

Constructors

  • new CircularBuffer<T>(init?: Iterable<T>, capacity?: number): CircularBuffer<T>
  • Constructs a new CircularBuffer from a dataset.

    Type parameters

    • T

    Parameters

    • init: Iterable<T> = []

      The initial data.

    • capacity: number = 32

    Returns CircularBuffer<T>

Properties

data: T[]

The data contained in the buffer.

end: number

The index of the end of the buffer.

len: number

The number of data elements in the buffer.

start: number

The index of the start of the buffer.

Methods

  • [iterator](): Generator<T, any, unknown>
  • Generates an iterator for the buffer.

    Returns Generator<T, any, unknown>

    An iterator.

  • capacity(): number
  • Returns the number of elements allocated.

    Returns number

    The number of elements allocated.

  • clear(): void
  • Clears the buffer, setting its length to 0.

    This doesn't deallocate any space.

    Returns void

  • get(ind: number): T
  • Gets an element from the buffer. Errors on out of bound access.

    Parameters

    • ind: number

      The index to get.

    Returns T

    The element at the index.

  • getEnd(): T
  • Utility function to get the last element of the buffer. Errors on an empty buffer.

    Returns T

    The element at the back.

  • popEnd(): T
  • popStart(): T
  • possiblyExpand(): void
  • possiblyShrink(): void
  • pushEnd(val: T): void
  • Pushes a value to the end of the buffer.

    Parameters

    • val: T

      The value to push.

    Returns void

  • pushStart(val: T): void
  • Pushes a value to the start of the buffer.

    Parameters

    • val: T

      The value to push.

    Returns void

  • resizeTo(capacity: number): void
  • Resizes the buffer to a certain capacity.

    This function also makes the buffer contiguous, so toArray will return faster.

    If the capacity is too small, then it will automatically be scaled up to the length of the buffer.

    Parameters

    • capacity: number

      The capacity to expand to.

    Returns void

  • set(ind: number, val: T): void
  • Sets an element in the buffer. Errors on out of bounds access.

    Parameters

    • ind: number

      The index to set.

    • val: T

      The value to set to.

    Returns void

  • setEnd(val: T): void
  • Utility function to set the last element of the buffer. Errors on an empty buffer.

    Parameters

    • val: T

      The value to set to.

    Returns void

  • size(): number
  • Returns the number of elements in the buffer.

    Returns number

    The number of elements.

  • toArray(): T[]
  • Returns a shallow-copied array of the data.

    This is faster than collecting the iterator.

    Returns T[]

    The array.

Generated using TypeDoc