ucx/comparator.c

changeset 94
57ea041df22f
parent 92
7625a8efcc97
equal deleted inserted replaced
93:a6a99e721660 94:57ea041df22f
1 #include "comparator.h"
2 #include "math.h"
3
4 int ucx_strcmp(void *s1, void *s2, void *data) {
5 return strcmp((char*)s1, (char*)s2);
6 }
7
8 int ucx_strncmp(void *s1, void *s2, void *n) {
9 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
10 }
11
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 }
21
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 }
32
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 }
43
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 }
51
52 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
53 return memcmp(ptr1, ptr2, *((size_t*)n));
54 }

mercurial