src/array_list.c

changeset 986
38fa7e41194c
parent 985
68754c7de906
--- a/src/array_list.c	Thu Nov 07 22:46:58 2024 +0100
+++ b/src/array_list.c	Mon Nov 11 21:42:14 2024 +0100
@@ -88,7 +88,7 @@
 
 // LOW LEVEL ARRAY LIST FUNCTIONS
 
-enum cx_array_result cx_array_copy(
+int cx_array_copy(
         void **target,
         size_t *size,
         size_t *capacity,
@@ -101,24 +101,20 @@
     // assert pointers
     assert(target != NULL);
     assert(size != NULL);
+    assert(capacity != NULL);
     assert(src != NULL);
+    assert(reallocator != NULL);
 
     // determine capacity
-    size_t cap = capacity == NULL ? *size : *capacity;
+    size_t cap = *capacity;
     assert(*target != NULL || cap == 0);
 
     // check if resize is required
     size_t minsize = index + elem_count;
     size_t newsize = *size < minsize ? minsize : *size;
-    bool needrealloc = newsize > cap;
 
     // reallocate if possible
-    if (needrealloc) {
-        // a reallocator and a capacity variable must be available
-        if (reallocator == NULL || capacity == NULL) {
-            return CX_ARRAY_REALLOC_NOT_SUPPORTED;
-        }
-
+    if (newsize > cap) {
         // check, if we need to repair the src pointer
         uintptr_t targetaddr = (uintptr_t) *target;
         uintptr_t srcaddr = (uintptr_t) src;
@@ -134,7 +130,7 @@
                 *target, cap, elem_size, reallocator
         );
         if (newmem == NULL) {
-            return CX_ARRAY_REALLOC_FAILED;
+            return 1;
         }
 
         // repair src pointer, if necessary
@@ -156,10 +152,10 @@
     *size = newsize;
 
     // return successfully
-    return CX_ARRAY_SUCCESS;
+    return 0;
 }
 
-enum cx_array_result cx_array_insert_sorted(
+int cx_array_insert_sorted(
         void **target,
         size_t *size,
         size_t *capacity,
@@ -193,7 +189,7 @@
         if (new_mem == NULL) {
             // give it up right away, there is no contract
             // that requires us to insert as much as we can
-            return CX_ARRAY_REALLOC_FAILED;
+            return 1;
         }
         *target = new_mem;
         *capacity = new_capacity;
@@ -267,7 +263,7 @@
     // still buffer elements left?
     // don't worry, we already moved them to the correct place
 
-    return CX_ARRAY_SUCCESS;
+    return 0;
 }
 
 size_t cx_array_binary_search_inf(
@@ -458,7 +454,7 @@
         size_t elems_to_move = list->collection.size - index;
         size_t start_of_moved = index + n;
 
-        if (CX_ARRAY_SUCCESS != cx_array_copy(
+        if (cx_array_copy(
                 &arl->data,
                 &list->collection.size,
                 &arl->capacity,
@@ -478,7 +474,7 @@
     // therefore, it is impossible to leave this function with an invalid array
 
     // place the new elements
-    if (CX_ARRAY_SUCCESS == cx_array_copy(
+    if (cx_array_copy(
             &arl->data,
             &list->collection.size,
             &arl->capacity,
@@ -488,10 +484,10 @@
             n,
             &arl->reallocator
     )) {
-        return n;
-    } else {
         // array list implementation is "all or nothing"
         return 0;
+    } else {
+        return n;
     }
 }
 
@@ -503,7 +499,7 @@
     // get a correctly typed pointer to the list
     cx_array_list *arl = (cx_array_list *) list;
 
-    if (CX_ARRAY_SUCCESS == cx_array_insert_sorted(
+    if (cx_array_insert_sorted(
             &arl->data,
             &list->collection.size,
             &arl->capacity,
@@ -513,10 +509,10 @@
             n,
             &arl->reallocator
     )) {
-        return n;
-    } else {
         // array list implementation is "all or nothing"
         return 0;
+    } else {
+        return n;
     }
 }
 

mercurial