diff -r 22257f6d06a3 -r bc782cca0759 ucx/cx/list.h --- a/ucx/cx/list.h Thu May 23 23:19:06 2024 +0200 +++ b/ucx/cx/list.h Thu May 23 23:23:36 2024 +0200 @@ -30,7 +30,6 @@ * \brief Interface for list implementations. * \author Mike Becker * \author Olaf Wintermann - * \version 3.0 * \copyright 2-Clause BSD License */ @@ -53,7 +52,7 @@ * Structure for holding the base data of a list. */ struct cx_list_s { - CX_COLLECTION_MEMBERS + CX_COLLECTION_BASE; /** * The list class definition. */ @@ -101,7 +100,7 @@ * Member function for inserting an element relative to an iterator position. */ int (*insert_iter)( - struct cx_mut_iterator_s *iter, + struct cx_iterator_s *iter, void const *elem, int prepend ); @@ -137,11 +136,12 @@ ); /** - * Member function for finding an element. + * Member function for finding and optionally removing an element. */ - ssize_t (*find)( - struct cx_list_s const *list, - void const *elem + ssize_t (*find_remove)( + struct cx_list_s *list, + void const *elem, + bool remove ); /** @@ -213,7 +213,7 @@ */ __attribute__((__nonnull__)) static inline bool cxListIsStoringPointers(CxList const *list) { - return list->store_pointer; + return list->collection.store_pointer; } /** @@ -224,7 +224,7 @@ */ __attribute__((__nonnull__)) static inline size_t cxListSize(CxList const *list) { - return list->size; + return list->collection.size; } /** @@ -240,7 +240,7 @@ CxList *list, void const *elem ) { - return list->cl->insert_element(list, list->size, elem); + return list->cl->insert_element(list, list->collection.size, elem); } /** @@ -265,7 +265,7 @@ void const *array, size_t n ) { - return list->cl->insert_array(list, list->size, array, n); + return list->cl->insert_array(list, list->collection.size, array, n); } /** @@ -336,10 +336,10 @@ */ __attribute__((__nonnull__)) static inline int cxListInsertAfter( - CxMutIterator *iter, + CxIterator *iter, void const *elem ) { - return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0); + return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 0); } /** @@ -359,10 +359,10 @@ */ __attribute__((__nonnull__)) static inline int cxListInsertBefore( - CxMutIterator *iter, + CxIterator *iter, void const *elem ) { - return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); + return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 1); } /** @@ -481,7 +481,7 @@ * @return a new iterator */ __attribute__((__nonnull__, __warn_unused_result__)) -CxMutIterator cxListMutIteratorAt( +CxIterator cxListMutIteratorAt( CxList *list, size_t index ); @@ -499,7 +499,7 @@ * @return a new iterator */ __attribute__((__nonnull__, __warn_unused_result__)) -CxMutIterator cxListMutBackwardsIteratorAt( +CxIterator cxListMutBackwardsIteratorAt( CxList *list, size_t index ); @@ -530,7 +530,7 @@ * @return a new iterator */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxMutIterator cxListMutIterator(CxList *list) { +static inline CxIterator cxListMutIterator(CxList *list) { return cxListMutIteratorAt(list, 0); } @@ -547,7 +547,7 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline CxIterator cxListBackwardsIterator(CxList const *list) { - return list->cl->iterator(list, list->size - 1, true); + return list->cl->iterator(list, list->collection.size - 1, true); } /** @@ -561,8 +561,8 @@ * @return a new iterator */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) { - return cxListMutBackwardsIteratorAt(list, list->size - 1); +static inline CxIterator cxListMutBackwardsIterator(CxList *list) { + return cxListMutBackwardsIteratorAt(list, list->collection.size - 1); } /** @@ -580,7 +580,25 @@ CxList const *list, void const *elem ) { - return list->cl->find(list, elem); + return list->cl->find_remove((CxList*)list, elem, false); +} + +/** + * Removes and returns the index of the first element that equals \p elem. + * + * Determining equality is performed by the list's comparator function. + * + * @param list the list + * @param elem the element to find and remove + * @return the index of the now removed element or a negative + * value when the element is not found or could not be removed + */ +__attribute__((__nonnull__)) +static inline ssize_t cxListFindRemove( + CxList *list, + void const *elem +) { + return list->cl->find_remove(list, elem, true); } /**