Wed, 31 Dec 2025 16:40:12 +0100
update ucx to version 4.0
| 174 | 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 | /** | |
| 440 | 29 | * @file hash_key.h |
| 30 | * @brief Interface for map implementations. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 174 | 34 | */ |
| 35 | ||
| 36 | ||
| 37 | #ifndef UCX_HASH_KEY_H | |
| 38 | #define UCX_HASH_KEY_H | |
| 39 | ||
| 40 | #include "common.h" | |
| 440 | 41 | #include "string.h" |
| 174 | 42 | |
| 43 | /** Internal structure for a key within a hash map. */ | |
| 44 | struct cx_hash_key_s { | |
| 845 | 45 | /** |
| 46 | * The key data. | |
| 47 | * May be NULL when the hash is collision-free. | |
| 48 | */ | |
| 324 | 49 | const void *data; |
| 174 | 50 | /** |
| 51 | * The key data length. | |
| 52 | */ | |
| 53 | size_t len; | |
| 54 | /** The hash value of the key data. */ | |
| 845 | 55 | uint64_t hash; |
| 174 | 56 | }; |
| 57 | ||
| 58 | /** | |
| 59 | * Type for a hash key. | |
| 60 | */ | |
| 61 | typedef struct cx_hash_key_s CxHashKey; | |
| 62 | ||
| 63 | /** | |
| 440 | 64 | * Computes a murmur2 32-bit hash. |
| 174 | 65 | * |
| 440 | 66 | * You need to initialize @c data and @c len in the key struct. |
| 174 | 67 | * The hash is then directly written to that struct. |
| 68 | * | |
| 440 | 69 | * Usually you should not need this function. |
| 70 | * Use cx_hash_key(), instead. | |
| 71 | * | |
| 72 | * @note If @c data is @c NULL, the hash is defined as 1574210520. | |
| 174 | 73 | * |
| 74 | * @param key the key, the hash shall be computed for | |
| 440 | 75 | * @see cx_hash_key() |
| 174 | 76 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
77 | CX_EXTERN CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
78 | void cx_hash_murmur(CxHashKey *key); |
| 174 | 79 | |
| 80 | /** | |
| 845 | 81 | * Mixes up a 32-bit integer to be used as a hash. |
| 82 | * | |
| 83 | * This function produces no collisions and has a good statistical distribution. | |
| 84 | * | |
| 85 | * @param x the integer | |
| 86 | * @return the hash | |
| 87 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
88 | CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
89 | uint32_t cx_hash_u32(uint32_t x) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
90 | x = ((x >> 16) ^ x) * 0x45d9f3bu; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
91 | x = ((x >> 16) ^ x) * 0x45d9f3bu; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
92 | x = (x >> 16) ^ x; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
93 | return x; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
94 | } |
| 845 | 95 | |
| 96 | /** | |
| 97 | * Mixes up a 64-bit integer to be used as a hash. | |
| 98 | * | |
| 99 | * This function produces no collisions and has a good statistical distribution. | |
| 100 | * | |
| 101 | * @param x the integer | |
| 102 | * @return the hash | |
| 103 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
104 | CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
105 | uint64_t cx_hash_u64(uint64_t x){ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
106 | x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
107 | x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
108 | x = x ^ (x >> 31); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
109 | return x; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
110 | } |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
111 | |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
112 | /** |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
113 | * Computes a hash key for an arbitrary object. |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
114 | * |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
115 | * The computation uses the in-memory representation that might not be |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
116 | * the same on different platforms. Therefore, this hash should not be |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
117 | * used for data exchange with different machines. |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
118 | * |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
119 | * @param obj a pointer to an arbitrary object |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
120 | * @param len the length of the object in memory |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
121 | * @return the hash key |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
122 | */ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
123 | CX_EXTERN CX_NODISCARD CX_ACCESS_R(1, 2) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
124 | CxHashKey cx_hash_key(const void *obj, size_t len); |
| 845 | 125 | |
| 126 | /** | |
| 127 | * Computes a hash key from a 32-bit integer. | |
| 128 | * | |
| 129 | * @param x the integer | |
| 130 | * @return the hash key | |
| 131 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
132 | CX_NODISCARD CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
133 | CxHashKey cx_hash_key_u32(uint32_t x) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
134 | CxHashKey key; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
135 | key.data = NULL; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
136 | key.len = 0; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
137 | key.hash = cx_hash_u32(x); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
138 | return key; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
139 | } |
| 845 | 140 | |
| 141 | /** | |
| 142 | * Computes a hash key from a 64-bit integer. | |
| 143 | * | |
| 144 | * @param x the integer | |
| 145 | * @return the hash key | |
| 146 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
147 | CX_NODISCARD CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
148 | CxHashKey cx_hash_key_u64(uint64_t x) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
149 | CxHashKey key; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
150 | key.data = NULL; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
151 | key.len = 0; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
152 | key.hash = cx_hash_u64(x); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
153 | return key; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
154 | } |
| 845 | 155 | |
| 156 | /** | |
| 174 | 157 | * Computes a hash key from a string. |
| 158 | * | |
| 159 | * The string needs to be zero-terminated. | |
| 160 | * | |
| 161 | * @param str the string | |
| 162 | * @return the hash key | |
| 163 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
164 | CX_NODISCARD CX_CSTR_ARG(1) CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
165 | CxHashKey cx_hash_key_str(const char *str) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
166 | return cx_hash_key((const void*)str, str == NULL ? 0 : strlen(str)); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
167 | } |
| 174 | 168 | |
| 169 | /** | |
| 845 | 170 | * Computes a hash key from a string. |
| 171 | * | |
| 172 | * Use this function when the string is represented | |
| 173 | * as an unsigned char array. | |
| 174 | * | |
| 175 | * The string needs to be zero-terminated. | |
| 176 | * | |
| 177 | * @param str the string | |
| 178 | * @return the hash key | |
| 179 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
180 | CX_NODISCARD CX_CSTR_ARG(1) CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
181 | CxHashKey cx_hash_key_ustr(const unsigned char *str) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
182 | return cx_hash_key((const void*)str, str == NULL ? 0 : strlen((const char*)str)); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
183 | } |
| 845 | 184 | |
| 185 | /** | |
| 174 | 186 | * Computes a hash key from a byte array. |
| 187 | * | |
| 188 | * @param bytes the array | |
| 189 | * @param len the length | |
| 190 | * @return the hash key | |
| 191 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
192 | CX_NODISCARD CX_ACCESS_R(1, 2) CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
193 | CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
194 | return cx_hash_key((const void*)bytes, len); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
195 | } |
| 174 | 196 | |
| 197 | /** | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
198 | * Computes a hash key from a UCX string. |
| 174 | 199 | * |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
200 | * @param str the string |
| 174 | 201 | * @return the hash key |
| 202 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
203 | CX_NODISCARD CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
204 | CxHashKey cx_hash_key_cxstr(cxstring str) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
205 | return cx_hash_key((void*)str.ptr, str.length); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
206 | } |
| 174 | 207 | |
| 208 | /** | |
| 209 | * Computes a hash key from a UCX string. | |
| 210 | * | |
| 211 | * @param str the string | |
| 212 | * @return the hash key | |
| 213 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
214 | CX_NODISCARD CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
215 | CxHashKey cx_hash_key_mutstr(cxmutstr str) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
216 | return cx_hash_key((void*)str.ptr, str.length); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
217 | } |
| 845 | 218 | |
| 219 | /** | |
| 220 | * The identity function for the CX_HASH_KEY() macro. | |
| 221 | * You should never need to use this manually. | |
| 222 | * | |
| 223 | * @param key the key | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
224 | * @return a copy of the key (not the data) |
| 845 | 225 | */ |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
226 | CX_NODISCARD CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
227 | CxHashKey cx_hash_key_identity(CxHashKey key) { |
| 845 | 228 | return key; |
| 229 | } | |
| 230 | ||
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
231 | /** |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
232 | * The dereference function for the CX_HASH_KEY() macro. |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
233 | * You should never need to use this manually. |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
234 | * |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
235 | * @param key a pointer to a key |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
236 | * @return a copy of the key (not the data) |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
237 | */ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
238 | CX_NODISCARD CX_INLINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
239 | CxHashKey cx_hash_key_deref(const CxHashKey *key) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
240 | return *key; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
241 | } |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
242 | |
| 845 | 243 | #ifndef __cplusplus |
| 244 | /** | |
| 245 | * Creates a hash key from any of the supported types with implicit length. | |
| 246 | * | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
247 | * Does nothing when passing a CxHashKey and dereferences CxHashKey pointers. |
| 845 | 248 | * |
| 249 | * Supported types are UCX strings, zero-terminated C strings, | |
| 250 | * and 32-bit or 64-bit unsigned integers. | |
| 251 | * | |
| 252 | * @param key the key data | |
| 253 | * @returns the @c CxHashKey | |
| 254 | */ | |
| 255 | #define CX_HASH_KEY(key) _Generic((key), \ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
256 | CxHashKey*: cx_hash_key_deref, \ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
257 | const CxHashKey*: cx_hash_key_deref, \ |
| 845 | 258 | CxHashKey: cx_hash_key_identity, \ |
| 259 | cxstring: cx_hash_key_cxstr, \ | |
| 260 | cxmutstr: cx_hash_key_mutstr, \ | |
| 261 | char*: cx_hash_key_str, \ | |
| 262 | const char*: cx_hash_key_str, \ | |
| 263 | unsigned char*: cx_hash_key_ustr, \ | |
| 264 | const unsigned char*: cx_hash_key_ustr, \ | |
| 265 | uint32_t: cx_hash_key_u32, \ | |
| 266 | uint64_t: cx_hash_key_u64) \ | |
| 267 | (key) | |
| 268 | #endif // __cplusplus | |
| 269 | ||
| 270 | /** | |
| 271 | * Compare function for hash keys. | |
| 272 | * | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
273 | * The pointers are untyped to be compatible with the cx_compare_func signature. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
274 | * |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
275 | * @param left (@c CxHashKey*) the first key |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
276 | * @param right (@c CxHashKey*) the second key |
| 845 | 277 | * @return zero when the keys equal, non-zero when they differ |
| 278 | */ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
279 | CX_EXTERN CX_NODISCARD CX_NONNULL |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
280 | int cx_hash_key_cmp(const void *left, const void *right); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
281 | |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
282 | /** |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
283 | * Interprets the key data as a string and returns it. |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
284 | * |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
285 | * @param key the key |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
286 | * @return the key data as a string |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
287 | */ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
288 | CX_EXTERN |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
289 | cxstring cx_hash_key_as_string(const CxHashKey *key); |
| 845 | 290 | |
| 174 | 291 | #ifdef __cplusplus |
| 845 | 292 | // ---------------------------------------------------------- |
| 293 | // Overloads of CX_HASH_KEY (the C++ version of a _Generic) | |
| 294 | // ---------------------------------------------------------- | |
| 295 | ||
| 870 | 296 | CX_CPPDECL CxHashKey CX_HASH_KEY(CxHashKey key) { |
| 845 | 297 | return key; |
| 298 | } | |
| 299 | ||
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
300 | CX_CPPDECL CxHashKey CX_HASH_KEY(const CxHashKey *key) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
301 | return *key; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
302 | } |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
303 | |
| 870 | 304 | CX_CPPDECL CxHashKey CX_HASH_KEY(cxstring str) { |
| 845 | 305 | return cx_hash_key_cxstr(str); |
| 306 | } | |
| 307 | ||
| 870 | 308 | CX_CPPDECL CxHashKey CX_HASH_KEY(cxmutstr str) { |
| 845 | 309 | return cx_hash_key_mutstr(str); |
| 310 | } | |
| 311 | ||
| 870 | 312 | CX_CPPDECL CxHashKey CX_HASH_KEY(const char *str) { |
| 845 | 313 | return cx_hash_key_str(str); |
| 314 | } | |
| 315 | ||
| 870 | 316 | CX_CPPDECL CxHashKey CX_HASH_KEY(const unsigned char *str) { |
| 845 | 317 | return cx_hash_key_ustr(str); |
| 318 | } | |
| 319 | ||
| 870 | 320 | CX_CPPDECL CxHashKey CX_HASH_KEY(uint32_t key) { |
| 845 | 321 | return cx_hash_key_u32(key); |
| 322 | } | |
| 323 | ||
| 870 | 324 | CX_CPPDECL CxHashKey CX_HASH_KEY(uint64_t key) { |
| 845 | 325 | return cx_hash_key_u64(key); |
| 326 | } | |
| 174 | 327 | #endif |
| 328 | ||
| 329 | #endif // UCX_HASH_KEY_H |