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
@type t() :: %ExVrp.RingBuffer{ buffer: list(), head: non_neg_integer(), maxlen: pos_integer(), size: non_neg_integer() }
Functions
Appends a value to the buffer.
If the buffer is full, the oldest value is overwritten.
Clears the buffer, resetting it to its initial state.
@spec length(t()) :: non_neg_integer()
Returns the current number of items in the buffer.
@spec maxlen(t()) :: pos_integer()
Returns the maximum length of the buffer.
@spec new(pos_integer()) :: t()
Creates a new ring buffer with the given maximum length.
Example
buffer = ExVrp.RingBuffer.new(3)
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.
Skips to the next slot without writing anything.
This moves the head pointer forward without changing the size.