dcsext.containers.RingBuffer

RingBuffer - fixed size circular buffer.

Contents

  1. Description
  2. Metamethods
    1. ringbuffer:__call(max)
  3. Methods
    1. ringbuffer:push(v)
    2. ringbuffer:pop()
    3. ringbuffer:empty()
    4. ringbuffer:full()
    5. ringbuffer:peek()
    6. ringbuffer:size()

Description

The buffer holds up to a fixed number of values; pushing into a full buffer drops the oldest value to make room, so the buffer always holds the most recently pushed values. Create a ring buffer by calling the class with an optional capacity, e.g. local rb = RingBuffer(32), defaulting to 10 slots.

Metamethods

ringbuffer:__call(max)

Constructor. Create a new, empty ring buffer by calling the class directly, optionally passing the buffer capacity.

Parameters

max
optional maximum number of values the buffer holds, defaulting to 10

Returns

  • a new, empty RingBuffer instance

Methods

ringbuffer:push(v)

Push a value into the buffer. When the buffer is full the oldest value is dropped to make room for the new value.

Parameters

v
value to store

ringbuffer:pop()

Remove and return the oldest value in the buffer.

Returns

  • the oldest stored value or nil when the buffer is empty

ringbuffer:empty()

Test if the buffer holds no values.

Returns

  • bool, true when the buffer is empty

ringbuffer:full()

Test if the buffer is filled to capacity.

Returns

  • bool, true when the number of stored values equals the buffer capacity

ringbuffer:peek()

Return the oldest value in the buffer without removing it.

Returns

  • the oldest stored value or nil when the buffer is empty

ringbuffer:size()

Return the number of values stored in the buffer.

Returns

  • the number of stored values