Add code generation for array
This commit is contained in:
18
codegen/array/snippets/alloc_capacity
Normal file
18
codegen/array/snippets/alloc_capacity
Normal file
@@ -0,0 +1,18 @@
|
||||
u64 allocation_size = sizeof({ArrayType}) + sizeof({T}) * capacity;
|
||||
{ArrayType} *array = NULL;
|
||||
|
||||
if (!allocator) {{
|
||||
goto RETURN_{Tupper}_ARRAY_ALLOC;
|
||||
}}
|
||||
|
||||
array = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||
if (!array) {{
|
||||
goto RETURN_{Tupper}_ARRAY_ALLOC;
|
||||
}}
|
||||
|
||||
array->items = ({T} *)((u8 *)array + sizeof({ArrayType}));
|
||||
array->count = 0;
|
||||
array->capacity = capacity;
|
||||
|
||||
RETURN_{Tupper}_ARRAY_ALLOC:
|
||||
return array;
|
20
codegen/array/snippets/append_alloc
Normal file
20
codegen/array/snippets/append_alloc
Normal file
@@ -0,0 +1,20 @@
|
||||
{ArrayType} *output = array;
|
||||
|
||||
if (!allocator || !array) {{
|
||||
goto RETURN_{Tupper}_ARRAY_APPEND_ALLOC;
|
||||
}}
|
||||
|
||||
if (array->count >= array->capacity) {{
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2);
|
||||
output = wapp_{Tlower}_array_alloc_capacity(allocator, new_capacity);
|
||||
if (!output) {{
|
||||
output = array;
|
||||
goto RETURN_{Tupper}_ARRAY_APPEND_ALLOC;
|
||||
}}
|
||||
wapp_{Tlower}_array_copy_capped(array, output);
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_append_capped(output, item);
|
||||
|
||||
RETURN_{Tupper}_ARRAY_APPEND_ALLOC:
|
||||
return output;
|
5
codegen/array/snippets/append_capped
Normal file
5
codegen/array/snippets/append_capped
Normal file
@@ -0,0 +1,5 @@
|
||||
if (!array || array->count >= array->capacity) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
array->items[(array->count)++] = item;
|
5
codegen/array/snippets/array_get
Normal file
5
codegen/array/snippets/array_get
Normal file
@@ -0,0 +1,5 @@
|
||||
if (!array || index >= array->count) {{
|
||||
return NULL;
|
||||
}}
|
||||
|
||||
return &(array->items[index]);
|
5
codegen/array/snippets/array_pop
Normal file
5
codegen/array/snippets/array_pop
Normal file
@@ -0,0 +1,5 @@
|
||||
if (!array || array->count == 0) {{
|
||||
return ({T}){{0}};
|
||||
}}
|
||||
|
||||
return array->items[--(array->count)];
|
5
codegen/array/snippets/array_set
Normal file
5
codegen/array/snippets/array_set
Normal file
@@ -0,0 +1,5 @@
|
||||
if (!array || index >= array->count) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
array->items[index] = item;
|
5
codegen/array/snippets/clear
Normal file
5
codegen/array/snippets/clear
Normal file
@@ -0,0 +1,5 @@
|
||||
if (!array) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
array->count = 0;
|
20
codegen/array/snippets/copy_alloc
Normal file
20
codegen/array/snippets/copy_alloc
Normal file
@@ -0,0 +1,20 @@
|
||||
{ArrayType} *output = dst;
|
||||
|
||||
if (!allocator || !src || !dst) {{
|
||||
goto RETURN_{Tupper}_ARRAY_COPY_ALLOC;
|
||||
}}
|
||||
|
||||
if (src->count >= dst->capacity) {{
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2);
|
||||
output = wapp_{Tlower}_array_alloc_capacity(allocator, new_capacity);
|
||||
if (!output) {{
|
||||
output = dst;
|
||||
goto RETURN_{Tupper}_ARRAY_COPY_ALLOC;
|
||||
}}
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_clear(output);
|
||||
wapp_{Tlower}_array_copy_capped(src, output);
|
||||
|
||||
RETURN_{Tupper}_ARRAY_COPY_ALLOC:
|
||||
return output;
|
16
codegen/array/snippets/copy_capped
Normal file
16
codegen/array/snippets/copy_capped
Normal file
@@ -0,0 +1,16 @@
|
||||
if (!src || !dst) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_clear(dst);
|
||||
|
||||
{T} *item;
|
||||
u64 to_copy = src->count < dst->capacity ? src->count : dst->capacity;
|
||||
for (u64 i = 0; i < to_copy; ++i) {{
|
||||
item = wapp_{Tlower}_array_get(src, i);
|
||||
if (!item) {{
|
||||
continue;
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_append_capped(dst, *item);
|
||||
}}
|
21
codegen/array/snippets/extend_alloc
Normal file
21
codegen/array/snippets/extend_alloc
Normal file
@@ -0,0 +1,21 @@
|
||||
{ArrayType} *output = array;
|
||||
|
||||
if (!allocator || !array || !other) {{
|
||||
goto RETURN_{Tupper}_ARRAY_EXTEND_ALLOC;
|
||||
}}
|
||||
|
||||
u64 remaining_capacity = array->capacity - array->count;
|
||||
if (other->count >= remaining_capacity) {{
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(array->capacity * 2);
|
||||
output = wapp_{Tlower}_array_alloc_capacity(allocator, new_capacity);
|
||||
if (!output) {{
|
||||
output = array;
|
||||
goto RETURN_{Tupper}_ARRAY_EXTEND_ALLOC;
|
||||
}}
|
||||
wapp_{Tlower}_array_copy_capped(array, output);
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_extend_capped(output, other);
|
||||
|
||||
RETURN_{Tupper}_ARRAY_EXTEND_ALLOC:
|
||||
return output;
|
19
codegen/array/snippets/extend_capped
Normal file
19
codegen/array/snippets/extend_capped
Normal file
@@ -0,0 +1,19 @@
|
||||
if (!array || !other) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
u64 remaining_capacity = array->capacity - array->count;
|
||||
if (other->count >= remaining_capacity) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{T} *item;
|
||||
u64 items_to_add = other->count;
|
||||
for (u64 i = 0; i < items_to_add; ++i) {{
|
||||
item = wapp_{Tlower}_array_get(other, i);
|
||||
if (!item) {{
|
||||
continue;
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_append_capped(array, *item);
|
||||
}}
|
5
codegen/array/snippets/stack_array
Normal file
5
codegen/array/snippets/stack_array
Normal file
@@ -0,0 +1,5 @@
|
||||
(({ArrayType}){{ \
|
||||
.items = ({T}[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2)]){{__VA_ARGS__}}, \
|
||||
.count = wapp_misc_utils_va_args_count({T}, __VA_ARGS__), \
|
||||
.capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2) \
|
||||
}})
|
1
codegen/array/snippets/stack_capacity_array
Normal file
1
codegen/array/snippets/stack_capacity_array
Normal file
@@ -0,0 +1 @@
|
||||
(({ArrayType}){{.items = ({T}[CAPACITY]){{0}}, .count = 0, .capacity = CAPACITY}})
|
Reference in New Issue
Block a user