/
container.d
370 lines (323 loc) · 8.88 KB
/
container.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/++
Containers and thereto related functionality.
Example:
---
{
Buffer!string buffer;
buffer.put("abc");
buffer.put("def");
assert(!buffer.empty);
assert(buffer.front == "abc");
buffer.popFront();
assert(buffer.front == "def");
buffer.popFront();
assert(buffer.empty);
}
{
Buffer!(char, Yes.dynamic, 3) buffer;
assert(!buffer.buf.length);
buffer ~= 'a';
assert(buffer.buf.length == 3);
buffer ~= 'b';
buffer ~= 'c';
assert(buffer.length == 3);
buffer ~= 'd';
assert(buffer.buf.length > 3);
assert(buffer[0..5] == "abcd");
buffer.clear();
assert(buffer.empty);
}
---
+/
module lu.container;
private:
import std.typecons : Flag, No, Yes;
public:
@safe:
// Buffer
/++
Simple buffer/queue for storing and fetching items of any type `T`. Does not use
manual memory allcation.
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.
Example:
---
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);
---
Params:
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 bufferCurrent position in the 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.
+/
struct Buffer(T, Flag!"dynamic" dynamic = No.dynamic, size_t originalSize = 128)
{
pure nothrow:
static if (dynamic)
{
/++
By how much to grow the buffer when we reach the end of it.
+/
private enum growthFactor = 1.5;
/++
Internal buffer dynamic array.
+/
T[] buf;
/++
Variable buffer size.
+/
size_t bufferSize;
}
else
{
/++
Internal buffer static array.
+/
T[bufferSize] buf;
/++
Static buffer size.
+/
alias bufferSize = originalSize;
}
/++
Current position in the array.
+/
ptrdiff_t pos;
/++
Position of last entry in the array.
+/
ptrdiff_t end;
static if (dynamic)
{
// put
/++
Append an item to the end of the buffer.
If it would be put beyond the end of the buffer, it will be resized to fit.
Params:
more = Item to add.
+/
void put(/*const*/ T more)
{
if (end == bufferSize)
{
bufferSize = !bufferSize ? originalSize : cast(size_t)(bufferSize * growthFactor);
buf.length = bufferSize;
}
buf[end++] = more;
}
}
else
{
// put
/++
Append an item to the end of the buffer.
If it would be put beyond the end of the buffer, it will assert.
Params:
more = Item to add.
+/
void put(/*const*/ T more) @nogc
in ((end < bufferSize), '`' ~ typeof(this).stringof ~ "` buffer overflow")
{
buf[end++] = more;
}
}
static if (dynamic)
{
// reserve
/++
Reserves enough room for the specified number of elements. If there
is already enough room, nothing is done. Otherwise the buffer is grown.
Params:
reserveSize = Number of elements to reserve size for.
+/
void reserve(const size_t reserveSize)
{
if (bufferSize < reserveSize)
{
bufferSize = reserveSize;
buf.length = bufferSize;
}
}
}
// opOpAssign
/++
Implements `buf ~= someT` (appending) by wrapping `put`.
Params:
op = Operation type, here specialised to "`~`".
more = Item to add.
+/
void opOpAssign(string op : "~")(const T more)
{
return put(more);
}
// front
/++
Fetches the item at the current position of the buffer.
Returns:
An item T.
+/
T front() const @nogc
in ((end > 0), '`' ~ typeof(this).stringof ~ "` buffer underrun")
{
return buf[pos];
}
// popFront
/++
Advances the current position to the next item in the buffer.
+/
void popFront() @nogc
{
if (++pos == end) reset();
}
// length
/++
Returns what amounts to the current length of the buffer; the distance
between the current position `pos` and the last element `end`.
Returns:
The buffer's current length.
+/
size_t length() const @nogc
{
return (end - pos);
}
// empty
/++
Returns whether or not the array is considered empty.
Mind that the buffer may well still contain old contents. Use `clear`
to zero it out.
Returns:
`true` if there are items available to get via `front`, `false` if not.
+/
bool empty() const @nogc
{
return (end == 0);
}
// reset
/++
Resets the array positions, effectively soft-emptying the buffer.
The old elements' values are still there, they will just be overwritten
as the buffer is appended to.
+/
void reset() @nogc
{
pos = 0;
end = 0;
}
// clear
/++
Zeroes out the buffer's elements, getting rid of old contents.
+/
void clear() @nogc
{
reset();
buf[] = T.init;
}
}
///
unittest
{
{
Buffer!(bool, No.dynamic, 4) buffer;
assert(buffer.empty);
buffer.put(true);
buffer.put(false);
assert(buffer.length == 2);
buffer.put(true);
buffer.put(false);
assert(!buffer.empty);
assert(buffer.front == true);
buffer.popFront();
assert(buffer.front == false);
buffer.popFront();
assert(buffer.front == true);
buffer.popFront();
assert(buffer.front == false);
buffer.popFront();
assert(buffer.empty);
assert(buffer.buf == [ true, false, true, false ]);
buffer.put(false);
assert(buffer.buf == [ false, false, true, false ]);
buffer.reset();
assert(buffer.empty);
buffer.clear();
assert(buffer.buf == [ false, false, false, false ]);
}
{
Buffer!(string, No.dynamic, 4) buffer;
assert(buffer.empty);
buffer.put("abc");
buffer.put("def");
buffer.put("ghi");
assert(!buffer.empty);
assert(buffer.front == "abc");
buffer.popFront();
assert(buffer.front == "def");
buffer.popFront();
buffer.put("JKL");
assert(buffer.front == "ghi");
buffer.popFront();
assert(buffer.front == "JKL");
buffer.popFront();
assert(buffer.empty);
assert(buffer.buf == [ "abc", "def", "ghi", "JKL" ]);
buffer.put("MNO");
assert(buffer.buf == [ "MNO", "def", "ghi", "JKL" ]);
buffer.clear();
assert(buffer.buf == [ string.init, string.init, string.init, string.init ]);
}
{
Buffer!(char, No.dynamic, 64) buffer;
buffer ~= 'a';
buffer ~= 'b';
buffer ~= 'c';
assert(buffer.buf[0..3] == "abc");
foreach (char_; buffer)
{
assert((char_ == 'a') || (char_ == 'b') || (char_ == 'c'));
}
}
{
Buffer!(int, Yes.dynamic, 3) buffer;
assert(!buffer.buf.length);
buffer ~= 1;
assert(buffer.buf.length == 3);
buffer ~= 2;
buffer ~= 3;
assert(buffer.front == 1);
buffer.popFront();
assert(buffer.front == 2);
buffer.popFront();
assert(buffer.front == 3);
buffer.popFront();
assert(buffer.empty);
buffer.reserve(64);
assert(buffer.buf.length == 64);
buffer.reserve(63);
assert(buffer.buf.length == 64);
}
{
Buffer!(char, No.dynamic, 4) buffer;
buffer ~= 'a';
buffer ~= 'b';
buffer ~= 'c';
buffer ~= 'd';
assert(buffer.buf == "abcd");
assert(buffer.length == 4);
buffer.reset();
assert(buffer.buf == "abcd");
buffer.clear();
assert(buffer.buf == typeof(buffer.buf).init);
}
}