RingBuffer

a simple @safe and @nogc-compatible template ringbuffer.

Members

Functions

clear
void clear()

empty the buffer

empty
bool empty()
front
DataType front()

range interface

opAssign
void opAssign(DataType[] rhs)

assignment

opOpAssign
void opOpAssign(DataType rhs)
void opOpAssign(DataType[] rhs)

push to buffer

pop
DataType pop()

retrieve item from buffer (lifo)

popFront
void popFront()

range interface

push
void push(DataType rhs)
void push(DataType[] rhs)

push to buffer

shift
DataType shift()

retrieve item from buffer (fifo)

Properties

capacity
auto capacity [@property getter]
length
auto length [@property getter]

Examples

example

RingBuffer!(int, 5) buff;
// non-power-of-two lengths are supported, though powers of two will be faster
buff.push(69);
buff ~= 420; // equivilent to the push syntax
assert(buff.shift == 69);
assert(buff.shift == 420);

import std.array : staticArray;
import std.range : iota;
immutable int[5] temp = staticArray!(iota(5));

buff.push(temp); // multiple items may be pushed in a single call

assert(buff.length == 5);
assert(buff.capacity == 0);

assert(buff.pop == 4);

assert(buff.length == 4);
assert(buff.capacity == 1);

buff.clear();

assert(buff.length == 0);
assert(buff.capacity == 5);

Meta

Authors

Susan

Date

Date: 2022-02-03 Licence: AGPL-3.0 or later