src/server/ucx/utils.c

changeset 71
069c152f6272
equal deleted inserted replaced
70:4e6e812c1d97 71:069c152f6272
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "utils.h"
30 #include "math.h"
31
32 /* COPY FUCNTIONS */
33 void* ucx_strcpy(void* s, void* data) {
34 char *str = (char*) s;
35 size_t n = 1+strlen(str);
36 char *cpy = (char*) malloc(n);
37 memcpy(cpy, str, n);
38 return cpy;
39 }
40
41 void* ucx_memcpy(void* m, void* n) {
42 size_t k = *((size_t*)n);
43 void *cpy = malloc(k);
44 memcpy(cpy, m, k);
45 return cpy;
46 }
47
48 /* COMPARE FUNCTION */
49
50 int ucx_strcmp(void *s1, void *s2, void *data) {
51 return strcmp((char*)s1, (char*)s2);
52 }
53
54 int ucx_strncmp(void *s1, void *s2, void *n) {
55 return strncmp((char*)s1, (char*)s2, *((size_t*) n));
56 }
57
58 int ucx_intcmp(void *i1, void *i2, void *data) {
59 int a = *((int*) i1);
60 int b = *((int*) i2);
61 if (a == b) {
62 return 0;
63 } else {
64 return a < b ? -1 : 1;
65 }
66 }
67
68 int ucx_floatcmp(void *f1, void *f2, void *epsilon) {
69 float a = *((float*) f1);
70 float b = *((float*) f2);
71 float e = !epsilon ? 1e-6f : *((float*)epsilon);
72 if (fabsf(a - b) < e) {
73 return 0;
74 } else {
75 return a < b ? -1 : 1;
76 }
77 }
78
79 int ucx_doublecmp(void *d1, void *d2, void *epsilon) {
80 double a = *((float*) d1);
81 double b = *((float*) d2);
82 double e = !epsilon ? 1e-14 : *((double*)epsilon);
83 if (fabs(a - b) < e) {
84 return 0;
85 } else {
86 return a < b ? -1 : 1;
87 }
88 }
89
90 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) {
91 if (ptr1 == ptr2) {
92 return 0;
93 } else {
94 return ptr1 < ptr2 ? -1 : 1;
95 }
96 }
97
98 int ucx_memcmp(void *ptr1, void *ptr2, void *n) {
99 return memcmp(ptr1, ptr2, *((size_t*)n));
100 }

mercurial