ucx/compare.c

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

mercurial