docs/Writerside/topics/hash_key.h.md

Sat, 25 Jan 2025 14:05:35 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jan 2025 14:05:35 +0100
branch
docs/3.1
changeset 1147
52802c36b261
parent 1145
1a8fe7b7dd8a
permissions
-rw-r--r--

fix minor bugs in hash_key.h and streams.h docs

relates to #451

# Hash Function

UCX implements the MurmurHash2 algorithm for computing hashes that are primarily used for [CxMap](map.h.md).
But it can be used for arbitrary custom scenarios, too.

## Overview
```C
#include <cx/hash_key.h>

void 	  cx_hash_murmur(CxHashKey *key);
CxHashKey cx_hash_key(const void *obj, size_t len);
CxHashKey cx_hash_key_str(const char *str);
CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len);
CxHashKey cx_hash_key_cxstr(cxstring str);
``` 

## Description

The primary function for creating a `CxHashKey` structure is `cx_hash_key()`.
The other functions do effectively the same, but

* `cx_hash_key_bytes()` is strongly typed if you want to avoid passing `void*` 
* `cx_hash_key_str()` conveniently takes a C string and computes the length
* `cx_hash_key_cxstr()` conveniently takes a [UCX string](string.h.md)

In all cases, the hash will be available in the `hash` field of the returned structure.

<note>
UCX assigns the hash value <code>1574210520</code> to the <code>NULL</code> pointer.
This is a careful choice which is not standard MurmurHash2 and an extension to support <code>NULL</code> pointers.
</note>

If you want to create a hash completely manually,
you can initialize the `data` and `len` members of `CxHashKey`
and call `cx_hash_murmur()`.
It is _not_ recommended to do so.

Example that is equivalent to `CxHashKey key = cx_hash_str(mystring)`
```C
CxHashKey key;
key.data = mystring;
key.len = strlen(mystring);
cx_hash_murmur(&key);
```

<seealso>
<category ref="apidoc">
<a href="https://ucx.sourceforge.io/api/hash__key_8h.html">hash_key.h</a>
</category>
</seealso>

mercurial