-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainers.array.hpp
434 lines (358 loc) · 14.5 KB
/
containers.array.hpp
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
#pragma once
#include "gen.hpp"
using namespace gen;
// Used to know what slot the array will be for generic selection
global s32 Array_DefinitionCounter = 0;
CodeBody gen_array_base()
{
CodeTypedef td_header = parse_typedef( code( typedef struct ArrayHeader ArrayHeader; ));
CodeStruct header = parse_struct( code(
struct ArrayHeader
{
AllocatorInfo Allocator;
usize Capacity;
usize Num;
};
));
Code grow_formula = untyped_str( txt( "#define array_grow_formula( value ) ( 2 * value + 8 )\n" ));
Code get_header = untyped_str( txt( "#define array_get_header( self ) ( (ArrayHeader*)( self ) - 1)\n" ));
Code type_define = untyped_str( txt( "#define Array(Type) gen_Array_##Type\n"));
Code array_begin = def_define(txt("array_begin(array)"), MT_Expression, { {}, code( (array) ) } );
Code array_end = def_define(txt("array_end(array)"), MT_Expression, { {}, code( (array + array_get_header(array)->Num ) ) } );
Code array_next = def_define(txt("array_next(array, entry)"), MT_Expression, { {}, code( (entry + 1) ) } );
return def_global_body( args(
fmt_newline,
td_header,
header,
type_define,
grow_formula,
get_header,
array_begin,
array_end,
array_next,
fmt_newline
));
};
CodeBody gen_array( Str type, Str array_name )
{
StrBuilder array_type = StrBuilder::fmt_buf( _ctx->Allocator_Temp, "%.*s", array_name.Len, array_name.Ptr );
StrBuilder fn = StrBuilder::fmt_buf( _ctx->Allocator_Temp, "%.*s", array_name.Len, array_name.Ptr );
// c_str_to_lower(fn.Data);
#pragma push_macro( "GEN_ASSERT" )
#pragma push_macro( "rcast" )
#pragma push_macro( "cast" )
#pragma push_macro( "typeof" )
#pragma push_macro( "forceinline" )
#undef GEN_ASSERT
#undef rcast
#undef cast
#undef typeof
#undef forceinline
CodeBody result = parse_global_body( token_fmt( "array_type", (Str)array_type, "fn", (Str)fn, "type", (Str)type
, stringize(
typedef <type>* <array_type>;
<array_type> <fn>_init ( AllocatorInfo allocator );
<array_type> <fn>_init_reserve ( AllocatorInfo allocator, usize capacity );
bool <fn>_append_array ( <array_type>* self, <array_type> other );
bool <fn>_append ( <array_type>* self, <type> value );
bool <fn>_append_items ( <array_type>* self, <type>* items, usize item_num );
bool <fn>_append_at ( <array_type>* self, <type> item, usize idx );
bool <fn>_append_items_at( <array_type>* self, <type>* items, usize item_num, usize idx );
<type>* <fn>_back ( <array_type> self );
void <fn>_clear ( <array_type> self );
bool <fn>_fill ( <array_type> self, usize begin, usize end, <type> value );
void <fn>_free ( <array_type>* self );
bool <fn>_grow ( <array_type>* self, usize min_capacity );
usize <fn>_num ( <array_type> self );
<type> <fn>_pop ( <array_type> self );
void <fn>_remove_at ( <array_type> self, usize idx );
bool <fn>_reserve ( <array_type>* self, usize new_capacity );
bool <fn>_resize ( <array_type>* self, usize num );
bool <fn>_set_capacity ( <array_type>* self, usize new_capacity );
forceinline
<array_type> <fn>_init( AllocatorInfo allocator )
{
size_t initial_size = array_grow_formula(0);
return array_init_reserve( <type>, allocator, initial_size );
}
inline
<array_type> <fn>_init_reserve( AllocatorInfo allocator, usize capacity )
{
GEN_ASSERT(capacity > 0);
ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof(<type>) * capacity));
if (header == nullptr)
return nullptr;
header->Allocator = allocator;
header->Capacity = capacity;
header->Num = 0;
return rcast(<type>*, header + 1);
}
forceinline
bool <fn>_append_array( <array_type>* self, <array_type> other )
{
return array_append_items( * self, (<array_type>)other, <fn>_num(other));
}
inline
bool <fn>_append( <array_type>* self, <type> value )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
ArrayHeader* header = array_get_header( * self );
if ( header->Num == header->Capacity )
{
if ( ! array_grow( self, header->Capacity))
return false;
header = array_get_header( * self );
}
(* self)[ header->Num ] = value;
header->Num++;
return true;
}
inline
bool <fn>_append_items( <array_type>* self, <type>* items, usize item_num )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
GEN_ASSERT(items != nullptr);
GEN_ASSERT(item_num > 0);
ArrayHeader* header = array_get_header( * self );
if ( header->Num + item_num > header->Capacity )
{
if ( ! array_grow( self, header->Capacity + item_num ))
return false;
header = array_get_header( * self );
}
mem_copy( (* self) + header->Num, items, sizeof(<type>) * item_num );
header->Num += item_num;
return true;
}
inline
bool <fn>_append_at( <array_type>* self, <type> item, usize idx )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
ArrayHeader* header = array_get_header( * self );
if ( idx >= header->Num )
idx = header->Num - 1;
if ( idx < 0 )
idx = 0;
if ( header->Capacity < header->Num + 1 )
{
if ( ! array_grow( self, header->Capacity + 1 ) )
return false;
header = array_get_header( * self );
}
<array_type> target = (* self) + idx;
mem_move( target + 1, target, (header->Num - idx) * sizeof(<type>) );
header->Num++;
return true;
}
inline
bool <fn>_append_items_at( <array_type>* self, <type>* items, usize item_num, usize idx )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
ArrayHeader* header = array_get_header( * self );
if ( idx >= header->Num )
{
return array_append_items( * self, items, item_num );
}
if ( item_num > header->Capacity )
{
if ( ! array_grow( self, item_num + header->Capacity ) )
return false;
header = array_get_header( * self );
}
<type>* target = (* self) + idx + item_num;
<type>* src = (* self) + idx;
mem_move( target, src, (header->Num - idx) * sizeof(<type>) );
mem_copy( src, items, item_num * sizeof(<type>) );
header->Num += item_num;
return true;
}
inline
<type>* <fn>_back( <array_type> self )
{
GEN_ASSERT(self != nullptr);
ArrayHeader* header = array_get_header( self );
if ( header->Num == 0 )
return NULL;
return self + header->Num - 1;
}
inline
void <fn>_clear( <array_type> self )
{
GEN_ASSERT(self != nullptr);
ArrayHeader* header = array_get_header( self );
header->Num = 0;
}
inline
bool <fn>_fill( <array_type> self, usize begin, usize end, <type> value )
{
GEN_ASSERT(self != nullptr);
GEN_ASSERT(begin <= end);
ArrayHeader* header = array_get_header( self );
if ( begin < 0 || end > header->Num )
return false;
for ( ssize idx = (ssize)begin; idx < (ssize)end; idx ++ )
self[ idx ] = value;
return true;
}
inline
void <fn>_free( <array_type>* self )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
ArrayHeader* header = array_get_header( * self );
allocator_free( header->Allocator, header );
self = NULL;
}
inline
bool <fn>_grow( <array_type>* self, usize min_capacity )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
GEN_ASSERT( min_capacity > 0 );
ArrayHeader* header = array_get_header( *self );
usize new_capacity = array_grow_formula( header->Capacity );
if ( new_capacity < min_capacity )
new_capacity = min_capacity;
return array_set_capacity( self, new_capacity );
}
forceinline
usize <fn>_num( <array_type> self )
{
GEN_ASSERT( self != nullptr);
return array_get_header(self)->Num;
}
inline
<type> <fn>_pop( <array_type> self )
{
GEN_ASSERT( self != nullptr);
ArrayHeader* header = array_get_header( self );
GEN_ASSERT( header->Num > 0 );
<type> result = self[ header->Num - 1 ];
header->Num--;
return result;
}
forceinline
void <fn>_remove_at( <array_type> self, usize idx )
{
GEN_ASSERT( self != nullptr);
ArrayHeader* header = array_get_header( self );
GEN_ASSERT( idx < header->Num );
mem_move( self + idx, self + idx + 1, sizeof( <type> ) * ( header->Num - idx - 1 ) );
header->Num--;
}
inline
bool <fn>_reserve( <array_type>* self, usize new_capacity )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
GEN_ASSERT(new_capacity > 0);
ArrayHeader* header = array_get_header( * self );
if ( header->Capacity < new_capacity )
return array_set_capacity( self, new_capacity );
return true;
}
inline
bool <fn>_resize( <array_type>* self, usize num )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
GEN_ASSERT(num > 0);
ArrayHeader* header = array_get_header( * self );
if ( header->Capacity < num )
{
if ( ! array_grow( self, num ) )
return false;
header = array_get_header( * self );
}
header->Num = num;
return true;
}
inline
bool <fn>_set_capacity( <array_type>* self, usize new_capacity )
{
GEN_ASSERT( self != nullptr);
GEN_ASSERT(* self != nullptr);
GEN_ASSERT( new_capacity > 0 );
ArrayHeader* header = array_get_header( * self );
if ( new_capacity == header->Capacity )
return true;
if ( new_capacity < header->Num )
{
header->Num = new_capacity;
return true;
}
usize size = sizeof( ArrayHeader ) + sizeof( <type> ) * new_capacity;
ArrayHeader* new_header = cast( ArrayHeader*, alloc( header->Allocator, size ));
if ( new_header == NULL )
return false;
mem_move( new_header, header, sizeof( ArrayHeader ) + sizeof( <type> ) * header->Num );
new_header->Capacity = new_capacity;
allocator_free( header->Allocator, & header );
* self = cast( <type>*, new_header + 1 );
return true;
}
)));
#pragma pop_macro( "GEN_ASSERT" )
#pragma pop_macro( "rcast" )
#pragma pop_macro( "cast" )
#pragma pop_macro( "typeof" )
#pragma pop_macro( "forceinline" )
++ Array_DefinitionCounter;
Str slot_str = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "%d", Array_DefinitionCounter).to_str();
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "array_type", (Str)array_type, "slot", (Str)slot_str,
R"(#define GENERIC_SLOT_<slot>__array_init <type>, <array_type>_init
#define GENERIC_SLOT_<slot>__array_init_reserve <type>, <array_type>_init_reserve
#define GENERIC_SLOT_<slot>__array_append <array_type>, <array_type>_append
#define GENERIC_SLOT_<slot>__array_append_items <array_type>, <array_type>_append_items
#define GENERIC_SLOT_<slot>__array_append_at <array_type>, <array_type>_append_at
#define GENERIC_SLOT_<slot>__array_append_items_at <array_type>, <array_type>_append_items_at
#define GENERIC_SLOT_<slot>__array_back <array_type>, <array_type>_back
#define GENERIC_SLOT_<slot>__array_clear <array_type>, <array_type>_clear
#define GENERIC_SLOT_<slot>__array_fill <array_type>, <array_type>_fill
#define GENERIC_SLOT_<slot>__array_free <array_type>, <array_type>_free
#define GENERIC_SLOT_<slot>__array_grow <array_type>*, <array_type>_grow
#define GENERIC_SLOT_<slot>__array_num <array_type>, <array_type>_num
#define GENERIC_SLOT_<slot>__array_pop <array_type>, <array_type>_pop
#define GENERIC_SLOT_<slot>__array_remove_at <array_type>, <array_type>_remove_at
#define GENERIC_SLOT_<slot>__array_reserve <array_type>, <array_type>_reserve
#define GENERIC_SLOT_<slot>__array_resize <array_type>, <array_type>_resize
#define GENERIC_SLOT_<slot>__array_set_capacity <array_type>*, <array_type>_set_capacity
)"
));
return def_global_body( args(
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( _ctx->Allocator_Temp, "region %SB", array_type ))),
fmt_newline,
generic_interface_slot,
fmt_newline,
result,
fmt_newline,
def_pragma( strbuilder_to_str(strbuilder_fmt_buf( _ctx->Allocator_Temp, "endregion %SB", array_type ))),
fmt_newline
));
};
CodeBody gen_array_generic_selection_interface()
{
CodeBody interface_defines = def_body(CT_Global_Body);
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_init"), GenericSel_Direct_Type ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_init_reserve"), GenericSel_Direct_Type ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append"), GenericSel_By_Ref ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append_at"), GenericSel_By_Ref ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append_items"), GenericSel_By_Ref ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append_items_at"), GenericSel_By_Ref ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_back"), GenericSel_Default, GenericSel_One_Arg ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_clear"), GenericSel_Default, GenericSel_One_Arg ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_fill")) );
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_free"), GenericSel_By_Ref, GenericSel_One_Arg ) );
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_grow")) );
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_num"), GenericSel_Default, GenericSel_One_Arg ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_pop"), GenericSel_Default, GenericSel_One_Arg ));
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_remove_at")) );
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_reserve"), GenericSel_By_Ref) );
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_resize"), GenericSel_By_Ref) );
interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_set_capacity")) );
return interface_defines;
}