docs/Writerside/topics/hash_key.h.md

Tue, 30 Sep 2025 22:35:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 30 Sep 2025 22:35:24 +0200
changeset 1409
6856869e29f1
parent 1400
7bc88ae62755
permissions
-rw-r--r--

finalize the documentation on the new hash key features

resolves #731

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Hash Function
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
2
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
3 UCX implements the MurmurHash2 algorithm for computing hashes that are primarily used for [CxMap](map.h.md).
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
4 But it can be used for arbitrary custom scenarios, too.
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
5
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
6 ## Overview
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
7 ```C
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
8 #include <cx/hash_key.h>
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
9
1400
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
10 void cx_hash_murmur(CxHashKey *key);
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
11
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
12 uint32_t cx_hash_u32(uint32_t x);
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
13 uint64_t cx_hash_u64(uint64_t x);
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
14
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
15 CxHashKey cx_hash_key(const void *obj, size_t len);
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
16 CxHashKey cx_hash_key_str(const char *str);
1409
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
17 CxHashKey cx_hash_key_ustr(const unsigned char *str);
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
18 CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len);
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
19 CxHashKey cx_hash_key_cxstr(cxstring str);
1400
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
20 CxHashKey cx_hash_key_u32(uint32_t x);
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
21 CxHashKey cx_hash_key_u64(uint64_t x);
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
22
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
23 int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right);
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
24 ```
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
25
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
26 ## Description
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
27
1400
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
28 The primary function for creating a `CxHashKey` structure from non-integers is `cx_hash_key()`.
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
29 The other functions effectively do the same, but
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
30
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
31 * `cx_hash_key_bytes()` is strongly typed if you want to avoid passing `void*`
1147
52802c36b261 fix minor bugs in hash_key.h and streams.h docs
Mike Becker <universe@uap-core.de>
parents: 1145
diff changeset
32 * `cx_hash_key_str()` conveniently takes a C string and computes the length
1409
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
33 * `cx_hash_key_ustr()` same as before, but for `unsigned char*`
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
34 * `cx_hash_key_cxstr()` conveniently takes a [UCX string](string.h.md)
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
35
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
36 In all cases, the hash will be available in the `hash` field of the returned structure.
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
37
1409
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
38 > Usually you will never need to call any of the other functions directly.
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
39 > You can always create a `CxHashKey` structure by using the `CX_HASH_KEY()` macro
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
40 > (when the length of the key is implicitly clear) or by using the `cx_hash_key()` function.
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
41 > {style="note"}
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
42
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1147
diff changeset
43 > UCX assigns the hash value `1574210520` to the `NULL` pointer.
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1147
diff changeset
44 > This is a careful choice which is not standard MurmurHash2 and an extension to support `NULL` pointers.
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
45
1409
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
46 Hashes from integers are created more efficiently by mixing up the bits to produce a good statistical distribution.
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
47 The function `cx_hash_u32()` and `cx_hash_u64()` are provided for this purpose and provide collision-free hashes.
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
48 The corresponding functions `cx_hash_key_u32()` and `cx_hash_key_u64()` can be used to create `CxHashKey` structures with those hashes.
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
49
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
50 > Since integer hashes are collision-free, there is no need to store any `data` in the `CxHashKey` structure.
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
51
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
52 If you want to create a hash completely manually,
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
53 you can initialize the `data` and `len` members of `CxHashKey`
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
54 and call `cx_hash_murmur()`.
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
55 It is _not_ recommended to do so.
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
56
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
57 Example that is equivalent to `CxHashKey key = cx_hash_str(mystring)`
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
58 ```C
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
59 CxHashKey key;
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
60 key.data = mystring;
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
61 key.len = strlen(mystring);
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
62 cx_hash_murmur(&key);
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
63 ```
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
64
1400
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
65 Hash keys are compared with `cx_hash_key_cmp()`.
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
66
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
67 <seealso>
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
68 <category ref="apidoc">
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
69 <a href="https://ucx.sourceforge.io/api/hash__key_8h.html">hash_key.h</a>
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
70 </category>
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
71 </seealso>

mercurial