From ba235435f07ed67cab1cebb7179b0a15fe5b0b36 Mon Sep 17 00:00:00 2001 From: momoyon Date: Mon, 2 Jun 2025 22:43:18 +0500 Subject: [PATCH] [commonlib.h] static-array functions... - [tests/static_array.c] New test. --- commonlib.h | 65 +++++++++++++++++++++++++ tests/.static_array.build.code.expected | 1 + tests/.static_array.build.err.expected | 0 tests/.static_array.build.in.expected | 0 tests/.static_array.build.out.expected | 0 tests/.static_array.code.expected | 1 + tests/.static_array.err.expected | 0 tests/.static_array.in.expected | 0 tests/.static_array.out.expected | 9 ++++ tests/static_array.c | 58 ++++++++++++++++++++++ 10 files changed, 134 insertions(+) create mode 100644 tests/.static_array.build.code.expected create mode 100644 tests/.static_array.build.err.expected create mode 100644 tests/.static_array.build.in.expected create mode 100644 tests/.static_array.build.out.expected create mode 100644 tests/.static_array.code.expected create mode 100644 tests/.static_array.err.expected create mode 100644 tests/.static_array.in.expected create mode 100644 tests/.static_array.out.expected create mode 100644 tests/static_array.c diff --git a/commonlib.h b/commonlib.h index 86f0f41..d28525f 100644 --- a/commonlib.h +++ b/commonlib.h @@ -30,6 +30,11 @@ #define darr_delete c_darr_delete #define DYNAMIC_ARRAY_INITIAL_CAPACITY c_DYNAMIC_ARRAY_INITIAL_CAPACITY // #define c_DYNAMIC_ARRAY_INITIAL_CAPACITY +#define arr_stack_init c_arr_stack_init +#define arr_heap_init c_arr_heap_init +#define arr_append c_arr_append +#define arr_remove c_arr_remove +#define arr_delete c_arr_delete #define os_get_timedate c_os_get_timedate #define os_file_exists c_os_file_exists @@ -237,6 +242,66 @@ typedef struct c_Arena c_Arena; }\ } while (0) +// +// Static-Array +// + +// #define DEFINE_DYNAMIC_ARRAY(struct_name, elm_type) typedef struct { +// elm_type *items; +// size_t count; +// size_t capacity; +// } + +// NOTE: Initializes the static-array on the heap +#define c_arr_heap_init(arr, cap) do {\ + if ((arr).items != NULL) {\ + log_error("%s:%d: This static-array is already initialized!", __FILE__, __LINE__);\ + exit(1);\ + }\ + (arr).capacity = cap;\ + (arr).items = C_CALLOC(sizeof(*(arr).items), (arr).capacity);\ + if ((arr).items == NULL) {\ + log_error("%s:%d: calloc failed while trying init this static-array!", __FILE__, __LINE__);\ + exit(1);\ + }\ + } while (0) + +// NOTE: Initializes the static-array on the stack +// NOTE: You are supposed to pass a struct that ateast has the same members as Dynamic-Arrays. +// NOTE: This just sets the `capacity` member of the struct to the capacity inferred by the stack-allocated items +// eg: ```C +// #define ARR_CAP 1024 +// typedef struct { +// int items[ARR_CAP]; +// size_t count; +// size_t capacity; +// } Array; +// ``` +#define c_arr_stack_init(arr) do {\ + if ((arr).items == NULL) {\ + log_error("%s:%d: Please pass an static-array that has it's `items` alloced on the stack!", __FILE__, __LINE__);\ + exit(1);\ + }\ + (arr).capacity = C_ARRAY_LEN((arr).items);\ + if ((arr).capacity == 0) {\ + log_error("%s:%d: Failed to set the capacity of the static-array!", __FILE__, __LINE__);\ + exit(1);\ + }\ + } while (0) + +#define c_arr_append(arr, elm) do {\ + if ((arr).items == NULL) {\ + log_error("%s:%d: Please initialize the static array!", __FILE__, __LINE__);\ + exit(1);\ + }\ + if ((arr).count + 1 < (arr).capacity) {\ + (arr).items[(arr).count++] = elm;\ + }\ + } while (0) + +#define c_arr_remove c_darr_remove +#define c_arr_delete c_darr_delete + // // OS // diff --git a/tests/.static_array.build.code.expected b/tests/.static_array.build.code.expected new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/.static_array.build.code.expected @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/.static_array.build.err.expected b/tests/.static_array.build.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.static_array.build.in.expected b/tests/.static_array.build.in.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.static_array.build.out.expected b/tests/.static_array.build.out.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.static_array.code.expected b/tests/.static_array.code.expected new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/.static_array.code.expected @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/.static_array.err.expected b/tests/.static_array.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.static_array.in.expected b/tests/.static_array.in.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.static_array.out.expected b/tests/.static_array.out.expected new file mode 100644 index 0000000..463f38c --- /dev/null +++ b/tests/.static_array.out.expected @@ -0,0 +1,9 @@ +[INFO] STACK: +[INFO] arr: 0, 1024 +[INFO] arr: 3, 1024 +[INFO] HEAP: +[INFO] harr: 0, 2048 +[INFO] harr: 3, 2048 +[INFO] Element 1: 420 +[INFO] harr.count after remove: 2 +[INFO] harr.count after delete: 1 diff --git a/tests/static_array.c b/tests/static_array.c new file mode 100644 index 0000000..a463f94 --- /dev/null +++ b/tests/static_array.c @@ -0,0 +1,58 @@ +#define COMMONLIB_IMPLEMENTATION +#define COMMONLIB_REMOVE_PREFIX +#include "../commonlib.h" + +// on the stack +#define ARR_CAP (1024) +typedef struct { + int items[ARR_CAP]; + size_t count; + size_t capacity; +} Stack_array; +typedef Stack_array Array; + +typedef struct { + int *items; + size_t count; + size_t capacity; +} Heap_array; + +int main(void) { + Array arr = {0}; + arr_stack_init(arr); + log_info("STACK:"); + log_info("arr: %zu, %zu ", arr.count, arr.capacity); + arr_append(arr, 69); + arr_append(arr, 420); + arr_append(arr, 1337); + log_info("arr: %zu, %zu ", arr.count, arr.capacity); + + Heap_array harr = {0}; + arr_heap_init(harr, 2048); + log_info("HEAP:"); + log_info("harr: %zu, %zu ", harr.count, harr.capacity); + arr_append(harr, 69); + arr_append(harr, 420); + arr_append(harr, 1337); + log_info("harr: %zu, %zu ", harr.count, harr.capacity); + + int elm = -1; + size_t i = 1; + arr_remove(harr, int, &elm, i); + + ASSERT(elm == 420, "This should be correct!"); + ASSERT(harr.count == 2, "This should be correct!"); + + log_info("Element %zu: %d", i, elm); + log_info("harr.count after remove: %zu", harr.count); + + arr_delete(harr, int, 1); + log_info("harr.count after delete: %zu", harr.count); + ASSERT(harr.count == 1, "This should be correct!"); + + + + return 0; +} + + -- 2.39.5