Fri, 24 Jan 2025 21:38:40 +0100
documentation of hash_key.h
relates to #451
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 | |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
6 | There are four functions which all generate a `CxHashKey` structure for given data. |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
7 | |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
8 | * `cx_hash_key()` takes an arbitrary pointer and a length |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
9 | * `cx_hash_key_bytes()` takes an `unsigned char*` and a length |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
10 | * `cx_hash_key_str()` takes a usual C string |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
11 | * `cx_hash_key_cxstr()` takes a [UCX string](string.h.md) |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
12 | |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
13 | The hash is then available in the `hash` field of the returned structure. |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
14 | |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
15 | <note> |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
16 | UCX assigns the hash value <code>1574210520</code> to the <code>NULL</code> pointer. |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
17 | This is a careful choice which is not standard MurmurHash2 and an extension to support <code>NULL</code> pointers. |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
18 | </note> |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
19 | |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
20 | 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
|
21 | 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
|
22 | and call `cx_hash_murmur()`. |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
23 | It is _not_ recommended to do so. |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
24 | |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
25 | 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
|
26 | ```C |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
27 | CxHashKey key; |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
28 | key.data = mystring; |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
29 | key.len = strlen(mystring); |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
30 | cx_hash_murmur(&key); |
b23d07fff6ca
documentation of hash_key.h
Mike Becker <universe@uap-core.de>
parents:
1143
diff
changeset
|
31 | ``` |