src/cx/hash_key.h

changeset 1402
6fa42f7e2624
parent 1400
7bc88ae62755
equal deleted inserted replaced
1401:a76249f50237 1402:6fa42f7e2624
136 cx_attr_cstr_arg(1) 136 cx_attr_cstr_arg(1)
137 cx_attr_export 137 cx_attr_export
138 CxHashKey cx_hash_key_str(const char *str); 138 CxHashKey cx_hash_key_str(const char *str);
139 139
140 /** 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 }
157
158 /**
141 * Computes a hash key from a byte array. 159 * Computes a hash key from a byte array.
142 * 160 *
143 * @param bytes the array 161 * @param bytes the array
144 * @param len the length 162 * @param len the length
145 * @return the hash key 163 * @return the hash key
183 } 201 }
184 202
185 /** 203 /**
186 * Computes a hash key from a UCX string. 204 * Computes a hash key from a UCX string.
187 * 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 /**
227 * Creates a hash key from any of the supported types with implicit length.
228 *
229 * Does nothing when passing a CxHashkey.
230 *
231 * Supported types are UCX strings, zero-terminated C strings,
232 * and 32-bit or 64-bit unsigned integers.
233 *
234 * @param key the key data
235 * @returns the @c CxHashKey
236 */
237 #define CX_HASH_KEY(key) _Generic((key), \
238 CxHashKey: cx_hash_key_identity, \
239 cxstring: cx_hash_key_cxstr, \
240 cxmutstr: cx_hash_key_mutstr, \
241 char*: cx_hash_key_str, \
242 const char*: cx_hash_key_str, \
243 unsigned char*: cx_hash_key_ustr, \
244 const unsigned char*: cx_hash_key_ustr, \
245 uint32_t: cx_hash_key_u32, \
246 uint64_t: cx_hash_key_u64) \
247 (key)
248
249
250 /**
251 * Computes a hash key from a UCX string.
252 * Convenience macro that accepts both cxstring and cxmutstr.
253 * @deprecated use the CX_HASH_KEY() macro instead
188 * @param str (@c cxstring or @c cxmutstr) the string 254 * @param str (@c cxstring or @c cxmutstr) the string
189 * @return (@c CxHashKey) the hash key 255 * @return (@c CxHashKey) the hash key
190 */ 256 */
191 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) 257 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str))
192 258

mercurial