--- a/src/cx/hash_key.h Wed Dec 24 12:00:33 2025 +0100 +++ b/src/cx/hash_key.h Wed Dec 24 12:13:59 2025 +0100 @@ -89,7 +89,12 @@ * @param x the integer * @return the hash */ -CX_EXPORT uint32_t cx_hash_u32(uint32_t x); +CX_INLINE uint32_t cx_hash_u32(uint32_t x) { + x = ((x >> 16) ^ x) * 0x45d9f3bu; + x = ((x >> 16) ^ x) * 0x45d9f3bu; + x = (x >> 16) ^ x; + return x; +} /** * Mixes up a 64-bit integer to be used as a hash. @@ -99,60 +104,12 @@ * @param x the integer * @return the hash */ -CX_EXPORT uint64_t cx_hash_u64(uint64_t x); - -/** - * Computes a hash key from a 32-bit integer. - * - * @param x the integer - * @return the hash key - */ -cx_attr_nodiscard -CX_EXPORT CxHashKey cx_hash_key_u32(uint32_t x); - -/** - * Computes a hash key from a 64-bit integer. - * - * @param x the integer - * @return the hash key - */ -cx_attr_nodiscard -CX_EXPORT CxHashKey cx_hash_key_u64(uint64_t x); - -/** - * Computes a hash key from a string. - * - * The string needs to be zero-terminated. - * - * @param str the string - * @return the hash key - */ -cx_attr_nodiscard cx_attr_cstr_arg(1) -CX_EXPORT CxHashKey cx_hash_key_str(const char *str); - -/** - * Computes a hash key from a string. - * - * Use this function when the string is represented - * as an unsigned char array. - * - * The string needs to be zero-terminated. - * - * @param str the string - * @return the hash key - */ -cx_attr_nodiscard cx_attr_cstr_arg(1) -CX_EXPORT CxHashKey cx_hash_key_ustr(const unsigned char *str); - -/** - * Computes a hash key from a byte array. - * - * @param bytes the array - * @param len the length - * @return the hash key - */ -cx_attr_nodiscard cx_attr_access_r(1, 2) -CX_EXPORT CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len); +CX_INLINE uint64_t cx_hash_u64(uint64_t x){ + x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); + x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); + x = x ^ (x >> 31); + return x; +} /** * Computes a hash key for an arbitrary object. @@ -170,13 +127,75 @@ CX_EXPORT CxHashKey cx_hash_key(const void *obj, size_t len); /** - * Computes a hash key from a UCX string. + * Computes a hash key from a 32-bit integer. + * + * @param x the integer + * @return the hash key + */ +cx_attr_nodiscard +CX_INLINE CxHashKey cx_hash_key_u32(uint32_t x) { + CxHashKey key; + key.data = NULL; + key.len = 0; + key.hash = cx_hash_u32(x); + return key; +} + +/** + * Computes a hash key from a 64-bit integer. + * + * @param x the integer + * @return the hash key + */ +cx_attr_nodiscard +CX_INLINE CxHashKey cx_hash_key_u64(uint64_t x) { + CxHashKey key; + key.data = NULL; + key.len = 0; + key.hash = cx_hash_u64(x); + return key; +} + +/** + * Computes a hash key from a string. + * + * The string needs to be zero-terminated. * * @param str the string * @return the hash key */ -cx_attr_nodiscard -CX_EXPORT CxHashKey cx_hash_key_cxstr(cxstring str); +cx_attr_nodiscard cx_attr_cstr_arg(1) +CX_INLINE CxHashKey cx_hash_key_str(const char *str) { + return cx_hash_key((const void*)str, str == NULL ? 0 : strlen(str)); +} + +/** + * Computes a hash key from a string. + * + * Use this function when the string is represented + * as an unsigned char array. + * + * The string needs to be zero-terminated. + * + * @param str the string + * @return the hash key + */ +cx_attr_nodiscard cx_attr_cstr_arg(1) +CX_INLINE CxHashKey cx_hash_key_ustr(const unsigned char *str) { + return cx_hash_key((const void*)str, str == NULL ? 0 : strlen((const char*)str)); +} + +/** + * Computes a hash key from a byte array. + * + * @param bytes the array + * @param len the length + * @return the hash key + */ +cx_attr_nodiscard cx_attr_access_r(1, 2) +CX_INLINE CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len) { + return cx_hash_key((const void*)bytes, len); +} /** * Computes a hash key from a UCX string. @@ -185,7 +204,20 @@ * @return the hash key */ cx_attr_nodiscard -CX_EXPORT CxHashKey cx_hash_key_mutstr(cxmutstr str); +CX_INLINE CxHashKey cx_hash_key_cxstr(cxstring str) { + return cx_hash_key((void*)str.ptr, str.length); +} + +/** + * Computes a hash key from a UCX string. + * + * @param str the string + * @return the hash key + */ +cx_attr_nodiscard +CX_INLINE CxHashKey cx_hash_key_mutstr(cxmutstr str) { + return cx_hash_key((void*)str.ptr, str.length); +} /** * The identity function for the CX_HASH_KEY() macro.