-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
424 lines (336 loc) · 9.95 KB
/
array.h
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#pragma once
#include "common.h"
//
// Resizable array/dynamic array starts here
// We chose to make Resizable_Array allocate according to the allocator.
// So they could be allocated on the heap/pool/...
//
template <typename Value_Type>
struct Resizable_Array
{
i64 allocated = 0;
i64 count = 0;
Value_Type *data = NULL;
Allocator allocator = {};
//
// Helper functions below this line
//
Value_Type *begin() { if ((data == NULL) || (count <= 0)) return NULL; return &data[0]; }
Value_Type *end() { if ((data == NULL) || (count <= 0)) return NULL; return &data[count]; }
Value_Type &operator[](i64 index)
{
assert(index < count);
assert(index >= 0);
assert(data != NULL);
return data[index];
}
operator bool() { return count > 0; }
Resizable_Array<Value_Type> operator=(std::initializer_list<Value_Type> rhs)
{
array_reset(this);
array_reserve(this, rhs.size());
i64 it = 0;
for (auto val : rhs)
{
array_add(this, val);
it += 1;
}
return *this;
}
};
template <typename V>
using RArr = Resizable_Array<V>;
template <typename V>
void array_init(RArr<V> *array)
{
array->count = 0;
array->allocated = 0;
array->allocator = (Allocator){};
}
template <typename V>
V *array_add(RArr<V> *array)
{
if (array == NULL) return NULL;
if (array->count >= array->allocated)
{
i64 reserve = array->allocated * 2;
if (reserve < 8) reserve = 8;
array_reserve(array, reserve);
}
auto value = &array->data[array->count];
array->count += 1;
V dummy; // @Speed: Doing this to properly initialized the newly added value.
*value = dummy;
return value;
}
template <typename V>
void array_add(RArr<V> *array, V value)
{
if (array == NULL) return;
if (array->count >= array->allocated)
{
i64 reserve = array->allocated * 2;
if (reserve < 8) reserve = 8;
array_reserve(array, reserve);
}
array->data[array->count] = value;
array->count += 1;
}
template <typename V>
void array_reset(RArr<V> *array)
{
if (array == NULL) return;
// @Note: We shouldn't reallocate the array, right?
array->count = 0; // set the count to 0, so that new elements override old one
}
template <typename V>
V *array_find(RArr<V> *array, V value)
{
if (array == NULL) return NULL;
auto it = array->begin();
auto i = 0;
while (i < array->count)
{
if (*it == value) return it;
i += 1;
it += 1;
}
return NULL;
}
template <typename V>
void array_copy(RArr<V> *dest, RArr<V> *src)
{
// @Todo: array_copy is not yet implemented
assert(0);
}
template <typename V>
void array_reserve(RArr<V> *array, i64 size)
{
if ((array == NULL) || (size <= 0) || (size <= array->allocated)) return;
// We make sure that the count does not get modified so that it is larger than
// the allocated prior to the reservation.
// @Note: Change the count to 0 if you don't care about the old value inside the array.
// Otherwise, any value of count that is greater than 0 will make the array do RESIZE.
assert(array->count <= array->allocated);
// @Important: We try to use the allocator inside the array.
// If there isn't one, we use the global context allocator.
// Then if that is still unavailable, we then try to use the heap allocator.
if (!array->allocator)
{
array->allocator = global_context.allocator;
if (!array->allocator) array->allocator = {NULL, __default_allocator};
}
// If there is something already inside the array,
// we do a RESIZE instead of ALLOCATE
Allocator_Mode mode;
if (array->count > 0) mode = Allocator_Mode::RESIZE;
else mode = Allocator_Mode::ALLOCATE;
auto new_memory = static_cast<V*>(array->allocator.proc(mode,
size * sizeof(V),
array->allocated * sizeof(V),
array->data, array->allocator.data));
assert((new_memory));
array->data = new_memory;
array->allocated = size;
}
template <typename V>
void array_resize(RArr<V> *array, i64 size)
{
array_reserve(array, size);
array->count = size;
}
template <typename V>
void array_free(RArr<V> *array)
{
if (!array->data || (array->count <= 0)) return;
array->allocator.proc(Allocator_Mode::FREE, 0, 0, array->data, array->allocator.data);
array->data = NULL;
array->count = 0;
array->allocated = 0;
}
template <typename V>
inline
void my_free(RArr<V> *array)
{
array_free(array);
}
template <typename T>
void swap_elements(T *a, T *b)
{
auto temp = *a;
*a = *b;
*b = temp;
}
template <typename T>
void array_ordered_remove_by_index(RArr<T> *array, i64 index)
{
assert((array->count != 0));
array->count -= 1;
auto size_of_one_element = sizeof(T);
u8 *dest = reinterpret_cast<u8*>(array->data) + index * size_of_one_element;
u8 *src = dest + size_of_one_element;
auto n = array->count - index;
if (!n) return;
array->data = reinterpret_cast<T*>(memcpy(dest, src, n * size_of_one_element));
}
// Swap the item to remove with the last element
// Then shrink the array by 1, so that the item is effectively removed
template <typename V>
bool array_unordered_remove_by_value(RArr<V> *array, V value)
{
for (i64 it = 0; it < array->count; ++it)
{
if ((*array)[it] == value)
{
(*array)[it] = (*array)[array->count - 1];
array->count -= 1;
return true;
}
}
return false;
}
template <typename T>
T pop(RArr<T> *array)
{
if (array->count == 0)
{
assert(0);
}
auto result = array->data[array->count - 1];
array->count -= 1;
return result;
}
template <typename T>
using Array_Sort_Func = bool(*)(T a, T b);
template <typename T>
void array_actual_qsort(RArr<T> *array, Array_Sort_Func<T> sort_function, i64 lo, i64 hi)
{
if (lo >= hi || lo < 0) return;
//
// Partition the array and get the pivot index
//
i64 pivot;
{
auto p = array->data[hi];
auto i = lo;
for (i64 j = lo; j < hi; ++j)
{
if (sort_function(array->data[j], p))
{
swap_elements(&array->data[i], &array->data[j]);
i += 1;
}
}
swap_elements(&array->data[i], &array->data[hi]);
pivot = i;
}
//
// Sort the two partitions
//
array_actual_qsort(array, sort_function, lo, pivot - 1);
array_actual_qsort(array, sort_function, pivot + 1, hi);
}
template <typename T>
void array_add_if_unique(RArr<T> *array, T x)
{
auto found = array_find(array, x);
if (found != NULL) return;
array_add(array, x);
}
template <typename T>
void array_qsort(RArr<T> *array, Array_Sort_Func<T> sort_function)
{
array_actual_qsort(array, sort_function, 0, array->count - 1);
}
//
// Static array starts here
// These are arrays that are allocated on the stacks.
// Use them for small data storage.
//
template <typename V>
struct Static_Array;
template <typename V>
Static_Array<V> NewArray(i64 count, i64 alignment = 8, Allocator allocator = {}); // @ForwardDeclare
template <typename Value_Type>
struct Static_Array
{
i64 count = 0;
Value_Type *data = NULL;
Allocator allocator;
explicit Static_Array() {}
explicit Static_Array(i64 c)
{
*this = NewArray<Value_Type>(c);
}
~Static_Array()
{
// This does not free the individual elements if the elements are allocated seperately.
// @Leak if you don't call array_free after exiting the scope.
// array_free(this);
}
//
// Helper functions below this line
//
void set(Value_Type rhs[])
{
memcpy(data, rhs, sizeof(data));
}
Static_Array &operator=(Value_Type rhs[]) { set(rhs); return *this; }
void clear()
{
memset(data, 0, sizeof(Value_Type) * count);
}
// @Todo: missing copy procedure...
Value_Type *begin() { if ((data == NULL) || (count <= 0)) return NULL; return &data[0]; }
Value_Type *end() { if ((data == NULL) || (count <= 0)) return NULL; return &data[count]; }
Value_Type &operator[](i64 index)
{
assert(index < count);
assert(index >= 0);
assert(data != NULL);
return data[index];
}
Static_Array<Value_Type> operator=(std::initializer_list<Value_Type> rhs)
{
i64 it = 0;
for (auto val : rhs)
{
if (it == count) break;
data[it] = val;
it += 1;
}
return *this;
}
operator bool() { return count > 0; }
};
template <typename V>
using SArr = Static_Array<V>;
template <typename V>
SArr<V> NewArray(i64 count, i64 alignment, Allocator allocator)
{
SArr<V> result;
result.allocator = allocator;
if (!result.allocator) result.allocator = global_context.allocator;
if (!result.allocator) result.allocator = {NULL, __default_allocator};
// @Note: We are disabling the array alignment partly because this can lead to unexpected assertions.
// auto alignment_extra_bytes = count % alignment; // This does not work if alignment is 1
// result.count = count + alignment - alignment_extra_bytes;
result.count = count;
result.data = reinterpret_cast<V*>(my_alloc(sizeof(V) * result.count, result.allocator));
result.clear();
return result;
}
template <typename V>
void array_free(SArr<V> *array)
{
if (!array->data || (array->count <= 0)) return;
array->allocator.proc(Allocator_Mode::FREE, 0, 0, array->data, array->allocator.data);
array->data = NULL;
array->count = 0;
}
template <typename V>
inline
void my_free(SArr<V> *array)
{
array_free(array);
}