ucx/comparator.c

Wed, 27 Feb 2013 10:57:40 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 10:57:40 +0100
changeset 92
7625a8efcc97
parent 91
91595a45fad6
permissions
-rw-r--r--

added floating point comparators

     1 #include "comparator.h"
     2 #include "math.h"
     4 int ucx_strcmp(void *s1, void *s2, void *data) {
     5     return strcmp((char*)s1, (char*)s2);
     6 }
     8 int ucx_strncmp(void *s1, void *s2, void *n) {
     9     return strncmp((char*)s1, (char*)s2, *((size_t*) n));
    10 }
    12 int ucx_intcmp(void *i1, void *i2, void *data) {
    13    int a = *((int*) i1);
    14    int b = *((int*) i2);
    15    if (a == b) {
    16        return 0;
    17    } else {
    18        return a < b ? -1 : 1;
    19    }
    20 }
    22 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
    23    float a = *((float*) f1);
    24    float b = *((float*) f2);
    25    float e = !epsilon ? 1e-6f : *((float*)epsilon);
    26    if (fabsf(a - b) < e) {
    27        return 0;
    28    } else {
    29        return a < b ? -1 : 1;
    30    }
    31 }
    33 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
    34    double a = *((float*) d1);
    35    double b = *((float*) d2);
    36    double e = !epsilon ? 1e-14 : *((double*)epsilon);
    37    if (fabs(a - b) < e) {
    38        return 0;
    39    } else {
    40        return a < b ? -1 : 1;
    41    }
    42 }
    44 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
    45     if (ptr1 == ptr2) {
    46         return 0;
    47     } else {
    48         return ptr1 < ptr2 ? -1 : 1;
    49     }
    50 }
    52 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
    53     return memcmp(ptr1, ptr2, *((size_t*)n));
    54 }

mercurial