From 90297d5e566a705b688add12a6385504ede5af7e Mon Sep 17 00:00:00 2001 From: ahmedsamyh Date: Sun, 1 Jun 2025 16:56:24 +0500 Subject: [PATCH] da_ -> darr_ --- commonlib.h | 22 +++++++++++----------- tests/dynamic_array.c | 14 +++++++------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/commonlib.h b/commonlib.h index a85490c..555fd53 100644 --- a/commonlib.h +++ b/commonlib.h @@ -23,10 +23,10 @@ #define ASSERT C_ASSERT #define ARRAY_LEN C_ARRAY_LEN -#define da_append c_da_append -#define da_free c_da_free -#define da_shift c_da_shift -#define da_remove c_da_remove +#define darr_append c_darr_append +#define darr_free c_darr_free +#define darr_shift c_darr_shift +#define darr_remove c_darr_remove #define DYNAMIC_ARRAY_INITIAL_CAPACITY c_DYNAMIC_ARRAY_INITIAL_CAPACITY // #define c_DYNAMIC_ARRAY_INITIAL_CAPACITY @@ -167,7 +167,7 @@ typedef struct c_Arena c_Arena; // Dynamic-Array // -// NOTE: To use c_da_append() the Dynamic-Array struct should be defined using +// NOTE: To use c_darr_append() the Dynamic-Array struct should be defined using // DEFINE_DYNAMIC_ARRAY or have the same members as below! // NOTE!!!: We actually don't want this since this makes the user want to // use this macro to define dynamic arrays. But the user might want extra fields @@ -188,7 +188,7 @@ typedef struct c_Arena c_Arena; #define c_DYNAMIC_ARRAY_INITIAL_CAPACITY (sizeof(size_t)) -#define c_da_append(da, elm) do {\ +#define c_darr_append(da, elm) do {\ if ((da).items == NULL) {\ (da).capacity = c_DYNAMIC_ARRAY_INITIAL_CAPACITY;\ (da).count = 0;\ @@ -203,15 +203,15 @@ typedef struct c_Arena c_Arena; } while (0) // NOTE: We cant do C_ASSERT() here because it aint one expression... -// NOTE: da_shift will make the da loose its ptr, so store the ptr elsewhere if you want to free it later!!! -#define c_da_shift(da) (assert((da).count > 0 && "Array is empty"), (da).count--, *(da).items++) -#define c_da_free(da) C_FREE((da).items) -#define c_da_remove(da, type, elm_ptr, idx) do {\ +// NOTE: darr_shift will make the da loose its ptr, so store the ptr elsewhere if you want to free it later!!! +#define c_darr_shift(da) (assert((da).count > 0 && "Array is empty"), (da).count--, *(da).items++) +#define c_darr_free(da) C_FREE((da).items) +#define c_darr_remove(da, type, elm_ptr, idx) do {\ if ((idx) >= 0 && (idx) <= (da).count-1) {\ type temp = (da).items[(idx)];\ (da).items[(idx)] = (da).items[(da).count-1];\ (da).items[(da).count-1] = temp;\ - if ((elm_ptr)) *(elm_ptr) = temp;\ + if ((elm_ptr) != NULL) *(elm_ptr) = temp;\ (da).count--;\ }\ } while (0) diff --git a/tests/dynamic_array.c b/tests/dynamic_array.c index 343ede9..a07dec2 100644 --- a/tests/dynamic_array.c +++ b/tests/dynamic_array.c @@ -10,15 +10,15 @@ typedef struct { int main(void) { Dynamic_Array da = {0}; - c_da_append(da, 6); + c_darr_append(da, 6); for (int i = 0; i < 10; ++i) { - if (i % 2 == 0) c_da_append(da, i); + if (i % 2 == 0) c_darr_append(da, i); } - // NOTE: da_shift will make the da loose its ptr, so store the ptr elsewhere if you want to free it later!!! - int a = c_da_shift(da); + // NOTE: darr_shift will make the da loose its ptr, so store the ptr elsewhere if you want to free it later!!! + int a = c_darr_shift(da); - C_ASSERT(a == 6, "da_shift did not return correct value"); + C_ASSERT(a == 6, "darr_shift did not return correct value"); c_log_info("Even numbers:"); for (int i = 0; i < da.count; ++i) { @@ -27,13 +27,13 @@ int main(void) { int third = 0; c_log_info("da count before remove: %zu", da.count); - c_da_remove(da, int, &third, 2); + c_darr_remove(da, int, &third, 2); c_log_info("Third element: %d", third); c_log_info("da count after remove: %zu", da.count); int outofbound_value = -1; - c_da_remove(da, int, &outofbound_value, 50); + c_darr_remove(da, int, &outofbound_value, 50); c_log_info("outofbound_value : %d", outofbound_value); c_log_info("da count after trying to remove outofbounds_value: %zu", da.count); -- 2.39.5