RingBuffer

An array-based implementation of a ring buffer.

Constructors

this
this()
Undocumented in source.
this
this(size_t bufferSize)
Undocumented in source.

Members

Aliases

put
alias put = pushFront
Undocumented in source.

Functions

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.

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]));

Meta