add cxCollectionCompareFunc() macro

Sat, 01 Nov 2025 19:31:48 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 01 Nov 2025 19:31:48 +0100
changeset 1464
9a10af83cfab
parent 1463
e228b5bde7f6
child 1465
dc886f1a6155

add cxCollectionCompareFunc() macro

CHANGELOG file | annotate | diff | comparison | revisions
docs/Writerside/topics/about.md file | annotate | diff | comparison | revisions
docs/Writerside/topics/collection.h.md file | annotate | diff | comparison | revisions
src/cx/collection.h file | annotate | diff | comparison | revisions
tests/test_kv_list.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Thu Oct 30 19:27:18 2025 +0100
+++ b/CHANGELOG	Sat Nov 01 19:31:48 2025 +0100
@@ -17,6 +17,8 @@
  * adds cxListInsertUnique() and cxListInsertUniqueArray()
  * adds cx_array_insert_unique() and various convenience macros
  * adds cx_linked_list_insert_unique() and cx_linked_list_insert_unique_chain()
+ * adds cxCollectionCompareFunc() to conveniently set a compare function for a collection
+   without needing to access the base struct manually
  * adds cxBufferShrink()
  * adds cxTreeSize()
  * adds CX_PRIstr and CX_SFMT macros for formatting UCX strings
--- a/docs/Writerside/topics/about.md	Thu Oct 30 19:27:18 2025 +0100
+++ b/docs/Writerside/topics/about.md	Sat Nov 01 19:31:48 2025 +0100
@@ -44,6 +44,8 @@
 * adds cxListInsertUnique() and cxListInsertUniqueArray()
 * adds cx_array_insert_unique() and various convenience macros
 * adds cx_linked_list_insert_unique() and cx_linked_list_insert_unique_chain()
+* adds cxCollectionCompareFunc() to conveniently set a compare function for a collection
+  without needing to access the base struct manually
 * adds cxBufferShrink()
 * adds cxTreeSize()
 * adds CX_PRIstr and CX_SFMT macros for formatting UCX strings
--- a/docs/Writerside/topics/collection.h.md	Thu Oct 30 19:27:18 2025 +0100
+++ b/docs/Writerside/topics/collection.h.md	Sat Nov 01 19:31:48 2025 +0100
@@ -44,6 +44,8 @@
 
 In each case the argument `c` is a pointer to your collection. The macro will then access the base data with `c->collection`.
 
+Similar to the above macros, the `cxCollectionCompareFunc(c,f)` macro can be used to set the compare function.
+
 ## Destructor Functions
 
 For working with destructors, the following macros are defined:
--- a/src/cx/collection.h	Thu Oct 30 19:27:18 2025 +0100
+++ b/src/cx/collection.h	Sat Nov 01 19:31:48 2025 +0100
@@ -154,6 +154,14 @@
 #define cxCollectionSorted(c) ((c)->collection.sorted || (c)->collection.size == 0)
 
 /**
+ * Sets the compare function for a collection.
+ *
+ * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
+ * @param func (@c cx_compare_func) the compare function that shall be used by @c c
+ */
+#define cxCollectionCompareFunc(c, func) (c)->collection.cmpfunc = (func)
+
+/**
  * Sets a simple destructor function for this collection.
  *
  * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
--- a/tests/test_kv_list.c	Thu Oct 30 19:27:18 2025 +0100
+++ b/tests/test_kv_list.c	Sat Nov 01 19:31:48 2025 +0100
@@ -122,7 +122,7 @@
 
 CX_TEST(test_kv_list_find_and_remove) {
     CxList *list = cxKvListCreateSimple(sizeof(int));
-    list->collection.cmpfunc = cx_cmp_int;
+    cxCollectionCompareFunc(list, cx_cmp_int);
     int x, y;
     CX_TEST_DO {
         CxMap *map = cxKvListAsMap(list);

mercurial