ringbuffer API reference
A fixed-capacity ring buffer of strings: a bounded FIFO that overwrites the oldest entry when full. push appends (dropping the oldest once capacity is exceeded); pop removes the oldest; first / last peek without removing. Entries are ordered oldest-to-newest. Useful for a sliding window of recent events, log lines, or samples.
Value-semantic: push / pop return the updated buffer (chain them, $rb = ringbuffer.push($rb, x), which updates in place - a push writes one slot, whatever the capacity). Since a value-semantic pop cannot return both the item and the new buffer, read the oldest with first before you pop it. Pure .j; runs on both binaries. Stores strings - serialize other values with convert.toString or json.
Import with import "ringbuffer.j" as ringbuffer;. See the ringbuffer guide for prose and examples.
Contents
| Name | What it is |
|---|---|
capacity | The buffer's capacity. |
first | The oldest item, without removing it. |
isEmpty | Whether the buffer holds no entries. |
isFull | Whether the buffer is at capacity. |
last | The newest item, without removing it. |
new | Create an empty ring buffer of the given capacity. |
pop | Remove the oldest item. |
push | Append an item, dropping the oldest if the buffer is already full. |
size | The number of entries currently held. |
toList | A copy of the entries, oldest to newest. |
RingBuffer | struct: A ring buffer. |
Functions
capacity
ringbuffer.capacity(rb as RingBuffer)The buffer's capacity.
rb- the buffer
- Returns
int - the capacity
first
ringbuffer.first(rb as RingBuffer)The oldest item, without removing it.
rb- the buffer
- Returns
string - the oldest item
- Throws
Error - kind "ringbuffer" if the buffer is empty
isEmpty
ringbuffer.isEmpty(rb as RingBuffer)Whether the buffer holds no entries.
rb- the buffer
- Returns
bool - true when empty
isFull
ringbuffer.isFull(rb as RingBuffer)Whether the buffer is at capacity.
rb- the buffer
- Returns
bool - true when full
last
ringbuffer.last(rb as RingBuffer)The newest item, without removing it.
rb- the buffer
- Returns
string - the newest item
- Throws
Error - kind "ringbuffer" if the buffer is empty
new
ringbuffer.new(capacity as int)Create an empty ring buffer of the given capacity.
capacity- the maximum number of entries (>= 1)
- Returns
RingBuffer - the empty buffer
- Throws
Error - kind "ringbuffer" if capacity is < 1
pop
ringbuffer.pop(rb as RingBuffer)Remove the oldest item. Read it with first before popping it.
rb- the buffer
- Returns
RingBuffer - the updated buffer
- Throws
Error - kind "ringbuffer" if the buffer is empty
push
ringbuffer.push(rb as RingBuffer, item as string)Append an item, dropping the oldest if the buffer is already full.
rb- the buffer
item- the item to append
- Returns
RingBuffer - the updated buffer
size
ringbuffer.size(rb as RingBuffer)The number of entries currently held.
rb- the buffer
- Returns
int - the entry count
toList
ringbuffer.toList(rb as RingBuffer)A copy of the entries, oldest to newest.
rb- the buffer
- Returns
list of string - the entries in order
Structs
RingBuffer
A ring buffer. items holds the slots in storage order, which is rotated once the buffer has wrapped - read the entries oldest-first with toList.
def struct RingBuffer {
items as list of string,
capacity as int,
head as int,
count as int
};items- the slots (storage order, not oldest-first)
capacity- the maximum number of entries
head- the slot index of the oldest entry
count- the number of entries held