Thu, 26 Jan 2023 20:59:36 +0100
add new pointer list wrapper - resolves #234
since we need a thread local variable, this drops C99 support
601 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
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 | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "cx/compare.h" | |
30 | ||
31 | #include <stdint.h> | |
32 | #include <math.h> | |
33 | ||
34 | int cx_cmp_int(void const *i1, void const *i2) { | |
35 | int a = *((const int*) i1); | |
36 | int b = *((const int*) i2); | |
37 | if (a == b) { | |
38 | return 0; | |
39 | } else { | |
40 | return a < b ? -1 : 1; | |
41 | } | |
42 | } | |
43 | ||
44 | int cx_cmp_longint(void const *i1, void const *i2) { | |
45 | long int a = *((const long int*) i1); | |
46 | long int b = *((const long int*) i2); | |
47 | if (a == b) { | |
48 | return 0; | |
49 | } else { | |
50 | return a < b ? -1 : 1; | |
51 | } | |
52 | } | |
53 | ||
54 | int cx_cmp_longlong(void const *i1, void const *i2) { | |
55 | long long a = *((const long long*) i1); | |
56 | long long b = *((const long long*) i2); | |
57 | if (a == b) { | |
58 | return 0; | |
59 | } else { | |
60 | return a < b ? -1 : 1; | |
61 | } | |
62 | } | |
63 | ||
64 | int cx_cmp_int16(void const *i1, void const *i2) { | |
65 | int16_t a = *((const int16_t*) i1); | |
66 | int16_t b = *((const int16_t*) i2); | |
67 | if (a == b) { | |
68 | return 0; | |
69 | } else { | |
70 | return a < b ? -1 : 1; | |
71 | } | |
72 | } | |
73 | ||
74 | int cx_cmp_int32(void const *i1, void const *i2) { | |
75 | int32_t a = *((const int32_t*) i1); | |
76 | int32_t b = *((const int32_t*) i2); | |
77 | if (a == b) { | |
78 | return 0; | |
79 | } else { | |
80 | return a < b ? -1 : 1; | |
81 | } | |
82 | } | |
83 | ||
84 | int cx_cmp_int64(void const *i1, void const *i2) { | |
85 | int64_t a = *((const int64_t*) i1); | |
86 | int64_t b = *((const int64_t*) i2); | |
87 | if (a == b) { | |
88 | return 0; | |
89 | } else { | |
90 | return a < b ? -1 : 1; | |
91 | } | |
92 | } | |
93 | ||
94 | int cx_cmp_uint(void const *i1, void const *i2) { | |
95 | unsigned int a = *((const unsigned int*) i1); | |
96 | unsigned int b = *((const unsigned int*) i2); | |
97 | if (a == b) { | |
98 | return 0; | |
99 | } else { | |
100 | return a < b ? -1 : 1; | |
101 | } | |
102 | } | |
103 | ||
104 | int cx_cmp_ulongint(void const *i1, void const *i2) { | |
105 | unsigned long int a = *((const unsigned long int*) i1); | |
106 | unsigned long int b = *((const unsigned long int*) i2); | |
107 | if (a == b) { | |
108 | return 0; | |
109 | } else { | |
110 | return a < b ? -1 : 1; | |
111 | } | |
112 | } | |
113 | ||
114 | int cx_cmp_ulonglong(void const *i1, void const *i2) { | |
115 | unsigned long long a = *((const unsigned long long*) i1); | |
116 | unsigned long long b = *((const unsigned long long*) i2); | |
117 | if (a == b) { | |
118 | return 0; | |
119 | } else { | |
120 | return a < b ? -1 : 1; | |
121 | } | |
122 | } | |
123 | ||
124 | int cx_cmp_uint16(void const *i1, void const *i2) { | |
125 | uint16_t a = *((const uint16_t*) i1); | |
126 | uint16_t b = *((const uint16_t*) i2); | |
127 | if (a == b) { | |
128 | return 0; | |
129 | } else { | |
130 | return a < b ? -1 : 1; | |
131 | } | |
132 | } | |
133 | ||
134 | int cx_cmp_uint32(void const *i1, void const *i2) { | |
135 | uint32_t a = *((const uint32_t*) i1); | |
136 | uint32_t b = *((const uint32_t*) i2); | |
137 | if (a == b) { | |
138 | return 0; | |
139 | } else { | |
140 | return a < b ? -1 : 1; | |
141 | } | |
142 | } | |
143 | ||
144 | int cx_cmp_uint64(void const *i1, void const *i2) { | |
145 | uint64_t a = *((const uint64_t*) i1); | |
146 | uint64_t b = *((const uint64_t*) i2); | |
147 | if (a == b) { | |
148 | return 0; | |
149 | } else { | |
150 | return a < b ? -1 : 1; | |
151 | } | |
152 | } | |
153 | ||
154 | int cx_cmp_float(void const *f1, void const *f2) { | |
155 | float a = *((const float*) f1); | |
156 | float b = *((const float*) f2); | |
157 | if (fabsf(a - b) < 1e-6f) { | |
158 | return 0; | |
159 | } else { | |
160 | return a < b ? -1 : 1; | |
161 | } | |
162 | } | |
163 | ||
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
164 | int cx_cmp_double( |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
165 | void const *d1, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
166 | void const *d2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
167 | ) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
168 | double a = *((const double *) d1); |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
169 | double b = *((const double *) d2); |
601 | 170 | if (fabs(a - b) < 1e-14) { |
171 | return 0; | |
172 | } else { | |
173 | return a < b ? -1 : 1; | |
174 | } | |
175 | } | |
176 | ||
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
177 | int cx_cmp_intptr( |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
178 | void const *ptr1, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
179 | void const *ptr2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
180 | ) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
181 | intptr_t p1 = *(const intptr_t *) ptr1; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
182 | intptr_t p2 = *(const intptr_t *) ptr2; |
601 | 183 | if (p1 == p2) { |
184 | return 0; | |
185 | } else { | |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
186 | return p1 < p2 ? -1 : 1; |
601 | 187 | } |
188 | } | |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
189 | |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
190 | int cx_cmp_uintptr( |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
191 | void const *ptr1, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
192 | void const *ptr2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
193 | ) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
194 | uintptr_t p1 = *(const uintptr_t *) ptr1; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
195 | uintptr_t p2 = *(const uintptr_t *) ptr2; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
196 | if (p1 == p2) { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
197 | return 0; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
198 | } else { |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
199 | return p1 < p2 ? -1 : 1; |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
200 | } |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
201 | } |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
601
diff
changeset
|
202 |