26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "cx/compare.h" |
29 #include "cx/compare.h" |
30 |
30 |
31 #include <stdint.h> |
|
32 #include <math.h> |
31 #include <math.h> |
33 |
32 |
34 int cx_cmp_int(void const *i1, void const *i2) { |
33 int cx_cmp_int(void const *i1, void const *i2) { |
35 int a = *((const int*) i1); |
34 int a = *((const int *) i1); |
36 int b = *((const int*) i2); |
35 int b = *((const int *) i2); |
37 if (a == b) { |
36 if (a == b) { |
38 return 0; |
37 return 0; |
39 } else { |
38 } else { |
40 return a < b ? -1 : 1; |
39 return a < b ? -1 : 1; |
41 } |
40 } |
42 } |
41 } |
43 |
42 |
44 int cx_cmp_longint(void const *i1, void const *i2) { |
43 int cx_cmp_longint(void const *i1, void const *i2) { |
45 long int a = *((const long int*) i1); |
44 long int a = *((const long int *) i1); |
46 long int b = *((const long int*) i2); |
45 long int b = *((const long int *) i2); |
47 if (a == b) { |
46 if (a == b) { |
48 return 0; |
47 return 0; |
49 } else { |
48 } else { |
50 return a < b ? -1 : 1; |
49 return a < b ? -1 : 1; |
51 } |
50 } |
52 } |
51 } |
53 |
52 |
54 int cx_cmp_longlong(void const *i1, void const *i2) { |
53 int cx_cmp_longlong(void const *i1, void const *i2) { |
55 long long a = *((const long long*) i1); |
54 long long a = *((const long long *) i1); |
56 long long b = *((const long long*) i2); |
55 long long b = *((const long long *) i2); |
57 if (a == b) { |
56 if (a == b) { |
58 return 0; |
57 return 0; |
59 } else { |
58 } else { |
60 return a < b ? -1 : 1; |
59 return a < b ? -1 : 1; |
61 } |
60 } |
62 } |
61 } |
63 |
62 |
64 int cx_cmp_int16(void const *i1, void const *i2) { |
63 int cx_cmp_int16(void const *i1, void const *i2) { |
65 int16_t a = *((const int16_t*) i1); |
64 int16_t a = *((const int16_t *) i1); |
66 int16_t b = *((const int16_t*) i2); |
65 int16_t b = *((const int16_t *) i2); |
67 if (a == b) { |
66 if (a == b) { |
68 return 0; |
67 return 0; |
69 } else { |
68 } else { |
70 return a < b ? -1 : 1; |
69 return a < b ? -1 : 1; |
71 } |
70 } |
72 } |
71 } |
73 |
72 |
74 int cx_cmp_int32(void const *i1, void const *i2) { |
73 int cx_cmp_int32(void const *i1, void const *i2) { |
75 int32_t a = *((const int32_t*) i1); |
74 int32_t a = *((const int32_t *) i1); |
76 int32_t b = *((const int32_t*) i2); |
75 int32_t b = *((const int32_t *) i2); |
77 if (a == b) { |
76 if (a == b) { |
78 return 0; |
77 return 0; |
79 } else { |
78 } else { |
80 return a < b ? -1 : 1; |
79 return a < b ? -1 : 1; |
81 } |
80 } |
82 } |
81 } |
83 |
82 |
84 int cx_cmp_int64(void const *i1, void const *i2) { |
83 int cx_cmp_int64(void const *i1, void const *i2) { |
85 int64_t a = *((const int64_t*) i1); |
84 int64_t a = *((const int64_t *) i1); |
86 int64_t b = *((const int64_t*) i2); |
85 int64_t b = *((const int64_t *) i2); |
87 if (a == b) { |
86 if (a == b) { |
88 return 0; |
87 return 0; |
89 } else { |
88 } else { |
90 return a < b ? -1 : 1; |
89 return a < b ? -1 : 1; |
91 } |
90 } |
92 } |
91 } |
93 |
92 |
94 int cx_cmp_uint(void const *i1, void const *i2) { |
93 int cx_cmp_uint(void const *i1, void const *i2) { |
95 unsigned int a = *((const unsigned int*) i1); |
94 unsigned int a = *((const unsigned int *) i1); |
96 unsigned int b = *((const unsigned int*) i2); |
95 unsigned int b = *((const unsigned int *) i2); |
97 if (a == b) { |
96 if (a == b) { |
98 return 0; |
97 return 0; |
99 } else { |
98 } else { |
100 return a < b ? -1 : 1; |
99 return a < b ? -1 : 1; |
101 } |
100 } |
102 } |
101 } |
103 |
102 |
104 int cx_cmp_ulongint(void const *i1, void const *i2) { |
103 int cx_cmp_ulongint(void const *i1, void const *i2) { |
105 unsigned long int a = *((const unsigned long int*) i1); |
104 unsigned long int a = *((const unsigned long int *) i1); |
106 unsigned long int b = *((const unsigned long int*) i2); |
105 unsigned long int b = *((const unsigned long int *) i2); |
107 if (a == b) { |
106 if (a == b) { |
108 return 0; |
107 return 0; |
109 } else { |
108 } else { |
110 return a < b ? -1 : 1; |
109 return a < b ? -1 : 1; |
111 } |
110 } |
112 } |
111 } |
113 |
112 |
114 int cx_cmp_ulonglong(void const *i1, void const *i2) { |
113 int cx_cmp_ulonglong(void const *i1, void const *i2) { |
115 unsigned long long a = *((const unsigned long long*) i1); |
114 unsigned long long a = *((const unsigned long long *) i1); |
116 unsigned long long b = *((const unsigned long long*) i2); |
115 unsigned long long b = *((const unsigned long long *) i2); |
117 if (a == b) { |
116 if (a == b) { |
118 return 0; |
117 return 0; |
119 } else { |
118 } else { |
120 return a < b ? -1 : 1; |
119 return a < b ? -1 : 1; |
121 } |
120 } |
122 } |
121 } |
123 |
122 |
124 int cx_cmp_uint16(void const *i1, void const *i2) { |
123 int cx_cmp_uint16(void const *i1, void const *i2) { |
125 uint16_t a = *((const uint16_t*) i1); |
124 uint16_t a = *((const uint16_t *) i1); |
126 uint16_t b = *((const uint16_t*) i2); |
125 uint16_t b = *((const uint16_t *) i2); |
127 if (a == b) { |
126 if (a == b) { |
128 return 0; |
127 return 0; |
129 } else { |
128 } else { |
130 return a < b ? -1 : 1; |
129 return a < b ? -1 : 1; |
131 } |
130 } |
132 } |
131 } |
133 |
132 |
134 int cx_cmp_uint32(void const *i1, void const *i2) { |
133 int cx_cmp_uint32(void const *i1, void const *i2) { |
135 uint32_t a = *((const uint32_t*) i1); |
134 uint32_t a = *((const uint32_t *) i1); |
136 uint32_t b = *((const uint32_t*) i2); |
135 uint32_t b = *((const uint32_t *) i2); |
137 if (a == b) { |
136 if (a == b) { |
138 return 0; |
137 return 0; |
139 } else { |
138 } else { |
140 return a < b ? -1 : 1; |
139 return a < b ? -1 : 1; |
141 } |
140 } |
142 } |
141 } |
143 |
142 |
144 int cx_cmp_uint64(void const *i1, void const *i2) { |
143 int cx_cmp_uint64(void const *i1, void const *i2) { |
145 uint64_t a = *((const uint64_t*) i1); |
144 uint64_t a = *((const uint64_t *) i1); |
146 uint64_t b = *((const uint64_t*) i2); |
145 uint64_t b = *((const uint64_t *) i2); |
147 if (a == b) { |
146 if (a == b) { |
148 return 0; |
147 return 0; |
149 } else { |
148 } else { |
150 return a < b ? -1 : 1; |
149 return a < b ? -1 : 1; |
151 } |
150 } |
152 } |
151 } |
153 |
152 |
154 int cx_cmp_float(void const *f1, void const *f2) { |
153 int cx_cmp_float(void const *f1, void const *f2) { |
155 float a = *((const float*) f1); |
154 float a = *((const float *) f1); |
156 float b = *((const float*) f2); |
155 float b = *((const float *) f2); |
157 if (fabsf(a - b) < 1e-6f) { |
156 if (fabsf(a - b) < 1e-6f) { |
158 return 0; |
157 return 0; |
159 } else { |
158 } else { |
160 return a < b ? -1 : 1; |
159 return a < b ? -1 : 1; |
161 } |
160 } |
162 } |
161 } |
163 |
162 |
164 int cx_cmp_double(void const *d1, void const *d2) { |
163 int cx_cmp_double( |
165 double a = *((const double*) d1); |
164 void const *d1, |
166 double b = *((const double*) d2); |
165 void const *d2 |
|
166 ) { |
|
167 double a = *((const double *) d1); |
|
168 double b = *((const double *) d2); |
167 if (fabs(a - b) < 1e-14) { |
169 if (fabs(a - b) < 1e-14) { |
168 return 0; |
170 return 0; |
169 } else { |
171 } else { |
170 return a < b ? -1 : 1; |
172 return a < b ? -1 : 1; |
171 } |
173 } |
172 } |
174 } |
173 |
175 |
174 int cx_cmp_ptr(void const *ptr1, void const *ptr2) { |
176 int cx_cmp_intptr( |
175 const intptr_t p1 = (const intptr_t) ptr1; |
177 void const *ptr1, |
176 const intptr_t p2 = (const intptr_t) ptr2; |
178 void const *ptr2 |
|
179 ) { |
|
180 intptr_t p1 = *(const intptr_t *) ptr1; |
|
181 intptr_t p2 = *(const intptr_t *) ptr2; |
177 if (p1 == p2) { |
182 if (p1 == p2) { |
178 return 0; |
183 return 0; |
179 } else { |
184 } else { |
180 return p1 < p2 ? -1 : 1; |
185 return p1 < p2 ? -1 : 1; |
181 } |
186 } |
182 } |
187 } |
|
188 |
|
189 int cx_cmp_uintptr( |
|
190 void const *ptr1, |
|
191 void const *ptr2 |
|
192 ) { |
|
193 uintptr_t p1 = *(const uintptr_t *) ptr1; |
|
194 uintptr_t p2 = *(const uintptr_t *) ptr2; |
|
195 if (p1 == p2) { |
|
196 return 0; |
|
197 } else { |
|
198 return p1 < p2 ? -1 : 1; |
|
199 } |
|
200 } |
|
201 |