src/cx/array_list.h

changeset 985
68754c7de906
parent 953
581ad4fd01e9
--- a/src/cx/array_list.h	Thu Nov 07 20:22:56 2024 +0100
+++ b/src/cx/array_list.h	Thu Nov 07 22:46:58 2024 +0100
@@ -28,7 +28,6 @@
 /**
  * \file array_list.h
  * \brief Array list implementation.
- * \details Also provides several low-level functions for custom array list implementations.
  * \author Mike Becker
  * \author Olaf Wintermann
  * \copyright 2-Clause BSD License
@@ -45,7 +44,8 @@
 #endif
 
 /**
- * The maximum item size in an array list that fits into stack buffer when swapped.
+ * The maximum item size in an array list that fits into stack buffer
+ * when swapped.
  */
 extern const unsigned cx_array_swap_sbo_size;
 
@@ -94,6 +94,9 @@
      * @param alloc a reference to this allocator
      * @return a pointer to the reallocated memory or \c NULL on failure
      */
+    cx_attr_nodiscard
+    cx_attr_nonnull_arg(4)
+    cx_attr_allocsize(2, 3)
     void *(*realloc)(
             void *array,
             size_t capacity,
@@ -184,7 +187,7 @@
  * if reallocation shall not happen
  * @return zero on success, non-zero error code on failure
  */
-__attribute__((__nonnull__(1, 2, 5)))
+cx_attr_nonnull_arg(1, 2, 5)
 enum cx_array_result cx_array_copy(
         void **target,
         size_t *size,
@@ -262,7 +265,7 @@
  * @param reallocator the array reallocator to use
  * @return zero on success, non-zero error code on failure
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 enum cx_array_result cx_array_insert_sorted(
         void **target,
         size_t *size,
@@ -342,7 +345,7 @@
  * @param cmp_func the compare function
  * @return the index of the largest lower bound, or \p size
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 size_t cx_array_binary_search_inf(
         const void *arr,
         size_t size,
@@ -365,23 +368,14 @@
  * @return the index of the element in the array, or \p size if the element
  * cannot be found
  */
-__attribute__((__nonnull__))
-static inline size_t cx_array_binary_search(
+cx_attr_nonnull
+size_t cx_array_binary_search(
         const void *arr,
         size_t size,
         size_t elem_size,
         const void *elem,
         cx_compare_func cmp_func
-) {
-    size_t index = cx_array_binary_search_inf(
-            arr, size, elem_size, elem, cmp_func
-    );
-    if (index < size && cmp_func(((const char *) arr) + index * elem_size, elem) == 0) {
-        return index;
-    } else {
-        return size;
-    }
-}
+);
 
 /**
  * Searches the smallest upper bound in a sorted array.
@@ -403,24 +397,14 @@
  * @param cmp_func the compare function
  * @return the index of the smallest upper bound, or \p size
  */
-__attribute__((__nonnull__))
-static inline size_t cx_array_binary_search_sup(
+cx_attr_nonnull
+size_t cx_array_binary_search_sup(
         const void *arr,
         size_t size,
         size_t elem_size,
         const void *elem,
         cx_compare_func cmp_func
-) {
-    size_t inf = cx_array_binary_search_inf(arr, size, elem_size, elem, cmp_func);
-    if (inf == size) {
-        // no infimum means, first element is supremum
-        return 0;
-    } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) {
-        return inf;
-    } else {
-        return inf + 1;
-    }
-}
+);
 
 /**
  * Swaps two array elements.
@@ -430,7 +414,7 @@
  * @param idx1 index of first element
  * @param idx2 index of second element
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
 void cx_array_swap(
         void *arr,
         size_t elem_size,
@@ -454,6 +438,9 @@
  * @param initial_capacity the initial number of elements the array can store
  * @return the created list
  */
+cx_attr_nodiscard
+cx_attr_malloc
+cx_attr_dealloc(cxListDestroy, 1)
 CxList *cxArrayListCreate(
         const CxAllocator *allocator,
         cx_compare_func comparator,

mercurial