Zycore 1.4.0.0
Loading...
Searching...
No Matches
Comparison.h
Go to the documentation of this file.
1/***************************************************************************************************
2
3 Zyan Core Library (Zycore-C)
4
5 Original Author : Florian Bernd
6
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24
25***************************************************************************************************/
26
32#ifndef ZYCORE_COMPARISON_H
33#define ZYCORE_COMPARISON_H
34
35#include <Zycore/Defines.h>
36#include <Zycore/Types.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* ============================================================================================== */
43/* Enums and types */
44/* ============================================================================================== */
45
55typedef ZyanBool (*ZyanEqualityComparison)(const void* left, const void* right);
56
68typedef ZyanI32 (*ZyanComparison)(const void* left, const void* right);
69
70/* ============================================================================================== */
71/* Macros */
72/* ============================================================================================== */
73
74/* ---------------------------------------------------------------------------------------------- */
75/* Equality comparison functions */
76/* ---------------------------------------------------------------------------------------------- */
77
84#define ZYAN_DECLARE_EQUALITY_COMPARISON(name, type) \
85 ZyanBool name(const type* left, const type* right) \
86 { \
87 ZYAN_ASSERT(left); \
88 ZYAN_ASSERT(right); \
89 \
90 return (*left == *right) ? ZYAN_TRUE : ZYAN_FALSE; \
91 }
92
101#define ZYAN_DECLARE_EQUALITY_COMPARISON_FOR_FIELD(name, type, field_name) \
102 ZyanBool name(const type* left, const type* right) \
103 { \
104 ZYAN_ASSERT(left); \
105 ZYAN_ASSERT(right); \
106 \
107 return (left->field_name == right->field_name) ? ZYAN_TRUE : ZYAN_FALSE; \
108 }
109
110/* ---------------------------------------------------------------------------------------------- */
111/* Comparison functions */
112/* ---------------------------------------------------------------------------------------------- */
113
120#define ZYAN_DECLARE_COMPARISON(name, type) \
121 ZyanI32 name(const type* left, const type* right) \
122 { \
123 ZYAN_ASSERT(left); \
124 ZYAN_ASSERT(right); \
125 \
126 if (*left < *right) \
127 { \
128 return -1; \
129 } \
130 if (*left > *right) \
131 { \
132 return 1; \
133 } \
134 return 0; \
135 }
136
145#define ZYAN_DECLARE_COMPARISON_FOR_FIELD(name, type, field_name) \
146 ZyanI32 name(const type* left, const type* right) \
147 { \
148 ZYAN_ASSERT(left); \
149 ZYAN_ASSERT(right); \
150 \
151 if (left->field_name < right->field_name) \
152 { \
153 return -1; \
154 } \
155 if (left->field_name > right->field_name) \
156 { \
157 return 1; \
158 } \
159 return 0; \
160 }
161
162 /* ---------------------------------------------------------------------------------------------- */
163
164/* ============================================================================================== */
165/* Exported functions */
166/* ============================================================================================== */
167
168/* ---------------------------------------------------------------------------------------------- */
169/* Default equality comparison functions */
170/* ---------------------------------------------------------------------------------------------- */
171
181ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsPointer, void* const)
182
183
192ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsBool, ZyanBool)
193
194
203ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric8, ZyanU8)
204
214ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric16, ZyanU16)
215
225ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric32, ZyanU32)
226
236ZYAN_INLINE ZYAN_DECLARE_EQUALITY_COMPARISON(ZyanEqualsNumeric64, ZyanU64)
237
238/* ---------------------------------------------------------------------------------------------- */
239/* Default comparison functions */
240/* ---------------------------------------------------------------------------------------------- */
241
251ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanComparePointer, void* const)
252
262ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareBool, ZyanBool)
263
273ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric8, ZyanU8)
274
284ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric16, ZyanU16)
285
295ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric32, ZyanU32)
296
306ZYAN_INLINE ZYAN_DECLARE_COMPARISON(ZyanCompareNumeric64, ZyanU64)
307
308/* ---------------------------------------------------------------------------------------------- */
309
310/* ============================================================================================== */
311
312#ifdef __cplusplus
313}
314#endif
315
316#endif /* ZYCORE_COMPARISON_H */
ZyanI32(* ZyanComparison)(const void *left, const void *right)
Definition Comparison.h:68
#define ZYAN_DECLARE_COMPARISON(name, type)
Definition Comparison.h:120
ZyanBool(* ZyanEqualityComparison)(const void *left, const void *right)
Definition Comparison.h:55
#define ZYAN_DECLARE_EQUALITY_COMPARISON(name, type)
Definition Comparison.h:84
ZyanU8 ZyanBool
Definition Types.h:192