diff -r 545010bc5e71 -r 22eca559aded src/ucx/cx/list.h --- a/src/ucx/cx/list.h Sun Nov 20 12:43:44 2022 +0100 +++ b/src/ucx/cx/list.h Sat Nov 26 17:07:08 2022 +0100 @@ -130,6 +130,15 @@ ); /** + * Member function for adding multiple elements. + */ + size_t (*add_array)( + struct cx_list_s *list, + void const *array, + size_t n + ); + + /** * Member function for inserting an element. */ int (*insert)( @@ -142,7 +151,7 @@ * Member function for inserting an element relative to an iterator position. */ int (*insert_iter)( - struct cx_iterator_s *iter, + struct cx_mut_iterator_s *iter, void const *elem, int prepend ); @@ -193,6 +202,14 @@ * Returns an iterator pointing to the specified index. */ struct cx_iterator_s (*iterator)( + struct cx_list_s const *list, + size_t index + ); + + /** + * Returns a mutating iterator pointing to the specified index. + */ + struct cx_mut_iterator_s (*mut_iterator)( struct cx_list_s *list, size_t index ); @@ -209,6 +226,7 @@ * @param list the list * @param elem a pointer to the element to add * @return zero on success, non-zero on memory allocation failure + * @see cxListAddArray() */ __attribute__((__nonnull__)) static inline int cxListAdd( @@ -219,6 +237,28 @@ } /** + * Adds multiple items to the end of the list. + * + * This method is more efficient than invoking cxListAdd() multiple times. + * + * If there is not enough memory to add all elements, the returned value is + * less than \p n. + * + * @param list the list + * @param array a pointer to the elements to add + * @param n the number of elements to add + * @return the number of added elements + */ +__attribute__((__nonnull__)) +static inline size_t cxListAddArray( + CxList *list, + void const *array, + size_t n +) { + return list->cl->add_array(list, array, n); +} + +/** * Inserts an item at the specified index. * * If \p index equals the list \c size, this is effectively cxListAdd(). @@ -257,7 +297,7 @@ */ __attribute__((__nonnull__)) static inline int cxListInsertAfter( - CxIterator *iter, + CxMutIterator *iter, void const *elem ) { return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0); @@ -280,7 +320,7 @@ */ __attribute__((__nonnull__)) static inline int cxListInsertBefore( - CxIterator *iter, + CxMutIterator *iter, void const *elem ) { return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); @@ -328,10 +368,29 @@ */ __attribute__((__nonnull__, __warn_unused_result__)) static inline CxIterator cxListIterator( + CxList const *list, + size_t index +) { + return list->cl->iterator(list, index); +} + +/** + * Returns a mutating iterator pointing to the item at the specified index. + * + * The returned iterator is position-aware. + * + * If the index is out of range, a past-the-end iterator will be returned. + * + * @param list the list + * @param index the index where the iterator shall point at + * @return a new iterator + */ +__attribute__((__nonnull__, __warn_unused_result__)) +static inline CxMutIterator cxListMutIterator( CxList *list, size_t index ) { - return list->cl->iterator(list, index); + return list->cl->mut_iterator(list, index); } /** @@ -345,11 +404,26 @@ * @return a new iterator */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline CxIterator cxListBegin(CxList *list) { +static inline CxIterator cxListBegin(CxList const *list) { return list->cl->iterator(list, 0); } /** + * Returns a mutating iterator pointing to the first item of the list. + * + * The returned iterator is position-aware. + * + * If the list is empty, a past-the-end iterator will be returned. + * + * @param list the list + * @return a new iterator + */ +__attribute__((__nonnull__, __warn_unused_result__)) +static inline CxMutIterator cxListBeginMut(CxList *list) { + return list->cl->mut_iterator(list, 0); +} + +/** * Returns the index of the first element that equals \p elem. * * Determining equality is performed by the list's comparator function. @@ -360,7 +434,7 @@ */ __attribute__((__nonnull__)) static inline size_t cxListFind( - CxList *list, + CxList const *list, void const *elem ) { return list->cl->find(list, elem); @@ -391,19 +465,19 @@ /** * Compares a list to another list of the same type. * - * First, the list sizes are compared. If they match, the lists are compared element-wise. + * First, the list sizes are compared. + * If they match, the lists are compared element-wise. * * @param list the list * @param other the list to compare to - * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger + * @return zero, if both lists are equal element wise, + * negative if the first list is smaller, positive if the first list is larger */ __attribute__((__nonnull__)) -static inline int cxListCompare( - CxList *list, - CxList *other -) { - return list->cl->compare(list, other); -} +int cxListCompare( + CxList const *list, + CxList const *other +); /** * Deallocates the memory of the specified list structure. @@ -419,7 +493,7 @@ void cxListDestroy(CxList *list); #ifdef __cplusplus -} /* extern "C" */ +} // extern "C" #endif -#endif /* UCX_LIST_H */ +#endif // UCX_LIST_H