/
arraybuilder.d
362 lines (318 loc) · 7.06 KB
/
arraybuilder.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
/**
This module implements an array with fast concatenation. See benchmark.
Authors: Poggel / Fr3nchK1ss
Copyright: Contact Fr3nchK1ss
*/
module dterrent.core.arraybuilder;
import dterrent.system.logger;
import std.format;
/**
* Behaves the same as built-in arrays, except about 3x faster with concatenation
*
* Use .data to access the underlying array.
* ArrayBuilder is a value type, so all copies point to the same data, unless either:
* - A slice is taken and then appended to.
* - .dup() is called.
*/
struct ArrayBuilder(T)
{
alias AT = ArrayBuilder!(T);
private T[] array; // array.length is capacity
private size_t size; // current used size
size_t reserve; // capacity will never be less than this size, takes effect after the next grow.
///
static AT opCall()
{
AT result;
return result;
}
///
static AT opCall(T[] elems)
{
AT result;
result.array = elems;
result.size = elems.length;
return result;
}
///
size_t capacity()
{
return array.length;
}
///
T[] data()
{
return array[0 .. size];
}
///
void dispose()
{
array.destroy();
}
///
AT dup()
{
AT result;
result.array = array.dup;
result.size = size;
return result;
}
///
size_t length()
{
return size;
}
///
void length(size_t l)
{
size = l;
grow();
}
///
int opApply(int delegate(ref T) dg)
{
int result = 0;
for (int i = 0; i < size; i++)
{
result = dg(array[i]);
if (result)
break;
}
return result;
}
///
AT opAssign(T[] elem)
{
array = elem;
size = elem.length;
return this;
}
///
AT opCat(T elem)
{
return AT(array ~ elem);
}
AT opCat(T[] elem) /// ditto
{
return AT(array ~ elem);
}
AT opCat(AT elem) /// ditto
{
return AT(array ~ elem.array);
}
///
void opOpAssign(string op)(T elem) if (op == "~")
{
size++;
grow();
array[size - 1] = elem;
}
/// Temporary until opCatAssign always works
T* append(T elem)
{
size++;
grow();
array[size - 1] = elem;
return &array[size - 1];
}
///
void opCatAssign(T[] elem)
{
size_t old_size = size; // same as push
size += elem.length;
grow();
array[old_size .. size] = elem[0 .. $];
}
///
void opOpAssign(string op)(AT elem) if (op == "~")
{
size_t old_size = size;
size += elem.array.length;
grow();
array[old_size .. size] = elem.array[0 .. $];
}
/// TODO: This returns a copy, so a[i].b = 3; doesn't work!!
T* opIndex(size_t i)
{
assert(i < size, format("array index %s out of bounds 0..%s", i, size));
return &array[i];
}
/// ditto
T opIndexAssign(T val, size_t i)
{
assert(i < size, format("array index %s out of bounds 0..%s", i, size));
return array[i] = val;
}
///
AT opSlice() // overloads a[], a slice of the entire array
{
return AT(array[0 .. size]);
}
/// ditto
AT opSlice(size_t start, size_t end)
{
assert(end <= size, format("array index %s out of bounds 0..%s", end, size));
return AT(array[start .. end]); // overloads a[i .. j]
}
///
AT opSliceAssign(T v) // overloads a[] = v
{
array[0 .. size] = v;
return this;
}
///
AT opSliceAssign(T v, size_t start, size_t end) // overloads a[i .. j] = v
{
assert(end <= size);
array[start .. end] = v;
return this;
}
void reserveAndClear()
{
if (reserve < array.length)
reserve = array.length;
size = 0;
}
///
T* ptr()
{
return array.ptr;
}
/// Remove an element, replacing it with one from the end.
void remove(size_t index)
{
array[index] = array[size - 1];
size--;
}
///
import std.algorithm.mutation: reverse;
AT reverse()
{
array.reverse;
return this;
}
/**
* Add and remove elements from the array, in-place.
* Params:
* index = pivot
* remove = Number of elements to remove, including and after index (can be 0)
* insert = Element to insert before index, after elements have been removed. */
void splice(size_t index, size_t remove, T[] insert...)
{
// Split member 'array' in two parts
T[] firstPart = array[0 .. index];
T[] secondPart = array[index + remove .. $];
length(firstPart.length + insert.length + secondPart.length);
array = firstPart ~ insert ~ secondPart;
}
///
string toString() const
{
return ""; // TODO this needs to output the data. swritef(data);
}
/// Grow the internal array
private void grow()
{
if (array.length < size || size * 4 < array.length)
{
ulong new_size = size * 2 + 1;
if (new_size < reserve)
new_size = reserve;
array.length = new_size;
}
}
}
unittest
{
{
struct A
{
int x, y;
}
A a;
ArrayBuilder!(A) array;
array ~= a;
array[0].x = 3;
assert(array[0].x == 3);
}
{ // Test slice and append; ensure copy on append is performed
immutable test = ArrayBuilder!(int)([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
auto original = ArrayBuilder!(int)([0, 1, 2, 3]);
auto s = original[1 .. 3];
assert(s[0] == original[1]); // compares pointer
s ~= 12;
assert(*s[0] == 1);
assert(*s[1] == 2);
assert(*s[2] == 12);
assert(*original[3] == 3);
}
{ // Test splice
auto test = ArrayBuilder!(int)([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
test.splice(3, 3);
assert(test.data == [0, 1, 2, 6, 7, 8, 9]);
test.splice(3, 0, [3, 4, 5]);
assert(test.data == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
test.splice(0, 3, [2, 1, 0]);
assert(test.data == [2, 1, 0, 3, 4, 5, 6, 7, 8, 9]);
test.splice(test.length, 0, 10);
assert(test.data == [2, 1, 0, 3, 4, 5, 6, 7, 8, 9, 10]);
auto test2 = ArrayBuilder!(int)();
test2.splice(0, 0, 1);
assert(test2.data == [1]);
}
/*
{
trace("*** ArrayBuilder Concatenation benchmark ***");
import std.datetime.stopwatch : benchmark, Duration;
import std.array : appender;
void concat_withStd()
{
int[] array;
for (int j = 0; j < 1000; j++)
array ~= j;
}
void concat_withStdReserve()
{
int[] array;
array.reserve(1000);
for (int j = 0; j < 1000; j++)
array ~= j;
}
void concat_withStdLength()
{
int[] array;
array.length = 1000;
for (int j = 0; j < 1000; j++)
array[j] = j;
}
void concat_withAppenderReserve()
{
auto array = appender!(int[]);
//trace ("array is ", array);
array.reserve(1000);
for (int j = 0; j < 1000; j++)
array ~= j;
}
void concat_withArrayBuilder()
{
ArrayBuilder!(int) array;
for (int j = 0; j < 1000; j++)
array ~= j;
}
auto r = benchmark!(concat_withStd,
concat_withStdReserve,
concat_withStdLength,
concat_withAppenderReserve,
concat_withArrayBuilder)(10_000);
Duration bench1 = r[0];
Duration bench2 = r[1];
Duration bench3 = r[2];
Duration bench4 = r[3];
Duration bench5 = r[4];
tracef("with std: %s", bench1);
tracef("with stdReserve: %s", bench2);
tracef("with stdLength: %s", bench3);
tracef("with AppenderReserve: %s", bench4);
tracef("with Arraybuilder: %s \n", bench5);
}*/
}