diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 4fc42c7..7845bfd 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -30,43 +30,38 @@ BEGIN_C_LINKAGE } NAME #ifdef WAPP_PLATFORM_CPP -#define wapp_array(TYPE, ...) ([&]() { \ - u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ - \ - TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \ - \ - wapp_persist u8 array[ \ - sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \ - ] = {0}; \ - ArrayHeader *header = (ArrayHeader *)array; \ - header->magic = WAPP_ARRAY_MAGIC; \ - header->count = _calc_array_count(TYPE, __VA_ARGS__); \ - header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \ - header->item_size = sizeof(TYPE); \ - \ - u8 *buf = (u8 *)(header + 1); \ - memcpy(buf, items, capacity * sizeof(TYPE)); \ - return (TYPE *)buf; \ +#define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) ([&]() { \ + wapp_persist ELEM_TYPE items[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE{}).items)) ? \ + _calc_array_capacity(ELEM_TYPE, __VA_ARGS__) : -1 \ + ] = {__VA_ARGS__}; \ + \ + return ARRAY_TYPE{ \ + WAPP_ARRAY_MAGIC, \ + _calc_array_count(ELEM_TYPE, __VA_ARGS__), \ + _calc_array_capacity(ELEM_TYPE, __VA_ARGS__), \ + sizeof(ELEM_TYPE), \ + items, \ + }; \ }()) -#define wapp_array_with_capacity(TYPE, CAPACITY) ([&]() { \ - wapp_persist u8 array[ \ - sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \ - ] = {0}; \ - ArrayHeader *header = (ArrayHeader *)array; \ - header->magic = WAPP_ARRAY_MAGIC; \ - header->count = 0; \ - header->capacity = CAPACITY; \ - header->item_size = sizeof(TYPE); \ - \ - return (TYPE *)(header + 1); \ +#define wapp_array_with_capacity(ELEM_TYPE, ARRAY_TYPE, CAPACITY) ([&]() { \ + wapp_persist ELEM_TYPE items[sizeof(ELEM_TYPE) == sizeof(*((ARRAY_TYPE{}).items)) ? \ + CAPACITY : -1] = {}; \ + \ + return ARRAY_TYPE{ \ + WAPP_ARRAY_MAGIC, \ + 0, \ + CAPACITY, \ + sizeof(ELEM_TYPE), \ + items, \ + }; \ }()) -#define wapp_array_pop(TYPE, ARRAY_PTR) ([&]() { \ - if (ARRAY_PTR == NULL || _array_count((u8 *)ARRAY_PTR) == 0) { \ - TYPE result{}; \ - return result; \ - } \ - \ - return *((TYPE *)_array_pop((u8 *)ARRAY_PTR, sizeof(TYPE))); \ +#define wapp_array_pop(ELEM_TYPE, ARRAY_PTR) ([&]() { \ + if (ARRAY_PTR == NULL || (ARRAY_PTR)->count == 0) { \ + ELEM_TYPE result{}; \ + return result; \ + } \ + \ + return *((ELEM_TYPE *)_array_pop((Array *)ARRAY_PTR, sizeof(ELEM_TYPE))); \ }()) #else #define wapp_array(ELEM_TYPE, ARRAY_TYPE, ...) \ diff --git a/tests/array/test_i32_array.cc b/tests/array/test_i32_array.cc index bb8d066..e4ce364 100644 --- a/tests/array/test_i32_array.cc +++ b/tests/array/test_i32_array.cc @@ -4,15 +4,15 @@ TestFuncResult test_i32_array(void) { b8 result; - i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7); - result = wapp_array_count(array) == 7 && wapp_array_capacity(array) == 16; + I32Array array = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7); + result = array.count == 7 && array.capacity == 16; i32 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { - item = wapp_array_get(i32, array, index); + item = wapp_array_get(i32, &array, index); result = result && item && (*item == (i32)(index + 1)); ++index; @@ -25,8 +25,8 @@ TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array_with_capacity(void) { b8 result; - i32 *array = wapp_array_with_capacity(i32, 64); - result = wapp_array_count(array) == 0 && wapp_array_capacity(array) == 64; + I32Array array = wapp_array_with_capacity(i32, I32Array, 64); + result = array.count == 0 && array.capacity == 64; return wapp_tester_result(result); } @@ -34,14 +34,14 @@ TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_get(void) { b8 result = true; - i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { - item = wapp_array_get(i32, array, index); + item = wapp_array_get(i32, &array, index); result = result && item && (*item == (i32)index); ++index; @@ -54,16 +54,16 @@ TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_set(void) { b8 result = true; - i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { i32 num = (i32)(index * 2); - wapp_array_set(i32, array, index, &num); - item = wapp_array_get(i32, array, index); + wapp_array_set(i32, &array, index, &num); + item = wapp_array_get(i32, &array, index); result = result && item && (*item == (i32)(index * 2)); ++index; @@ -76,19 +76,19 @@ TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_append_capped(void) { b8 result; - i32 *array = wapp_array_with_capacity(i32, 64); + I32Array array = wapp_array_with_capacity(i32, I32Array, 64); i32 item1 = 10; - wapp_array_append_capped(i32, array, &item1); + wapp_array_append_capped(i32, &array, &item1); - result = wapp_array_count(array) == 1; - i32 *item = wapp_array_get(i32, array, 0); + result = array.count == 1; + i32 *item = wapp_array_get(i32, &array, 0); result = result && item && *item == 10; - array = wapp_array(i32, 1); + array = wapp_array(i32, I32Array, 1); i32 item2 = 10; - wapp_array_append_capped(i32, array, &item2); + wapp_array_append_capped(i32, &array, &item2); - result = result && wapp_array_count(array) == 2; + result = result && array.count == 2; return wapp_tester_result(result); } @@ -96,14 +96,14 @@ TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_extend_capped(void) { b8 result; - i32 *array1 = wapp_array(i32, 1, 2, 3, 4); - i32 *array2 = wapp_array(i32, 10, 20); + I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4); + I32Array array2 = wapp_array(i32, I32Array, 10, 20); - result = wapp_array_count(array1) == 4 && wapp_array_count(array2) == 2; + result = array1.count == 4 && array2.count == 2; - wapp_array_extend_capped(i32, array1, array2); + wapp_array_extend_capped(i32, &array1, &array2); - result = result && wapp_array_count(array1) == 6; + result = result && array1.count == 6; return wapp_tester_result(result); } @@ -111,31 +111,31 @@ TestFuncResult test_i32_array_extend_capped(void) { TestFuncResult test_i32_array_copy_capped(void) { b8 result; - i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); - i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); - i32 *dst2 = wapp_array(i32, 1, 2); + I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); + I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); + I32Array dst2 = wapp_array(i32, I32Array, 1, 2); u64 expected_count = 5; - wapp_array_copy_capped(i32, dst1, src); - result = wapp_array_count(dst1) == expected_count; + wapp_array_copy_capped(i32, &dst1, &src); + result = dst1.count == expected_count; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst1, index)); + result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst1, index)); ++index; running = index < expected_count; } expected_count = 4; - wapp_array_copy_capped(i32, dst2, src); - result = result && wapp_array_count(dst2) == expected_count; + wapp_array_copy_capped(i32, &dst2, &src); + result = result && dst2.count == expected_count; index = 0; running = true; while (running) { - result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, dst2, index)); + result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, &dst2, index)); ++index; running = index < expected_count; @@ -149,9 +149,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) { Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); u64 capacity = 32; - i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity); + I32Array *array = wapp_array_alloc_capacity(i32, I32Array, &allocator, capacity); - result = array && wapp_array_capacity(array) == capacity; + result = array && array->capacity == capacity; wapp_mem_arena_allocator_destroy(&allocator); @@ -162,24 +162,24 @@ TestFuncResult test_i32_array_append_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *array2 = wapp_array(i32, 1, 2); + I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array2 = wapp_array(i32, I32Array, 1, 2); i32 num = 10; - i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num); - result = arr_ptr == array1; + I32Array *arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array1, &num); + result = arr_ptr == &array1; u64 count = 4; u64 index = 0; b8 running = true; while (running) { i32 num = (i32)index; - arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num); + arr_ptr = wapp_array_append_alloc(i32, I32Array, &allocator, &array2, &num); ++index; running = index < count; } - result = result && arr_ptr != array2; + result = result && arr_ptr != &array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -190,15 +190,15 @@ TestFuncResult test_i32_array_extend_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *array2 = wapp_array(i32, 1, 2); - i32 *array3 = wapp_array(i32, 1, 2, 3, 4); + I32Array array1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array2 = wapp_array(i32, I32Array, 1, 2); + I32Array array3 = wapp_array(i32, I32Array, 1, 2, 3, 4); - i32 *arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3); - result = arr_ptr == array1; + I32Array *arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array1, &array3); + result = arr_ptr == &array1; - arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3); - result = result && arr_ptr != array2; + arr_ptr = wapp_array_extend_alloc(i32, I32Array, &allocator, &array2, &array3); + result = result && arr_ptr != &array2; wapp_mem_arena_allocator_destroy(&allocator); @@ -209,32 +209,32 @@ TestFuncResult test_i32_array_copy_alloc(void) { b8 result; Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); - i32 *src = wapp_array(i32, 1, 2, 3, 4, 5); - i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6); - i32 *dst2 = wapp_array(i32, 1, 2); - i32 *array_ptr = nullptr; + I32Array src = wapp_array(i32, I32Array, 1, 2, 3, 4, 5); + I32Array dst1 = wapp_array(i32, I32Array, 1, 2, 3, 4, 5, 6); + I32Array dst2 = wapp_array(i32, I32Array, 1, 2); + I32Array *array_ptr = nullptr; u64 expected_count = 5; - array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src); - result = wapp_array_count(array_ptr) == expected_count && array_ptr == dst1; + array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst1, &src); + result = array_ptr->count == expected_count && array_ptr == &dst1; u64 index = 0; b8 running = true; while (running) { - result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index)); + result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index)); ++index; running = index < expected_count; } expected_count = 5; - array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src); - result = result && wapp_array_count(array_ptr) == expected_count && array_ptr != dst2; + array_ptr = wapp_array_copy_alloc(i32, I32Array, &allocator, &dst2, &src); + result = result && array_ptr->count == expected_count && array_ptr != &dst2; index = 0; running = true; while (running) { - result = result && (*wapp_array_get(i32, src, index) == *wapp_array_get(i32, array_ptr, index)); + result = result && (*wapp_array_get(i32, &src, index) == *wapp_array_get(i32, array_ptr, index)); ++index; running = index < expected_count; @@ -248,12 +248,12 @@ TestFuncResult test_i32_array_copy_alloc(void) { TestFuncResult test_i32_array_clear(void) { b8 result; - i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - result = wapp_array_count(array) == 9; + I32Array array = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); + result = array.count == 9; - wapp_array_clear(i32, array); + wapp_array_clear(i32, &array); - result = result && wapp_array_count(array) == 0; + result = result && array.count == 0; return wapp_tester_result(result); } @@ -261,11 +261,11 @@ TestFuncResult test_i32_array_clear(void) { TestFuncResult test_i32_array_pop(void) { b8 result; - i32 *array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8); - i32 *array2 = wapp_array_with_capacity(i32, 32); + I32Array array1 = wapp_array(i32, I32Array, 0, 1, 2, 3, 4, 5, 6, 7, 8); + I32Array array2 = wapp_array_with_capacity(i32, I32Array, 32); - i32 item1 = wapp_array_pop(i32, array1); - i32 item2 = wapp_array_pop(i32, array2); + i32 item1 = wapp_array_pop(i32, &array1); + i32 item2 = wapp_array_pop(i32, &array2); result = item1 == 8 && item2 == 0; diff --git a/tests/array/test_str8_array.cc b/tests/array/test_str8_array.cc index 74fd544..bc0c14a 100644 --- a/tests/array/test_str8_array.cc +++ b/tests/array/test_str8_array.cc @@ -10,16 +10,16 @@ TestFuncResult test_str8_array(void) { Str8 str1 = wapp_str8_lit("Hello"); Str8 str2 = wapp_str8_lit("Hi"); Str8 str3 = wapp_str8_lit("Bye"); - Str8 *array = wapp_array(Str8, str1, str2, str3); + Str8Array array = wapp_array(Str8, Str8Array, str1, str2, str3); - result = wapp_array_count(array) == 3 && wapp_array_capacity(array) == 8; + result = array.count == 3 && array.capacity == 8; Str8 *item; - u64 count = wapp_array_count(array); + u64 count = array.count; u64 index = 0; b8 running = true; while (running) { - item = wapp_array_get(Str8, array, index); + item = wapp_array_get(Str8, &array, index); result = result && item && (wapp_str8_equal(item, &expected[index])); ++index; diff --git a/tests/str8/test_str8.cc b/tests/str8/test_str8.cc index ffe27ea..b38a473 100644 --- a/tests/str8/test_str8.cc +++ b/tests/str8/test_str8.cc @@ -617,10 +617,10 @@ TestFuncResult test_str8_from_bytes(void) { Str8 str = wapp_str8_buf(1024); Str8 expected = wapp_str8_lit_ro("WAPP"); - u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P'); - wapp_str8_from_bytes(&str, bytes); + U8Array bytes = wapp_array(u8, U8Array, 'W', 'A', 'P', 'P'); + wapp_str8_from_bytes(&str, &bytes); - result = str.size == wapp_array_count(bytes) * wapp_array_item_size(bytes); + result = str.size == bytes.count * bytes.item_size; result = result && wapp_str8_equal(&str, &expected); return wapp_tester_result(result);