ucx/cx/compare.h

changeset 11
0aa8cbd7912e
parent 0
1a157da63d7c
child 16
04c9f8d8f03b
--- a/ucx/cx/compare.h	Fri Jan 03 21:40:57 2025 +0100
+++ b/ucx/cx/compare.h	Sat Jan 04 13:03:01 2025 +0100
@@ -42,16 +42,24 @@
 extern "C" {
 #endif
 
-#ifndef CX_COMPARE_FUNC_DEFINED
-#define CX_COMPARE_FUNC_DEFINED
 /**
- * A comparator function comparing two collection elements.
+ * A comparator function comparing two arbitrary values.
+ *
+ * All functions from compare.h with the cx_cmp prefix are
+ * compatible with this signature and can be used as
+ * compare function for collections, or other implementations
+ * that need to be type-agnostic.
+ *
+ * For simple comparisons the cx_vcmp family of functions
+ * can be used, but they are NOT compatible with this function
+ * pointer.
  */
+cx_attr_nonnull
+cx_attr_nodiscard
 typedef int(*cx_compare_func)(
-        void const *left,
-        void const *right
+        const void *left,
+        const void *right
 );
-#endif // CX_COMPARE_FUNC_DEFINED
 
 /**
  * Compares two integers of type int.
@@ -61,7 +69,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_int(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_int(const void *i1, const void *i2);
+
+/**
+ * Compares two ints.
+ *
+ * @param i1 integer one
+ * @param i2 integer two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_int(int i1, int i2);
 
 /**
  * Compares two integers of type long int.
@@ -71,7 +92,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_longint(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_longint(const void *i1, const void *i2);
+
+/**
+ * Compares two long ints.
+ *
+ * @param i1 long integer one
+ * @param i2 long integer two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_longint(long int i1, long int i2);
 
 /**
  * Compares two integers of type long long.
@@ -81,7 +115,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_longlong(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_longlong(const void *i1, const void *i2);
+
+/**
+ * Compares twolong long ints.
+ *
+ * @param i1 long long int one
+ * @param i2 long long int two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_longlong(long long int i1, long long int i2);
 
 /**
  * Compares two integers of type int16_t.
@@ -91,7 +138,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_int16(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_int16(const void *i1, const void *i2);
+
+/**
+ * Compares two integers of type int16_t.
+ *
+ * @param i1 int16_t one
+ * @param i2 int16_t two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_int16(int16_t i1, int16_t i2);
 
 /**
  * Compares two integers of type int32_t.
@@ -101,7 +161,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_int32(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_int32(const void *i1, const void *i2);
+
+/**
+ * Compares two integers of type int32_t.
+ *
+ * @param i1 int32_t one
+ * @param i2 int32_t two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_int32(int32_t i1, int32_t i2);
 
 /**
  * Compares two integers of type int64_t.
@@ -111,7 +184,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_int64(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_int64(const void *i1, const void *i2);
+
+/**
+ * Compares two integers of type int64_t.
+ *
+ * @param i1 int64_t one
+ * @param i2 int64_t two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_int64(int64_t i1, int64_t i2);
 
 /**
  * Compares two integers of type unsigned int.
@@ -121,7 +207,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_uint(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_uint(const void *i1, const void *i2);
+
+/**
+ * Compares two unsigned ints.
+ *
+ * @param i1 unsigned integer one
+ * @param i2 unsigned integer two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_uint(unsigned int i1, unsigned int i2);
 
 /**
  * Compares two integers of type unsigned long int.
@@ -131,7 +230,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_ulongint(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_ulongint(const void *i1, const void *i2);
+
+/**
+ * Compares two unsigned long ints.
+ *
+ * @param i1 unsigned long integer one
+ * @param i2 unsigned long integer two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_ulongint(unsigned long int i1, unsigned long int i2);
 
 /**
  * Compares two integers of type unsigned long long.
@@ -141,7 +253,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_ulonglong(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_ulonglong(const void *i1, const void *i2);
+
+/**
+ * Compares two unsigned long long ints.
+ *
+ * @param i1 unsigned long long one
+ * @param i2 unsigned long long two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_ulonglong(unsigned long long int i1, unsigned long long int i2);
 
 /**
  * Compares two integers of type uint16_t.
@@ -151,7 +276,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_uint16(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_uint16(const void *i1, const void *i2);
+
+/**
+ * Compares two integers of type uint16_t.
+ *
+ * @param i1 uint16_t one
+ * @param i2 uint16_t two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_uint16(uint16_t i1, uint16_t i2);
 
 /**
  * Compares two integers of type uint32_t.
@@ -161,7 +299,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_uint32(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_uint32(const void *i1, const void *i2);
+
+/**
+ * Compares two integers of type uint32_t.
+ *
+ * @param i1 uint32_t one
+ * @param i2 uint32_t two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_uint32(uint32_t i1, uint32_t i2);
 
 /**
  * Compares two integers of type uint64_t.
@@ -171,7 +322,20 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int cx_cmp_uint64(void const *i1, void const *i2);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_uint64(const void *i1, const void *i2);
+
+/**
+ * Compares two integers of type uint64_t.
+ *
+ * @param i1 uint64_t one
+ * @param i2 uint64_t two
+ * @return -1, if i1 is less than i2, 0 if both are equal,
+ * 1 if i1 is greater than i2
+ */
+cx_attr_nodiscard
+int cx_vcmp_uint64(uint64_t i1, uint64_t i2);
 
 /**
  * Compares two real numbers of type float with precision 1e-6f.
@@ -181,8 +345,20 @@
  * @return -1, if *f1 is less than *f2, 0 if both are equal,
  * 1 if *f1 is greater than *f2
  */
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_float(const void *f1, const void *f2);
 
-int cx_cmp_float(void const *f1, void const *f2);
+/**
+ * Compares two real numbers of type float with precision 1e-6f.
+ *
+ * @param f1 float one
+ * @param f2 float two
+ * @return -1, if f1 is less than f2, 0 if both are equal,
+ * 1 if f1 is greater than f2
+ */
+cx_attr_nodiscard
+int cx_vcmp_float(float f1, float f2);
 
 /**
  * Compares two real numbers of type double with precision 1e-14.
@@ -192,36 +368,66 @@
  * @return -1, if *d1 is less than *d2, 0 if both are equal,
  * 1 if *d1 is greater than *d2
  */
-int cx_cmp_double(
-        void const *d1,
-        void const *d2
-);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_double(const void *d1, const void *d2);
+
+/**
+ * Convenience function
+ *
+ * @param d1 double one
+ * @param d2 double two
+ * @return -1, if d1 is less than d2, 0 if both are equal,
+ * 1 if d1 is greater than d2
+ */
+cx_attr_nodiscard
+int cx_vcmp_double(double d1, double d2);
+
+/**
+ * Compares the integer representation of two pointers.
+ *
+ * @param ptr1 pointer to pointer one (const intptr_t*)
+ * @param ptr2 pointer to pointer two (const intptr_t*)
+ * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal,
+ * 1 if *ptr1 is greater than *ptr2
+ */
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_intptr(const void *ptr1, const void *ptr2);
 
 /**
  * Compares the integer representation of two pointers.
  *
- * @param ptr1 pointer to pointer one (intptr_t const*)
- * @param ptr2 pointer to pointer two (intptr_t const*)
- * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal,
- * 1 if *ptr1 is greater than *ptr2
+ * @param ptr1 pointer one
+ * @param ptr2 pointer two
+ * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
+ * 1 if ptr1 is greater than ptr2
  */
-int cx_cmp_intptr(
-        void const *ptr1,
-        void const *ptr2
-);
+cx_attr_nodiscard
+int cx_vcmp_intptr(intptr_t ptr1, intptr_t ptr2);
 
 /**
  * Compares the unsigned integer representation of two pointers.
  *
- * @param ptr1 pointer to pointer one (uintptr_t const*)
- * @param ptr2 pointer to pointer two (uintptr_t const*)
+ * @param ptr1 pointer to pointer one (const uintptr_t*)
+ * @param ptr2 pointer to pointer two (const uintptr_t*)
  * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal,
  * 1 if *ptr1 is greater than *ptr2
  */
-int cx_cmp_uintptr(
-        void const *ptr1,
-        void const *ptr2
-);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_uintptr(const void *ptr1, const void *ptr2);
+
+/**
+ * Compares the unsigned integer representation of two pointers.
+ *
+ * @param ptr1 pointer one
+ * @param ptr2 pointer two
+ * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
+ * 1 if ptr1 is greater than ptr2
+ */
+cx_attr_nodiscard
+int cx_vcmp_uintptr(uintptr_t ptr1, uintptr_t ptr2);
 
 /**
  * Compares the pointers specified in the arguments without de-referencing.
@@ -231,10 +437,9 @@
  * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
  * 1 if ptr1 is greater than ptr2
  */
-int cx_cmp_ptr(
-        void const *ptr1,
-        void const *ptr2
-);
+cx_attr_nonnull
+cx_attr_nodiscard
+int cx_cmp_ptr(const void *ptr1, const void *ptr2);
 
 #ifdef __cplusplus
 } // extern "C"

mercurial