ucx
UAP Common Extensions
Loading...
Searching...
No Matches
hash_key.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
35
36
37#ifndef UCX_HASH_KEY_H
38#define UCX_HASH_KEY_H
39
40#include "common.h"
41#include "string.h"
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
53 const void *data;
57 size_t len;
59 uint64_t hash;
60};
61
65typedef struct cx_hash_key_s CxHashKey;
66
83
92CX_EXPORT uint32_t cx_hash_u32(uint32_t x);
93
102CX_EXPORT uint64_t cx_hash_u64(uint64_t x);
103
112
121
132
145CX_EXPORT CxHashKey cx_hash_key_ustr(const unsigned char *str);
146
155CX_EXPORT CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len);
156
170CX_EXPORT CxHashKey cx_hash_key(const void *obj, size_t len);
171
180
189
201
202#ifndef __cplusplus
214#define CX_HASH_KEY(key) _Generic((key), \
215 CxHashKey: cx_hash_key_identity, \
216 cxstring: cx_hash_key_cxstr, \
217 cxmutstr: cx_hash_key_mutstr, \
218 char*: cx_hash_key_str, \
219 const char*: cx_hash_key_str, \
220 unsigned char*: cx_hash_key_ustr, \
221 const unsigned char*: cx_hash_key_ustr, \
222 uint32_t: cx_hash_key_u32, \
223 uint64_t: cx_hash_key_u64) \
224 (key)
225#endif // __cplusplus
226
237CX_EXPORT int cx_hash_key_cmp(const void *left, const void *right);
238
239#ifdef __cplusplus
240} // extern "C"
241
242// ----------------------------------------------------------
243// Overloads of CX_HASH_KEY (the C++ version of a _Generic)
244// ----------------------------------------------------------
245
247 return key;
248}
249
251 return cx_hash_key_cxstr(str);
252}
253
255 return cx_hash_key_mutstr(str);
256}
257
258CX_CPPDECL CxHashKey CX_HASH_KEY(const char *str) {
259 return cx_hash_key_str(str);
260}
261
262CX_CPPDECL CxHashKey CX_HASH_KEY(const unsigned char *str) {
263 return cx_hash_key_ustr(str);
264}
265
266CX_CPPDECL CxHashKey CX_HASH_KEY(uint32_t key) {
267 return cx_hash_key_u32(key);
268}
269
270CX_CPPDECL CxHashKey CX_HASH_KEY(uint64_t key) {
271 return cx_hash_key_u64(key);
272}
273#endif
274
275#endif // UCX_HASH_KEY_H
Common definitions and feature checks.
#define CX_INLINE
Declares a function to be inlined.
Definition common.h:290
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:278
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:256
#define cx_attr_access_r(...)
Specifies that the function will only read through the given pointer.
Definition common.h:230
#define cx_attr_cstr_arg(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:211
#define CX_CPPDECL
Declares a compatibility function for C++ builds.
Definition common.h:295
CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len)
Computes a hash key from a byte array.
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.
uint64_t cx_hash_u64(uint64_t x)
Mixes up a 64-bit integer to be used as a hash.
void cx_hash_murmur(CxHashKey *key)
Computes a murmur2 32-bit hash.
CxHashKey cx_hash_key_cxstr(cxstring str)
Computes a hash key from a UCX string.
uint32_t cx_hash_u32(uint32_t x)
Mixes up a 32-bit integer to be used as a hash.
#define CX_HASH_KEY(key)
Creates a hash key from any of the supported types with implicit length.
Definition hash_key.h:214
CxHashKey cx_hash_key_u32(uint32_t x)
Computes a hash key from a 32-bit integer.
CxHashKey cx_hash_key(const void *obj, size_t len)
Computes a hash key for an arbitrary object.
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
int cx_hash_key_cmp(const void *left, const void *right)
Compare function for hash keys.
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.
Strings that know their length.
struct cx_string_s cxstring
An immutable string.
Definition string.h:87
struct cx_mutstr_s cxmutstr
A mutable string.
Definition string.h:69
Internal structure for a key within a hash map.
Definition hash_key.h:48
const void * data
The key data.
Definition hash_key.h:53
uint64_t hash
The hash value of the key data.
Definition hash_key.h:59
size_t len
The key data length.
Definition hash_key.h:57