ucx/utils.c

changeset 157
0b33b9396851
parent 152
62921b370c60
child 162
18892c0a9adc
equal deleted inserted replaced
156:62f1a55535e7 157:0b33b9396851
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2016 Olaf Wintermann. All rights reserved. 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "utils.h" 29 #include "ucx/utils.h"
30
30 #include <math.h> 31 #include <math.h>
31 #include <stdio.h> 32 #include <stdio.h>
32 #include <limits.h> 33 #include <limits.h>
33 #include <errno.h> 34 #include <errno.h>
34 35
85 return ncp; 86 return ncp;
86 } 87 }
87 88
88 /* COMPARE FUNCTIONS */ 89 /* COMPARE FUNCTIONS */
89 90
90 int ucx_strcmp(const void *s1, const void *s2, void *data) { 91 int ucx_cmp_str(const void *s1, const void *s2, void *data) {
91 return strcmp((const char*)s1, (const char*)s2); 92 return strcmp((const char*)s1, (const char*)s2);
92 } 93 }
93 94
94 int ucx_strncmp(const void *s1, const void *s2, void *n) { 95 int ucx_cmp_strn(const void *s1, const void *s2, void *n) {
95 return strncmp((const char*)s1, (const char*)s2, *((size_t*) n)); 96 return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
96 } 97 }
97 98
98 int ucx_intcmp(const void *i1, const void *i2, void *data) { 99 int ucx_cmp_sstr(const void *s1, const void *s2, void *data) {
100 sstr_t a = *(const sstr_t*) s1;
101 sstr_t b = *(const sstr_t*) s2;
102 return sstrcmp(a, b);
103 }
104
105 int ucx_cmp_int(const void *i1, const void *i2, void *data) {
99 int a = *((const int*) i1); 106 int a = *((const int*) i1);
100 int b = *((const int*) i2); 107 int b = *((const int*) i2);
101 if (a == b) { 108 if (a == b) {
102 return 0; 109 return 0;
103 } else { 110 } else {
104 return a < b ? -1 : 1; 111 return a < b ? -1 : 1;
105 } 112 }
106 } 113 }
107 114
108 int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) { 115 int ucx_cmp_longint(const void *i1, const void *i2, void *data) {
116 int a = *((const long int*) i1);
117 int b = *((const long int*) i2);
118 if (a == b) {
119 return 0;
120 } else {
121 return a < b ? -1 : 1;
122 }
123 }
124
125 intmax_t ucx_dist_int(const void *i1, const void *i2, void *data) {
126 intmax_t a = *((const int*) i1);
127 intmax_t b = *((const int*) i2);
128 return a - b;
129 }
130
131 intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data) {
132 intmax_t a = *((const long int*) i1);
133 intmax_t b = *((const long int*) i2);
134 return a - b;
135 }
136
137 int ucx_cmp_float(const void *f1, const void *f2, void *epsilon) {
109 float a = *((const float*) f1); 138 float a = *((const float*) f1);
110 float b = *((const float*) f2); 139 float b = *((const float*) f2);
111 float e = !epsilon ? 1e-6f : *((float*)epsilon); 140 float e = !epsilon ? 1e-6f : *((float*)epsilon);
112 if (fabsf(a - b) < e) { 141 if (fabsf(a - b) < e) {
113 return 0; 142 return 0;
114 } else { 143 } else {
115 return a < b ? -1 : 1; 144 return a < b ? -1 : 1;
116 } 145 }
117 } 146 }
118 147
119 int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) { 148 int ucx_cmp_double(const void *d1, const void *d2, void *epsilon) {
120 double a = *((const double*) d1); 149 double a = *((const double*) d1);
121 double b = *((const double*) d2); 150 double b = *((const double*) d2);
122 double e = !epsilon ? 1e-14 : *((double*)epsilon); 151 double e = !epsilon ? 1e-14 : *((double*)epsilon);
123 if (fabs(a - b) < e) { 152 if (fabs(a - b) < e) {
124 return 0; 153 return 0;
125 } else { 154 } else {
126 return a < b ? -1 : 1; 155 return a < b ? -1 : 1;
127 } 156 }
128 } 157 }
129 158
130 int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) { 159 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data) {
131 const intptr_t p1 = (const intptr_t) ptr1; 160 const intptr_t p1 = (const intptr_t) ptr1;
132 const intptr_t p2 = (const intptr_t) ptr2; 161 const intptr_t p2 = (const intptr_t) ptr2;
133 if (p1 == p2) { 162 if (p1 == p2) {
134 return 0; 163 return 0;
135 } else { 164 } else {
136 return p1 < p2 ? -1 : 1; 165 return p1 < p2 ? -1 : 1;
137 } 166 }
138 } 167 }
139 168
140 int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) { 169 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n) {
141 return memcmp(ptr1, ptr2, *((size_t*)n)); 170 return memcmp(ptr1, ptr2, *((size_t*)n));
142 } 171 }
143 172
144 /* PRINTF FUNCTIONS */ 173 /* PRINTF FUNCTIONS */
145 174

mercurial