ucx/linked_list.c

changeset 748
49a284f61e8c
parent 747
efbd59642577
child 750
4d7a2238c5ac
equal deleted inserted replaced
747:efbd59642577 748:49a284f61e8c
54 i < index ? i++ : i--; 54 i < index ? i++ : i--;
55 } 55 }
56 return (void *) cur; 56 return (void *) cur;
57 } 57 }
58 58
59 size_t cx_linked_list_find( 59 ssize_t cx_linked_list_find(
60 void const *start, 60 void const *start,
61 ptrdiff_t loc_advance, 61 ptrdiff_t loc_advance,
62 ptrdiff_t loc_data, 62 ptrdiff_t loc_data,
63 cx_compare_func cmp_func, 63 cx_compare_func cmp_func,
64 void const *elem 64 void const *elem
67 assert(loc_advance >= 0); 67 assert(loc_advance >= 0);
68 assert(loc_data >= 0); 68 assert(loc_data >= 0);
69 assert(cmp_func); 69 assert(cmp_func);
70 70
71 void const *node = start; 71 void const *node = start;
72 size_t index = 0; 72 ssize_t index = 0;
73 do { 73 do {
74 void *current = ll_data(node); 74 void *current = ll_data(node);
75 if (cmp_func(current, elem) == 0) { 75 if (cmp_func(current, elem) == 0) {
76 return index; 76 return index;
77 } 77 }
78 node = ll_advance(node); 78 node = ll_advance(node);
79 index++; 79 index++;
80 } while (node != NULL); 80 } while (node != NULL);
81 return index; 81 return -1;
82 } 82 }
83 83
84 void *cx_linked_list_first( 84 void *cx_linked_list_first(
85 void const *node, 85 void const *node,
86 ptrdiff_t loc_prev 86 ptrdiff_t loc_prev
352 352
353 void *lc, *ls, *le, *re; 353 void *lc, *ls, *le, *re;
354 354
355 // set start node 355 // set start node
356 ls = *begin; 356 ls = *begin;
357
358 // early exit when this list is empty
359 if (ls == NULL) return;
357 360
358 // check how many elements are already sorted 361 // check how many elements are already sorted
359 lc = ls; 362 lc = ls;
360 size_t ln = 1; 363 size_t ln = 1;
361 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) { 364 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
727 cx_linked_list *ll = (cx_linked_list *) list; 730 cx_linked_list *ll = (cx_linked_list *) list;
728 cx_linked_list_node *node = cx_ll_node_at(ll, index); 731 cx_linked_list_node *node = cx_ll_node_at(ll, index);
729 return node == NULL ? NULL : node->payload; 732 return node == NULL ? NULL : node->payload;
730 } 733 }
731 734
732 static size_t cx_ll_find( 735 static ssize_t cx_ll_find(
733 struct cx_list_s const *list, 736 struct cx_list_s const *list,
734 void const *elem 737 void const *elem
735 ) { 738 ) {
736 return cx_linked_list_find(((cx_linked_list *) list)->begin, 739 return cx_linked_list_find(((cx_linked_list *) list)->begin,
737 CX_LL_LOC_NEXT, CX_LL_LOC_DATA, 740 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,

mercurial