Add copy array with allocation and finish writing array tests

This commit is contained in:
2025-04-27 19:30:14 +01:00
parent d3f1686d58
commit 77f3c40ebd
5 changed files with 152 additions and 13 deletions

View File

@@ -66,7 +66,7 @@ i32 wapp_i32_array_pop(I32Array *array) {
return array->items[--(array->count)];
}
void wapp_i32_array_copy(const I32Array *src, I32Array *dst) {
void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst) {
if (!src || !dst) {
return;
}
@@ -90,19 +90,19 @@ I32Array *wapp_i32_array_alloc_capacity(const Allocator *allocator, u64 capacity
I32Array *array = NULL;
if (!allocator) {
goto RETURN_INT_ARRAY_ALLOC;
goto RETURN_I32_ARRAY_ALLOC;
}
array = wapp_mem_allocator_alloc(allocator, allocation_size);
if (!array) {
goto RETURN_INT_ARRAY_ALLOC;
goto RETURN_I32_ARRAY_ALLOC;
}
array->items = (i32 *)((u8 *)array + sizeof(I32Array));
array->count = 0;
array->capacity = capacity;
RETURN_INT_ARRAY_ALLOC:
RETURN_I32_ARRAY_ALLOC:
return array;
}
@@ -110,7 +110,7 @@ I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *arra
I32Array *output = array;
if (!allocator || !array) {
goto RETURN_INT_ARRAY_APPEND_ALLOC;
goto RETURN_I32_ARRAY_APPEND_ALLOC;
}
if (array->count >= array->capacity) {
@@ -118,14 +118,14 @@ I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *arra
output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
if (!output) {
output = array;
goto RETURN_INT_ARRAY_APPEND_ALLOC;
goto RETURN_I32_ARRAY_APPEND_ALLOC;
}
wapp_i32_array_copy(array, output);
wapp_i32_array_copy_capped(array, output);
}
wapp_i32_array_append_capped(output, item);
RETURN_INT_ARRAY_APPEND_ALLOC:
RETURN_I32_ARRAY_APPEND_ALLOC:
return output;
}
@@ -133,7 +133,7 @@ I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *arra
I32Array *output = array;
if (!allocator || !array || !other) {
goto RETURN_INT_ARRAY_EXTEND_ALLOC;
goto RETURN_I32_ARRAY_EXTEND_ALLOC;
}
u64 remaining_capacity = array->capacity - array->count;
@@ -142,13 +142,36 @@ I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *arra
output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
if (!output) {
output = array;
goto RETURN_INT_ARRAY_EXTEND_ALLOC;
goto RETURN_I32_ARRAY_EXTEND_ALLOC;
}
wapp_i32_array_copy(array, output);
wapp_i32_array_copy_capped(array, output);
}
wapp_i32_array_extend_capped(output, other);
RETURN_INT_ARRAY_EXTEND_ALLOC:
RETURN_I32_ARRAY_EXTEND_ALLOC:
return output;
}
I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst) {
I32Array *output = dst;
if (!allocator || !src || !dst) {
goto RETURN_I32_ARRAY_COPY_ALLOC;
}
if (src->count >= dst->capacity) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2);
output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
if (!output) {
output = dst;
goto RETURN_I32_ARRAY_COPY_ALLOC;
}
}
wapp_i32_array_clear(output);
wapp_i32_array_copy_capped(src, output);
RETURN_I32_ARRAY_COPY_ALLOC:
return output;
}

View File

@@ -30,11 +30,12 @@ void wapp_i32_array_append_capped(I32Array *array, i32 item);
void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other);
void wapp_i32_array_clear(I32Array *array);
i32 wapp_i32_array_pop(I32Array *array);
void wapp_i32_array_copy(const I32Array *src, I32Array *dst);
void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst);
I32Array *wapp_i32_array_alloc_capacity(const Allocator *allocator, u64 capacity);
I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 item);
I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other);
I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst);
#ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE