-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_array.h
593 lines (476 loc) · 19.6 KB
/
rjd_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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#pragma once
#define RJD_ARRAY_H 1
// static array count
// Note that GCC is awesome and has a warning if buf is a pointer. See -Wsizeof-pointer-div
#define rjd_countof(buf) (sizeof(buf) / sizeof(*(buf)))
enum
{
RJD_ARRAY_DEFAULT_CAPACITY = 0,
RJD_ARRAY_NOT_FOUND = -1,
};
// dyanmic array
#define rjd_array_alloc(type, capacity, allocator) ((type*)(rjd_array_alloc_impl((capacity), (allocator), sizeof(type))))
#define rjd_array_clone(buf, allocator) rjd_array_clone_impl((buf), allocator, sizeof(*(buf)))
#define rjd_array_free(buf) rjd_array_free_impl(buf)
#define rjd_array_capacity(buf) ((buf)?(const uint32_t)(*rjd_array_capacity_impl(buf)):0)
#define rjd_array_count(buf) ((buf)?(const uint32_t)(*rjd_array_count_impl(buf)):0)
#define rjd_array_clear(buf) (*rjd_array_count_impl(buf) = 0)
#define rjd_array_reserve(buf, capacity) (buf = rjd_array_reserve_impl((buf), capacity, sizeof(*(buf))))
#define rjd_array_resize(buf, size) (buf = rjd_array_resize_impl((buf), size, sizeof(*(buf))))
#define rjd_array_trim(buf) (buf = rjd_array_trim_impl((buf), sizeof(*(buf))))
#define rjd_array_erase(buf, index) rjd_array_erase_impl((buf), index, sizeof(*(buf)))
#define rjd_array_erase_unordered(buf, index) rjd_array_erase_unordered_impl((buf), index, sizeof(*(buf)))
#define rjd_array_empty(buf) (rjd_array_count(buf) == 0)
#define rjd_array_full(buf) (rjd_array_count(buf) == rjd_array_capacity(buf))
#define rjd_array_push(buf, value) (buf = rjd_array_push_impl((buf), \
sizeof(*(buf))), (buf)[rjd_array_count(buf) - 1] = value)
#define rjd_array_pop(buf) (rjd_array_pop_impl(buf), \
--*rjd_array_count_impl(buf), \
*(buf + rjd_array_count(buf)))
#define rjd_array_insert(buf, ptr, index) (buf = rjd_array_insert_impl((buf), (ptr), sizeof(*(buf)), index))
#define rjd_array_get(buf, index) (rjd_array_get_validate((buf), (index)), (buf + index))
#define rjd_array_first(buf) (rjd_array_get_validate((buf), 0), (buf)[0])
#define rjd_array_last(buf) (rjd_array_get_validate((buf), 0), (buf)[rjd_array_count(buf) - 1])
// searching/sorting
typedef int32_t RJD_COMPILER_MSVC_ONLY(__cdecl) rjd_array_compare_func(const void* left, const void* right);
typedef int32_t RJD_COMPILER_MSVC_ONLY(__cdecl) rjd_array_compare_c_func(void* context, const void* left, const void* right);
#define rjd_array_find(buf, ptr) rjd_array_find_impl((buf), (ptr), sizeof(*(buf)), RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
#define rjd_array_contains(buf, ptr) rjd_array_contains_impl((buf), (ptr), sizeof(*(buf)), RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
// These functions sort or deal with sorted data
#define rjd_array_sort(buf, comparer) \
rjd_array_sort_impl((buf), sizeof(*(buf)), comparer)
#define rjd_array_lowerbound(buf, ptr, comparer) \
rjd_array_lowerbound_impl((buf), ptr, sizeof(*(buf)), comparer, RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
#define rjd_array_find_sorted(buf, ptr, comparer) \
rjd_array_find_sorted_impl((buf), (ptr), sizeof(*(buf)), comparer, RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
#define rjd_array_contains_sorted(buf, ptr, comparer) \
rjd_array_contains_sorted_impl((buf), (ptr), sizeof(*(buf)), comparer, RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
// These versions of the sort/find sorted functions allow you to provide a content parameter that
// is passed into the compare func.
#define rjd_array_sort_c(buf, comparer, context) \
rjd_array_sort_c_impl((buf), sizeof(*(buf)), comparer, context)
#define rjd_array_lowerbound_c(buf, ptr, comparer, context) \
rjd_array_lowerbound_c_impl((buf), (ptr), sizeof(*(buf)), comparer, context, RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
#define rjd_array_find_sorted_c(buf, ptr, comparer, context) \
rjd_array_find_sorted_c_impl((buf), (ptr), sizeof(*(buf)), comparer, context, RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
#define rjd_array_contains_sorted_c(buf, ptr, comparer, context) \
rjd_array_contains_sorted_c_impl((buf), (ptr), sizeof(*(buf)), comparer, context, RJD_MUST_BE_SAME_TYPE_TEST((buf), (ptr)))
// predicate helpers for the functional-style interface
#define rjd_array_sum_predicate(acc, element) (acc + element)
// functional-style helpers
#define rjd_array_filter(buf, predicate, context) for(int _i = (int)rjd_array_count(buf) - 1; _i >= 0; --_i) { \
if (!(predicate((buf)[_i]))) { rjd_array_erase((buf), _i); } \
}
#define rjd_array_map(in, out, predicate) rjd_array_resize((out), rjd_array_count(in)); \
for (size_t _i = 0; _i < rjd_array_count(in); ++_i) { \
out[_i] = predicate(in[_i]); \
}
#define rjd_array_reduce(buf, acc, predicate) for(size_t _i = 0; _i < rjd_array_count(buf); ++_i) { \
(acc) = predicate(acc, ((buf)[_i])); \
}
#define rjd_array_sum(buf, acc) for(size_t _i = 0; _i < rjd_array_count(buf); ++_i) { \
(acc) = rjd_array_sum_predicate((acc), ((buf)[_i])); \
}
#define rjd_array_reverse(buf) rjd_array_reverse_impl(buf, sizeof(*buf))
// randomness helpers
#define rjd_array_sample(buf, rng) ((buf)[rjd_rng_range32(rng, 0, rjd_array_count(buf))])
#define rjd_array_shuffle(buf, rng) rjd_array_shuffle_impl(buf, rng, sizeof(*buf))
struct rjd_mem_allocator;
struct rjd_rng;
void* rjd_array_alloc_impl(uint32_t capacity, struct rjd_mem_allocator* allocator, size_t sizeof_type);
void* rjd_array_clone_impl(const void* array, struct rjd_mem_allocator* allocator, size_t sizeof_type);
void rjd_array_free_impl(const void* array);
uint32_t* rjd_array_capacity_impl(const void* array);
uint32_t* rjd_array_count_impl(const void* array);
void* rjd_array_reserve_impl(void* array, uint32_t newcapacity, size_t sizeof_type);
void* rjd_array_resize_impl(void* array, uint32_t newcount, size_t sizeof_type);
void* rjd_array_trim_impl(void* array, size_t sizeof_type);
void rjd_array_erase_impl(void* array, uint32_t index, size_t sizeof_type);
void rjd_array_erase_unordered_impl(void* array, uint32_t index, size_t sizeof_type);
void* rjd_array_push_impl(void* array, size_t sizeof_type);
void rjd_array_pop_impl(void* array);
void* rjd_array_insert_impl(void* array, const void* insert, size_t sizeof_type, uint32_t index);
void rjd_array_get_validate(void* array, uint32_t index);
int32_t rjd_array_find_impl(const void* array, const void* search, size_t sizeof_type, int unused);
bool rjd_array_contains_impl(const void* array, const void* search, size_t sizeof_type, int unused);
void rjd_array_sort_impl(void* array, size_t sizeof_type, rjd_array_compare_func* comparer);
int32_t rjd_array_lowerbound_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_func* comparer, int unused);
int32_t rjd_array_find_sorted_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_func* comparer, int unused);
bool rjd_array_contains_sorted_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_func* comparer, int unused);
void rjd_array_sort_c_impl(void* array, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context);
int32_t rjd_array_lowerbound_c_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context, int unused);
int32_t rjd_array_find_sorted_c_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context, int unused);
bool rjd_array_contains_sorted_c_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context, int unused);
void rjd_array_shuffle_impl(void* array, struct rjd_rng* rng, size_t sizeof_type);
void rjd_array_reverse_impl(void* array, size_t sizeof_type);
#if RJD_IMPL
// Copied from stdlib.h. We want this extra platform functionality but don't want to turn on all the defines
// to get us there.
#if RJD_COMPILER_GCC
void __bsd_qsort_r (void *__base, size_t __nmemb, size_t __size, void *__thunk, int (*_compar)(void *, const void *, const void *));
# define qsort_r __bsd_qsort_r
#endif
struct rjd_array_header
{
struct rjd_mem_allocator* allocator;
uint32_t capacity;
uint32_t count;
uint32_t debug_sentinel;
};
const uint32_t RJD_ARRAY_DEBUG_SENTINEL32 = 0xA7A7A7A7;
static struct rjd_array_header* rjd_array_getheader(void* array);
static struct rjd_mem_allocator* rjd_array_allocator(void* array);
static inline void rjd_array_validate(const void* array);
static void* rjd_array_grow(void* array, size_t sizeof_type);
void* rjd_array_alloc_impl(uint32_t capacity, struct rjd_mem_allocator* allocator, size_t sizeof_type)
{
RJD_ASSERT(allocator);
RJD_ASSERT(sizeof_type > 0);
if (capacity == 0) {
capacity = 128 / (uint32_t)sizeof_type;
if (capacity < 4) {
capacity = 4;
}
}
size_t rawsize = sizeof(struct rjd_array_header) + (sizeof_type * capacity);
char* raw = rjd_mem_alloc_array(char, rawsize, allocator);
struct rjd_array_header* header = (struct rjd_array_header*)raw;
header->allocator = allocator;
header->capacity = capacity;
header->count = 0;
header->debug_sentinel = RJD_ARRAY_DEBUG_SENTINEL32;
char* buf = raw + sizeof(struct rjd_array_header);
return buf;
}
void* rjd_array_clone_impl(const void* array, struct rjd_mem_allocator* allocator, size_t sizeof_type)
{
uint32_t count = rjd_array_count(array);
void* clone = rjd_array_alloc_impl(count, allocator, sizeof_type);
clone = rjd_array_resize_impl(clone, count, sizeof_type);
memcpy(clone, array, count * sizeof_type);
return clone;
}
void rjd_array_free_impl(const void* array)
{
if (!array) {
return;
}
rjd_array_validate(array);
char* raw = (char*)array;
rjd_mem_free(raw - sizeof(struct rjd_array_header));
}
uint32_t* rjd_array_capacity_impl(const void* array)
{
RJD_ASSERT(array);
rjd_array_validate(array);
struct rjd_array_header* header = rjd_array_getheader((void*)array);
return &header->capacity;
}
uint32_t* rjd_array_count_impl(const void* array)
{
RJD_ASSERT(array);
rjd_array_validate(array);
struct rjd_array_header* header = rjd_array_getheader((void*)array);
return &header->count;
}
void* rjd_array_reserve_impl(void* array, uint32_t newcapacity, size_t sizeof_type)
{
RJD_ASSERT(array);
RJD_ASSERT(sizeof_type > 0);
rjd_array_validate(array);
uint32_t oldcapacity = rjd_array_capacity(array);
if (oldcapacity < newcapacity) {
struct rjd_mem_allocator* allocator = rjd_array_allocator(array);
char* newarray = rjd_array_alloc_impl(newcapacity, allocator, sizeof_type);
uint32_t* count = rjd_array_count_impl(newarray);
*count = rjd_array_count(array);
memcpy(newarray, array, (*count * sizeof_type));
rjd_array_free(array);
return newarray;
}
return array;
}
void* rjd_array_resize_impl(void* array, uint32_t newcount, size_t sizeof_type)
{
RJD_ASSERT(array);
RJD_ASSERT(sizeof_type > 0);
rjd_array_validate(array);
array = rjd_array_reserve_impl(array, newcount, sizeof_type);
uint32_t* count = rjd_array_count_impl(array);
// zero new members
if (newcount > *count) {
memset((char*)array + (*count * sizeof_type), 0, (newcount - *count) * sizeof_type);
}
*count = newcount;
return array;
}
void* rjd_array_trim_impl(void* array, size_t sizeof_type)
{
if (rjd_array_count(array) == rjd_array_capacity(array)) {
return array;
}
void* newarray = rjd_array_alloc_impl(rjd_array_count(array), rjd_array_allocator(array), sizeof_type);
memcpy(newarray, array, rjd_array_count(array) * sizeof_type);
*rjd_array_count_impl(newarray) = rjd_array_count(array);
rjd_array_free(array);
return newarray;
}
void rjd_array_erase_impl(void* array, uint32_t index, size_t sizeof_type)
{
RJD_ASSERT(array);
RJD_ASSERT(index < rjd_array_count(array));
RJD_ASSERT(sizeof_type > 0);
rjd_array_validate(array);
char* raw = array;
size_t toshift = rjd_array_count(array) - index - 1;
if (toshift > 0) {
memmove(raw + index * sizeof_type, raw + (index + 1) * sizeof_type, toshift * sizeof_type);
}
--*rjd_array_count_impl(array);
}
void rjd_array_erase_unordered_impl(void* array, uint32_t index, size_t sizeof_type)
{
RJD_ASSERT(array);
RJD_ASSERT(index < rjd_array_count(array));
RJD_ASSERT(sizeof_type > 0);
rjd_array_validate(array);
char* raw = array;
uint32_t* count = rjd_array_count_impl(array);
if (*count > 1) {
char* erase = raw + index * sizeof_type;
char* swap = raw + (*count - 1) * sizeof_type;
memcpy(erase, swap, sizeof_type);
}
if (*count > 0) {
--*rjd_array_count_impl(array);
}
}
void* rjd_array_push_impl(void* array, size_t sizeof_type)
{
array = rjd_array_grow(array, sizeof_type);
++*rjd_array_count_impl(array);
// skip init new element to 0 since it will be set in the push macro
return array;
}
void rjd_array_pop_impl(void* array)
{
RJD_ASSERT(rjd_array_count(array) > 0);
}
void* rjd_array_insert_impl(void* array, const void* insert, size_t sizeof_type, uint32_t index)
{
array = rjd_array_grow(array, sizeof_type);
uint32_t* count = rjd_array_count_impl(array);
++*count;
RJD_ASSERT(*count > index);
void* where_to_insert = (char*)array + sizeof_type * index;
void* element_after_insert = (char*)where_to_insert + sizeof_type;
memmove(element_after_insert, where_to_insert, sizeof_type * (*count - index));
memcpy(where_to_insert, insert, sizeof_type);
return array;
}
void rjd_array_get_validate(void* array, uint32_t index)
{
rjd_array_validate(array);
RJD_ASSERT(index < rjd_array_count(array));
}
int32_t rjd_array_find_impl(const void* array, const void* search, size_t sizeof_type, int unused)
{
RJD_UNUSED_PARAM(unused);
rjd_array_validate(array);
if (!array) {
return false;
}
char* raw = (char*)array;
for (int32_t i = 0; i < (int32_t)rjd_array_count(array); ++i) {
if (!memcmp(raw + i * sizeof_type, search, sizeof_type)) {
return i;
}
}
return RJD_ARRAY_NOT_FOUND;
}
bool rjd_array_contains_impl(const void* array, const void* search, size_t sizeof_type, int unused)
{
RJD_UNUSED_PARAM(unused);
const int32_t index = rjd_array_find_impl(array, search, sizeof_type, 0);
return index != RJD_ARRAY_NOT_FOUND;
}
void rjd_array_sort_impl(void* array, size_t sizeof_type, rjd_array_compare_func* comparer)
{
rjd_array_validate(array);
size_t length = rjd_array_count(array);
qsort(array, length, sizeof_type, comparer);
}
int32_t rjd_array_lowerbound_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_func* comparer, int unused)
{
RJD_UNUSED_PARAM(unused);
rjd_array_validate(array);
uint32_t length = rjd_array_count(array);
if (length == 0) {
return 0;
}
int32_t first = 0;
int32_t index = 0;
while (length > 0)
{
int32_t step = length / 2;
index = first + step;
const void* value = (const char*)array + index * sizeof_type;
const int32_t compared_value = comparer(value, needle);
if (compared_value < 0) {
++index;
first = index;
length -= step + 1;
} else if (compared_value > 0) {
length = step;
} else {
break;
}
}
return index;
}
int32_t rjd_array_find_sorted_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_func* comparer, int unused)
{
RJD_UNUSED_PARAM(unused);
const int32_t index = rjd_array_lowerbound_impl(array, needle, sizeof_type, comparer, 0);
const void* value = (const char*)array + (index * sizeof_type);
if (comparer(array, value) == 0) {
return index;
}
return RJD_ARRAY_NOT_FOUND;
}
bool rjd_array_contains_sorted_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_func* comparer, int unused)
{
RJD_UNUSED_PARAM(unused);
const int32_t index = rjd_array_find_sorted_impl(array, needle, sizeof_type, comparer, 0);
return index != RJD_ARRAY_NOT_FOUND;
}
void rjd_array_sort_c_impl(void* array, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context)
{
rjd_array_validate(array);
const size_t length = rjd_array_count(array);
#if RJD_COMPILER_MSVC
qsort_s(array, length, sizeof_type, comparer, context);
#else
qsort_r(array, length, sizeof_type, context, comparer);
#endif
}
int32_t rjd_array_lowerbound_c_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context, int unused)
{
RJD_UNUSED_PARAM(unused);
rjd_array_validate(array);
uint32_t length = rjd_array_count(array);
if (length == 0) {
return 0;
}
int32_t first = 0;
int32_t index = 0;
while (length > 0)
{
int32_t step = length / 2;
index = first + step;
const void* value = (const char*)array + index * sizeof_type;
const int32_t compared_value = comparer(context, value, needle);
if (compared_value < 0) {
++index;
first = index;
length -= step + 1;
} else if (compared_value > 0) {
length = step;
} else {
break;
}
}
return index;
}
int32_t rjd_array_find_sorted_c_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context, int unused)
{
RJD_UNUSED_PARAM(unused);
const int32_t index = rjd_array_lowerbound_c_impl(array, needle, sizeof_type, comparer, context, 0);
const void* value = (const char*)array + (index * sizeof_type);
if (comparer(context, array, value) == 0) {
return index;
}
return RJD_ARRAY_NOT_FOUND;
}
bool rjd_array_contains_sorted_c_impl(const void* array, const void* needle, size_t sizeof_type, rjd_array_compare_c_func* comparer, void* context, int unused)
{
RJD_UNUSED_PARAM(unused);
const int32_t index = rjd_array_find_sorted_c_impl(array, needle, sizeof_type, comparer, context, 0);
return index != RJD_ARRAY_NOT_FOUND;
}
void rjd_array_shuffle_impl(void* array, struct rjd_rng* rng, size_t sizeof_type)
{
if (!array) {
return;
}
rjd_array_validate(array);
char tmp[512];
RJD_ASSERTMSG(sizeof_type <= sizeof(tmp),
"tmp (%u bytes) must be greater than or equal to sizeof_type (%u bytes)",
(unsigned) sizeof(tmp), (unsigned) sizeof_type);
char* raw = (char*)array;
for (uint32_t i = 0; i < rjd_array_count(array); ++i) {
uint32_t k = rjd_rng_range32(rng, 0, rjd_array_count(array));
if (i == k) {
continue;
}
char* a = raw + (i * sizeof_type);
char* b = raw + (k * sizeof_type);
memcpy(tmp, a, sizeof_type);
memcpy(a, b, sizeof_type);
memcpy(b, tmp, sizeof_type);
}
}
void rjd_array_reverse_impl(void* array, size_t sizeof_type)
{
if (!array) {
return;
}
rjd_array_validate(array);
uint8_t* raw = array;
for (uint8_t* begin = raw, *end = raw + (int32_t)sizeof_type * ((int32_t)rjd_array_count(array) - 1);
begin < end;
begin += sizeof_type, end -= sizeof_type)
{
rjd_mem_swap(begin, end, sizeof_type);
}
}
////////////////////////////////////////////////////////////////////////////////
// local helpers
static struct rjd_array_header* rjd_array_getheader(void* array)
{
if (!array) {
return NULL;
}
rjd_array_validate(array);
char* raw = array;
char* raw_header = raw - sizeof(struct rjd_array_header);
return (struct rjd_array_header*) raw_header;
}
static struct rjd_mem_allocator* rjd_array_allocator(void* array)
{
RJD_ASSERT(array);
return rjd_array_getheader(array)->allocator;
}
static inline void rjd_array_validate(const void* array)
{
RJD_ASSERT(array);
const char* raw = array;
const struct rjd_array_header* header = (struct rjd_array_header*)(raw - sizeof(struct rjd_array_header));
RJD_ASSERTMSG(header->debug_sentinel == RJD_ARRAY_DEBUG_SENTINEL32,
"Debug sentinel was either corrupted by an underrun or this is not an rjd_array.");
RJD_UNUSED_PARAM(header);
}
static void* rjd_array_grow(void* array, size_t sizeof_type)
{
RJD_ASSERT(array);
RJD_ASSERT(sizeof_type > 0);
rjd_array_validate(array);
uint32_t count = rjd_array_count(array);
uint32_t capacity = rjd_array_capacity(array);
if (count == capacity) {
array = rjd_array_reserve_impl(array, capacity * 2, sizeof_type);
}
return array;
}
#endif //RJD_IMPL