src/cx/hash_key.h

changeset 1426
3a89b31f0724
parent 1424
563033aa998c
equal deleted inserted replaced
1425:83284b289430 1426:3a89b31f0724
77 * 77 *
78 * @param key the key, the hash shall be computed for 78 * @param key the key, the hash shall be computed for
79 * @see cx_hash_key() 79 * @see cx_hash_key()
80 */ 80 */
81 cx_attr_nonnull 81 cx_attr_nonnull
82 cx_attr_export 82 CX_EXPORT void cx_hash_murmur(CxHashKey *key);
83 void cx_hash_murmur(CxHashKey *key);
84 83
85 /** 84 /**
86 * Mixes up a 32-bit integer to be used as a hash. 85 * Mixes up a 32-bit integer to be used as a hash.
87 * 86 *
88 * This function produces no collisions and has a good statistical distribution. 87 * This function produces no collisions and has a good statistical distribution.
89 * 88 *
90 * @param x the integer 89 * @param x the integer
91 * @return the hash 90 * @return the hash
92 */ 91 */
93 cx_attr_export 92 CX_EXPORT uint32_t cx_hash_u32(uint32_t x);
94 uint32_t cx_hash_u32(uint32_t x);
95 93
96 /** 94 /**
97 * Mixes up a 64-bit integer to be used as a hash. 95 * Mixes up a 64-bit integer to be used as a hash.
98 * 96 *
99 * This function produces no collisions and has a good statistical distribution. 97 * This function produces no collisions and has a good statistical distribution.
100 * 98 *
101 * @param x the integer 99 * @param x the integer
102 * @return the hash 100 * @return the hash
103 */ 101 */
104 cx_attr_export 102 CX_EXPORT uint64_t cx_hash_u64(uint64_t x);
105 uint64_t cx_hash_u64(uint64_t x);
106 103
107 /** 104 /**
108 * Computes a hash key from a 32-bit integer. 105 * Computes a hash key from a 32-bit integer.
109 * 106 *
110 * @param x the integer 107 * @param x the integer
111 * @return the hash key 108 * @return the hash key
112 */ 109 */
113 cx_attr_nodiscard 110 cx_attr_nodiscard
114 cx_attr_export 111 CX_EXPORT CxHashKey cx_hash_key_u32(uint32_t x);
115 CxHashKey cx_hash_key_u32(uint32_t x);
116 112
117 /** 113 /**
118 * Computes a hash key from a 64-bit integer. 114 * Computes a hash key from a 64-bit integer.
119 * 115 *
120 * @param x the integer 116 * @param x the integer
121 * @return the hash key 117 * @return the hash key
122 */ 118 */
123 cx_attr_nodiscard 119 cx_attr_nodiscard
124 cx_attr_export 120 CX_EXPORT CxHashKey cx_hash_key_u64(uint64_t x);
125 CxHashKey cx_hash_key_u64(uint64_t x);
126 121
127 /** 122 /**
128 * Computes a hash key from a string. 123 * Computes a hash key from a string.
129 * 124 *
130 * The string needs to be zero-terminated. 125 * The string needs to be zero-terminated.
131 * 126 *
132 * @param str the string 127 * @param str the string
133 * @return the hash key 128 * @return the hash key
134 */ 129 */
135 cx_attr_nodiscard 130 cx_attr_nodiscard cx_attr_cstr_arg(1)
136 cx_attr_cstr_arg(1) 131 CX_EXPORT CxHashKey cx_hash_key_str(const char *str);
137 cx_attr_export
138 CxHashKey cx_hash_key_str(const char *str);
139 132
140 /** 133 /**
141 * Computes a hash key from a string. 134 * Computes a hash key from a string.
142 * 135 *
143 * Use this function when the string is represented 136 * Use this function when the string is represented
146 * The string needs to be zero-terminated. 139 * The string needs to be zero-terminated.
147 * 140 *
148 * @param str the string 141 * @param str the string
149 * @return the hash key 142 * @return the hash key
150 */ 143 */
151 cx_attr_nodiscard 144 cx_attr_nodiscard cx_attr_cstr_arg(1)
152 cx_attr_cstr_arg(1) 145 CX_EXPORT CxHashKey cx_hash_key_ustr(const unsigned char *str);
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 146
158 /** 147 /**
159 * Computes a hash key from a byte array. 148 * Computes a hash key from a byte array.
160 * 149 *
161 * @param bytes the array 150 * @param bytes the array
162 * @param len the length 151 * @param len the length
163 * @return the hash key 152 * @return the hash key
164 */ 153 */
165 cx_attr_nodiscard 154 cx_attr_nodiscard cx_attr_access_r(1, 2)
166 cx_attr_access_r(1, 2) 155 CX_EXPORT CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len);
167 cx_attr_export
168 CxHashKey cx_hash_key_bytes(
169 const unsigned char *bytes,
170 size_t len
171 );
172 156
173 /** 157 /**
174 * Computes a hash key for an arbitrary object. 158 * Computes a hash key for an arbitrary object.
175 * 159 *
176 * The computation uses the in-memory representation that might not be 160 * The computation uses the in-memory representation that might not be
181 * @param len the length of the object in memory 165 * @param len the length of the object in memory
182 * @return the hash key 166 * @return the hash key
183 */ 167 */
184 cx_attr_nodiscard 168 cx_attr_nodiscard
185 cx_attr_access_r(1, 2) 169 cx_attr_access_r(1, 2)
186 cx_attr_export 170 CX_EXPORT CxHashKey cx_hash_key(const void *obj, size_t len);
187 CxHashKey cx_hash_key(
188 const void *obj,
189 size_t len
190 );
191 171
192 /** 172 /**
193 * Computes a hash key from a UCX string. 173 * Computes a hash key from a UCX string.
194 * 174 *
195 * @param str the string 175 * @param str the string
196 * @return the hash key 176 * @return the hash key
197 */ 177 */
198 cx_attr_nodiscard 178 cx_attr_nodiscard
199 static inline CxHashKey cx_hash_key_cxstr(cxstring str) { 179 CX_EXPORT CxHashKey cx_hash_key_cxstr(cxstring str);
200 return cx_hash_key(str.ptr, str.length);
201 }
202 180
203 /** 181 /**
204 * Computes a hash key from a UCX string. 182 * Computes a hash key from a UCX string.
205 * 183 *
206 * @param str the string 184 * @param str the string
207 * @return the hash key 185 * @return the hash key
208 */ 186 */
209 cx_attr_nodiscard 187 cx_attr_nodiscard
210 static inline CxHashKey cx_hash_key_mutstr(cxmutstr str) { 188 CX_EXPORT CxHashKey cx_hash_key_mutstr(cxmutstr str);
211 return cx_hash_key(str.ptr, str.length);
212 }
213 189
214 /** 190 /**
215 * The identity function for the CX_HASH_KEY() macro. 191 * The identity function for the CX_HASH_KEY() macro.
216 * You should never need to use this manually. 192 * You should never need to use this manually.
217 * 193 *
218 * @param key the key 194 * @param key the key
219 * @return a copy of the key 195 * @return a copy of the key
220 */ 196 */
221 cx_attr_nodiscard 197 cx_attr_nodiscard
222 static inline CxHashKey cx_hash_key_identity(CxHashKey key) { 198 CX_INLINE CxHashKey cx_hash_key_identity(CxHashKey key) {
223 return key; 199 return key;
224 } 200 }
225 201
226 #ifndef __cplusplus 202 #ifndef __cplusplus
227 /** 203 /**
247 uint64_t: cx_hash_key_u64) \ 223 uint64_t: cx_hash_key_u64) \
248 (key) 224 (key)
249 #endif // __cplusplus 225 #endif // __cplusplus
250 226
251 /** 227 /**
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
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))
259
260 /**
261 * Compare function for hash keys. 228 * Compare function for hash keys.
262 * 229 *
263 * @param left the first key 230 * @param left the first key
264 * @param right the second key 231 * @param right the second key
265 * @return zero when the keys equal, non-zero when they differ 232 * @return zero when the keys equal, non-zero when they differ
266 */ 233 */
267 cx_attr_nodiscard 234 cx_attr_nodiscard cx_attr_nonnull
268 cx_attr_nonnull 235 CX_EXPORT int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right);
269 cx_attr_export
270 int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right);
271 236
272 #ifdef __cplusplus 237 #ifdef __cplusplus
273 } // extern "C" 238 } // extern "C"
274 239
275 // ---------------------------------------------------------- 240 // ----------------------------------------------------------
276 // Overloads of CX_HASH_KEY (the C++ version of a _Generic) 241 // Overloads of CX_HASH_KEY (the C++ version of a _Generic)
277 // ---------------------------------------------------------- 242 // ----------------------------------------------------------
278 243
279 static inline CxHashKey CX_HASH_KEY(CxHashKey key) { 244 CX_CPPDECL CxHashKey CX_HASH_KEY(CxHashKey key) {
280 return key; 245 return key;
281 } 246 }
282 247
283 static inline CxHashKey CX_HASH_KEY(cxstring str) { 248 CX_CPPDECL CxHashKey CX_HASH_KEY(cxstring str) {
284 return cx_hash_key_cxstr(str); 249 return cx_hash_key_cxstr(str);
285 } 250 }
286 251
287 static inline CxHashKey CX_HASH_KEY(cxmutstr str) { 252 CX_CPPDECL CxHashKey CX_HASH_KEY(cxmutstr str) {
288 return cx_hash_key_mutstr(str); 253 return cx_hash_key_mutstr(str);
289 } 254 }
290 255
291 static inline CxHashKey CX_HASH_KEY(const char *str) { 256 CX_CPPDECL CxHashKey CX_HASH_KEY(const char *str) {
292 return cx_hash_key_str(str); 257 return cx_hash_key_str(str);
293 } 258 }
294 259
295 static inline CxHashKey CX_HASH_KEY(const unsigned char *str) { 260 CX_CPPDECL CxHashKey CX_HASH_KEY(const unsigned char *str) {
296 return cx_hash_key_ustr(str); 261 return cx_hash_key_ustr(str);
297 } 262 }
298 263
299 static inline CxHashKey CX_HASH_KEY(uint32_t key) { 264 CX_CPPDECL CxHashKey CX_HASH_KEY(uint32_t key) {
300 return cx_hash_key_u32(key); 265 return cx_hash_key_u32(key);
301 } 266 }
302 267
303 static inline CxHashKey CX_HASH_KEY(uint64_t key) { 268 CX_CPPDECL CxHashKey CX_HASH_KEY(uint64_t key) {
304 return cx_hash_key_u64(key); 269 return cx_hash_key_u64(key);
305 } 270 }
306 #endif 271 #endif
307 272
308 #endif // UCX_HASH_KEY_H 273 #endif // UCX_HASH_KEY_H

mercurial