Thu, 23 May 2024 15:05:24 +0200
add iterator over raw C arrays - closes #389
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 | * \file compare.h | |
30 | * \brief A collection of simple compare functions. | |
31 | * \author Mike Becker | |
32 | * \author Olaf Wintermann | |
33 | * \copyright 2-Clause BSD License | |
34 | */ | |
35 | ||
36 | #ifndef UCX_COMPARE_H | |
37 | #define UCX_COMPARE_H | |
38 | ||
650
77021e06b1a8
fix code not compiling under windows+mingw
Mike Becker <universe@uap-core.de>
parents:
631
diff
changeset
|
39 | #include "common.h" |
77021e06b1a8
fix code not compiling under windows+mingw
Mike Becker <universe@uap-core.de>
parents:
631
diff
changeset
|
40 | |
601 | 41 | #ifdef __cplusplus |
42 | extern "C" { | |
43 | #endif | |
44 | ||
786
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
45 | #ifndef CX_COMPARE_FUNC_DEFINED |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
46 | #define CX_COMPARE_FUNC_DEFINED |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
47 | /** |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
48 | * A comparator function comparing two collection elements. |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
49 | */ |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
50 | typedef int(*cx_compare_func)( |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
51 | void const *left, |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
52 | void const *right |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
53 | ); |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
54 | #endif // CX_COMPARE_FUNC_DEFINED |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
55 | |
601 | 56 | /** |
57 | * Compares two integers of type int. | |
58 | * | |
59 | * @param i1 pointer to integer one | |
60 | * @param i2 pointer to integer two | |
61 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
62 | * 1 if *i1 is greater than *i2 | |
63 | */ | |
64 | int cx_cmp_int(void const *i1, void const *i2); | |
65 | ||
66 | /** | |
67 | * Compares two integers of type long int. | |
68 | * | |
69 | * @param i1 pointer to long integer one | |
70 | * @param i2 pointer to long integer two | |
71 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
72 | * 1 if *i1 is greater than *i2 | |
73 | */ | |
74 | int cx_cmp_longint(void const *i1, void const *i2); | |
75 | ||
76 | /** | |
77 | * Compares two integers of type long long. | |
78 | * | |
79 | * @param i1 pointer to long long one | |
80 | * @param i2 pointer to long long two | |
81 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
82 | * 1 if *i1 is greater than *i2 | |
83 | */ | |
84 | int cx_cmp_longlong(void const *i1, void const *i2); | |
85 | ||
86 | /** | |
87 | * Compares two integers of type int16_t. | |
88 | * | |
89 | * @param i1 pointer to int16_t one | |
90 | * @param i2 pointer to int16_t two | |
91 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
92 | * 1 if *i1 is greater than *i2 | |
93 | */ | |
94 | int cx_cmp_int16(void const *i1, void const *i2); | |
95 | ||
96 | /** | |
97 | * Compares two integers of type int32_t. | |
98 | * | |
99 | * @param i1 pointer to int32_t one | |
100 | * @param i2 pointer to int32_t two | |
101 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
102 | * 1 if *i1 is greater than *i2 | |
103 | */ | |
104 | int cx_cmp_int32(void const *i1, void const *i2); | |
105 | ||
106 | /** | |
107 | * Compares two integers of type int64_t. | |
108 | * | |
109 | * @param i1 pointer to int64_t one | |
110 | * @param i2 pointer to int64_t two | |
111 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
112 | * 1 if *i1 is greater than *i2 | |
113 | */ | |
114 | int cx_cmp_int64(void const *i1, void const *i2); | |
115 | ||
116 | /** | |
117 | * Compares two integers of type unsigned int. | |
118 | * | |
119 | * @param i1 pointer to unsigned integer one | |
120 | * @param i2 pointer to unsigned integer two | |
121 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
122 | * 1 if *i1 is greater than *i2 | |
123 | */ | |
124 | int cx_cmp_uint(void const *i1, void const *i2); | |
125 | ||
126 | /** | |
127 | * Compares two integers of type unsigned long int. | |
128 | * | |
129 | * @param i1 pointer to unsigned long integer one | |
130 | * @param i2 pointer to unsigned long integer two | |
131 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
132 | * 1 if *i1 is greater than *i2 | |
133 | */ | |
134 | int cx_cmp_ulongint(void const *i1, void const *i2); | |
135 | ||
136 | /** | |
137 | * Compares two integers of type unsigned long long. | |
138 | * | |
139 | * @param i1 pointer to unsigned long long one | |
140 | * @param i2 pointer to unsigned long long two | |
141 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
142 | * 1 if *i1 is greater than *i2 | |
143 | */ | |
144 | int cx_cmp_ulonglong(void const *i1, void const *i2); | |
145 | ||
146 | /** | |
147 | * Compares two integers of type uint16_t. | |
148 | * | |
149 | * @param i1 pointer to uint16_t one | |
150 | * @param i2 pointer to uint16_t two | |
151 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
152 | * 1 if *i1 is greater than *i2 | |
153 | */ | |
154 | int cx_cmp_uint16(void const *i1, void const *i2); | |
155 | ||
156 | /** | |
157 | * Compares two integers of type uint32_t. | |
158 | * | |
159 | * @param i1 pointer to uint32_t one | |
160 | * @param i2 pointer to uint32_t two | |
161 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
162 | * 1 if *i1 is greater than *i2 | |
163 | */ | |
164 | int cx_cmp_uint32(void const *i1, void const *i2); | |
165 | ||
166 | /** | |
167 | * Compares two integers of type uint64_t. | |
168 | * | |
169 | * @param i1 pointer to uint64_t one | |
170 | * @param i2 pointer to uint64_t two | |
171 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
172 | * 1 if *i1 is greater than *i2 | |
173 | */ | |
174 | int cx_cmp_uint64(void const *i1, void const *i2); | |
175 | ||
176 | /** | |
177 | * Compares two real numbers of type float with precision 1e-6f. | |
178 | * | |
179 | * @param f1 pointer to float one | |
180 | * @param f2 pointer to float two | |
181 | * @return -1, if *f1 is less than *f2, 0 if both are equal, | |
182 | * 1 if *f1 is greater than *f2 | |
183 | */ | |
184 | ||
185 | int cx_cmp_float(void const *f1, void const *f2); | |
186 | ||
187 | /** | |
188 | * Compares two real numbers of type double with precision 1e-14. | |
189 | * | |
190 | * @param d1 pointer to double one | |
191 | * @param d2 pointer to double two | |
192 | * @return -1, if *d1 is less than *d2, 0 if both are equal, | |
193 | * 1 if *d1 is greater than *d2 | |
194 | */ | |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
195 | int cx_cmp_double( |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
196 | void const *d1, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
197 | void const *d2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
198 | ); |
601 | 199 | |
200 | /** | |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
201 | * Compares the integer representation of two pointers. |
601 | 202 | * |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
203 | * @param ptr1 pointer to pointer one (intptr_t const*) |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
204 | * @param ptr2 pointer to pointer two (intptr_t const*) |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
205 | * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
206 | * 1 if *ptr1 is greater than *ptr2 |
601 | 207 | */ |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
208 | int cx_cmp_intptr( |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
209 | void const *ptr1, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
210 | void const *ptr2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
211 | ); |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
212 | |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
213 | /** |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
214 | * Compares the unsigned integer representation of two pointers. |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
215 | * |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
216 | * @param ptr1 pointer to pointer one (uintptr_t const*) |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
217 | * @param ptr2 pointer to pointer two (uintptr_t const*) |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
218 | * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
219 | * 1 if *ptr1 is greater than *ptr2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
220 | */ |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
221 | int cx_cmp_uintptr( |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
222 | void const *ptr1, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
223 | void const *ptr2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
224 | ); |
601 | 225 | |
762
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
226 | /** |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
227 | * Compares the pointers specified in the arguments without de-referencing. |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
228 | * |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
229 | * @param ptr1 pointer one |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
230 | * @param ptr2 pointer two |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
231 | * @return -1 if ptr1 is less than ptr2, 0 if both are equal, |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
232 | * 1 if ptr1 is greater than ptr2 |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
233 | */ |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
234 | int cx_cmp_ptr( |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
235 | void const *ptr1, |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
236 | void const *ptr2 |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
237 | ); |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
238 | |
601 | 239 | #ifdef __cplusplus |
240 | } // extern "C" | |
241 | #endif | |
242 | ||
243 | #endif //UCX_COMPARE_H |