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
49 const void *data;
53 size_t len;
55 uint64_t hash;
56};
57
61typedef struct cx_hash_key_s CxHashKey;
62
79
89uint32_t cx_hash_u32(uint32_t x) {
90 x = ((x >> 16) ^ x) * 0x45d9f3bu;
91 x = ((x >> 16) ^ x) * 0x45d9f3bu;
92 x = (x >> 16) ^ x;
93 return x;
94}
95
105uint64_t cx_hash_u64(uint64_t x){
106 x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
107 x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
108 x = x ^ (x >> 31);
109 return x;
110}
111
124CxHashKey cx_hash_key(const void *obj, size_t len);
125
134 CxHashKey key;
135 key.data = NULL;
136 key.len = 0;
137 key.hash = cx_hash_u32(x);
138 return key;
139}
140
149 CxHashKey key;
150 key.data = NULL;
151 key.len = 0;
152 key.hash = cx_hash_u64(x);
153 return key;
154}
155
165CxHashKey cx_hash_key_str(const char *str) {
166 return cx_hash_key((const void*)str, str == NULL ? 0 : strlen(str));
167}
168
181CxHashKey cx_hash_key_ustr(const unsigned char *str) {
182 return cx_hash_key((const void*)str, str == NULL ? 0 : strlen((const char*)str));
183}
184
193CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len) {
194 return cx_hash_key((const void*)bytes, len);
195}
196
205 return cx_hash_key((void*)str.ptr, str.length);
206}
207
216 return cx_hash_key((void*)str.ptr, str.length);
217}
218
228 return key;
229}
230
240 return *key;
241}
242
243#ifndef __cplusplus
255#define CX_HASH_KEY(key) _Generic((key), \
256 CxHashKey*: cx_hash_key_deref, \
257 const CxHashKey*: cx_hash_key_deref, \
258 CxHashKey: cx_hash_key_identity, \
259 cxstring: cx_hash_key_cxstr, \
260 cxmutstr: cx_hash_key_mutstr, \
261 char*: cx_hash_key_str, \
262 const char*: cx_hash_key_str, \
263 unsigned char*: cx_hash_key_ustr, \
264 const unsigned char*: cx_hash_key_ustr, \
265 uint32_t: cx_hash_key_u32, \
266 uint64_t: cx_hash_key_u64) \
267 (key)
268#endif // __cplusplus
269
280int cx_hash_key_cmp(const void *left, const void *right);
281
290
291#ifdef __cplusplus
292// ----------------------------------------------------------
293// Overloads of CX_HASH_KEY (the C++ version of a _Generic)
294// ----------------------------------------------------------
295
297 return key;
298}
299
301 return *key;
302}
303
305 return cx_hash_key_cxstr(str);
306}
307
309 return cx_hash_key_mutstr(str);
310}
311
312CX_CPPDECL CxHashKey CX_HASH_KEY(const char *str) {
313 return cx_hash_key_str(str);
314}
315
316CX_CPPDECL CxHashKey CX_HASH_KEY(const unsigned char *str) {
317 return cx_hash_key_ustr(str);
318}
319
320CX_CPPDECL CxHashKey CX_HASH_KEY(uint32_t key) {
321 return cx_hash_key_u32(key);
322}
323
324CX_CPPDECL CxHashKey CX_HASH_KEY(uint64_t key) {
325 return cx_hash_key_u64(key);
326}
327#endif
328
329#endif // UCX_HASH_KEY_H
Common definitions and feature checks.
#define CX_INLINE
Declares a function to be inlined.
Definition common.h:311
#define CX_CSTR_ARG(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:211
#define CX_NONNULL
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_NODISCARD
Warn about discarded return value.
Definition common.h:256
#define CX_EXTERN
Declares a function with external linkage.
Definition common.h:297
#define CX_ACCESS_R(...)
Specifies that the function will only read through the given pointer.
Definition common.h:230
#define CX_CPPDECL
Declares a compatibility function for C++ builds.
Definition common.h:316
static CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len)
Computes a hash key from a byte array.
Definition hash_key.h:193
static CxHashKey cx_hash_key_deref(const CxHashKey *key)
The dereference function for the CX_HASH_KEY() macro.
Definition hash_key.h:239
void cx_hash_murmur(CxHashKey *key)
Computes a murmur2 32-bit hash.
static uint64_t cx_hash_u64(uint64_t x)
Mixes up a 64-bit integer to be used as a hash.
Definition hash_key.h:105
static CxHashKey cx_hash_key_u32(uint32_t x)
Computes a hash key from a 32-bit integer.
Definition hash_key.h:133
#define CX_HASH_KEY(key)
Creates a hash key from any of the supported types with implicit length.
Definition hash_key.h:255
static uint32_t cx_hash_u32(uint32_t x)
Mixes up a 32-bit integer to be used as a hash.
Definition hash_key.h:89
CxHashKey cx_hash_key(const void *obj, size_t len)
Computes a hash key for an arbitrary object.
static CxHashKey cx_hash_key_ustr(const unsigned char *str)
Computes a hash key from a string.
Definition hash_key.h:181
struct cx_hash_key_s CxHashKey
Type for a hash key.
Definition hash_key.h:61
static CxHashKey cx_hash_key_identity(CxHashKey key)
The identity function for the CX_HASH_KEY() macro.
Definition hash_key.h:227
static CxHashKey cx_hash_key_u64(uint64_t x)
Computes a hash key from a 64-bit integer.
Definition hash_key.h:148
int cx_hash_key_cmp(const void *left, const void *right)
Compare function for hash keys.
static CxHashKey cx_hash_key_mutstr(cxmutstr str)
Computes a hash key from a UCX string.
Definition hash_key.h:215
static CxHashKey cx_hash_key_cxstr(cxstring str)
Computes a hash key from a UCX string.
Definition hash_key.h:204
static CxHashKey cx_hash_key_str(const char *str)
Computes a hash key from a string.
Definition hash_key.h:165
cxstring cx_hash_key_as_string(const CxHashKey *key)
Interprets the key data as a string and returns it.
Strings that know their length.
struct cx_string_s cxstring
An immutable string.
Definition string.h:92
struct cx_mutstr_s cxmutstr
A mutable string.
Definition string.h:74
Internal structure for a key within a hash map.
Definition hash_key.h:44
const void * data
The key data.
Definition hash_key.h:49
uint64_t hash
The hash value of the key data.
Definition hash_key.h:55
size_t len
The key data length.
Definition hash_key.h:53
char * ptr
A pointer to the string.
Definition string.h:66
size_t length
The length of the string.
Definition string.h:68
size_t length
The length of the string.
Definition string.h:86
const char * ptr
A pointer to the immutable string.
Definition string.h:84