diff -r 2946e13c89a4 -r 4373c2a90066 src/linked_list.c --- a/src/linked_list.c Sat May 21 11:22:47 2022 +0200 +++ b/src/linked_list.c Sat May 21 12:10:25 2022 +0200 @@ -38,7 +38,8 @@ #define ll_prev(node) CX_LL_PTR(node, loc_prev) #define ll_next(node) CX_LL_PTR(node, loc_next) #define ll_advance(node) CX_LL_PTR(node, loc_advance) -#define ll_data(node) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data)) +#define ll_data_f(node, follow_ptr) ((follow_ptr)?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data)) +#define ll_data(node) ll_data_f(node,follow_ptr) void *cx_linked_list_at( void const *start, @@ -403,13 +404,16 @@ void const *begin_right, ptrdiff_t loc_advance, ptrdiff_t loc_data, - bool follow_ptr, + bool follow_ptr_left, + bool follow_ptr_right, CxListComparator cmp_func ) { void const *left = begin_left, *right = begin_right; while (left != NULL && right != NULL) { - int result = cmp_func(ll_data(left), ll_data(right)); + void const *left_data = ll_data_f(left, follow_ptr_left); + void const *right_data = ll_data_f(right, follow_ptr_right); + int result = cmp_func(left_data, right_data); if (result != 0) return result; left = ll_advance(left); right = ll_advance(right); @@ -468,6 +472,7 @@ struct cx_list_s base; cx_linked_list_node *begin; cx_linked_list_node *end; + bool follow_ptr; } cx_linked_list; static cx_linked_list_node *cx_ll_node_at( @@ -595,32 +600,17 @@ struct cx_list_s const *list, void const *elem ) { + cx_linked_list *ll = (cx_linked_list *) list; return cx_linked_list_find(((cx_linked_list *) list)->begin, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, - false, list->cmpfunc, elem); -} - -static size_t cx_pll_find( - struct cx_list_s const *list, - void const *elem -) { - return cx_linked_list_find(((cx_linked_list *) list)->begin, - CX_LL_LOC_NEXT, CX_LL_LOC_DATA, - true, list->cmpfunc, elem); + ll->follow_ptr, list->cmpfunc, elem); } static void cx_ll_sort(struct cx_list_s *list) { 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, - false, list->cmpfunc); -} - -static void cx_pll_sort(struct cx_list_s *list) { - 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, - true, list->cmpfunc); + ll->follow_ptr, list->cmpfunc); } static void cx_ll_reverse(struct cx_list_s *list) { @@ -636,18 +626,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, - false, list->cmpfunc); -} - -static int cx_pll_compare( - struct cx_list_s const *list, - struct cx_list_s const *other -) { - cx_linked_list *left = (cx_linked_list *) list; - 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, - true, list->cmpfunc); + left->follow_ptr, right->follow_ptr, list->cmpfunc); } static bool cx_ll_iter_valid(CxIterator const *iter) { @@ -766,9 +745,9 @@ cx_pll_insert_iter, cx_ll_remove, cx_pll_at, - cx_pll_find, - cx_pll_sort, - cx_pll_compare, + cx_ll_find, + cx_ll_sort, + cx_ll_compare, cx_ll_reverse, cx_pll_iterator, }; @@ -781,6 +760,7 @@ cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); if (list == NULL) return NULL; + list->follow_ptr = false; list->base.cl = &cx_linked_list_class; list->base.allocator = allocator; list->base.cmpfunc = comparator; @@ -797,6 +777,7 @@ cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); if (list == NULL) return NULL; + list->follow_ptr = true; list->base.cl = &cx_pointer_linked_list_class; list->base.allocator = allocator; list->base.cmpfunc = comparator;