add size_t compare functions

Sat, 27 Sep 2025 17:47:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 27 Sep 2025 17:47:10 +0200
changeset 1399
40c3b850f859
parent 1398
c8af55ed4cae
child 1400
7bc88ae62755

add size_t compare functions

docs/Writerside/topics/compare.h.md file | annotate | diff | comparison | revisions
src/compare.c file | annotate | diff | comparison | revisions
src/cx/compare.h file | annotate | diff | comparison | revisions
--- a/docs/Writerside/topics/compare.h.md	Sat Sep 27 17:03:35 2025 +0200
+++ b/docs/Writerside/topics/compare.h.md	Sat Sep 27 17:47:10 2025 +0200
@@ -48,6 +48,7 @@
 cx_vcmp_uint16
 cx_vcmp_uint32
 cx_vcmp_uint64
+cx_vcmp_size
 cx_vcmp_uintptr
 cx_vcmp_ulongint
 cx_vcmp_ulonglong
@@ -67,6 +68,7 @@
 cx_cmp_uint16
 cx_cmp_uint32
 cx_cmp_uint64
+cx_cmp_size
 cx_cmp_uintptr
 cx_cmp_ulongint
 cx_cmp_ulonglong
--- a/src/compare.c	Sat Sep 27 17:03:35 2025 +0200
+++ b/src/compare.c	Sat Sep 27 17:47:10 2025 +0200
@@ -198,6 +198,20 @@
     return cx_vcmp_uint64(a, b);
 }
 
+int cx_vcmp_size(size_t a, size_t b) {
+    if (a == b) {
+        return 0;
+    } else {
+        return a < b ? -1 : 1;
+    }
+}
+
+int cx_cmp_size(const void *i1, const void *i2) {
+    size_t a = *((const size_t *) i1);
+    size_t b = *((const size_t *) i2);
+    return cx_vcmp_size(a, b);
+}
+
 int cx_vcmp_float(float a, float b) {
     if (fabsf(a - b) < 1e-6f) {
         return 0;
--- a/src/cx/compare.h	Sat Sep 27 17:03:35 2025 +0200
+++ b/src/cx/compare.h	Sat Sep 27 17:47:10 2025 +0200
@@ -423,6 +423,36 @@
 int cx_vcmp_uint64(uint64_t i1, uint64_t i2);
 
 /**
+ * Compares two integers of type size_t.
+ *
+ * @note the parameters deliberately have type @c void* to be
+ * compatible with #cx_compare_func without the need of a cast.
+ *
+ * @param i1 pointer to size_t one
+ * @param i2 pointer to size_t two
+ * @retval -1 if the left argument is less than the right argument
+ * @retval 0 if both arguments are equal
+ * @retval 1 if the left argument is greater than the right argument
+ */
+cx_attr_nonnull
+cx_attr_nodiscard
+cx_attr_export
+int cx_cmp_size(const void *i1, const void *i2);
+
+/**
+ * Compares two integers of type size_t.
+ *
+ * @param i1 size_t one
+ * @param i2 size_t two
+ * @retval -1 if the left argument is less than the right argument
+ * @retval 0 if both arguments are equal
+ * @retval 1 if the left argument is greater than the right argument
+ */
+cx_attr_nodiscard
+cx_attr_export
+int cx_vcmp_size(size_t i1, size_t i2);
+
+/**
  * Compares two real numbers of type float with precision 1e-6f.
  *
  * @note the parameters deliberately have type @c void* to be

mercurial