Libraries / ringbuffer
Overview
A zero-dependency, fixed-size circular buffer (RingBuffer) for HolyPHP, designed for streaming data, bounded queues, and sliding windows.
When the buffer reaches capacity, pushing a new element automatically overwrites the oldest element in O(1) time.
bundled — works offline
zero dependencies
circular
queue
Quick start
import "ringbuffer";
$rb = new RingBuffer(3);
$rb->push(10);
$rb->push(20);
$rb->push(30);
// Buffer is now full: [10, 20, 30]
$rb->push(40);
// Oldest element (10) is overwritten: [20, 30, 40]
println($rb->peek()); // 20
println($rb->pop()); // 20
println($rb->size()); // 2
println(json_encode($rb->toArray())); // [30, 40]
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install ringbuffer
This adds the library to hphp.yaml, installs it into .hphp/packages/ringbuffer/ and pins the exact version in hphp.lock:
require:
ringbuffer: "^1.0"
Use it
import "ringbuffer";
ringbuffer is bundled with the compiler, so import "ringbuffer"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install ringbuffer # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install ringbuffer
Documentation
API Reference
Class RingBuffer
new RingBuffer(int $capacity)
Creates a new fixed-size circular buffer with the given positive integer $capacity.
Throws: Exception if $capacity <= 0.
push($value)
Appends $value to the tail of the buffer. If the buffer is already full, the oldest element at the head is overwritten.
pop()
Removes and returns the oldest element from the head of the buffer.
Returns: The oldest stored value.
Throws: Exception if the buffer is empty.
peek()
Returns the oldest element at the head of the buffer without removing it.
Returns: The oldest stored value.
Throws: Exception if the buffer is empty.
isFull(): bool
Returns true if size() === capacity(), otherwise false.
isEmpty(): bool
Returns true if size() === 0, otherwise false.
size(): int
Returns the current number of elements stored in the buffer.
capacity(): int
Returns the maximum number of elements the buffer can hold.
toArray(): array
Returns all elements currently in the buffer as a 0-indexed array in FIFO order (oldest to newest).
clear()
Removes all elements from the buffer and resets its internal pointers.
Helper Function
ringbuffer_create(int $capacity): RingBuffer
Convenience factory function that returns new RingBuffer($capacity).
Error Handling
ringbuffer throws Exception with descriptive, prefixed messages on invalid operations:
Invalid capacity (<= 0): ringbuffer: capacity must be a positive integer, got <value>
Popping from an empty buffer: ringbuffer: cannot pop from an empty buffer
Peeking an empty buffer: ringbuffer: cannot peek an empty buffer
try {
$rb = new RingBuffer(0);
} catch (Exception $e) {
println($e->getMessage());
// ringbuffer: capacity must be a positive integer, got 0
}
Examples
01 Quick start
Bundled with the compiler — import "ringbuffer"; works offline in any file, or add it to a project with hphp install ringbuffer.
import "ringbuffer";
$rb = new RingBuffer(3);
$rb->push(10);
$rb->push(20);
$rb->push(30);
// Buffer is now full: [10, 20, 30]
$rb->push(40);
// Oldest element (10) is overwritten: [20, 30, 40]
println($rb->peek()); // 20
println($rb->pop()); // 20
println($rb->size()); // 2
println(json_encode($rb->toArray())); // [30, 40]
Try it hphp install ringbuffer
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_ringbuffer.hphp.