ucx/utils.c

changeset 94
57ea041df22f
parent 92
7625a8efcc97
child 103
08018864fb91
equal deleted inserted replaced
93:a6a99e721660 94:57ea041df22f
1 #include "utils.h"
2 #include "math.h"
3
4 /* COPY FUCNTIONS */
5 void* ucx_strcpy(void* s, void* data) {
6 char *str = (char*) s;
7 size_t n = 1+strlen(str);
8 char *cpy = (char*) malloc(n);
9 memcpy(cpy, str, n);
10 return cpy;
11 }
12
13 void* ucx_memcpy(void* m, void* n) {
14 size_t k = *((size_t*)n);
15 void *cpy = malloc(k);
16 memcpy(cpy, m, k);
17 return cpy;
18 }
19
20 /* COMPARE FUNCTION */
21
22 int ucx_strcmp(void *s1, void *s2, void *data) {
23 return strcmp((char*)s1, (char*)s2);
24 }
25
26 int ucx_strncmp(void *s1, void *s2, void *n) {
27 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
28 }
29
30 int ucx_intcmp(void *i1, void *i2, void *data) {
31 int a = *((int*) i1);
32 int b = *((int*) i2);
33 if (a == b) {
34 return 0;
35 } else {
36 return a < b ? -1 : 1;
37 }
38 }
39
40 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
41 float a = *((float*) f1);
42 float b = *((float*) f2);
43 float e = !epsilon ? 1e-6f : *((float*)epsilon);
44 if (fabsf(a - b) < e) {
45 return 0;
46 } else {
47 return a < b ? -1 : 1;
48 }
49 }
50
51 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
52 double a = *((float*) d1);
53 double b = *((float*) d2);
54 double e = !epsilon ? 1e-14 : *((double*)epsilon);
55 if (fabs(a - b) < e) {
56 return 0;
57 } else {
58 return a < b ? -1 : 1;
59 }
60 }
61
62 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
63 if (ptr1 == ptr2) {
64 return 0;
65 } else {
66 return ptr1 < ptr2 ? -1 : 1;
67 }
68 }
69
70 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
71 return memcmp(ptr1, ptr2, *((size_t*)n));
72 }

mercurial