ringbuffer
stableA fixed-capacity circular buffer that overwrites the oldest element when full, with FIFO push/pop and random access.
use plugin ringbuffer::{RingBuffer.new, push, pop, …} Functions (13)
- RingBuffer.new Create a ring buffer with a fixed capacity
- push Add a value, overwriting oldest if full
- pop Remove and return the oldest value
- peek Read the oldest value without removing it
- peek_back Read the most recently pushed value
- get Read a value by index without removing it
- len Return the number of stored elements
- capacity Return the maximum capacity
- is_empty Check if the buffer is empty
- is_full Check if the buffer is at capacity
- contains Check if a value is present
- clear Remove all elements
- to_table Return all elements as an ordered table
Create a ring buffer with a fixed capacity
Creates a new ring buffer with the given fixed capacity. Once full, pushing a new value silently overwrites the oldest element.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(4)
buf.push(10)
buf.push(20)
buf.push(30)
buf.push(40)
buf.push(50) // overwrites 10
print(buf.len()) // 4
Add a value, overwriting oldest if full
Appends a value to the back of the buffer. If the buffer is full, the oldest (front) element is discarded to make room.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(3)
buf.push("a")
buf.push("b")
buf.push("c")
buf.push("d") // "a" is dropped
print(buf.peek()) // b
Remove and return the oldest value
Removes and returns the oldest element (FIFO order). Returns nil if the buffer is empty.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(3)
buf.push(1)
buf.push(2)
print(buf.pop()) // 1
print(buf.pop()) // 2
print(buf.pop()) // nil
Read the oldest value without removing it
Returns the oldest element without removing it. Returns nil if empty.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(3)
buf.push("first")
print(buf.peek()) // first
print(buf.len()) // 1 (unchanged)
Read the most recently pushed value
Returns the most recently pushed element without removing it. Returns nil if empty. Useful for inspecting the latest value in a sensor stream.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(5)
buf.push(100)
buf.push(200)
buf.push(300)
print(buf.peek_back()) // 300
Read a value by index without removing it
Returns the element at the given zero-based index (oldest = 0). Returns nil if out of range. Does not remove the element.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(4)
buf.push("x")
buf.push("y")
buf.push("z")
print(buf.get(1)) // y
Return the number of stored elements
Returns the number of elements currently in the buffer.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(10)
buf.push(1)
buf.push(2)
print(buf.len()) // 2
Return the maximum capacity
Returns the fixed capacity the buffer was created with.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(8)
print(buf.capacity()) // 8
Check if the buffer is empty
Returns true if the buffer contains no elements.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(3)
print(buf.is_empty()) // true
buf.push(1)
print(buf.is_empty()) // false
Check if the buffer is at capacity
Returns true if the number of elements equals the capacity.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(2)
buf.push("a")
buf.push("b")
print(buf.is_full()) // true
Check if a value is present
Returns true if the buffer contains an element equal to value. Supports int, float, bool, string, and nil comparisons.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(5)
buf.push("apple")
buf.push("banana")
print(buf.contains("apple")) // true
print(buf.contains("cherry")) // false
Remove all elements
Removes all elements, resetting the buffer to empty while keeping its capacity.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(4)
buf.push(1)
buf.push(2)
buf.clear()
print(buf.len()) // 0
Return all elements as an ordered table
Returns all current elements as a 1-indexed table in FIFO order (oldest first). Does not modify the buffer.
use plugin ringbuffer::{RingBuffer}
let buf = RingBuffer.new(4)
buf.push("a")
buf.push("b")
buf.push("c")
let t = buf.to_table()
print(t[1]) // a
print(t[2]) // b