ucx/cx/list.h

changeset 440
7c4b9cba09ca
parent 324
ce13a778654a
--- a/ucx/cx/list.h	Sun Jan 05 17:41:39 2025 +0100
+++ b/ucx/cx/list.h	Sun Jan 05 22:00:39 2025 +0100
@@ -26,11 +26,11 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 /**
- * \file list.h
- * \brief Interface for list implementations.
- * \author Mike Becker
- * \author Olaf Wintermann
- * \copyright 2-Clause BSD License
+ * @file list.h
+ * @brief Interface for list implementations.
+ * @author Mike Becker
+ * @author Olaf Wintermann
+ * @copyright 2-Clause BSD License
  */
 
 #ifndef UCX_LIST_H
@@ -52,6 +52,9 @@
  * Structure for holding the base data of a list.
  */
 struct cx_list_s {
+    /**
+     * Common members for collections.
+     */
     CX_COLLECTION_BASE;
     /**
      * The list class definition.
@@ -71,9 +74,9 @@
      * Destructor function.
      *
      * Implementations SHALL invoke the content destructor functions if provided
-     * and SHALL deallocate the list memory.
+     * and SHALL deallocate the entire list memory.
      */
-    void (*destructor)(struct cx_list_s *list);
+    void (*deallocate)(struct cx_list_s *list);
 
     /**
      * Member function for inserting a single element.
@@ -88,6 +91,7 @@
     /**
      * Member function for inserting multiple elements.
      * Implementors SHOULD see to performant implementations for corner cases.
+     *
      * @see cx_list_default_insert_array()
      */
     size_t (*insert_array)(
@@ -118,11 +122,20 @@
     );
 
     /**
-     * Member function for removing an element.
+     * Member function for removing elements.
+     *
+     * Implementations SHALL check if @p targetbuf is set and copy the elements
+     * to the buffer without invoking any destructor.
+     * When @p targetbuf is not set, the destructors SHALL be invoked.
+     *
+     * The function SHALL return the actual number of elements removed, which
+     * might be lower than @p num when going out of bounds.
      */
-    int (*remove)(
+    size_t (*remove)(
             struct cx_list_s *list,
-            size_t index
+            size_t index,
+            size_t num,
+            void *targetbuf
     );
 
     /**
@@ -132,6 +145,7 @@
 
     /**
      * Member function for swapping two elements.
+     *
      * @see cx_list_default_swap()
      */
     int (*swap)(
@@ -158,7 +172,8 @@
     );
 
     /**
-     * Member function for sorting the list in-place.
+     * Member function for sorting the list.
+     *
      * @see cx_list_default_sort()
      */
     void (*sort)(struct cx_list_s *list);
@@ -166,8 +181,9 @@
     /**
      * Optional member function for comparing this list
      * to another list of the same type.
-     * If set to \c NULL, comparison won't be optimized.
+     * If set to @c NULL, comparison won't be optimized.
      */
+    cx_attr_nonnull
     int (*compare)(
             const struct cx_list_s *list,
             const struct cx_list_s *other
@@ -202,7 +218,7 @@
  * @param n the number of elements to insert
  * @return the number of elements actually inserted
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 size_t cx_list_default_insert_array(
         struct cx_list_s *list,
         size_t index,
@@ -216,7 +232,7 @@
  * This function uses the array insert function to insert consecutive groups
  * of sorted data.
  *
- * The source data \em must already be sorted wrt. the list's compare function.
+ * The source data @em must already be sorted wrt. the list's compare function.
  *
  * Use this in your own list class if you do not want to implement an optimized
  * version for your list.
@@ -226,7 +242,7 @@
  * @param n the number of elements to insert
  * @return the number of elements actually inserted
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 size_t cx_list_default_insert_sorted(
         struct cx_list_s *list,
         const void *sorted_data,
@@ -244,7 +260,7 @@
  *
  * @param list the list that shall be sorted
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 void cx_list_default_sort(struct cx_list_s *list);
 
 /**
@@ -256,10 +272,11 @@
  * @param list the list in which to swap
  * @param i index of one element
  * @param j index of the other element
- * @return zero on success, non-zero when indices are out of bounds or memory
+ * @retval zero success
+ * @retval non-zero when indices are out of bounds or memory
  * allocation for the temporary buffer fails
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j);
 
 /**
@@ -276,7 +293,7 @@
  * @param list the list
  * @see cxListStorePointers()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 void cxListStoreObjects(CxList *list);
 
 /**
@@ -291,7 +308,7 @@
  * @param list the list
  * @see cxListStoreObjects()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 void cxListStorePointers(CxList *list);
 
 /**
@@ -301,7 +318,7 @@
  * @return true, if this list is storing pointers
  * @see cxListStorePointers()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline bool cxListIsStoringPointers(const CxList *list) {
     return list->collection.store_pointer;
 }
@@ -312,7 +329,7 @@
  * @param list the list
  * @return the number of currently stored elements
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline size_t cxListSize(const CxList *list) {
     return list->collection.size;
 }
@@ -322,10 +339,11 @@
  *
  * @param list the list
  * @param elem a pointer to the element to add
- * @return zero on success, non-zero on memory allocation failure
+ * @retval zero success
+ * @retval non-zero memory allocation failure
  * @see cxListAddArray()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline int cxListAdd(
         CxList *list,
         const void *elem
@@ -339,9 +357,9 @@
  * 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.
+ * less than @p n.
  *
- * If this list is storing pointers instead of objects \p array is expected to
+ * If this list is storing pointers instead of objects @p array is expected to
  * be an array of pointers.
  *
  * @param list the list
@@ -349,7 +367,7 @@
  * @param n the number of elements to add
  * @return the number of added elements
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline size_t cxListAddArray(
         CxList *list,
         const void *array,
@@ -361,17 +379,17 @@
 /**
  * Inserts an item at the specified index.
  *
- * If \p index equals the list \c size, this is effectively cxListAdd().
+ * If @p index equals the list @c size, this is effectively cxListAdd().
  *
  * @param list the list
  * @param index the index the element shall have
  * @param elem a pointer to the element to add
- * @return zero on success, non-zero on memory allocation failure
- * or when the index is out of bounds
+ * @retval zero success
+ * @retval non-zero memory allocation failure or the index is out of bounds
  * @see cxListInsertAfter()
  * @see cxListInsertBefore()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline int cxListInsert(
         CxList *list,
         size_t index,
@@ -385,9 +403,10 @@
  *
  * @param list the list
  * @param elem a pointer to the element to add
- * @return zero on success, non-zero on memory allocation failure
+ * @retval zero success
+ * @retval non-zero memory allocation failure
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline int cxListInsertSorted(
         CxList *list,
         const void *elem
@@ -398,15 +417,15 @@
 
 /**
  * Inserts multiple items to the list at the specified index.
- * If \p index equals the list size, this is effectively cxListAddArray().
+ * If @p index equals the list size, this is effectively cxListAddArray().
  *
  * This method is usually more efficient than invoking cxListInsert()
  * multiple times.
  *
  * If there is not enough memory to add all elements, the returned value is
- * less than \p n.
+ * less than @p n.
  *
- * If this list is storing pointers instead of objects \p array is expected to
+ * If this list is storing pointers instead of objects @p array is expected to
  * be an array of pointers.
  *
  * @param list the list
@@ -415,7 +434,7 @@
  * @param n the number of elements to add
  * @return the number of added elements
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline size_t cxListInsertArray(
         CxList *list,
         size_t index,
@@ -432,9 +451,9 @@
  * because consecutive chunks of sorted data are inserted in one pass.
  *
  * If there is not enough memory to add all elements, the returned value is
- * less than \p n.
+ * less than @p n.
  *
- * If this list is storing pointers instead of objects \p array is expected to
+ * If this list is storing pointers instead of objects @p array is expected to
  * be an array of pointers.
  *
  * @param list the list
@@ -442,7 +461,7 @@
  * @param n the number of elements to add
  * @return the number of added elements
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline size_t cxListInsertSortedArray(
         CxList *list,
         const void *array,
@@ -457,16 +476,17 @@
  * The used iterator remains operational, but all other active iterators should
  * be considered invalidated.
  *
- * If \p iter is not a list iterator, the behavior is undefined.
- * If \p iter is a past-the-end iterator, the new element gets appended to the list.
+ * If @p iter is not a list iterator, the behavior is undefined.
+ * If @p iter is a past-the-end iterator, the new element gets appended to the list.
  *
  * @param iter an iterator
  * @param elem the element to insert
- * @return zero on success, non-zero on memory allocation failure
+ * @retval zero success
+ * @retval non-zero memory allocation failure
  * @see cxListInsert()
  * @see cxListInsertBefore()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline int cxListInsertAfter(
         CxIterator *iter,
         const void *elem
@@ -480,16 +500,17 @@
  * The used iterator remains operational, but all other active iterators should
  * be considered invalidated.
  *
- * If \p iter is not a list iterator, the behavior is undefined.
- * If \p iter is a past-the-end iterator, the new element gets appended to the list.
+ * If @p iter is not a list iterator, the behavior is undefined.
+ * If @p iter is a past-the-end iterator, the new element gets appended to the list.
  *
  * @param iter an iterator
  * @param elem the element to insert
- * @return zero on success, non-zero on memory allocation failure
+ * @retval zero success
+ * @retval non-zero memory allocation failure
  * @see cxListInsert()
  * @see cxListInsertAfter()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline int cxListInsertBefore(
         CxIterator *iter,
         const void *elem
@@ -505,25 +526,95 @@
  *
  * @param list the list
  * @param index the index of the element
- * @return zero on success, non-zero if the index is out of bounds
+ * @retval zero success
+ * @retval non-zero index out of bounds
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline int cxListRemove(
         CxList *list,
         size_t index
 ) {
-    return list->cl->remove(list, index);
+    return list->cl->remove(list, index, 1, NULL) == 0;
+}
+
+/**
+ * Removes and returns the element at the specified index.
+ *
+ * No destructor is called and instead the element is copied to the
+ * @p targetbuf which MUST be large enough to hold the removed element.
+ *
+ * @param list the list
+ * @param index the index of the element
+ * @param targetbuf a buffer where to copy the element
+ * @retval zero success
+ * @retval non-zero index out of bounds
+ */
+cx_attr_nonnull
+cx_attr_access_w(3)
+static inline int cxListRemoveAndGet(
+        CxList *list,
+        size_t index,
+        void *targetbuf
+) {
+    return list->cl->remove(list, index, 1, targetbuf) == 0;
+}
+
+/**
+ * Removes multiple element starting at the specified index.
+ *
+ * If an element destructor function is specified, it is called for each
+ * element. It is guaranteed that the destructor is called before removing
+ * the element, however, due to possible optimizations it is neither guaranteed
+ * that the destructors are invoked for all elements before starting to remove
+ * them, nor that the element is removed immediately after the destructor call
+ * before proceeding to the next element.
+ *
+ * @param list the list
+ * @param index the index of the element
+ * @param num the number of elements to remove
+ * @return the actual number of removed elements
+ */
+cx_attr_nonnull
+static inline size_t cxListRemoveArray(
+        CxList *list,
+        size_t index,
+        size_t num
+) {
+    return list->cl->remove(list, index, num, NULL);
+}
+
+/**
+ * Removes and returns multiple element starting at the specified index.
+ *
+ * No destructor is called and instead the elements are copied to the
+ * @p targetbuf which MUST be large enough to hold all removed elements.
+ *
+ * @param list the list
+ * @param index the index of the element
+ * @param num the number of elements to remove
+ * @param targetbuf a buffer where to copy the elements
+ * @return the actual number of removed elements
+ */
+cx_attr_nonnull
+cx_attr_access_w(4)
+static inline size_t cxListRemoveArrayAndGet(
+        CxList *list,
+        size_t index,
+        size_t num,
+        void *targetbuf
+) {
+    return list->cl->remove(list, index, num, targetbuf);
 }
 
 /**
  * Removes all elements from this list.
  *
- * If an element destructor function is specified, it is called for each
+ * If element destructor functions are specified, they are called for each
  * element before removing them.
  *
  * @param list the list
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline void cxListClear(CxList *list) {
     list->cl->clear(list);
 }
@@ -537,9 +628,11 @@
  * @param list the list
  * @param i the index of the first element
  * @param j the index of the second element
- * @return zero on success, non-zero if one of the indices is out of bounds
+ * @retval zero success
+ * @retval non-zero one of the indices is out of bounds
+ * or the swap needed extra memory but allocation failed
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline int cxListSwap(
         CxList *list,
         size_t i,
@@ -553,11 +646,11 @@
  *
  * @param list the list
  * @param index the index of the element
- * @return a pointer to the element or \c NULL if the index is out of bounds
+ * @return a pointer to the element or @c NULL if the index is out of bounds
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline void *cxListAt(
-        CxList *list,
+        const CxList *list,
         size_t index
 ) {
     return list->cl->at(list, index);
@@ -574,7 +667,8 @@
  * @param index the index where the iterator shall point at
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 static inline CxIterator cxListIteratorAt(
         const CxList *list,
         size_t index
@@ -593,7 +687,8 @@
  * @param index the index where the iterator shall point at
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 static inline CxIterator cxListBackwardsIteratorAt(
         const CxList *list,
         size_t index
@@ -612,7 +707,8 @@
  * @param index the index where the iterator shall point at
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 CxIterator cxListMutIteratorAt(
         CxList *list,
         size_t index
@@ -630,7 +726,8 @@
  * @param index the index where the iterator shall point at
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 CxIterator cxListMutBackwardsIteratorAt(
         CxList *list,
         size_t index
@@ -646,7 +743,8 @@
  * @param list the list
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 static inline CxIterator cxListIterator(const CxList *list) {
     return list->cl->iterator(list, 0, false);
 }
@@ -661,7 +759,8 @@
  * @param list the list
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 static inline CxIterator cxListMutIterator(CxList *list) {
     return cxListMutIteratorAt(list, 0);
 }
@@ -677,7 +776,8 @@
  * @param list the list
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 static inline CxIterator cxListBackwardsIterator(const CxList *list) {
     return list->cl->iterator(list, list->collection.size - 1, true);
 }
@@ -692,13 +792,14 @@
  * @param list the list
  * @return a new iterator
  */
-__attribute__((__nonnull__, __warn_unused_result__))
+cx_attr_nonnull
+cx_attr_nodiscard
 static inline CxIterator cxListMutBackwardsIterator(CxList *list) {
     return cxListMutBackwardsIteratorAt(list, list->collection.size - 1);
 }
 
 /**
- * Returns the index of the first element that equals \p elem.
+ * Returns the index of the first element that equals @p elem.
  *
  * Determining equality is performed by the list's comparator function.
  *
@@ -707,7 +808,8 @@
  * @return the index of the element or a negative
  * value when the element is not found
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
+cx_attr_nodiscard
 static inline ssize_t cxListFind(
         const CxList *list,
         const void *elem
@@ -716,7 +818,7 @@
 }
 
 /**
- * Removes and returns the index of the first element that equals \p elem.
+ * Removes and returns the index of the first element that equals @p elem.
  *
  * Determining equality is performed by the list's comparator function.
  *
@@ -725,7 +827,7 @@
  * @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__))
+cx_attr_nonnull
 static inline ssize_t cxListFindRemove(
         CxList *list,
         const void *elem
@@ -734,13 +836,13 @@
 }
 
 /**
- * Sorts the list in-place.
+ * Sorts the list.
  *
- * \remark The underlying sort algorithm is implementation defined.
+ * @remark The underlying sort algorithm is implementation defined.
  *
  * @param list the list
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline void cxListSort(CxList *list) {
     list->cl->sort(list);
 }
@@ -750,7 +852,7 @@
  *
  * @param list the list
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 static inline void cxListReverse(CxList *list) {
     list->cl->reverse(list);
 }
@@ -763,10 +865,14 @@
  *
  * @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, positive if the first list is larger
+ * @retval zero both lists are equal element wise
+ * @retval negative the first list is smaller
+ * or the first non-equal element in the first list is smaller
+ * @retval positive the first list is larger
+ * or the first non-equal element in the first list is larger
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
+cx_attr_nodiscard
 int cxListCompare(
         const CxList *list,
         const CxList *other
@@ -775,22 +881,21 @@
 /**
  * Deallocates the memory of the specified list structure.
  *
- * Also calls content a destructor function, depending on the configuration
- * in CxList.content_destructor_type.
- *
- * This function itself is a destructor function for the CxList.
+ * Also calls the content destructor functions for each element, if specified.
  *
- * @param list the list which shall be destroyed
+ * @param list the list which shall be freed
  */
-__attribute__((__nonnull__))
-void cxListDestroy(CxList *list);
+void cxListFree(CxList *list);
 
 /**
  * A shared instance of an empty list.
  *
- * Writing to that list is undefined.
+ * Writing to that list is not allowed.
+ *
+ * You can use this is a placeholder for initializing CxList pointers
+ * for which you do not want to reserve memory right from the beginning.
  */
-extern CxList * const cxEmptyList;
+extern CxList *const cxEmptyList;
 
 
 #ifdef __cplusplus

mercurial