Sat, 22 Apr 2023 14:21:02 +0200
make list find return a negative value when elem not found
src/array_list.c | file | annotate | diff | comparison | revisions | |
src/cx/linked_list.h | file | annotate | diff | comparison | revisions | |
src/cx/list.h | file | annotate | diff | comparison | revisions | |
src/linked_list.c | file | annotate | diff | comparison | revisions | |
src/list.c | file | annotate | diff | comparison | revisions | |
tests/test_list.cpp | file | annotate | diff | comparison | revisions |
--- a/src/array_list.c Sat Apr 22 14:09:46 2023 +0200 +++ b/src/array_list.c Sat Apr 22 14:21:02 2023 +0200 @@ -345,21 +345,22 @@ } } -static size_t cx_arl_find( +static ssize_t cx_arl_find( struct cx_list_s const *list, void const *elem ) { assert(list->cmpfunc != NULL); + assert(list->size < SIZE_MAX / 2); char *cur = ((cx_array_list const *) list)->data; - for (size_t i = 0; i < list->size; i++) { + for (ssize_t i = 0; i < (ssize_t) list->size; i++) { if (0 == list->cmpfunc(elem, cur)) { return i; } cur += list->item_size; } - return list->size; + return -1; } static void cx_arl_sort(struct cx_list_s *list) {
--- a/src/cx/linked_list.h Sat Apr 22 14:09:46 2023 +0200 +++ b/src/cx/linked_list.h Sat Apr 22 14:21:02 2023 +0200 @@ -118,9 +118,9 @@ * @param loc_data the location of the \c data pointer within your node struct * @param cmp_func a compare function to compare \p elem against the node data * @param elem a pointer to the element to find - * @return the index of the element or a past-one index if the element could not be found + * @return the index of the element or a negative value if it could not be found */ -size_t cx_linked_list_find( +ssize_t cx_linked_list_find( void const *start, ptrdiff_t loc_advance, ptrdiff_t loc_data,
--- a/src/cx/list.h Sat Apr 22 14:09:46 2023 +0200 +++ b/src/cx/list.h Sat Apr 22 14:21:02 2023 +0200 @@ -136,7 +136,7 @@ /** * Member function for finding an element. */ - size_t (*find)( + ssize_t (*find)( struct cx_list_s const *list, void const *elem ); @@ -569,10 +569,11 @@ * * @param list the list * @param elem the element to find - * @return the index of the element or \c size if the element is not found + * @return the index of the element or a negative + * value when the element is not found */ __attribute__((__nonnull__)) -static inline size_t cxListFind( +static inline ssize_t cxListFind( CxList const *list, void const *elem ) {
--- a/src/linked_list.c Sat Apr 22 14:09:46 2023 +0200 +++ b/src/linked_list.c Sat Apr 22 14:21:02 2023 +0200 @@ -56,7 +56,7 @@ return (void *) cur; } -size_t cx_linked_list_find( +ssize_t cx_linked_list_find( void const *start, ptrdiff_t loc_advance, ptrdiff_t loc_data, @@ -69,7 +69,7 @@ assert(cmp_func); void const *node = start; - size_t index = 0; + ssize_t index = 0; do { void *current = ll_data(node); if (cmp_func(current, elem) == 0) { @@ -78,7 +78,7 @@ node = ll_advance(node); index++; } while (node != NULL); - return index; + return -1; } void *cx_linked_list_first( @@ -729,7 +729,7 @@ return node == NULL ? NULL : node->payload; } -static size_t cx_ll_find( +static ssize_t cx_ll_find( struct cx_list_s const *list, void const *elem ) {
--- a/src/list.c Sat Apr 22 14:09:46 2023 +0200 +++ b/src/list.c Sat Apr 22 14:21:02 2023 +0200 @@ -115,12 +115,12 @@ return ptr == NULL ? NULL : *ptr; } -static size_t cx_pl_find( +static ssize_t cx_pl_find( struct cx_list_s const *list, void const *elem ) { cx_pl_hack_cmpfunc(list); - size_t ret = list->climpl->find(list, &elem); + ssize_t ret = list->climpl->find(list, &elem); cx_pl_unhack_cmpfunc(list); return ret; }
--- a/tests/test_list.cpp Sat Apr 22 14:09:46 2023 +0200 +++ b/tests/test_list.cpp Sat Apr 22 14:21:02 2023 +0200 @@ -177,9 +177,9 @@ s = 8; EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 3); s = 10; - EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4); + EXPECT_LT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0); s = -2; - EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4); + EXPECT_LT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0); } TEST(LinkedList_LowLevel, cx_linked_list_compare) { @@ -837,7 +837,7 @@ } int notinlist = -1; - EXPECT_EQ(cxListSize(list), cxListFind(list, ¬inlist)); + EXPECT_LT(cxListFind(list, ¬inlist), 0); } void verifySort(CxList *list) const {