RingBuffer

An array-based implementation of a ring buffer.

Constructors

this
this(size_t bufferSize)
Undocumented in source.

Members

Aliases

capacity
alias capacity = bufferSize
Undocumented in source.
put
alias put = pushBack
Undocumented in source.

Functions

clear
void clear()
Undocumented in source. Be warned that the author may not have intended to support it.
popBack
void popBack()
Undocumented in source. Be warned that the author may not have intended to support it.
popFront
void popFront()
Undocumented in source. Be warned that the author may not have intended to support it.
pushBack
void pushBack(T value)
Undocumented in source. Be warned that the author may not have intended to support it.
pushFront
void pushFront(T value)
Undocumented in source. Be warned that the author may not have intended to support it.
reserve
void reserve(size_t num)
Undocumented in source. Be warned that the author may not have intended to support it.

Manifest constants

bufferSize
enum bufferSize;
Undocumented in source.

Properties

back
T back [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
back
T back [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.
bufferSize
size_t bufferSize [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
empty
bool empty [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
front
T front [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
front
T front [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.
length
size_t length [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
save
RingBuffer!(T, staticBufferSize) save [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

Ring buffer stores the bufferSize most recent elements.

import std.algorithm;

auto buffer = RingBuffer!int(5);

buffer.pushFront(1);
buffer.pushFront(2);
buffer.pushFront(3);
buffer.pushFront(4);
buffer.pushFront(5);

assert(buffer.front == 5);
assert(equal(buffer, [5, 4, 3, 2, 1]));

buffer.pushFront(6);

assert(buffer.front == 6);
assert(equal(buffer, [6, 5, 4, 3, 2]));

Ring buffer may have a static size.

import std.algorithm;

auto buffer = RingBuffer!(int, 5)();

buffer.pushFront(1);
buffer.pushFront(2);
buffer.pushFront(3);
buffer.pushFront(4);
buffer.pushFront(5);

assert(buffer.front == 5);
assert(equal(buffer, [5, 4, 3, 2, 1]));

buffer.pushFront(6);

assert(buffer.front == 6);
assert(equal(buffer, [6, 5, 4, 3, 2]));

Elements can be removed.

import std.algorithm;

auto buffer = RingBuffer!int(5);

buffer.pushFront(1);
buffer.pushFront(2);
buffer.pushFront(3);
buffer.pushFront(4);
buffer.pushFront(5);

assert(buffer.length == 5);
assert(equal(buffer, [5, 4, 3, 2, 1]));

buffer.popFront();
buffer.popBack();

assert(buffer.length == 3);
assert(equal(buffer, [4, 3, 2]));

The buffer is double-ended.

import std.algorithm;

auto buffer = RingBuffer!int(5);

buffer.pushFront(1);
buffer.pushFront(2);
buffer.pushFront(3);
buffer.pushFront(4);
buffer.pushFront(5);
buffer.pushFront(6);

assert(buffer.front == 6);
assert(buffer.back == 2);
assert(equal(buffer, [6, 5, 4, 3, 2]));

buffer.pushBack(1);

assert(buffer.front == 5);
assert(buffer.back == 1);
assert(equal(buffer, [5, 4, 3, 2, 1]));

The buffer can be used as an output range.

import std.algorithm;
import std.range;

auto buffer = RingBuffer!int(32);

iota(8).copy(&buffer);

assert(buffer.front == 0);
buffer.popFront();

assert(buffer.front == 1);
buffer.popFront();

assert(equal(buffer, iota(2, 8)));

The size of the underlying array may be increased.

import std.algorithm;

auto buffer = RingBuffer!int(5);

buffer.pushBack(1); // [1]
buffer.pushBack(2); // [1, 2]
buffer.pushBack(3); // [1, 2, 3]
buffer.pushBack(4); // [1, 2, 3, 4]
buffer.pushBack(5); // [1, 2, 3, 4, 5]
// elements are no longer linear in memory
buffer.pushBack(6); // [2, 3, 4, 5, 6]

assert(buffer.capacity == 5);
buffer.reserve(10);
assert(equal(buffer, [2, 3, 4, 5, 6]));

Meta