1 # Hash Function |
1 # Hash Function |
2 |
2 |
3 UCX implements the MurmurHash2 algorithm for computing hashes that are primarily used for [CxMap](map.h.md). |
3 UCX implements the MurmurHash2 algorithm for computing hashes that are primarily used for [CxMap](map.h.md). |
4 But it can be used for arbitrary custom scenarios, too. |
4 But it can be used for arbitrary custom scenarios, too. |
5 |
5 |
6 There are four functions which all generate a `CxHashKey` structure for given data. |
6 ## Overview |
|
7 ```C |
|
8 #include <cx/hash_key.h> |
7 |
9 |
8 * `cx_hash_key()` takes an arbitrary pointer and a length |
10 void cx_hash_murmur(CxHashKey *key); |
9 * `cx_hash_key_bytes()` takes an `unsigned char*` and a length |
11 CxHashKey cx_hash_key(const void *obj, size_t len); |
10 * `cx_hash_key_str()` takes a usual C string |
12 CxHashKey cx_hash_key_str(const char *str); |
11 * `cx_hash_key_cxstr()` takes a [UCX string](string.h.md) |
13 CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len); |
|
14 CxHashKey cx_hash_key_cxstr(cxstring str); |
|
15 ``` |
12 |
16 |
13 The hash is then available in the `hash` field of the returned structure. |
17 ## Description |
|
18 |
|
19 The primary function for creating a `CxHashKey` structure is `cx_hash_key()`. |
|
20 The other functions do effectively the same, but |
|
21 |
|
22 * `cx_hash_key_bytes()` is strongly typed if you want to avoid passing `void*` |
|
23 * `cx_hash_key_str()` conveniently takes a C string and computes the length on its own |
|
24 * `cx_hash_key_cxstr()` conveniently takes a [UCX string](string.h.md) |
|
25 |
|
26 In all cases, the hash will be available in the `hash` field of the returned structure. |
14 |
27 |
15 <note> |
28 <note> |
16 UCX assigns the hash value <code>1574210520</code> to the <code>NULL</code> pointer. |
29 UCX assigns the hash value <code>1574210520</code> to the <code>NULL</code> pointer. |
17 This is a careful choice which is not standard MurmurHash2 and an extension to support <code>NULL</code> pointers. |
30 This is a careful choice which is not standard MurmurHash2 and an extension to support <code>NULL</code> pointers. |
18 </note> |
31 </note> |