ExVrp.RingBuffer (ExVrp v0.4.2)

Copy Markdown View Source

A fixed-size circular buffer for tracking recently inserted values.

The ring buffer stores up to maxlen items. When full, new items overwrite the oldest ones. The peek/1 function returns the value that would be overwritten by the next append, or nil if that slot hasn't been written yet.

Example

buffer = ExVrp.RingBuffer.new(2)
buffer = ExVrp.RingBuffer.append(buffer, :a)
buffer = ExVrp.RingBuffer.append(buffer, :b)
ExVrp.RingBuffer.peek(buffer)  # => :a (next to be overwritten)

Summary

Functions

Appends a value to the buffer.

Clears the buffer, resetting it to its initial state.

Returns the current number of items in the buffer.

Returns the maximum length of the buffer.

Creates a new ring buffer with the given maximum length.

Peeks at the value in the next slot (the one that would be overwritten next).

Skips to the next slot without writing anything.

Types

t()

@type t() :: %ExVrp.RingBuffer{
  buffer: list(),
  head: non_neg_integer(),
  maxlen: pos_integer(),
  size: non_neg_integer()
}

Functions

append(rb, value)

@spec append(t(), term()) :: t()

Appends a value to the buffer.

If the buffer is full, the oldest value is overwritten.

clear(ring_buffer)

@spec clear(t()) :: t()

Clears the buffer, resetting it to its initial state.

length(ring_buffer)

@spec length(t()) :: non_neg_integer()

Returns the current number of items in the buffer.

maxlen(ring_buffer)

@spec maxlen(t()) :: pos_integer()

Returns the maximum length of the buffer.

new(maxlen)

@spec new(pos_integer()) :: t()

Creates a new ring buffer with the given maximum length.

Example

buffer = ExVrp.RingBuffer.new(3)

peek(rb)

@spec peek(t()) :: term() | nil

Peeks at the value in the next slot (the one that would be overwritten next).

Returns nil if that slot hasn't been written to yet.

skip(rb)

@spec skip(t()) :: t()

Skips to the next slot without writing anything.

This moves the head pointer forward without changing the size.