src/linked_list.c

changeset 856
6bbbf219251d
parent 855
35bcb3216c0d
child 874
cdce47f34d48
--- a/src/linked_list.c	Thu May 23 20:31:37 2024 +0200
+++ b/src/linked_list.c	Thu May 23 20:43:04 2024 +0200
@@ -501,10 +501,10 @@
         cx_linked_list const *list,
         size_t index
 ) {
-    if (index >= list->base.base.size) {
+    if (index >= list->base.collection.size) {
         return NULL;
-    } else if (index > list->base.base.size / 2) {
-        return cx_linked_list_at(list->end, list->base.base.size - 1, CX_LL_LOC_PREV, index);
+    } else if (index > list->base.collection.size / 2) {
+        return cx_linked_list_at(list->end, list->base.collection.size - 1, CX_LL_LOC_PREV, index);
     } else {
         return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
     }
@@ -517,15 +517,15 @@
 ) {
 
     // create the new new_node
-    cx_linked_list_node *new_node = cxMalloc(list->base.allocator,
-                                             sizeof(cx_linked_list_node) + list->base.elem_size);
+    cx_linked_list_node *new_node = cxMalloc(list->collection.allocator,
+                                             sizeof(cx_linked_list_node) + list->collection.elem_size);
 
     // sortir if failed
     if (new_node == NULL) return 1;
 
     // initialize new new_node
     new_node->prev = new_node->next = NULL;
-    memcpy(new_node->payload, elem, list->base.elem_size);
+    memcpy(new_node->payload, elem, list->collection.elem_size);
 
     // insert
     cx_linked_list *ll = (cx_linked_list *) list;
@@ -536,7 +536,7 @@
     );
 
     // increase the size and return
-    list->base.size++;
+    list->collection.size++;
     return 0;
 }
 
@@ -547,7 +547,7 @@
         size_t n
 ) {
     // out-of bounds and corner case check
-    if (index > list->base.size || n == 0) return 0;
+    if (index > list->collection.size || n == 0) return 0;
 
     // find position efficiently
     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
@@ -566,7 +566,7 @@
     // we can add the remaining nodes and immedately advance to the inserted node
     char const *source = array;
     for (size_t i = 1; i < n; i++) {
-        source += list->base.elem_size;
+        source += list->collection.elem_size;
         if (0 != cx_ll_insert_at(list, node, source)) {
             return i;
         }
@@ -601,27 +601,27 @@
                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
 
     // adjust size
-    list->base.size--;
+    list->collection.size--;
 
     // free and return
-    cxFree(list->base.allocator, node);
+    cxFree(list->collection.allocator, node);
 
     return 0;
 }
 
 static void cx_ll_clear(struct cx_list_s *list) {
-    if (list->base.size == 0) return;
+    if (list->collection.size == 0) return;
 
     cx_linked_list *ll = (cx_linked_list *) list;
     cx_linked_list_node *node = ll->begin;
     while (node != NULL) {
         cx_invoke_destructor(list, node->payload);
         cx_linked_list_node *next = node->next;
-        cxFree(list->base.allocator, node);
+        cxFree(list->collection.allocator, node);
         node = next;
     }
     ll->begin = ll->end = NULL;
-    list->base.size = 0;
+    list->collection.size = 0;
 }
 
 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
@@ -634,12 +634,12 @@
         size_t i,
         size_t j
 ) {
-    if (i >= list->base.size || j >= list->base.size) return 1;
+    if (i >= list->collection.size || j >= list->collection.size) return 1;
     if (i == j) return 0;
 
     // perform an optimized search that finds both elements in one run
     cx_linked_list *ll = (cx_linked_list *) list;
-    size_t mid = list->base.size / 2;
+    size_t mid = list->collection.size / 2;
     size_t left, right;
     if (i < j) {
         left = i;
@@ -671,7 +671,7 @@
         // chose the closest to begin / end
         size_t closest;
         size_t other;
-        size_t diff2boundary = list->base.size - right - 1;
+        size_t diff2boundary = list->collection.size - right - 1;
         if (left <= diff2boundary) {
             closest = left;
             other = right;
@@ -707,7 +707,7 @@
         }
     }
 
-    if (list->base.elem_size > CX_LINKED_LIST_SWAP_SBO_SIZE) {
+    if (list->collection.elem_size > CX_LINKED_LIST_SWAP_SBO_SIZE) {
         cx_linked_list_node *prev = nleft->prev;
         cx_linked_list_node *next = nright->next;
         cx_linked_list_node *midstart = nleft->next;
@@ -739,9 +739,9 @@
     } else {
         // swap payloads to avoid relinking
         char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
-        memcpy(buf, nleft->payload, list->base.elem_size);
-        memcpy(nleft->payload, nright->payload, list->base.elem_size);
-        memcpy(nright->payload, buf, list->base.elem_size);
+        memcpy(buf, nleft->payload, list->collection.elem_size);
+        memcpy(nleft->payload, nright->payload, list->collection.elem_size);
+        memcpy(nright->payload, buf, list->collection.elem_size);
     }
 
     return 0;
@@ -768,21 +768,21 @@
                 (void **) &node,
                 ll->begin,
                 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                list->base.cmpfunc, elem
+                list->collection.cmpfunc, elem
         );
         if (node != NULL) {
             cx_invoke_destructor(list, node->payload);
             cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
                                   CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
-            list->base.size--;
-            cxFree(list->base.allocator, node);
+            list->collection.size--;
+            cxFree(list->collection.allocator, node);
         }
         return index;
     } else {
         return cx_linked_list_find(
                 ((cx_linked_list *) list)->begin,
                 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                list->base.cmpfunc, elem
+                list->collection.cmpfunc, elem
         );
     }
 }
@@ -791,7 +791,7 @@
     cx_linked_list *ll = (cx_linked_list *) list;
     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                        list->base.cmpfunc);
+                        list->collection.cmpfunc);
 }
 
 static void cx_ll_reverse(struct cx_list_s *list) {
@@ -807,7 +807,7 @@
     cx_linked_list *right = (cx_linked_list *) other;
     return cx_linked_list_compare(left->begin, right->begin,
                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
-                                  list->base.cmpfunc);
+                                  list->collection.cmpfunc);
 }
 
 static bool cx_ll_iter_valid(void const *it) {
@@ -826,8 +826,8 @@
         cx_invoke_destructor(list, node->payload);
         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
-        list->base.size--;
-        cxFree(list->base.allocator, node);
+        list->collection.size--;
+        cxFree(list->collection.allocator, node);
     } else {
         iter->index++;
         cx_linked_list_node *node = iter->elem_handle;
@@ -847,8 +847,8 @@
         cx_invoke_destructor(list, node->payload);
         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
-        list->base.size--;
-        cxFree(list->base.allocator, node);
+        list->collection.size--;
+        cxFree(list->collection.allocator, node);
     } else {
         iter->index--;
         cx_linked_list_node *node = iter->elem_handle;
@@ -871,8 +871,8 @@
     iter.index = index;
     iter.src_handle.c = list;
     iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
-    iter.elem_size = list->base.elem_size;
-    iter.elem_count = list->base.size;
+    iter.elem_size = list->collection.elem_size;
+    iter.elem_count = list->collection.size;
     iter.base.valid = cx_ll_iter_valid;
     iter.base.current = cx_ll_iter_current;
     iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
@@ -895,8 +895,8 @@
         iter->index += prepend * (0 == result);
         return result;
     } else {
-        int result = cx_ll_insert_element(list, list->base.size, elem);
-        iter->index = list->base.size;
+        int result = cx_ll_insert_element(list, list->collection.size, elem);
+        iter->index = list->collection.size;
         return result;
     }
 }
@@ -908,11 +908,11 @@
     while (node) {
         cx_invoke_destructor(list, node->payload);
         void *next = node->next;
-        cxFree(list->base.allocator, node);
+        cxFree(list->collection.allocator, node);
         node = next;
     }
 
-    cxFree(list->base.allocator, list);
+    cxFree(list->collection.allocator, list);
 }
 
 static cx_list_class cx_linked_list_class = {
@@ -944,13 +944,13 @@
     if (list == NULL) return NULL;
 
     list->base.cl = &cx_linked_list_class;
-    list->base.base.allocator = allocator;
+    list->base.collection.allocator = allocator;
 
     if (elem_size > 0) {
-        list->base.base.elem_size = elem_size;
-        list->base.base.cmpfunc = comparator;
+        list->base.collection.elem_size = elem_size;
+        list->base.collection.cmpfunc = comparator;
     } else {
-        list->base.base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
+        list->base.collection.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
         cxListStorePointers((CxList *) list);
     }
 

mercurial