src/linked_list.c

changeset 699
35b2b99ee523
parent 677
b09aae58bba4
child 702
3390b58ad15a
equal deleted inserted replaced
698:7345ee0a0301 699:35b2b99ee523
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
727 cx_linked_list *ll = (cx_linked_list *) list; 727 cx_linked_list *ll = (cx_linked_list *) list;
728 cx_linked_list_node *node = cx_ll_node_at(ll, index); 728 cx_linked_list_node *node = cx_ll_node_at(ll, index);
729 return node == NULL ? NULL : node->payload; 729 return node == NULL ? NULL : node->payload;
730 } 730 }
731 731
732 static size_t cx_ll_find( 732 static ssize_t cx_ll_find(
733 struct cx_list_s const *list, 733 struct cx_list_s const *list,
734 void const *elem 734 void const *elem
735 ) { 735 ) {
736 return cx_linked_list_find(((cx_linked_list *) list)->begin, 736 return cx_linked_list_find(((cx_linked_list *) list)->begin,
737 CX_LL_LOC_NEXT, CX_LL_LOC_DATA, 737 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,

mercurial