ucx/cx/iterator.h

changeset 101
7b3a3130be44
parent 49
2f71f4ee247a
--- a/ucx/cx/iterator.h	Thu Dec 12 20:01:43 2024 +0100
+++ b/ucx/cx/iterator.h	Mon Jan 06 22:22:55 2025 +0100
@@ -26,11 +26,11 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 /**
- * \file iterator.h
- * \brief Interface for iterator implementations.
- * \author Mike Becker
- * \author Olaf Wintermann
- * \copyright 2-Clause BSD License
+ * @file iterator.h
+ * @brief Interface for iterator implementations.
+ * @author Mike Becker
+ * @author Olaf Wintermann
+ * @copyright 2-Clause BSD License
  */
 
 #ifndef UCX_ITERATOR_H
@@ -38,11 +38,18 @@
 
 #include "common.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Common data for all iterators.
+ */
 struct cx_iterator_base_s {
     /**
      * True iff the iterator points to valid data.
      */
-    __attribute__ ((__nonnull__))
+    cx_attr_nonnull
     bool (*valid)(const void *);
 
     /**
@@ -50,13 +57,15 @@
      *
      * When valid returns false, the behavior of this function is undefined.
      */
-    __attribute__ ((__nonnull__))
+    cx_attr_nonnull
+    cx_attr_nodiscard
     void *(*current)(const void *);
 
     /**
      * Original implementation in case the function needs to be wrapped.
      */
-    __attribute__ ((__nonnull__))
+    cx_attr_nonnull
+    cx_attr_nodiscard
     void *(*current_impl)(const void *);
 
     /**
@@ -64,7 +73,7 @@
      *
      * When valid returns false, the behavior of this function is undefined.
      */
-    __attribute__ ((__nonnull__))
+    cx_attr_nonnull
     void (*next)(void *);
     /**
      * Indicates whether this iterator may remove elements.
@@ -86,6 +95,9 @@
  * Internal iterator struct - use CxIterator.
  */
 struct cx_iterator_s {
+    /**
+     * Inherited common data for all iterators.
+     */
     CX_ITERATOR_BASE;
 
     /**
@@ -141,7 +153,7 @@
 
     /**
      * May contain the total number of elements, if known.
-     * Shall be set to \c SIZE_MAX when the total number is unknown during iteration.
+     * Shall be set to @c SIZE_MAX when the total number is unknown during iteration.
      */
     size_t elem_count;
 };
@@ -154,7 +166,7 @@
  * to be "position-aware", which means that they keep track of the current index within the collection.
  *
  * @note Objects that are pointed to by an iterator are always mutable through that iterator. However,
- * any concurrent mutation of the collection other than by this iterator makes this iterator invalid
+ * any concurrent mutation of the collection other than by this iterator makes this iterator invalid,
  * and it must not be used anymore.
  */
 typedef struct cx_iterator_s CxIterator;
@@ -165,7 +177,8 @@
  * This is especially false for past-the-end iterators.
  *
  * @param iter the iterator
- * @return true iff the iterator points to valid data
+ * @retval true if the iterator points to valid data
+ * @retval false if the iterator already moved past the end
  */
 #define cxIteratorValid(iter) (iter).base.valid(&(iter))
 
@@ -176,6 +189,7 @@
  *
  * @param iter the iterator
  * @return a pointer to the current element
+ * @see cxIteratorValid()
  */
 #define cxIteratorCurrent(iter) (iter).base.current(&iter)
 
@@ -189,6 +203,8 @@
 /**
  * Flags the current element for removal, if this iterator is mutating.
  *
+ * Does nothing for non-mutating iterators.
+ *
  * @param iter the iterator
  */
 #define cxIteratorFlagRemoval(iter) (iter).base.remove |= (iter).base.mutating
@@ -199,11 +215,13 @@
  * This is useful for APIs that expect some iterator as an argument.
  *
  * @param iter the iterator
+ * @return (@c CxIterator*) a pointer to the iterator
  */
 #define cxIteratorRef(iter) &((iter).base)
 
 /**
  * Loops over an iterator.
+ *
  * @param type the type of the elements
  * @param elem the name of the iteration variable
  * @param iter the iterator
@@ -215,16 +233,21 @@
 /**
  * Creates an iterator for the specified plain array.
  *
- * The \p array can be \c NULL in which case the iterator will be immediately
- * initialized such that #cxIteratorValid() returns \c false.
+ * The @p array can be @c NULL in which case the iterator will be immediately
+ * initialized such that #cxIteratorValid() returns @c false.
  *
+ * This iterator yields the addresses of the array elements.
+ * If you want to iterator over an array of pointers, you can
+ * use cxIteratorPtr() to create an iterator which directly
+ * yields the stored pointers.
  *
- * @param array a pointer to the array (can be \c NULL)
+ * @param array a pointer to the array (can be @c NULL)
  * @param elem_size the size of one array element
  * @param elem_count the number of elements in the array
  * @return an iterator for the specified array
+ * @see cxIteratorPtr()
  */
-__attribute__((__warn_unused_result__))
+cx_attr_nodiscard
 CxIterator cxIterator(
         const void *array,
         size_t elem_size,
@@ -238,23 +261,23 @@
  * elements through #cxIteratorFlagRemoval(). Every other change to the array
  * will bring this iterator to an undefined state.
  *
- * When \p remove_keeps_order is set to \c false, removing an element will only
+ * When @p remove_keeps_order is set to @c false, removing an element will only
  * move the last element to the position of the removed element, instead of
  * moving all subsequent elements by one. Usually, when the order of elements is
- * not important, this parameter should be set to \c false.
+ * not important, this parameter should be set to @c false.
  *
- * The \p array can be \c NULL in which case the iterator will be immediately
- * initialized such that #cxIteratorValid() returns \c false.
+ * The @p array can be @c NULL in which case the iterator will be immediately
+ * initialized such that #cxIteratorValid() returns @c false.
  *
  *
- * @param array a pointer to the array (can be \c NULL)
+ * @param array a pointer to the array (can be @c NULL)
  * @param elem_size the size of one array element
  * @param elem_count the number of elements in the array
- * @param remove_keeps_order \c true if the order of elements must be preserved
+ * @param remove_keeps_order @c true if the order of elements must be preserved
  * when removing an element
  * @return an iterator for the specified array
  */
-__attribute__((__warn_unused_result__))
+cx_attr_nodiscard
 CxIterator cxMutIterator(
         void *array,
         size_t elem_size,
@@ -262,4 +285,48 @@
         bool remove_keeps_order
 );
 
+/**
+ * Creates an iterator for the specified plain pointer array.
+ *
+ * This iterator assumes that every element in the array is a pointer
+ * and yields exactly those pointers during iteration (while in contrast
+ * an iterator created with cxIterator() would return the addresses
+ * of those pointers within the array).
+ *
+ * @param array a pointer to the array (can be @c NULL)
+ * @param elem_count the number of elements in the array
+ * @return an iterator for the specified array
+ * @see cxIterator()
+ */
+cx_attr_nodiscard
+CxIterator cxIteratorPtr(
+        const void *array,
+        size_t elem_count
+);
+
+/**
+ * Creates a mutating iterator for the specified plain pointer array.
+ *
+ * This is the mutating variant of cxIteratorPtr(). See also
+ * cxMutIterator().
+ *
+ * @param array a pointer to the array (can be @c NULL)
+ * @param elem_count the number of elements in the array
+ * @param remove_keeps_order @c true if the order of elements must be preserved
+ * when removing an element
+ * @return an iterator for the specified array
+ * @see cxMutIterator()
+ * @see cxIteratorPtr()
+ */
+cx_attr_nodiscard
+CxIterator cxMutIteratorPtr(
+        void *array,
+        size_t elem_count,
+        bool remove_keeps_order
+);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
 #endif // UCX_ITERATOR_H

mercurial