5 |
5 |
6 ## Overview |
6 ## Overview |
7 ```C |
7 ```C |
8 #include <cx/hash_key.h> |
8 #include <cx/hash_key.h> |
9 |
9 |
10 void cx_hash_murmur(CxHashKey *key); |
10 void cx_hash_murmur(CxHashKey *key); |
|
11 |
|
12 uint32_t cx_hash_u32(uint32_t x); |
|
13 uint64_t cx_hash_u64(uint64_t x); |
|
14 |
11 CxHashKey cx_hash_key(const void *obj, size_t len); |
15 CxHashKey cx_hash_key(const void *obj, size_t len); |
12 CxHashKey cx_hash_key_str(const char *str); |
16 CxHashKey cx_hash_key_str(const char *str); |
13 CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len); |
17 CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len); |
14 CxHashKey cx_hash_key_cxstr(cxstring str); |
18 CxHashKey cx_hash_key_cxstr(cxstring str); |
|
19 CxHashKey cx_hash_key_u32(uint32_t x); |
|
20 CxHashKey cx_hash_key_u64(uint64_t x); |
|
21 |
|
22 int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right); |
15 ``` |
23 ``` |
16 |
24 |
17 ## Description |
25 ## Description |
18 |
26 |
19 The primary function for creating a `CxHashKey` structure is `cx_hash_key()`. |
27 The primary function for creating a `CxHashKey` structure from non-integers is `cx_hash_key()`. |
20 The other functions do effectively the same, but |
28 The other functions effectively do the same, but |
21 |
29 |
22 * `cx_hash_key_bytes()` is strongly typed if you want to avoid passing `void*` |
30 * `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 |
31 * `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) |
32 * `cx_hash_key_cxstr()` conveniently takes a [UCX string](string.h.md) |
25 |
33 |
26 In all cases, the hash will be available in the `hash` field of the returned structure. |
34 In all cases, the hash will be available in the `hash` field of the returned structure. |
27 |
|
28 |
35 |
29 > UCX assigns the hash value `1574210520` to the `NULL` pointer. |
36 > UCX assigns the hash value `1574210520` to the `NULL` pointer. |
30 > This is a careful choice which is not standard MurmurHash2 and an extension to support `NULL` pointers. |
37 > This is a careful choice which is not standard MurmurHash2 and an extension to support `NULL` pointers. |
31 |
38 |
32 If you want to create a hash completely manually, |
39 If you want to create a hash completely manually, |
40 key.data = mystring; |
47 key.data = mystring; |
41 key.len = strlen(mystring); |
48 key.len = strlen(mystring); |
42 cx_hash_murmur(&key); |
49 cx_hash_murmur(&key); |
43 ``` |
50 ``` |
44 |
51 |
|
52 Hashes from integers are created more efficiently by mixing up the bits to produce a good statistical distribution. |
|
53 The function `cx_hash_u32()` and `cx_hash_u64()` are provided for this purpose and provide collision-free hashes. |
|
54 The corresponding functions `cx_hash_key_u32()` and `cx_hash_key_u64()` can be used to create `CxHashKey` structures with those hashes. |
|
55 |
|
56 > Since integer hashes are collision-free, there is no need to store any `data` in the `CxHashKey` structure. |
|
57 |
|
58 Hash keys are compared with `cx_hash_key_cmp()`. |
|
59 |
45 <seealso> |
60 <seealso> |
46 <category ref="apidoc"> |
61 <category ref="apidoc"> |
47 <a href="https://ucx.sourceforge.io/api/hash__key_8h.html">hash_key.h</a> |
62 <a href="https://ucx.sourceforge.io/api/hash__key_8h.html">hash_key.h</a> |
48 </category> |
63 </category> |
49 </seealso> |
64 </seealso> |