ucx/compare.c

changeset 852
83fdf679df99
parent 816
839fefbdedc7
equal deleted inserted replaced
850:bbe2925eb590 852:83fdf679df99
28 28
29 #include "cx/compare.h" 29 #include "cx/compare.h"
30 30
31 #include <math.h> 31 #include <math.h>
32 32
33 int cx_cmp_int(void const *i1, void const *i2) { 33 int cx_vcmp_int(int a, int b) {
34 if (a == b) {
35 return 0;
36 } else {
37 return a < b ? -1 : 1;
38 }
39 }
40
41 int cx_cmp_int(const void *i1, const void *i2) {
34 int a = *((const int *) i1); 42 int a = *((const int *) i1);
35 int b = *((const int *) i2); 43 int b = *((const int *) i2);
36 if (a == b) { 44 return cx_vcmp_int(a, b);
37 return 0; 45 }
38 } else { 46
39 return a < b ? -1 : 1; 47 int cx_vcmp_longint(long int a, long int b) {
40 } 48 if (a == b) {
41 } 49 return 0;
42 50 } else {
43 int cx_cmp_longint(void const *i1, void const *i2) { 51 return a < b ? -1 : 1;
52 }
53 }
54
55 int cx_cmp_longint(const void *i1, const void *i2) {
44 long int a = *((const long int *) i1); 56 long int a = *((const long int *) i1);
45 long int b = *((const long int *) i2); 57 long int b = *((const long int *) i2);
46 if (a == b) { 58 return cx_vcmp_longint(a, b);
47 return 0; 59 }
48 } else { 60
49 return a < b ? -1 : 1; 61 int cx_vcmp_longlong(long long a, long long b) {
50 } 62 if (a == b) {
51 } 63 return 0;
52 64 } else {
53 int cx_cmp_longlong(void const *i1, void const *i2) { 65 return a < b ? -1 : 1;
66 }
67 }
68
69 int cx_cmp_longlong(const void *i1, const void *i2) {
54 long long a = *((const long long *) i1); 70 long long a = *((const long long *) i1);
55 long long b = *((const long long *) i2); 71 long long b = *((const long long *) i2);
56 if (a == b) { 72 return cx_vcmp_longlong(a, b);
57 return 0; 73 }
58 } else { 74
59 return a < b ? -1 : 1; 75 int cx_vcmp_int16(int16_t a, int16_t b) {
60 } 76 if (a == b) {
61 } 77 return 0;
62 78 } else {
63 int cx_cmp_int16(void const *i1, void const *i2) { 79 return a < b ? -1 : 1;
80 }
81 }
82
83 int cx_cmp_int16(const void *i1, const void *i2) {
64 int16_t a = *((const int16_t *) i1); 84 int16_t a = *((const int16_t *) i1);
65 int16_t b = *((const int16_t *) i2); 85 int16_t b = *((const int16_t *) i2);
66 if (a == b) { 86 return cx_vcmp_int16(a, b);
67 return 0; 87 }
68 } else { 88
69 return a < b ? -1 : 1; 89 int cx_vcmp_int32(int32_t a, int32_t b) {
70 } 90 if (a == b) {
71 } 91 return 0;
72 92 } else {
73 int cx_cmp_int32(void const *i1, void const *i2) { 93 return a < b ? -1 : 1;
94 }
95 }
96
97 int cx_cmp_int32(const void *i1, const void *i2) {
74 int32_t a = *((const int32_t *) i1); 98 int32_t a = *((const int32_t *) i1);
75 int32_t b = *((const int32_t *) i2); 99 int32_t b = *((const int32_t *) i2);
76 if (a == b) { 100 return cx_vcmp_int32(a, b);
77 return 0; 101 }
78 } else { 102
79 return a < b ? -1 : 1; 103 int cx_vcmp_int64(int64_t a, int64_t b) {
80 } 104 if (a == b) {
81 } 105 return 0;
82 106 } else {
83 int cx_cmp_int64(void const *i1, void const *i2) { 107 return a < b ? -1 : 1;
108 }
109 }
110
111 int cx_cmp_int64(const void *i1, const void *i2) {
84 int64_t a = *((const int64_t *) i1); 112 int64_t a = *((const int64_t *) i1);
85 int64_t b = *((const int64_t *) i2); 113 int64_t b = *((const int64_t *) i2);
86 if (a == b) { 114 return cx_vcmp_int64(a, b);
87 return 0; 115 }
88 } else { 116
89 return a < b ? -1 : 1; 117 int cx_vcmp_uint(unsigned int a, unsigned int b) {
90 } 118 if (a == b) {
91 } 119 return 0;
92 120 } else {
93 int cx_cmp_uint(void const *i1, void const *i2) { 121 return a < b ? -1 : 1;
122 }
123 }
124
125 int cx_cmp_uint(const void *i1, const void *i2) {
94 unsigned int a = *((const unsigned int *) i1); 126 unsigned int a = *((const unsigned int *) i1);
95 unsigned int b = *((const unsigned int *) i2); 127 unsigned int b = *((const unsigned int *) i2);
96 if (a == b) { 128 return cx_vcmp_uint(a, b);
97 return 0; 129 }
98 } else { 130
99 return a < b ? -1 : 1; 131 int cx_vcmp_ulongint(unsigned long int a, unsigned long int b) {
100 } 132 if (a == b) {
101 } 133 return 0;
102 134 } else {
103 int cx_cmp_ulongint(void const *i1, void const *i2) { 135 return a < b ? -1 : 1;
136 }
137 }
138
139 int cx_cmp_ulongint(const void *i1, const void *i2) {
104 unsigned long int a = *((const unsigned long int *) i1); 140 unsigned long int a = *((const unsigned long int *) i1);
105 unsigned long int b = *((const unsigned long int *) i2); 141 unsigned long int b = *((const unsigned long int *) i2);
106 if (a == b) { 142 return cx_vcmp_ulongint(a, b);
107 return 0; 143 }
108 } else { 144
109 return a < b ? -1 : 1; 145 int cx_vcmp_ulonglong(unsigned long long a, unsigned long long b) {
110 } 146 if (a == b) {
111 } 147 return 0;
112 148 } else {
113 int cx_cmp_ulonglong(void const *i1, void const *i2) { 149 return a < b ? -1 : 1;
150 }
151 }
152
153 int cx_cmp_ulonglong(const void *i1, const void *i2) {
114 unsigned long long a = *((const unsigned long long *) i1); 154 unsigned long long a = *((const unsigned long long *) i1);
115 unsigned long long b = *((const unsigned long long *) i2); 155 unsigned long long b = *((const unsigned long long *) i2);
116 if (a == b) { 156 return cx_vcmp_ulonglong(a, b);
117 return 0; 157 }
118 } else { 158
119 return a < b ? -1 : 1; 159 int cx_vcmp_uint16(uint16_t a, uint16_t b) {
120 } 160 if (a == b) {
121 } 161 return 0;
122 162 } else {
123 int cx_cmp_uint16(void const *i1, void const *i2) { 163 return a < b ? -1 : 1;
164 }
165 }
166
167 int cx_cmp_uint16(const void *i1, const void *i2) {
124 uint16_t a = *((const uint16_t *) i1); 168 uint16_t a = *((const uint16_t *) i1);
125 uint16_t b = *((const uint16_t *) i2); 169 uint16_t b = *((const uint16_t *) i2);
126 if (a == b) { 170 return cx_vcmp_uint16(a, b);
127 return 0; 171 }
128 } else { 172
129 return a < b ? -1 : 1; 173 int cx_vcmp_uint32(uint32_t a, uint32_t b) {
130 } 174 if (a == b) {
131 } 175 return 0;
132 176 } else {
133 int cx_cmp_uint32(void const *i1, void const *i2) { 177 return a < b ? -1 : 1;
178 }
179 }
180
181 int cx_cmp_uint32(const void *i1, const void *i2) {
134 uint32_t a = *((const uint32_t *) i1); 182 uint32_t a = *((const uint32_t *) i1);
135 uint32_t b = *((const uint32_t *) i2); 183 uint32_t b = *((const uint32_t *) i2);
136 if (a == b) { 184 return cx_vcmp_uint32(a, b);
137 return 0; 185 }
138 } else { 186
139 return a < b ? -1 : 1; 187 int cx_vcmp_uint64(uint64_t a, uint64_t b) {
140 } 188 if (a == b) {
141 } 189 return 0;
142 190 } else {
143 int cx_cmp_uint64(void const *i1, void const *i2) { 191 return a < b ? -1 : 1;
192 }
193 }
194
195 int cx_cmp_uint64(const void *i1, const void *i2) {
144 uint64_t a = *((const uint64_t *) i1); 196 uint64_t a = *((const uint64_t *) i1);
145 uint64_t b = *((const uint64_t *) i2); 197 uint64_t b = *((const uint64_t *) i2);
146 if (a == b) { 198 return cx_vcmp_uint64(a, b);
147 return 0; 199 }
148 } else { 200
149 return a < b ? -1 : 1; 201 int cx_vcmp_float(float a, float b) {
150 } 202 if (fabsf(a - b) < 1e-6f) {
151 } 203 return 0;
152 204 } else {
153 int cx_cmp_float(void const *f1, void const *f2) { 205 return a < b ? -1 : 1;
206 }
207 }
208
209 int cx_cmp_float(const void *f1, const void *f2) {
154 float a = *((const float *) f1); 210 float a = *((const float *) f1);
155 float b = *((const float *) f2); 211 float b = *((const float *) f2);
156 if (fabsf(a - b) < 1e-6f) { 212 return cx_vcmp_float(a, b);
213 }
214
215 int cx_vcmp_double(double a, double b) {
216 if (fabs(a - b) < 1e-14) {
157 return 0; 217 return 0;
158 } else { 218 } else {
159 return a < b ? -1 : 1; 219 return a < b ? -1 : 1;
160 } 220 }
161 } 221 }
162 222
163 int cx_cmp_double( 223 int cx_cmp_double(
164 void const *d1, 224 const void *d1,
165 void const *d2 225 const void *d2
166 ) { 226 ) {
167 double a = *((const double *) d1); 227 double a = *((const double *) d1);
168 double b = *((const double *) d2); 228 double b = *((const double *) d2);
169 if (fabs(a - b) < 1e-14) { 229 return cx_vcmp_double(a, b);
170 return 0; 230 }
171 } else { 231
172 return a < b ? -1 : 1; 232 int cx_vcmp_intptr(intptr_t p1, intptr_t p2) {
233 if (p1 == p2) {
234 return 0;
235 } else {
236 return p1 < p2 ? -1 : 1;
173 } 237 }
174 } 238 }
175 239
176 int cx_cmp_intptr( 240 int cx_cmp_intptr(
177 void const *ptr1, 241 const void *ptr1,
178 void const *ptr2 242 const void *ptr2
179 ) { 243 ) {
180 intptr_t p1 = *(const intptr_t *) ptr1; 244 intptr_t p1 = *(const intptr_t *) ptr1;
181 intptr_t p2 = *(const intptr_t *) ptr2; 245 intptr_t p2 = *(const intptr_t *) ptr2;
246 return cx_vcmp_intptr(p1, p2);
247 }
248
249 int cx_vcmp_uintptr(uintptr_t p1, uintptr_t p2) {
182 if (p1 == p2) { 250 if (p1 == p2) {
183 return 0; 251 return 0;
184 } else { 252 } else {
185 return p1 < p2 ? -1 : 1; 253 return p1 < p2 ? -1 : 1;
186 } 254 }
187 } 255 }
188 256
189 int cx_cmp_uintptr( 257 int cx_cmp_uintptr(
190 void const *ptr1, 258 const void *ptr1,
191 void const *ptr2 259 const void *ptr2
192 ) { 260 ) {
193 uintptr_t p1 = *(const uintptr_t *) ptr1; 261 uintptr_t p1 = *(const uintptr_t *) ptr1;
194 uintptr_t p2 = *(const uintptr_t *) ptr2; 262 uintptr_t p2 = *(const uintptr_t *) ptr2;
195 if (p1 == p2) { 263 return cx_vcmp_uintptr(p1, p2);
196 return 0;
197 } else {
198 return p1 < p2 ? -1 : 1;
199 }
200 } 264 }
201 265
202 int cx_cmp_ptr( 266 int cx_cmp_ptr(
203 void const *ptr1, 267 const void *ptr1,
204 void const *ptr2 268 const void *ptr2
205 ) { 269 ) {
206 uintptr_t p1 = (uintptr_t) ptr1; 270 uintptr_t p1 = (uintptr_t) ptr1;
207 uintptr_t p2 = (uintptr_t) ptr2; 271 uintptr_t p2 = (uintptr_t) ptr2;
208 if (p1 == p2) { 272 if (p1 == p2) {
209 return 0; 273 return 0;

mercurial