From: momoyon Date: Mon, 2 Jun 2025 17:33:08 +0000 (+0500) Subject: [commonlib.h] Don't allow to pass NULL as elm_ptr to c_darr_delete... X-Git-Url: https://www.git.momoyon.org/?a=commitdiff_plain;h=41da410472c357a8aa620aa41efbaaeb877d5dee;p=commonlib.git [commonlib.h] Don't allow to pass NULL as elm_ptr to c_darr_delete... - Implement c_darr_delete to remove element without saving the element. --- diff --git a/commonlib.h b/commonlib.h index 555fd53..86f0f41 100644 --- a/commonlib.h +++ b/commonlib.h @@ -27,6 +27,7 @@ #define darr_free c_darr_free #define darr_shift c_darr_shift #define darr_remove c_darr_remove +#define darr_delete c_darr_delete #define DYNAMIC_ARRAY_INITIAL_CAPACITY c_DYNAMIC_ARRAY_INITIAL_CAPACITY // #define c_DYNAMIC_ARRAY_INITIAL_CAPACITY @@ -92,6 +93,9 @@ #ifndef C_MALLOC #define C_MALLOC malloc #endif +#ifndef C_CALLOC +#define C_CALLOC calloc +#endif #ifndef C_FREE #define C_FREE free #endif @@ -206,13 +210,30 @@ typedef struct c_Arena c_Arena; // 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_delete(da, type, 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] = temp;\ + } else {\ + c_log_error("%s:%d: Trying to remove from outofbounds! %zu != (0 ~ %zu)", __FILE__, __LINE__, idx, (da).count);\ + exit(1);\ + }\ + } while (0) #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) != NULL) *(elm_ptr) = temp;\ - (da).count--;\ + (da).items[--(da).count] = temp;\ + if ((elm_ptr) == NULL) {\ + c_log_error("%s:%d: You cant pass NULL as the elm_ptr! please use c_darr_delete to not get the element removed!", __FILE__, __LINE__);\ + exit(1);\ + }\ + type *temp_ptr = elm_ptr; \ + *temp_ptr = temp; \ + } else {\ + c_log_error("%s:%d: Trying to remove from outofbounds! %zu != (0 ~ %zu)", __FILE__, __LINE__, idx, (da).count);\ + exit(1);\ }\ } while (0) diff --git a/tests/.dynamic_array.out.expected b/tests/.dynamic_array.out.expected index 99b9b9c..4f93822 100644 --- a/tests/.dynamic_array.out.expected +++ b/tests/.dynamic_array.out.expected @@ -7,5 +7,4 @@ [INFO] da count before remove: 5 [INFO] Third element: 4 [INFO] da count after remove: 4 -[INFO] outofbound_value : -1 -[INFO] da count after trying to remove outofbounds_value: 4 +[INFO] da count after delete: 3 diff --git a/tests/dynamic_array.c b/tests/dynamic_array.c index a07dec2..2e1b102 100644 --- a/tests/dynamic_array.c +++ b/tests/dynamic_array.c @@ -32,10 +32,9 @@ int main(void) { c_log_info("Third element: %d", third); c_log_info("da count after remove: %zu", da.count); - int outofbound_value = -1; - 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); + c_darr_delete(da, int, 0); + + c_log_info("da count after delete: %zu", da.count); return 0; }