docs/Writerside/topics/hash_key.h.md

Mon, 22 Dec 2025 16:12:20 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 22 Dec 2025 16:12:20 +0100
changeset 1653
6a842bd49fea
parent 1424
563033aa998c
permissions
-rw-r--r--

adds cx_hash_key_as_string()

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);
1653
6a842bd49fea adds cx_hash_key_as_string()
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
24
6a842bd49fea adds cx_hash_key_as_string()
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
25 cxstring cx_hash_key_as_string(const CxHashKey *key);
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
26 ```
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
27
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
28 ## Description
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
29
1400
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
30 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
31 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
32
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
33 * `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
34 * `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
35 * `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
36 * `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
37
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
38 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
39
1409
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
40 > 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
41 > 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
42 > (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
43 > {style="note"}
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
44
1244
9a8e781258ac complete most of the map.h documentation
Mike Becker <universe@uap-core.de>
parents: 1147
diff changeset
45 > UCX assigns the hash value `1574210520` to the `NULL` pointer.
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1409
diff changeset
46 > This is a careful choice that 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
47
1409
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
48 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
49 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
50 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
51
6856869e29f1 finalize the documentation on the new hash key features
Mike Becker <universe@uap-core.de>
parents: 1400
diff changeset
52 > 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
53
1144
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
54 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
55 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
56 and call `cx_hash_murmur()`.
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
57 It is _not_ recommended to do so.
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
58
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
59 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
60 ```C
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
61 CxHashKey key;
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
62 key.data = mystring;
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
63 key.len = strlen(mystring);
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
64 cx_hash_murmur(&key);
b23d07fff6ca documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
65 ```
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
66
1400
7bc88ae62755 add support for integer keys - resolves #720
Mike Becker <universe@uap-core.de>
parents: 1244
diff changeset
67 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
68
1653
6a842bd49fea adds cx_hash_key_as_string()
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
69 When you have a key that stores a string, you can retrieve it by using `cx_hash_key_as_string()`.
6a842bd49fea adds cx_hash_key_as_string()
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
70
1145
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
71 <seealso>
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
72 <category ref="apidoc">
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
73 <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
74 </category>
1a8fe7b7dd8a add stream.h docs and reworks hash_key.h docs
Mike Becker <universe@uap-core.de>
parents: 1144
diff changeset
75 </seealso>

mercurial