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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *i1, void const *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(void const *f1, void const *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 } |