Skip to content
Jennifer Programming Language

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

NameWhat it is
capacityThe buffer's capacity.
firstThe oldest item, without removing it.
isEmptyWhether the buffer holds no entries.
isFullWhether the buffer is at capacity.
lastThe newest item, without removing it.
newCreate an empty ring buffer of the given capacity.
popRemove the oldest item.
pushAppend an item, dropping the oldest if the buffer is already full.
sizeThe number of entries currently held.
toListA copy of the entries, oldest to newest.
RingBufferstruct: A ring buffer.

Functions

capacity

jennifer
ringbuffer.capacity(rb as RingBuffer)

The buffer's capacity.

rb
the buffer
Returns int
the capacity

first

jennifer
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

jennifer
ringbuffer.isEmpty(rb as RingBuffer)

Whether the buffer holds no entries.

rb
the buffer
Returns bool
true when empty

isFull

jennifer
ringbuffer.isFull(rb as RingBuffer)

Whether the buffer is at capacity.

rb
the buffer
Returns bool
true when full

last

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
ringbuffer.size(rb as RingBuffer)

The number of entries currently held.

rb
the buffer
Returns int
the entry count

toList

jennifer
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.

jennifer
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