Refactor array to avoid having to include external types
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
u64 allocation_size = sizeof({ArrayType}) + sizeof({T}) * capacity;
|
||||
{ArrayType} *array = NULL;
|
||||
u64 allocation_size = sizeof({ArrayType}) + item_size * capacity;
|
||||
{ArrayType} *array = NULL;
|
||||
|
||||
if (!allocator) {{
|
||||
goto RETURN_{Tupper}_ARRAY_ALLOC;
|
||||
goto RETURN_GENERIC_ARRAY_ALLOC;
|
||||
}}
|
||||
|
||||
array = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||
if (!array) {{
|
||||
goto RETURN_{Tupper}_ARRAY_ALLOC;
|
||||
goto RETURN_GENERIC_ARRAY_ALLOC;
|
||||
}}
|
||||
|
||||
array->items = ({T} *)((u8 *)array + sizeof({ArrayType}));
|
||||
array->count = 0;
|
||||
array->capacity = capacity;
|
||||
array->items = ({T} *)((u8 *)array + sizeof({ArrayType}));
|
||||
array->count = 0;
|
||||
array->capacity = capacity;
|
||||
array->item_size = item_size;
|
||||
|
||||
RETURN_{Tupper}_ARRAY_ALLOC:
|
||||
RETURN_GENERIC_ARRAY_ALLOC:
|
||||
return array;
|
||||
|
1
codegen/array/snippets/alloc_capacity_macro
Normal file
1
codegen/array/snippets/alloc_capacity_macro
Normal file
@@ -0,0 +1 @@
|
||||
(({ArrayType} *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof({T})))
|
@@ -6,7 +6,7 @@
|
||||
|
||||
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);
|
||||
output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, array->item_size);
|
||||
if (!output) {{
|
||||
output = array;
|
||||
goto RETURN_{Tupper}_ARRAY_APPEND_ALLOC;
|
||||
|
@@ -2,4 +2,5 @@
|
||||
return;
|
||||
}}
|
||||
|
||||
array->items[(array->count)++] = item;
|
||||
u64 index = (array->count)++;
|
||||
wapp_{Tlower}_array_set(array, index, item);
|
||||
|
@@ -2,4 +2,5 @@
|
||||
return NULL;
|
||||
}}
|
||||
|
||||
return &(array->items[index]);
|
||||
u8 *ptr = (u8 *)(array->items) + (array->item_size * index);
|
||||
return ({T} *)ptr;
|
||||
|
@@ -1,5 +1,4 @@
|
||||
if (!array || array->count == 0) {{
|
||||
return ({T}){{0}};
|
||||
}}
|
||||
|
||||
return array->items[--(array->count)];
|
||||
u64 index = array->count - 1;
|
||||
{T} *out = wapp_{Tlower}_array_get(array, index);
|
||||
--(array->count);
|
||||
return out;
|
||||
|
4
codegen/array/snippets/array_pop_macro
Normal file
4
codegen/array/snippets/array_pop_macro
Normal file
@@ -0,0 +1,4 @@
|
||||
(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \
|
||||
*_{Tlower}_array_pop(ARRAY_PTR) : \
|
||||
({T}){{0}} \
|
||||
)
|
@@ -1,5 +1,6 @@
|
||||
if (!array || index >= array->count) {{
|
||||
{T} *ptr = wapp_{Tlower}_array_get(array, index);
|
||||
if (!ptr) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
array->items[index] = item;
|
||||
memcpy((void *)ptr, (void *)item, array->item_size);
|
||||
|
@@ -6,7 +6,7 @@
|
||||
|
||||
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);
|
||||
output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, src->item_size);
|
||||
if (!output) {{
|
||||
output = dst;
|
||||
goto RETURN_{Tupper}_ARRAY_COPY_ALLOC;
|
||||
|
@@ -20,5 +20,5 @@
|
||||
continue;
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_append_capped(dst, *item);
|
||||
wapp_{Tlower}_array_append_capped(dst, item);
|
||||
}}
|
||||
|
@@ -7,7 +7,7 @@
|
||||
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);
|
||||
output = ({ArrayType} *)_array_alloc_capacity(allocator, new_capacity, array->item_size);
|
||||
if (!output) {{
|
||||
output = array;
|
||||
goto RETURN_{Tupper}_ARRAY_EXTEND_ALLOC;
|
||||
|
@@ -23,5 +23,5 @@
|
||||
continue;
|
||||
}}
|
||||
|
||||
wapp_{Tlower}_array_append_capped(array, *item);
|
||||
}}
|
||||
wapp_{Tlower}_array_append_capped(array, item);
|
||||
}}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
(({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) \
|
||||
.capacity = wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2), \
|
||||
.item_size = sizeof({T}) \
|
||||
}})
|
||||
|
@@ -1 +1 @@
|
||||
(({ArrayType}){{.items = ({T}[CAPACITY]){{0}}, .count = 0, .capacity = CAPACITY}})
|
||||
(({ArrayType}){{.items = ({T}[CAPACITY]){{0}}, .count = 0, .capacity = CAPACITY, .item_size = sizeof({T})}})
|
||||
|
Reference in New Issue
Block a user