ringbuffer - fixed-capacity ring buffer
Import with import "ringbuffer.j" as ringbuffer;. A fixed-capacity FIFO of strings that overwrites the oldest entry when full - a sliding window of the most recent items (log lines, events, samples). Pure .j; runs on both binaries.
import "ringbuffer.j" as ringbuffer;
def rb as ringbuffer.RingBuffer init ringbuffer.new(3);
$rb = ringbuffer.push($rb, "a");
$rb = ringbuffer.push($rb, "b");
$rb = ringbuffer.push($rb, "c");
$rb = ringbuffer.push($rb, "d"); # overwrites "a"; buffer is now [b, c, d]
ringbuffer.first($rb); # "b" (oldest)
$rb = ringbuffer.pop($rb); # buffer is now [c, d]Runnable: examples/modules/ringbuffer_demo.j.
Surface
A RingBuffer has items (the slots, in storage order), capacity, head (the slot of the oldest entry) and count. A capacity below 1, and pop / first / last on an empty buffer, throw Error{kind: "ringbuffer"}.
| Call | Returns | |
|---|---|---|
ringbuffer.new(capacity) | RingBuffer | an empty buffer of the given capacity (>= 1) |
ringbuffer.push(rb, item) | RingBuffer | append, dropping the oldest if already full |
ringbuffer.pop(rb) | RingBuffer | remove the oldest (throws if empty) |
ringbuffer.first(rb) | string | peek the oldest (throws if empty) |
ringbuffer.last(rb) | string | peek the newest (throws if empty) |
ringbuffer.size(rb) | int | the number of entries held |
ringbuffer.capacity(rb) | int | the capacity |
ringbuffer.isEmpty(rb) | bool | whether it holds no entries |
ringbuffer.isFull(rb) | bool | whether it is at capacity |
ringbuffer.toList(rb) | list of string | a copy of the entries, oldest to newest |
Reading while removing
Because the buffer is value-semantic, a pop cannot return both the removed item and the new buffer. Read the oldest with first before you pop it:
use io;
import "ringbuffer.j" as ringbuffer;
def rb as ringbuffer.RingBuffer init ringbuffer.push(ringbuffer.push(ringbuffer.new(4), "a"), "b");
while (not ringbuffer.isEmpty($rb)) {
def oldest as string init ringbuffer.first($rb);
io.printf("%s\n", $oldest);
$rb = ringbuffer.pop($rb);
}Scope
- Strings only. Store other values through
convert.toStringor ajson/encodingrepresentation. - Value-semantic.
push/popreturn the updated buffer; assign the result back ($rb = ringbuffer.push($rb, x)), which updates it in place: a push writes one slot and a pop moves the head, whatever the capacity. Any other use copies the buffer first, as value semantics require. itemsis storage order. Once the buffer wraps, the oldest entry sits at slothead, not at 0. Read the entries withtoList(oldest to newest).- Overwrite on full is silent.
pushdrops the oldest when at capacity with no signal - checkisFullfirst if you need to know.
See also
- bloom.md - the sibling data-structure module.
- modules/index.md - the module catalog and import rules.
Academy talks
| Talk | What it shows |
|---|---|
| A terminal game in Jennifer | the snake body as a growing ring buffer |
API reference
Every exported function, struct and constant of ringbuffer, with its signature, generated from the module's doc comments: the ringbuffer API reference.