Buffer

Simple buffer/queue for storing and fetching items of any type T. Does not use manual memory allocation.

It can use a static array internally to store elements on the stack, which imposes a hard limit on how many items can be added, or a dynamic heap one with a resizable buffer.

Members

Aliases

bufferSize
alias bufferSize = originalSize

Static buffer size.

Functions

clear
void clear()

Zeroes out the buffer's elements, getting rid of old contents.

empty
auto empty()

Returns whether or not the container is considered empty.

front
auto ref front()

Fetches the item at the current position of the buffer.

length
auto length()

Returns what amounts to the current length of the buffer; the distance between the current position pos and the last element end.

opOpAssign
void opOpAssign(T more)

Implements buf ~= someT (appending) by wrapping put.

popFront
void popFront()

Advances the current position to the next item in the buffer.

put
void put(T more)

Append an item to the end of the buffer.

put
void put(T more)

Append an item to the end of the buffer.

reserve
void reserve(size_t reserveSize)

Reserves enough room for the specified number of elements. If there is already enough room, nothing is done. Otherwise the buffer is grown.

reset
void reset()

Resets the array positions, effectively soft-emptying the buffer.

Manifest constants

growthFactor
enum growthFactor;

By how much to grow the buffer when we reach the end of it.

Variables

buf
T[] buf;

Internal buffer dynamic array.

buf
T[bufferSize] buf;

Internal buffer static array.

bufferSize
size_t bufferSize;

Variable buffer size.

end
ptrdiff_t end;

Position of last entry in the array.

pos
ptrdiff_t pos;

Current position in the array.

Parameters

T

Buffer item type.

dynamic

Whether to use a dynamic array whose size can be grown at runtime, or to use a static array with a fixed size. Trying to add more elements than there is room for will cause an assert. Defaults to No.dynamic; a static array.

originalSize

How many items to allocate space for. If No.dynamic was passed it will assert if you attempt to store anything past this amount.

Examples

Buffer!(string, No.dynamic, 16) buffer;

buffer.put("abc");
buffer ~= "def";
assert(!buffer.empty);
assert(buffer.front == "abc");
buffer.popFront();
assert(buffer.front == "def");
buffer.popFront();
assert(buffer.empty);
1 {
2     Buffer!(bool, No.dynamic, 4) buffer;
3 
4     assert(buffer.empty);
5     buffer.put(true);
6     buffer.put(false);
7     assert(buffer.length == 2);
8     buffer.put(true);
9     buffer.put(false);
10 
11     assert(!buffer.empty);
12     assert(buffer.front == true);
13     buffer.popFront();
14     assert(buffer.front == false);
15     buffer.popFront();
16     assert(buffer.front == true);
17     buffer.popFront();
18     assert(buffer.front == false);
19     buffer.popFront();
20     assert(buffer.empty);
21     assert(buffer.buf == [ true, false, true, false ]);
22     buffer.put(false);
23     assert(buffer.buf == [ false, false, true, false ]);
24     buffer.reset();
25     assert(buffer.empty);
26     buffer.clear();
27     assert(buffer.buf == [ false, false, false, false ]);
28 }
29 {
30     Buffer!(string, No.dynamic, 4) buffer;
31 
32     assert(buffer.empty);
33     buffer.put("abc");
34     buffer.put("def");
35     buffer.put("ghi");
36 
37     assert(!buffer.empty);
38     assert(buffer.front == "abc");
39     buffer.popFront();
40     assert(buffer.front == "def");
41     buffer.popFront();
42     buffer.put("JKL");
43     assert(buffer.front == "ghi");
44     buffer.popFront();
45     assert(buffer.front == "JKL");
46     buffer.popFront();
47     assert(buffer.empty);
48     assert(buffer.buf == [ "abc", "def", "ghi", "JKL" ]);
49     buffer.put("MNO");
50     assert(buffer.buf == [ "MNO", "def", "ghi", "JKL" ]);
51     buffer.clear();
52     assert(buffer.buf == [ string.init, string.init, string.init, string.init ]);
53 }
54 {
55     Buffer!(char, No.dynamic, 64) buffer;
56     buffer ~= 'a';
57     buffer ~= 'b';
58     buffer ~= 'c';
59     assert(buffer.buf[0..3] == "abc");
60 
61     foreach (char_; buffer)
62     {
63         assert((char_ == 'a') || (char_ == 'b') || (char_ == 'c'));
64     }
65 }
66 {
67     Buffer!(int, Yes.dynamic, 3) buffer;
68     assert(!buffer.buf.length);
69     buffer ~= 1;
70     assert(buffer.buf.length == 3);
71     buffer ~= 2;
72     buffer ~= 3;
73     assert(buffer.front == 1);
74     buffer.popFront();
75     assert(buffer.front == 2);
76     buffer.popFront();
77     assert(buffer.front == 3);
78     buffer.popFront();
79     assert(buffer.empty);
80     buffer.reserve(64);
81     assert(buffer.buf.length == 64);
82     buffer.reserve(63);
83     assert(buffer.buf.length == 64);
84 }
85 {
86     Buffer!(char, No.dynamic, 4) buffer;
87     buffer ~= 'a';
88     buffer ~= 'b';
89     buffer ~= 'c';
90     buffer ~= 'd';
91     assert(buffer.buf == "abcd");
92     assert(buffer.length == 4);
93     buffer.reset();
94     assert(buffer.buf == "abcd");
95     buffer.clear();
96     assert(buffer.buf == typeof(buffer.buf).init);
97 }