add convenience array_sort functions - relates to #622

Thu, 18 Dec 2025 12:26:25 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Dec 2025 12:26:25 +0100
changeset 1621
c52a4c67e29e
parent 1620
bf5d647f939d
child 1622
27e7a4bf1a39

add convenience array_sort functions - relates to #622

docs/Writerside/topics/array_list.h.md file | annotate | diff | comparison | revisions
src/array_list.c file | annotate | diff | comparison | revisions
src/cx/array_list.h file | annotate | diff | comparison | revisions
--- a/docs/Writerside/topics/array_list.h.md	Thu Dec 18 12:11:30 2025 +0100
+++ b/docs/Writerside/topics/array_list.h.md	Thu Dec 18 12:26:25 2025 +0100
@@ -114,6 +114,30 @@
 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements.
 >{style="note"}
 
+## Sorting
+
+```C
+#include <cx/array_list.h>
+
+void cx_array_qsort_c(void *array, size_t nmemb, size_t size,
+        cx_compare_func2 fn, void *context);
+
+void cx_array_sort(CxArray array, cx_compare_func fn);
+        
+void cx_array_sort_c(CxArray array,
+        cx_compare_func2 fn, void *context);
+```
+
+With simple `cx_compare_func` functions, arrays can always be sorted with standard `qsort()`.
+Sorting arrays with `cx_compare_func2` functions, however, need special support by either `qsort_r()` (GNU) or `qsort_s()` (ISO).
+However, both functions come with their own challenges.
+On the one hand, `qsort_r()` is not ISO standard, and on the other hand `qsort_s()` is only optional and has an incorrect parameter order in the Microsoft C library.
+
+To provide a platform independent solution, UCX detects if `qsort_r()` is available and implements a fallback when it is not.
+You can safely use `cx_array_qsort_c()` everywhere wehere you would use `qsort_r()`.
+
+The functions `cx_array_sort()` and `cx_array_sort_c()` are for convenient work with arrays declared with `CX_ARRAY()`.
+
 ## Insertion Sort
 
 ```C
--- a/src/array_list.c	Thu Dec 18 12:11:30 2025 +0100
+++ b/src/array_list.c	Thu Dec 18 12:26:25 2025 +0100
@@ -372,6 +372,16 @@
 #endif
 }
 
+void cx_array_sort_(CxArray *array, size_t elem_size,
+        cx_compare_func fn) {
+    qsort(array->data, array->size, elem_size, fn);
+}
+
+void cx_array_sort_c_(CxArray *array, size_t elem_size,
+        cx_compare_func2 fn, void *context) {
+    cx_array_qsort_c(array->data, array->size, elem_size, fn, context);
+}
+
 CxIterator cx_array_iterator_(CxArray *array, size_t elem_size) {
     return cxIterator(array->data, elem_size, array->size);
 }
--- a/src/cx/array_list.h	Thu Dec 18 12:11:30 2025 +0100
+++ b/src/cx/array_list.h	Thu Dec 18 12:26:25 2025 +0100
@@ -560,6 +560,51 @@
         cx_compare_func2 fn, void *context);
 
 /**
+ * Sorts an array.
+ *
+ * Internal function - do not use.
+ *
+ * @param array a pointer to the array structure
+ * @param elem_size the size of one element
+ * @param fn the compare function
+ */
+CX_EXPORT void cx_array_sort_(CxArray *array, size_t elem_size,
+        cx_compare_func fn);
+
+/**
+ * Sorts an array.
+ *
+ * Internal function - do not use.
+ *
+ * @param array a pointer to the array structure
+ * @param elem_size the size of one element
+ * @param fn the compare function
+ * @param context the context for the compare function
+ */
+CX_EXPORT void cx_array_sort_c_(CxArray *array, size_t elem_size,
+        cx_compare_func2 fn, void *context);
+
+/**
+ * Sorts an array.
+ *
+ * @param array the name of the array
+ * @param fn (@c cx_compare_func) the compare function
+ * @param context (@c void*) the context for the compare function
+ */
+#define cx_array_sort(array, fn) \
+        cx_array_sort_((CxArray*)&(array), sizeof((array).data[0]), fn)
+
+/**
+ * Sorts an array.
+ *
+ * @param array the name of the array
+ * @param fn (@c cx_compare_func2) the compare function
+ * @param context (@c void*) the context for the compare function
+ */
+#define cx_array_sort_c(array, fn, context) \
+        cx_array_sort_c_((CxArray*)&(array), sizeof((array).data[0]), fn, context)
+
+/**
  * Creates an iterator over the elements of an array.
  *
  * Internal function - do not use.

mercurial