Add code generation for array

This commit is contained in:
2025-05-04 23:26:03 +01:00
parent f444911452
commit 12f083edb0
18 changed files with 4466 additions and 1 deletions

View 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;

View 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;

View File

@@ -0,0 +1,5 @@
if (!array || array->count >= array->capacity) {{
return;
}}
array->items[(array->count)++] = item;

View File

@@ -0,0 +1,5 @@
if (!array || index >= array->count) {{
return NULL;
}}
return &(array->items[index]);

View File

@@ -0,0 +1,5 @@
if (!array || array->count == 0) {{
return ({T}){{0}};
}}
return array->items[--(array->count)];

View File

@@ -0,0 +1,5 @@
if (!array || index >= array->count) {{
return;
}}
array->items[index] = item;

View File

@@ -0,0 +1,5 @@
if (!array) {{
return;
}}
array->count = 0;

View 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;

View 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);
}}

View 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;

View 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);
}}

View 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) \
}})

View File

@@ -0,0 +1 @@
(({ArrayType}){{.items = ({T}[CAPACITY]){{0}}, .count = 0, .capacity = CAPACITY}})