adds const qualifiers to compare, distance and copy function signatures

Sat, 15 Jul 2017 20:46:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 15 Jul 2017 20:46:18 +0200
changeset 244
98dc2d3a9b1d
parent 243
2e74828c5e94
child 245
db732f8c083a

adds const qualifiers to compare, distance and copy function signatures

test/avl_tests.c file | annotate | diff | comparison | revisions
ucx/ucx.h file | annotate | diff | comparison | revisions
ucx/utils.c file | annotate | diff | comparison | revisions
ucx/utils.h file | annotate | diff | comparison | revisions
--- a/test/avl_tests.c	Sat Jul 15 19:20:06 2017 +0200
+++ b/test/avl_tests.c	Sat Jul 15 20:46:18 2017 +0200
@@ -224,7 +224,7 @@
     ucx_avl_free(tree4);
 }
 
-static intmax_t dist_int(void* a, void* b, void* n) {
+static intmax_t dist_int(const void* a, const void* b, void* n) {
     return ((intmax_t)a)-((intmax_t)b);
 }
 
--- a/ucx/ucx.h	Sat Jul 15 19:20:06 2017 +0200
+++ b/ucx/ucx.h	Sat Jul 15 20:46:18 2017 +0200
@@ -87,7 +87,7 @@
  * and 0 if both arguments are equal. If the third argument is
  * <code>NULL</code>, it shall be ignored.
  */
-typedef int(*cmp_func)(void*,void*,void*);
+typedef int(*cmp_func)(const void*,const void*,void*);
 
 /**
  * Function pointer to a distance function.
@@ -96,7 +96,7 @@
  * the distance shall be computed and optional additional data.
  * The function shall then return the signed distance as integer value.
  */
-typedef intmax_t(*distance_func)(void*,void*,void*);
+typedef intmax_t(*distance_func)(const void*,const void*,void*);
 
 /**
  * Function pointer to a copy function.
@@ -109,7 +109,7 @@
  * passed to <code>free()</code> depends on the implementation of the
  * respective <code>copy_func</code>.
  */
-typedef void*(*copy_func)(void*,void*);
+typedef void*(*copy_func)(const void*,void*);
 
 /**
  * Function pointer to a write function.
--- a/ucx/utils.c	Sat Jul 15 19:20:06 2017 +0200
+++ b/ucx/utils.c	Sat Jul 15 20:46:18 2017 +0200
@@ -33,15 +33,15 @@
 #include <errno.h>
 
 /* COPY FUCNTIONS */
-void* ucx_strcpy(void* s, void* data) {
-    char *str = (char*) s;
+void* ucx_strcpy(const void* s, void* data) {
+    const char *str = (const char*) s;
     size_t n = 1+strlen(str);
     char *cpy = (char*) malloc(n);
     memcpy(cpy, str, n);
     return cpy;
 }
 
-void* ucx_memcpy(void* m, void* n) {
+void* ucx_memcpy(const void* m, void* n) {
     size_t k = *((size_t*)n);
     void *cpy = malloc(k);
     memcpy(cpy, m, k);
@@ -87,17 +87,17 @@
 
 /* COMPARE FUNCTIONS */
 
-int ucx_strcmp(void *s1, void *s2, void *data) {
-    return strcmp((char*)s1, (char*)s2);
+int ucx_strcmp(const void *s1, const void *s2, void *data) {
+    return strcmp((const char*)s1, (const char*)s2);
 }
 
-int ucx_strncmp(void *s1, void *s2, void *n) {
-    return strncmp((char*)s1, (char*)s2, *((size_t*) n));
+int ucx_strncmp(const void *s1, const void *s2, void *n) {
+    return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
 }
 
-int ucx_intcmp(void *i1, void *i2, void *data) {
-   int a = *((int*) i1);
-   int b = *((int*) i2);
+int ucx_intcmp(const void *i1, const void *i2, void *data) {
+   int a = *((const int*) i1);
+   int b = *((const int*) i2);
    if (a == b) {
        return 0;
    } else {
@@ -105,9 +105,9 @@
    }
 }
 
-int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
-   float a = *((float*) f1);
-   float b = *((float*) f2);
+int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) {
+   float a = *((const float*) f1);
+   float b = *((const float*) f2);
    float e = !epsilon ? 1e-6f : *((float*)epsilon);
    if (fabsf(a - b) < e) {
        return 0;
@@ -116,9 +116,9 @@
    }
 }
 
-int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
-   double a = *((float*) d1);
-   double b = *((float*) d2);
+int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) {
+   double a = *((const double*) d1);
+   double b = *((const double*) d2);
    double e = !epsilon ? 1e-14 : *((double*)epsilon);
    if (fabs(a - b) < e) {
        return 0;
@@ -127,9 +127,9 @@
    }
 }
 
-int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
-    intptr_t p1 = (intptr_t) ptr1;
-    intptr_t p2 = (intptr_t) ptr2;
+int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) {
+    const intptr_t p1 = (const intptr_t) ptr1;
+    const intptr_t p2 = (const intptr_t) ptr2;
     if (p1 == p2) {
         return 0;
     } else {
@@ -137,7 +137,7 @@
     }
 }
 
-int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
+int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) {
     return memcmp(ptr1, ptr2, *((size_t*)n));
 }
 
--- a/ucx/utils.h	Sat Jul 15 19:20:06 2017 +0200
+++ b/ucx/utils.h	Sat Jul 15 20:46:18 2017 +0200
@@ -60,7 +60,7 @@
  * @param data omitted
  * @return a pointer to a copy of s1 that can be passed to free(void*)
  */
-void *ucx_strcpy(void *s, void *data);
+void *ucx_strcpy(const void *s, void *data);
 
 /**
  * Copies a memory area.
@@ -69,7 +69,7 @@
  * @return a pointer to a copy of the specified memory area that can
  * be passed to free(void*)
  */
-void *ucx_memcpy(void *m, void *n);
+void *ucx_memcpy(const void *m, void *n);
 
 
 /**
@@ -144,7 +144,7 @@
  * @param data omitted
  * @return the result of strcmp(s1, s2)
  */
-int ucx_strcmp(void *s1, void *s2, void *data);
+int ucx_strcmp(const void *s1, const void *s2, void *data);
 
 /**
  * Wraps the strncmp function.
@@ -153,7 +153,7 @@
  * @param n a pointer to the size_t containing the third strncmp parameter
  * @return the result of strncmp(s1, s2, *n)
  */
-int ucx_strncmp(void *s1, void *s2, void *n);
+int ucx_strncmp(const void *s1, const void *s2, void *n);
 
 /**
  * Compares two integers of type int.
@@ -163,7 +163,7 @@
  * @return -1, if *i1 is less than *i2, 0 if both are equal,
  * 1 if *i1 is greater than *i2
  */
-int ucx_intcmp(void *i1, void *i2, void *data);
+int ucx_intcmp(const void *i1, const void *i2, void *data);
 
 /**
  * Compares two real numbers of type float.
@@ -174,7 +174,7 @@
  * 1 if *f1 is greater than *f2
  */
 
-int ucx_floatcmp(void *f1, void *f2, void *data);
+int ucx_floatcmp(const void *f1, const void *f2, void *data);
 
 /**
  * Compares two real numbers of type double.
@@ -184,7 +184,7 @@
  * @return -1, if *d1 is less than *d2, 0 if both are equal,
  * 1 if *d1 is greater than *d2
  */
-int ucx_doublecmp(void *d1, void *d2, void *data);
+int ucx_doublecmp(const void *d1, const void *d2, void *data);
 
 /**
  * Compares two pointers.
@@ -194,7 +194,7 @@
  * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
  * 1 if ptr1 is greater than ptr2
  */
-int ucx_ptrcmp(void *ptr1, void *ptr2, void *data);
+int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data);
 
 /**
  * Compares two memory areas.
@@ -203,7 +203,7 @@
  * @param n a pointer to the size_t containing the third parameter for memcmp
  * @return the result of memcmp(ptr1, ptr2, *n)
  */
-int ucx_memcmp(void *ptr1, void *ptr2, void *n);
+int ucx_memcmp(const void *ptr1, const void *ptr2, void *n);
 
 /**
  * A <code>printf()</code> like function which writes the output to a stream by

mercurial