]> www.git.momoyon.org Git - commonlib.git/commitdiff
[commonlib.h] Don't allow to pass NULL as elm_ptr to c_darr_delete...
authormomoyon <momoyon@momoyon.org>
Mon, 2 Jun 2025 17:33:08 +0000 (22:33 +0500)
committermomoyon <momoyon@momoyon.org>
Mon, 2 Jun 2025 17:33:08 +0000 (22:33 +0500)
- Implement c_darr_delete to remove element without saving the element.

commonlib.h
tests/.dynamic_array.out.expected
tests/dynamic_array.c

index 555fd531feaa9ac0ba6bd35095d03297bcccfe6e..86f0f41b96475e7da202baddad1f94f5c4c70b2a 100644 (file)
@@ -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)
 
index 99b9b9ca99133b046e83ac1a166b8388b42d7e22..4f9382206dc8e07ff36c4bcdbd11c98f79ac32a5 100644 (file)
@@ -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
index a07dec2e2b8bf2ecd9ccbf3155bfd77bcc1ef1b0..2e1b102c9589db0d9953310f6468a8c5c4d863ce 100644 (file)
@@ -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;
 }