Mon, 13 Oct 2025 21:31:58 +0200
update ucx
| 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 | #ifdef __cplusplus | |
| 44 | extern "C" { | |
| 45 | #endif | |
| 46 | ||
| 47 | /** Internal structure for a key within a hash map. */ | |
| 48 | struct cx_hash_key_s { | |
| 845 | 49 | /** |
| 50 | * The key data. | |
| 51 | * May be NULL when the hash is collision-free. | |
| 52 | */ | |
| 324 | 53 | const void *data; |
| 174 | 54 | /** |
| 55 | * The key data length. | |
| 56 | */ | |
| 57 | size_t len; | |
| 58 | /** The hash value of the key data. */ | |
| 845 | 59 | uint64_t hash; |
| 174 | 60 | }; |
| 61 | ||
| 62 | /** | |
| 63 | * Type for a hash key. | |
| 64 | */ | |
| 65 | typedef struct cx_hash_key_s CxHashKey; | |
| 66 | ||
| 67 | /** | |
| 440 | 68 | * Computes a murmur2 32-bit hash. |
| 174 | 69 | * |
| 440 | 70 | * You need to initialize @c data and @c len in the key struct. |
| 174 | 71 | * The hash is then directly written to that struct. |
| 72 | * | |
| 440 | 73 | * Usually you should not need this function. |
| 74 | * Use cx_hash_key(), instead. | |
| 75 | * | |
| 76 | * @note If @c data is @c NULL, the hash is defined as 1574210520. | |
| 174 | 77 | * |
| 78 | * @param key the key, the hash shall be computed for | |
| 440 | 79 | * @see cx_hash_key() |
| 174 | 80 | */ |
| 440 | 81 | cx_attr_nonnull |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
82 | cx_attr_export |
| 174 | 83 | void cx_hash_murmur(CxHashKey *key); |
| 84 | ||
| 85 | /** | |
| 845 | 86 | * Mixes up a 32-bit integer to be used as a hash. |
| 87 | * | |
| 88 | * This function produces no collisions and has a good statistical distribution. | |
| 89 | * | |
| 90 | * @param x the integer | |
| 91 | * @return the hash | |
| 92 | */ | |
| 93 | cx_attr_export | |
| 94 | uint32_t cx_hash_u32(uint32_t x); | |
| 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 | */ | |
| 104 | cx_attr_export | |
| 105 | uint64_t cx_hash_u64(uint64_t x); | |
| 106 | ||
| 107 | /** | |
| 108 | * Computes a hash key from a 32-bit integer. | |
| 109 | * | |
| 110 | * @param x the integer | |
| 111 | * @return the hash key | |
| 112 | */ | |
| 113 | cx_attr_nodiscard | |
| 114 | cx_attr_export | |
| 115 | CxHashKey cx_hash_key_u32(uint32_t x); | |
| 116 | ||
| 117 | /** | |
| 118 | * Computes a hash key from a 64-bit integer. | |
| 119 | * | |
| 120 | * @param x the integer | |
| 121 | * @return the hash key | |
| 122 | */ | |
| 123 | cx_attr_nodiscard | |
| 124 | cx_attr_export | |
| 125 | CxHashKey cx_hash_key_u64(uint64_t x); | |
| 126 | ||
| 127 | /** | |
| 174 | 128 | * Computes a hash key from a string. |
| 129 | * | |
| 130 | * The string needs to be zero-terminated. | |
| 131 | * | |
| 132 | * @param str the string | |
| 133 | * @return the hash key | |
| 134 | */ | |
| 440 | 135 | cx_attr_nodiscard |
| 136 | cx_attr_cstr_arg(1) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
137 | cx_attr_export |
| 324 | 138 | CxHashKey cx_hash_key_str(const char *str); |
| 174 | 139 | |
| 140 | /** | |
| 845 | 141 | * Computes a hash key from a string. |
| 142 | * | |
| 143 | * Use this function when the string is represented | |
| 144 | * as an unsigned char array. | |
| 145 | * | |
| 146 | * The string needs to be zero-terminated. | |
| 147 | * | |
| 148 | * @param str the string | |
| 149 | * @return the hash key | |
| 150 | */ | |
| 151 | cx_attr_nodiscard | |
| 152 | cx_attr_cstr_arg(1) | |
| 153 | cx_attr_export | |
| 154 | static inline CxHashKey cx_hash_key_ustr(const unsigned char *str) { | |
| 155 | return cx_hash_key_str((const char*)str); | |
| 156 | } | |
| 157 | ||
| 158 | /** | |
| 174 | 159 | * Computes a hash key from a byte array. |
| 160 | * | |
| 161 | * @param bytes the array | |
| 162 | * @param len the length | |
| 163 | * @return the hash key | |
| 164 | */ | |
| 440 | 165 | cx_attr_nodiscard |
| 166 | cx_attr_access_r(1, 2) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
167 | cx_attr_export |
| 174 | 168 | CxHashKey cx_hash_key_bytes( |
| 324 | 169 | const unsigned char *bytes, |
| 174 | 170 | size_t len |
| 171 | ); | |
| 172 | ||
| 173 | /** | |
| 174 | * Computes a hash key for an arbitrary object. | |
| 175 | * | |
| 176 | * The computation uses the in-memory representation that might not be | |
| 177 | * the same on different platforms. Therefore, this hash should not be | |
| 178 | * used for data exchange with different machines. | |
| 179 | * | |
| 180 | * @param obj a pointer to an arbitrary object | |
| 845 | 181 | * @param len the length of the object in memory |
| 174 | 182 | * @return the hash key |
| 183 | */ | |
| 440 | 184 | cx_attr_nodiscard |
| 185 | cx_attr_access_r(1, 2) | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
186 | cx_attr_export |
| 174 | 187 | CxHashKey cx_hash_key( |
| 324 | 188 | const void *obj, |
| 174 | 189 | size_t len |
| 190 | ); | |
| 191 | ||
| 192 | /** | |
| 193 | * Computes a hash key from a UCX string. | |
| 194 | * | |
| 195 | * @param str the string | |
| 196 | * @return the hash key | |
| 197 | */ | |
| 440 | 198 | cx_attr_nodiscard |
| 199 | static inline CxHashKey cx_hash_key_cxstr(cxstring str) { | |
| 200 | return cx_hash_key(str.ptr, str.length); | |
| 201 | } | |
| 202 | ||
| 203 | /** | |
| 204 | * Computes a hash key from a UCX string. | |
| 205 | * | |
| 845 | 206 | * @param str the string |
| 207 | * @return the hash key | |
| 208 | */ | |
| 209 | cx_attr_nodiscard | |
| 210 | static inline CxHashKey cx_hash_key_mutstr(cxmutstr str) { | |
| 211 | return cx_hash_key(str.ptr, str.length); | |
| 212 | } | |
| 213 | ||
| 214 | /** | |
| 215 | * The identity function for the CX_HASH_KEY() macro. | |
| 216 | * You should never need to use this manually. | |
| 217 | * | |
| 218 | * @param key the key | |
| 219 | * @return a copy of the key | |
| 220 | */ | |
| 221 | cx_attr_nodiscard | |
| 222 | static inline CxHashKey cx_hash_key_identity(CxHashKey key) { | |
| 223 | return key; | |
| 224 | } | |
| 225 | ||
| 226 | #ifndef __cplusplus | |
| 227 | /** | |
| 228 | * Creates a hash key from any of the supported types with implicit length. | |
| 229 | * | |
| 230 | * Does nothing when passing a CxHashkey. | |
| 231 | * | |
| 232 | * Supported types are UCX strings, zero-terminated C strings, | |
| 233 | * and 32-bit or 64-bit unsigned integers. | |
| 234 | * | |
| 235 | * @param key the key data | |
| 236 | * @returns the @c CxHashKey | |
| 237 | */ | |
| 238 | #define CX_HASH_KEY(key) _Generic((key), \ | |
| 239 | CxHashKey: cx_hash_key_identity, \ | |
| 240 | cxstring: cx_hash_key_cxstr, \ | |
| 241 | cxmutstr: cx_hash_key_mutstr, \ | |
| 242 | char*: cx_hash_key_str, \ | |
| 243 | const char*: cx_hash_key_str, \ | |
| 244 | unsigned char*: cx_hash_key_ustr, \ | |
| 245 | const unsigned char*: cx_hash_key_ustr, \ | |
| 246 | uint32_t: cx_hash_key_u32, \ | |
| 247 | uint64_t: cx_hash_key_u64) \ | |
| 248 | (key) | |
| 249 | #endif // __cplusplus | |
| 250 | ||
| 251 | /** | |
| 252 | * Computes a hash key from a UCX string. | |
| 253 | * Convenience macro that accepts both cxstring and cxmutstr. | |
| 254 | * @deprecated use the CX_HASH_KEY() macro instead | |
| 440 | 255 | * @param str (@c cxstring or @c cxmutstr) the string |
| 256 | * @return (@c CxHashKey) the hash key | |
| 257 | */ | |
| 258 | #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) | |
| 174 | 259 | |
| 845 | 260 | /** |
| 261 | * Compare function for hash keys. | |
| 262 | * | |
| 263 | * @param left the first key | |
| 264 | * @param right the second key | |
| 265 | * @return zero when the keys equal, non-zero when they differ | |
| 266 | */ | |
| 267 | cx_attr_nodiscard | |
| 268 | cx_attr_nonnull | |
| 269 | cx_attr_export | |
| 270 | int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right); | |
| 271 | ||
| 174 | 272 | #ifdef __cplusplus |
| 273 | } // extern "C" | |
| 845 | 274 | |
| 275 | // ---------------------------------------------------------- | |
| 276 | // Overloads of CX_HASH_KEY (the C++ version of a _Generic) | |
| 277 | // ---------------------------------------------------------- | |
| 278 | ||
| 279 | static inline CxHashKey CX_HASH_KEY(CxHashKey key) { | |
| 280 | return key; | |
| 281 | } | |
| 282 | ||
| 283 | static inline CxHashKey CX_HASH_KEY(cxstring str) { | |
| 284 | return cx_hash_key_cxstr(str); | |
| 285 | } | |
| 286 | ||
| 287 | static inline CxHashKey CX_HASH_KEY(cxmutstr str) { | |
| 288 | return cx_hash_key_mutstr(str); | |
| 289 | } | |
| 290 | ||
| 291 | static inline CxHashKey CX_HASH_KEY(const char *str) { | |
| 292 | return cx_hash_key_str(str); | |
| 293 | } | |
| 294 | ||
| 295 | static inline CxHashKey CX_HASH_KEY(const unsigned char *str) { | |
| 296 | return cx_hash_key_ustr(str); | |
| 297 | } | |
| 298 | ||
| 299 | static inline CxHashKey CX_HASH_KEY(uint32_t key) { | |
| 300 | return cx_hash_key_u32(key); | |
| 301 | } | |
| 302 | ||
| 303 | static inline CxHashKey CX_HASH_KEY(uint64_t key) { | |
| 304 | return cx_hash_key_u64(key); | |
| 305 | } | |
| 174 | 306 | #endif |
| 307 | ||
| 308 | #endif // UCX_HASH_KEY_H |