]> www.git.momoyon.org Git - commonlib.git/commitdiff
da_ -> darr_
authorahmedsamyh <ahmedsamyh10@gmail.com>
Sun, 1 Jun 2025 11:56:24 +0000 (16:56 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Sun, 1 Jun 2025 11:56:24 +0000 (16:56 +0500)
commonlib.h
tests/dynamic_array.c

index a85490ce279086138aad4c9076c54388ead00b93..555fd531feaa9ac0ba6bd35095d03297bcccfe6e 100644 (file)
 #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)
index 343ede92f5e71300250530d0a00c9b0d1fff1a78..a07dec2e2b8bf2ecd9ccbf3155bfd77bcc1ef1b0 100644 (file)
@@ -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);