ucx/cx/hash_key.h

changeset 112
c3f2f16fa4b8
parent 102
64ded9f6a6c6
child 113
dde28a806552
equal deleted inserted replaced
111:81c4f73236a4 112:c3f2f16fa4b8
44 extern "C" { 44 extern "C" {
45 #endif 45 #endif
46 46
47 /** Internal structure for a key within a hash map. */ 47 /** Internal structure for a key within a hash map. */
48 struct cx_hash_key_s { 48 struct cx_hash_key_s {
49 /** The key data. */ 49 /**
50 * The key data.
51 * May be NULL when the hash is collision-free.
52 */
50 const void *data; 53 const void *data;
51 /** 54 /**
52 * The key data length. 55 * The key data length.
53 */ 56 */
54 size_t len; 57 size_t len;
55 /** The hash value of the key data. */ 58 /** The hash value of the key data. */
56 unsigned hash; 59 uint64_t hash;
57 }; 60 };
58 61
59 /** 62 /**
60 * Type for a hash key. 63 * Type for a hash key.
61 */ 64 */
78 cx_attr_nonnull 81 cx_attr_nonnull
79 cx_attr_export 82 cx_attr_export
80 void cx_hash_murmur(CxHashKey *key); 83 void cx_hash_murmur(CxHashKey *key);
81 84
82 /** 85 /**
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 /**
83 * Computes a hash key from a string. 128 * Computes a hash key from a string.
84 * 129 *
85 * The string needs to be zero-terminated. 130 * The string needs to be zero-terminated.
86 * 131 *
87 * @param str the string 132 * @param str the string
89 */ 134 */
90 cx_attr_nodiscard 135 cx_attr_nodiscard
91 cx_attr_cstr_arg(1) 136 cx_attr_cstr_arg(1)
92 cx_attr_export 137 cx_attr_export
93 CxHashKey cx_hash_key_str(const char *str); 138 CxHashKey cx_hash_key_str(const char *str);
139
140 /**
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 }
94 157
95 /** 158 /**
96 * Computes a hash key from a byte array. 159 * Computes a hash key from a byte array.
97 * 160 *
98 * @param bytes the array 161 * @param bytes the array
113 * The computation uses the in-memory representation that might not be 176 * The computation uses the in-memory representation that might not be
114 * the same on different platforms. Therefore, this hash should not be 177 * the same on different platforms. Therefore, this hash should not be
115 * used for data exchange with different machines. 178 * used for data exchange with different machines.
116 * 179 *
117 * @param obj a pointer to an arbitrary object 180 * @param obj a pointer to an arbitrary object
118 * @param len the length of object in memory 181 * @param len the length of the object in memory
119 * @return the hash key 182 * @return the hash key
120 */ 183 */
121 cx_attr_nodiscard 184 cx_attr_nodiscard
122 cx_attr_access_r(1, 2) 185 cx_attr_access_r(1, 2)
123 cx_attr_export 186 cx_attr_export
138 } 201 }
139 202
140 /** 203 /**
141 * Computes a hash key from a UCX string. 204 * Computes a hash key from a UCX string.
142 * 205 *
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
143 * @param str (@c cxstring or @c cxmutstr) the string 255 * @param str (@c cxstring or @c cxmutstr) the string
144 * @return (@c CxHashKey) the hash key 256 * @return (@c CxHashKey) the hash key
145 */ 257 */
146 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) 258 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str))
147 259
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
148 #ifdef __cplusplus 272 #ifdef __cplusplus
149 } // extern "C" 273 } // extern "C"
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 }
150 #endif 306 #endif
151 307
152 #endif // UCX_HASH_KEY_H 308 #endif // UCX_HASH_KEY_H

mercurial