diff -r d4baf4dd55c3 -r fe0d69d72bcd src/linked_list.c --- a/src/linked_list.c Thu May 23 19:29:14 2024 +0200 +++ b/src/linked_list.c Thu May 23 20:29:28 2024 +0200 @@ -501,10 +501,10 @@ cx_linked_list const *list, size_t index ) { - if (index >= list->base.size) { + if (index >= list->base.base.size) { return NULL; - } else if (index > list->base.size / 2) { - return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index); + } 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 { 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->allocator, - sizeof(cx_linked_list_node) + list->item_size); + cx_linked_list_node *new_node = cxMalloc(list->base.allocator, + sizeof(cx_linked_list_node) + list->base.item_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->item_size); + memcpy(new_node->payload, elem, list->base.item_size); // insert cx_linked_list *ll = (cx_linked_list *) list; @@ -536,7 +536,7 @@ ); // increase the size and return - list->size++; + list->base.size++; return 0; } @@ -547,7 +547,7 @@ size_t n ) { // out-of bounds and corner case check - if (index > list->size || n == 0) return 0; + if (index > list->base.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->item_size; + source += list->base.item_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->size--; + list->base.size--; // free and return - cxFree(list->allocator, node); + cxFree(list->base.allocator, node); return 0; } static void cx_ll_clear(struct cx_list_s *list) { - if (list->size == 0) return; + if (list->base.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->allocator, node); + cxFree(list->base.allocator, node); node = next; } ll->begin = ll->end = NULL; - list->size = 0; + list->base.size = 0; } #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE @@ -634,12 +634,12 @@ size_t i, size_t j ) { - if (i >= list->size || j >= list->size) return 1; + if (i >= list->base.size || j >= list->base.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->size / 2; + size_t mid = list->base.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->size - right - 1; + size_t diff2boundary = list->base.size - right - 1; if (left <= diff2boundary) { closest = left; other = right; @@ -707,7 +707,7 @@ } } - if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE) { + if (list->base.item_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->item_size); - memcpy(nleft->payload, nright->payload, list->item_size); - memcpy(nright->payload, buf, list->item_size); + memcpy(buf, nleft->payload, list->base.item_size); + memcpy(nleft->payload, nright->payload, list->base.item_size); + memcpy(nright->payload, buf, list->base.item_size); } return 0; @@ -768,21 +768,21 @@ (void **) &node, ll->begin, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, - list->cmpfunc, elem + list->base.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->size--; - cxFree(list->allocator, node); + list->base.size--; + cxFree(list->base.allocator, node); } return index; } else { return cx_linked_list_find( ((cx_linked_list *) list)->begin, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, - list->cmpfunc, elem + list->base.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->cmpfunc); + list->base.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->cmpfunc); + list->base.cmpfunc); } static bool cx_ll_iter_valid(void const *it) { @@ -817,8 +817,8 @@ static void cx_ll_iter_next(void *it) { struct cx_iterator_s *iter = it; - if (iter->remove) { - iter->remove = false; + if (iter->base.remove) { + iter->base.remove = false; struct cx_list_s *list = iter->src_handle.m; cx_linked_list *ll = iter->src_handle.m; cx_linked_list_node *node = iter->elem_handle; @@ -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->size--; - cxFree(list->allocator, node); + list->base.size--; + cxFree(list->base.allocator, node); } else { iter->index++; cx_linked_list_node *node = iter->elem_handle; @@ -837,8 +837,8 @@ static void cx_ll_iter_prev(void *it) { struct cx_iterator_s *iter = it; - if (iter->remove) { - iter->remove = false; + if (iter->base.remove) { + iter->base.remove = false; struct cx_list_s *list = iter->src_handle.m; cx_linked_list *ll = iter->src_handle.m; 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->size--; - cxFree(list->allocator, node); + list->base.size--; + cxFree(list->base.allocator, node); } else { iter->index--; cx_linked_list_node *node = iter->elem_handle; @@ -871,13 +871,13 @@ 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->item_size; - iter.elem_count = list->size; - iter.valid = cx_ll_iter_valid; - iter.current = cx_ll_iter_current; - iter.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next; - iter.mutating = false; - iter.remove = false; + iter.elem_size = list->base.item_size; + iter.elem_count = list->base.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; + iter.base.mutating = false; + iter.base.remove = false; return iter; } @@ -895,8 +895,8 @@ iter->index += prepend * (0 == result); return result; } else { - int result = cx_ll_insert_element(list, list->size, elem); - iter->index = list->size; + int result = cx_ll_insert_element(list, list->base.size, elem); + iter->index = list->base.size; return result; } } @@ -908,11 +908,11 @@ while (node) { cx_invoke_destructor(list, node->payload); void *next = node->next; - cxFree(list->allocator, node); + cxFree(list->base.allocator, node); node = next; } - cxFree(list->allocator, list); + cxFree(list->base.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.allocator = allocator; + list->base.base.allocator = allocator; if (item_size > 0) { - list->base.item_size = item_size; - list->base.cmpfunc = comparator; + list->base.base.item_size = item_size; + list->base.base.cmpfunc = comparator; } else { - list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; + list->base.base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; cxListStorePointers((CxList *) list); }