src/cx/compare.h

changeset 1618
ef7cab6eb131
parent 1426
3a89b31f0724
--- a/src/cx/compare.h	Tue Dec 16 21:33:58 2025 +0100
+++ b/src/cx/compare.h	Wed Dec 17 19:05:50 2025 +0100
@@ -57,6 +57,13 @@
 typedef int (*cx_compare_func)(const void *left, const void *right);
 
 /**
+ * A comparator function comparing two arbitrary values.
+ *
+ * Functions with this signature allow specifying a pointer to custom data.
+ */
+typedef int (*cx_compare_func2)(const void *left, const void *right, void *data);
+
+/**
  * Compares two integers of type int.
  *
  * @note the parameters deliberately have type @c void* to be
@@ -527,6 +534,41 @@
 cx_attr_nonnull cx_attr_nodiscard
 CX_EXPORT int cx_cmp_ptr(const void *ptr1, const void *ptr2);
 
+/**
+ * A @c cx_compare_func2 compatible wrapper for @c memcmp().
+ *
+ * @param ptr1 pointer one
+ * @param ptr2 pointer two
+ * @param n (@c size_t*) a pointer to the length
+ * @return the result of @c memcmp()
+ */
+cx_attr_nonnull cx_attr_nodiscard
+CX_EXPORT int cx_acmp_memcmp(const void *ptr1, const void *ptr2, void *n);
+
+/** Wraps a compare function for cx_acmp_wrap. */
+typedef struct {
+    /** The wrapped compare function */
+    cx_compare_func cmp;
+} cx_compare_func_wrapper;
+
+/**
+ * A @c cx_compare_func2 wrapper for a @c cx_compare_func().
+ *
+ * This is not strictly compatible with a @c cx_compare_func2 because
+ * ISO C does not define conversions between function and object pointers.
+ *
+ * But it works on all tested platforms to cast a pointer to this function to
+ * a @c cx_compare_func2.
+ *
+ * @param ptr1 pointer one
+ * @param ptr2 pointer two
+ * @param cmp_wrapper a pointer to a @c cx_compare_func_wrapper
+ * @return the result of the invoked compare function
+ * @see cx_compare_func_wrapper_s
+ */
+cx_attr_nonnull cx_attr_nodiscard
+CX_EXPORT int cx_acmp_wrap(const void *ptr1, const void *ptr2, void* cmp_wrapper);
+
 #ifdef __cplusplus
 } // extern "C"
 #endif

mercurial