| |
1 # Hash Function |
| |
2 |
| |
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. |
| |
5 |
| |
6 ## Overview |
| |
7 ```C |
| |
8 #include <cx/hash_key.h> |
| |
9 |
| |
10 void cx_hash_murmur(CxHashKey *key); |
| |
11 CxHashKey cx_hash_key(const void *obj, size_t len); |
| |
12 CxHashKey cx_hash_key_str(const char *str); |
| |
13 CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len); |
| |
14 CxHashKey cx_hash_key_cxstr(cxstring str); |
| |
15 ``` |
| |
16 |
| |
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 |
| |
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. |
| |
27 |
| |
28 <note> |
| |
29 UCX assigns the hash value <code>1574210520</code> to the <code>NULL</code> pointer. |
| |
30 This is a careful choice which is not standard MurmurHash2 and an extension to support <code>NULL</code> pointers. |
| |
31 </note> |
| |
32 |
| |
33 If you want to create a hash completely manually, |
| |
34 you can initialize the `data` and `len` members of `CxHashKey` |
| |
35 and call `cx_hash_murmur()`. |
| |
36 It is _not_ recommended to do so. |
| |
37 |
| |
38 Example that is equivalent to `CxHashKey key = cx_hash_str(mystring)` |
| |
39 ```C |
| |
40 CxHashKey key; |
| |
41 key.data = mystring; |
| |
42 key.len = strlen(mystring); |
| |
43 cx_hash_murmur(&key); |
| |
44 ``` |
| |
45 |
| |
46 <seealso> |
| |
47 <category ref="apidoc"> |
| |
48 <a href="https://ucx.sourceforge.io/api/hash__key_8h.html">hash_key.h</a> |
| |
49 </category> |
| |
50 </seealso> |