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
     1.1 --- a/test/avl_tests.c	Sat Jul 15 19:20:06 2017 +0200
     1.2 +++ b/test/avl_tests.c	Sat Jul 15 20:46:18 2017 +0200
     1.3 @@ -224,7 +224,7 @@
     1.4      ucx_avl_free(tree4);
     1.5  }
     1.6  
     1.7 -static intmax_t dist_int(void* a, void* b, void* n) {
     1.8 +static intmax_t dist_int(const void* a, const void* b, void* n) {
     1.9      return ((intmax_t)a)-((intmax_t)b);
    1.10  }
    1.11  
     2.1 --- a/ucx/ucx.h	Sat Jul 15 19:20:06 2017 +0200
     2.2 +++ b/ucx/ucx.h	Sat Jul 15 20:46:18 2017 +0200
     2.3 @@ -87,7 +87,7 @@
     2.4   * and 0 if both arguments are equal. If the third argument is
     2.5   * <code>NULL</code>, it shall be ignored.
     2.6   */
     2.7 -typedef int(*cmp_func)(void*,void*,void*);
     2.8 +typedef int(*cmp_func)(const void*,const void*,void*);
     2.9  
    2.10  /**
    2.11   * Function pointer to a distance function.
    2.12 @@ -96,7 +96,7 @@
    2.13   * the distance shall be computed and optional additional data.
    2.14   * The function shall then return the signed distance as integer value.
    2.15   */
    2.16 -typedef intmax_t(*distance_func)(void*,void*,void*);
    2.17 +typedef intmax_t(*distance_func)(const void*,const void*,void*);
    2.18  
    2.19  /**
    2.20   * Function pointer to a copy function.
    2.21 @@ -109,7 +109,7 @@
    2.22   * passed to <code>free()</code> depends on the implementation of the
    2.23   * respective <code>copy_func</code>.
    2.24   */
    2.25 -typedef void*(*copy_func)(void*,void*);
    2.26 +typedef void*(*copy_func)(const void*,void*);
    2.27  
    2.28  /**
    2.29   * Function pointer to a write function.
     3.1 --- a/ucx/utils.c	Sat Jul 15 19:20:06 2017 +0200
     3.2 +++ b/ucx/utils.c	Sat Jul 15 20:46:18 2017 +0200
     3.3 @@ -33,15 +33,15 @@
     3.4  #include <errno.h>
     3.5  
     3.6  /* COPY FUCNTIONS */
     3.7 -void* ucx_strcpy(void* s, void* data) {
     3.8 -    char *str = (char*) s;
     3.9 +void* ucx_strcpy(const void* s, void* data) {
    3.10 +    const char *str = (const char*) s;
    3.11      size_t n = 1+strlen(str);
    3.12      char *cpy = (char*) malloc(n);
    3.13      memcpy(cpy, str, n);
    3.14      return cpy;
    3.15  }
    3.16  
    3.17 -void* ucx_memcpy(void* m, void* n) {
    3.18 +void* ucx_memcpy(const void* m, void* n) {
    3.19      size_t k = *((size_t*)n);
    3.20      void *cpy = malloc(k);
    3.21      memcpy(cpy, m, k);
    3.22 @@ -87,17 +87,17 @@
    3.23  
    3.24  /* COMPARE FUNCTIONS */
    3.25  
    3.26 -int ucx_strcmp(void *s1, void *s2, void *data) {
    3.27 -    return strcmp((char*)s1, (char*)s2);
    3.28 +int ucx_strcmp(const void *s1, const void *s2, void *data) {
    3.29 +    return strcmp((const char*)s1, (const char*)s2);
    3.30  }
    3.31  
    3.32 -int ucx_strncmp(void *s1, void *s2, void *n) {
    3.33 -    return strncmp((char*)s1, (char*)s2, *((size_t*) n));
    3.34 +int ucx_strncmp(const void *s1, const void *s2, void *n) {
    3.35 +    return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
    3.36  }
    3.37  
    3.38 -int ucx_intcmp(void *i1, void *i2, void *data) {
    3.39 -   int a = *((int*) i1);
    3.40 -   int b = *((int*) i2);
    3.41 +int ucx_intcmp(const void *i1, const void *i2, void *data) {
    3.42 +   int a = *((const int*) i1);
    3.43 +   int b = *((const int*) i2);
    3.44     if (a == b) {
    3.45         return 0;
    3.46     } else {
    3.47 @@ -105,9 +105,9 @@
    3.48     }
    3.49  }
    3.50  
    3.51 -int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
    3.52 -   float a = *((float*) f1);
    3.53 -   float b = *((float*) f2);
    3.54 +int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) {
    3.55 +   float a = *((const float*) f1);
    3.56 +   float b = *((const float*) f2);
    3.57     float e = !epsilon ? 1e-6f : *((float*)epsilon);
    3.58     if (fabsf(a - b) < e) {
    3.59         return 0;
    3.60 @@ -116,9 +116,9 @@
    3.61     }
    3.62  }
    3.63  
    3.64 -int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
    3.65 -   double a = *((float*) d1);
    3.66 -   double b = *((float*) d2);
    3.67 +int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) {
    3.68 +   double a = *((const double*) d1);
    3.69 +   double b = *((const double*) d2);
    3.70     double e = !epsilon ? 1e-14 : *((double*)epsilon);
    3.71     if (fabs(a - b) < e) {
    3.72         return 0;
    3.73 @@ -127,9 +127,9 @@
    3.74     }
    3.75  }
    3.76  
    3.77 -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
    3.78 -    intptr_t p1 = (intptr_t) ptr1;
    3.79 -    intptr_t p2 = (intptr_t) ptr2;
    3.80 +int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) {
    3.81 +    const intptr_t p1 = (const intptr_t) ptr1;
    3.82 +    const intptr_t p2 = (const intptr_t) ptr2;
    3.83      if (p1 == p2) {
    3.84          return 0;
    3.85      } else {
    3.86 @@ -137,7 +137,7 @@
    3.87      }
    3.88  }
    3.89  
    3.90 -int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
    3.91 +int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) {
    3.92      return memcmp(ptr1, ptr2, *((size_t*)n));
    3.93  }
    3.94  
     4.1 --- a/ucx/utils.h	Sat Jul 15 19:20:06 2017 +0200
     4.2 +++ b/ucx/utils.h	Sat Jul 15 20:46:18 2017 +0200
     4.3 @@ -60,7 +60,7 @@
     4.4   * @param data omitted
     4.5   * @return a pointer to a copy of s1 that can be passed to free(void*)
     4.6   */
     4.7 -void *ucx_strcpy(void *s, void *data);
     4.8 +void *ucx_strcpy(const void *s, void *data);
     4.9  
    4.10  /**
    4.11   * Copies a memory area.
    4.12 @@ -69,7 +69,7 @@
    4.13   * @return a pointer to a copy of the specified memory area that can
    4.14   * be passed to free(void*)
    4.15   */
    4.16 -void *ucx_memcpy(void *m, void *n);
    4.17 +void *ucx_memcpy(const void *m, void *n);
    4.18  
    4.19  
    4.20  /**
    4.21 @@ -144,7 +144,7 @@
    4.22   * @param data omitted
    4.23   * @return the result of strcmp(s1, s2)
    4.24   */
    4.25 -int ucx_strcmp(void *s1, void *s2, void *data);
    4.26 +int ucx_strcmp(const void *s1, const void *s2, void *data);
    4.27  
    4.28  /**
    4.29   * Wraps the strncmp function.
    4.30 @@ -153,7 +153,7 @@
    4.31   * @param n a pointer to the size_t containing the third strncmp parameter
    4.32   * @return the result of strncmp(s1, s2, *n)
    4.33   */
    4.34 -int ucx_strncmp(void *s1, void *s2, void *n);
    4.35 +int ucx_strncmp(const void *s1, const void *s2, void *n);
    4.36  
    4.37  /**
    4.38   * Compares two integers of type int.
    4.39 @@ -163,7 +163,7 @@
    4.40   * @return -1, if *i1 is less than *i2, 0 if both are equal,
    4.41   * 1 if *i1 is greater than *i2
    4.42   */
    4.43 -int ucx_intcmp(void *i1, void *i2, void *data);
    4.44 +int ucx_intcmp(const void *i1, const void *i2, void *data);
    4.45  
    4.46  /**
    4.47   * Compares two real numbers of type float.
    4.48 @@ -174,7 +174,7 @@
    4.49   * 1 if *f1 is greater than *f2
    4.50   */
    4.51  
    4.52 -int ucx_floatcmp(void *f1, void *f2, void *data);
    4.53 +int ucx_floatcmp(const void *f1, const void *f2, void *data);
    4.54  
    4.55  /**
    4.56   * Compares two real numbers of type double.
    4.57 @@ -184,7 +184,7 @@
    4.58   * @return -1, if *d1 is less than *d2, 0 if both are equal,
    4.59   * 1 if *d1 is greater than *d2
    4.60   */
    4.61 -int ucx_doublecmp(void *d1, void *d2, void *data);
    4.62 +int ucx_doublecmp(const void *d1, const void *d2, void *data);
    4.63  
    4.64  /**
    4.65   * Compares two pointers.
    4.66 @@ -194,7 +194,7 @@
    4.67   * @return -1 if ptr1 is less than ptr2, 0 if both are equal,
    4.68   * 1 if ptr1 is greater than ptr2
    4.69   */
    4.70 -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data);
    4.71 +int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data);
    4.72  
    4.73  /**
    4.74   * Compares two memory areas.
    4.75 @@ -203,7 +203,7 @@
    4.76   * @param n a pointer to the size_t containing the third parameter for memcmp
    4.77   * @return the result of memcmp(ptr1, ptr2, *n)
    4.78   */
    4.79 -int ucx_memcmp(void *ptr1, void *ptr2, void *n);
    4.80 +int ucx_memcmp(const void *ptr1, const void *ptr2, void *n);
    4.81  
    4.82  /**
    4.83   * A <code>printf()</code> like function which writes the output to a stream by

mercurial