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 |
1.1 --- a/src/array_list.c Sat Apr 22 14:09:46 2023 +0200 1.2 +++ b/src/array_list.c Sat Apr 22 14:21:02 2023 +0200 1.3 @@ -345,21 +345,22 @@ 1.4 } 1.5 } 1.6 1.7 -static size_t cx_arl_find( 1.8 +static ssize_t cx_arl_find( 1.9 struct cx_list_s const *list, 1.10 void const *elem 1.11 ) { 1.12 assert(list->cmpfunc != NULL); 1.13 + assert(list->size < SIZE_MAX / 2); 1.14 char *cur = ((cx_array_list const *) list)->data; 1.15 1.16 - for (size_t i = 0; i < list->size; i++) { 1.17 + for (ssize_t i = 0; i < (ssize_t) list->size; i++) { 1.18 if (0 == list->cmpfunc(elem, cur)) { 1.19 return i; 1.20 } 1.21 cur += list->item_size; 1.22 } 1.23 1.24 - return list->size; 1.25 + return -1; 1.26 } 1.27 1.28 static void cx_arl_sort(struct cx_list_s *list) {
2.1 --- a/src/cx/linked_list.h Sat Apr 22 14:09:46 2023 +0200 2.2 +++ b/src/cx/linked_list.h Sat Apr 22 14:21:02 2023 +0200 2.3 @@ -118,9 +118,9 @@ 2.4 * @param loc_data the location of the \c data pointer within your node struct 2.5 * @param cmp_func a compare function to compare \p elem against the node data 2.6 * @param elem a pointer to the element to find 2.7 - * @return the index of the element or a past-one index if the element could not be found 2.8 + * @return the index of the element or a negative value if it could not be found 2.9 */ 2.10 -size_t cx_linked_list_find( 2.11 +ssize_t cx_linked_list_find( 2.12 void const *start, 2.13 ptrdiff_t loc_advance, 2.14 ptrdiff_t loc_data,
3.1 --- a/src/cx/list.h Sat Apr 22 14:09:46 2023 +0200 3.2 +++ b/src/cx/list.h Sat Apr 22 14:21:02 2023 +0200 3.3 @@ -136,7 +136,7 @@ 3.4 /** 3.5 * Member function for finding an element. 3.6 */ 3.7 - size_t (*find)( 3.8 + ssize_t (*find)( 3.9 struct cx_list_s const *list, 3.10 void const *elem 3.11 ); 3.12 @@ -569,10 +569,11 @@ 3.13 * 3.14 * @param list the list 3.15 * @param elem the element to find 3.16 - * @return the index of the element or \c size if the element is not found 3.17 + * @return the index of the element or a negative 3.18 + * value when the element is not found 3.19 */ 3.20 __attribute__((__nonnull__)) 3.21 -static inline size_t cxListFind( 3.22 +static inline ssize_t cxListFind( 3.23 CxList const *list, 3.24 void const *elem 3.25 ) {
4.1 --- a/src/linked_list.c Sat Apr 22 14:09:46 2023 +0200 4.2 +++ b/src/linked_list.c Sat Apr 22 14:21:02 2023 +0200 4.3 @@ -56,7 +56,7 @@ 4.4 return (void *) cur; 4.5 } 4.6 4.7 -size_t cx_linked_list_find( 4.8 +ssize_t cx_linked_list_find( 4.9 void const *start, 4.10 ptrdiff_t loc_advance, 4.11 ptrdiff_t loc_data, 4.12 @@ -69,7 +69,7 @@ 4.13 assert(cmp_func); 4.14 4.15 void const *node = start; 4.16 - size_t index = 0; 4.17 + ssize_t index = 0; 4.18 do { 4.19 void *current = ll_data(node); 4.20 if (cmp_func(current, elem) == 0) { 4.21 @@ -78,7 +78,7 @@ 4.22 node = ll_advance(node); 4.23 index++; 4.24 } while (node != NULL); 4.25 - return index; 4.26 + return -1; 4.27 } 4.28 4.29 void *cx_linked_list_first( 4.30 @@ -729,7 +729,7 @@ 4.31 return node == NULL ? NULL : node->payload; 4.32 } 4.33 4.34 -static size_t cx_ll_find( 4.35 +static ssize_t cx_ll_find( 4.36 struct cx_list_s const *list, 4.37 void const *elem 4.38 ) {
5.1 --- a/src/list.c Sat Apr 22 14:09:46 2023 +0200 5.2 +++ b/src/list.c Sat Apr 22 14:21:02 2023 +0200 5.3 @@ -115,12 +115,12 @@ 5.4 return ptr == NULL ? NULL : *ptr; 5.5 } 5.6 5.7 -static size_t cx_pl_find( 5.8 +static ssize_t cx_pl_find( 5.9 struct cx_list_s const *list, 5.10 void const *elem 5.11 ) { 5.12 cx_pl_hack_cmpfunc(list); 5.13 - size_t ret = list->climpl->find(list, &elem); 5.14 + ssize_t ret = list->climpl->find(list, &elem); 5.15 cx_pl_unhack_cmpfunc(list); 5.16 return ret; 5.17 }
6.1 --- a/tests/test_list.cpp Sat Apr 22 14:09:46 2023 +0200 6.2 +++ b/tests/test_list.cpp Sat Apr 22 14:21:02 2023 +0200 6.3 @@ -177,9 +177,9 @@ 6.4 s = 8; 6.5 EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 3); 6.6 s = 10; 6.7 - EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4); 6.8 + EXPECT_LT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0); 6.9 s = -2; 6.10 - EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 4); 6.11 + EXPECT_LT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0); 6.12 } 6.13 6.14 TEST(LinkedList_LowLevel, cx_linked_list_compare) { 6.15 @@ -837,7 +837,7 @@ 6.16 } 6.17 6.18 int notinlist = -1; 6.19 - EXPECT_EQ(cxListSize(list), cxListFind(list, ¬inlist)); 6.20 + EXPECT_LT(cxListFind(list, ¬inlist), 0); 6.21 } 6.22 6.23 void verifySort(CxList *list) const {