ucx
UAP Common Extensions
Loading...
Searching...
No Matches
hash_key.h File Reference

Interface for map implementations. More...

#include "common.h"
#include "string.h"

Go to the source code of this file.

Data Structures

struct  cx_hash_key_s
 Internal structure for a key within a hash map. More...
 

Macros

#define CX_HASH_KEY(key)
 Creates a hash key from any of the supported types with implicit length.
 

Typedefs

typedef struct cx_hash_key_s CxHashKey
 Type for a hash key.
 

Functions

void cx_hash_murmur (CxHashKey *key)
 Computes a murmur2 32-bit hash.
 
uint32_t cx_hash_u32 (uint32_t x)
 Mixes up a 32-bit integer to be used as a hash.
 
uint64_t cx_hash_u64 (uint64_t x)
 Mixes up a 64-bit integer to be used as a hash.
 
CxHashKey cx_hash_key_u32 (uint32_t x)
 Computes a hash key from a 32-bit integer.
 
CxHashKey cx_hash_key_u64 (uint64_t x)
 Computes a hash key from a 64-bit integer.
 
CxHashKey cx_hash_key_str (const char *str)
 Computes a hash key from a string.
 
CxHashKey cx_hash_key_ustr (const unsigned char *str)
 Computes a hash key from a string.
 
CxHashKey cx_hash_key_bytes (const unsigned char *bytes, size_t len)
 Computes a hash key from a byte array.
 
CxHashKey cx_hash_key (const void *obj, size_t len)
 Computes a hash key for an arbitrary object.
 
CxHashKey cx_hash_key_cxstr (cxstring str)
 Computes a hash key from a UCX string.
 
CxHashKey cx_hash_key_mutstr (cxmutstr str)
 Computes a hash key from a UCX string.
 
static CxHashKey cx_hash_key_identity (CxHashKey key)
 The identity function for the CX_HASH_KEY() macro.
 
int cx_hash_key_cmp (const void *left, const void *right)
 Compare function for hash keys.
 

Detailed Description

Interface for map implementations.

Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ CX_HASH_KEY

#define CX_HASH_KEY ( key)
Value:
_Generic((key), \
char*: cx_hash_key_str, \
const char*: cx_hash_key_str, \
unsigned char*: cx_hash_key_ustr, \
const unsigned char*: cx_hash_key_ustr, \
uint32_t: cx_hash_key_u32, \
uint64_t: cx_hash_key_u64) \
(key)
CxHashKey cx_hash_key_str(const char *str)
Computes a hash key from a string.
CxHashKey cx_hash_key_u64(uint64_t x)
Computes a hash key from a 64-bit integer.
CxHashKey cx_hash_key_cxstr(cxstring str)
Computes a hash key from a UCX string.
CxHashKey cx_hash_key_u32(uint32_t x)
Computes a hash key from a 32-bit integer.
struct cx_hash_key_s CxHashKey
Type for a hash key.
Definition hash_key.h:65
static CxHashKey cx_hash_key_identity(CxHashKey key)
The identity function for the CX_HASH_KEY() macro.
Definition hash_key.h:198
CxHashKey cx_hash_key_mutstr(cxmutstr str)
Computes a hash key from a UCX string.
CxHashKey cx_hash_key_ustr(const unsigned char *str)
Computes a hash key from a string.
struct cx_string_s cxstring
An immutable string.
Definition string.h:87
struct cx_mutstr_s cxmutstr
A mutable string.
Definition string.h:69

Creates a hash key from any of the supported types with implicit length.

Does nothing when passing a CxHashkey.

Supported types are UCX strings, zero-terminated C strings, and 32-bit or 64-bit unsigned integers.

Parameters
keythe key data
Returns
the CxHashKey

Function Documentation

◆ cx_hash_key()

CxHashKey cx_hash_key ( const void * obj,
size_t len )

Computes a hash key for an arbitrary object.

The computation uses the in-memory representation that might not be the same on different platforms. Therefore, this hash should not be used for data exchange with different machines.

Parameters
obja pointer to an arbitrary object
lenthe length of the object in memory
Returns
the hash key

◆ cx_hash_key_bytes()

CxHashKey cx_hash_key_bytes ( const unsigned char * bytes,
size_t len )

Computes a hash key from a byte array.

Parameters
bytesthe array
lenthe length
Returns
the hash key

◆ cx_hash_key_cmp()

int cx_hash_key_cmp ( const void * left,
const void * right )

Compare function for hash keys.

The pointers are untyped to be compatible with the cx_compare_func signature.

Parameters
left(CxHashKey*) the first key
right(CxHashKey*) the second key
Returns
zero when the keys equal, non-zero when they differ

◆ cx_hash_key_cxstr()

CxHashKey cx_hash_key_cxstr ( cxstring str)

Computes a hash key from a UCX string.

Parameters
strthe string
Returns
the hash key

◆ cx_hash_key_identity()

static CxHashKey cx_hash_key_identity ( CxHashKey key)
inlinestatic

The identity function for the CX_HASH_KEY() macro.

You should never need to use this manually.

Parameters
keythe key
Returns
a copy of the key

◆ cx_hash_key_mutstr()

CxHashKey cx_hash_key_mutstr ( cxmutstr str)

Computes a hash key from a UCX string.

Parameters
strthe string
Returns
the hash key

◆ cx_hash_key_str()

CxHashKey cx_hash_key_str ( const char * str)

Computes a hash key from a string.

The string needs to be zero-terminated.

Parameters
strthe string
Returns
the hash key

◆ cx_hash_key_u32()

CxHashKey cx_hash_key_u32 ( uint32_t x)

Computes a hash key from a 32-bit integer.

Parameters
xthe integer
Returns
the hash key

◆ cx_hash_key_u64()

CxHashKey cx_hash_key_u64 ( uint64_t x)

Computes a hash key from a 64-bit integer.

Parameters
xthe integer
Returns
the hash key

◆ cx_hash_key_ustr()

CxHashKey cx_hash_key_ustr ( const unsigned char * str)

Computes a hash key from a string.

Use this function when the string is represented as an unsigned char array.

The string needs to be zero-terminated.

Parameters
strthe string
Returns
the hash key

◆ cx_hash_murmur()

void cx_hash_murmur ( CxHashKey * key)

Computes a murmur2 32-bit hash.

You need to initialize data and len in the key struct. The hash is then directly written to that struct.

Usually you should not need this function. Use cx_hash_key(), instead.

Note
If data is NULL, the hash is defined as 1574210520.
Parameters
keythe key, the hash shall be computed for
See also
cx_hash_key()

◆ cx_hash_u32()

uint32_t cx_hash_u32 ( uint32_t x)

Mixes up a 32-bit integer to be used as a hash.

This function produces no collisions and has a good statistical distribution.

Parameters
xthe integer
Returns
the hash

◆ cx_hash_u64()

uint64_t cx_hash_u64 ( uint64_t x)

Mixes up a 64-bit integer to be used as a hash.

This function produces no collisions and has a good statistical distribution.

Parameters
xthe integer
Returns
the hash