ucx/utils.c

changeset 94
57ea041df22f
parent 92
7625a8efcc97
child 103
08018864fb91
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ucx/utils.c	Wed Feb 27 11:48:29 2013 +0100
     1.3 @@ -0,0 +1,72 @@
     1.4 +#include "utils.h"
     1.5 +#include "math.h"
     1.6 +
     1.7 +/* COPY FUCNTIONS */
     1.8 +void* ucx_strcpy(void* s, void* data) {
     1.9 +    char *str = (char*) s;
    1.10 +    size_t n = 1+strlen(str);
    1.11 +    char *cpy = (char*) malloc(n);
    1.12 +    memcpy(cpy, str, n);
    1.13 +    return cpy;
    1.14 +}
    1.15 +
    1.16 +void* ucx_memcpy(void* m, void* n) {
    1.17 +    size_t k = *((size_t*)n);
    1.18 +    void *cpy = malloc(k);
    1.19 +    memcpy(cpy, m, k);
    1.20 +    return cpy;
    1.21 +}
    1.22 +
    1.23 +/* COMPARE FUNCTION */
    1.24 +
    1.25 +int ucx_strcmp(void *s1, void *s2, void *data) {
    1.26 +    return strcmp((char*)s1, (char*)s2);
    1.27 +}
    1.28 +
    1.29 +int ucx_strncmp(void *s1, void *s2, void *n) {
    1.30 +    return strncmp((char*)s1, (char*)s2, *((size_t*) n));
    1.31 +}
    1.32 +
    1.33 +int ucx_intcmp(void *i1, void *i2, void *data) {
    1.34 +   int a = *((int*) i1);
    1.35 +   int b = *((int*) i2);
    1.36 +   if (a == b) {
    1.37 +       return 0;
    1.38 +   } else {
    1.39 +       return a < b ? -1 : 1;
    1.40 +   }
    1.41 +}
    1.42 +
    1.43 +int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
    1.44 +   float a = *((float*) f1);
    1.45 +   float b = *((float*) f2);
    1.46 +   float e = !epsilon ? 1e-6f : *((float*)epsilon);
    1.47 +   if (fabsf(a - b) < e) {
    1.48 +       return 0;
    1.49 +   } else {
    1.50 +       return a < b ? -1 : 1;
    1.51 +   }
    1.52 +}
    1.53 +
    1.54 +int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
    1.55 +   double a = *((float*) d1);
    1.56 +   double b = *((float*) d2);
    1.57 +   double e = !epsilon ? 1e-14 : *((double*)epsilon);
    1.58 +   if (fabs(a - b) < e) {
    1.59 +       return 0;
    1.60 +   } else {
    1.61 +       return a < b ? -1 : 1;
    1.62 +   }
    1.63 +}
    1.64 +
    1.65 +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
    1.66 +    if (ptr1 == ptr2) {
    1.67 +        return 0;
    1.68 +    } else {
    1.69 +        return ptr1 < ptr2 ? -1 : 1;
    1.70 +    }
    1.71 +}
    1.72 +
    1.73 +int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
    1.74 +    return memcmp(ptr1, ptr2, *((size_t*)n));
    1.75 +}

mercurial