@zhelvis/structure-ts
    Preparing search index...

    Class RingBuffer<T>

    A ring buffer (or circular buffer) is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. It is particularly useful for buffering data streams and implementing memory efficient queues and stacks.

    Type Parameters

    • T
    Index

    Constructors

    • Creates a new RingBuffer instance.

      Type Parameters

      • T

      Parameters

      • capacity: number

        Maximum number of items the buffer can hold.

      • Optionalitems: T[]

        Optional array of initial items to populate the buffer. If the number of items exceeds capacity, the oldest items will be discarded.

      Returns RingBuffer<T>

      RangeError if capacity is not a positive safe integer.

    Accessors

    • get capacity(): number

      The capacity of the ring buffer.

      Returns number

    • get size(): number

      The current size of the ring buffer.

      Returns number

    Methods

    • An iterator for the items in the buffer.

      Returns Iterator<T>

    • Gets the item at index, allowing for positive and negative integers. Negative integers count back from the last item in the buffer.

      Parameters

      • index: number

        The index of the item to get.

      Returns T

      The item at the specified index.

      RangeError if the index is out of bounds.

    • Removes and returns the last item from the buffer.

      Returns undefined | T

      The last item from the buffer, or undefined if the buffer is empty.

    • Adds items to the end of the buffer. If the buffer is full, it will delete the items at the head.

      Parameters

      • ...items: T[]

        The items to add to the end of the buffer.

      Returns number

      The new size of the buffer.

    • Sets an item at index, allowing for positive and negative integers. Negative integers count back from the last item in the buffer.

      Parameters

      • index: number

        The index of the item to set.

      • item: T

        The new item to set at the specified index.

      Returns void

      RangeError if the index is out of bounds.

    • Removes and returns the first item from the buffer.

      Returns undefined | T

      The first item from the buffer, or undefined if the buffer is empty.

    • Adds items to the start of the buffer. If the buffer is full, it will delete the items at the tail.

      Parameters

      • ...items: T[]

        The items to add to the start of the buffer.

      Returns number

      The new size of the buffer.