1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
44 extern "C" {
45 #endif
46
47
48 struct cx_hash_key_s {
49
50
51
52
53 const void *data;
54
55
56
57 size_t len;
58
59 uint64_t hash;
60 };
61
62
63
64
65 typedef struct cx_hash_key_s CxHashKey;
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 cx_attr_nonnull
82 CX_EXPORT void cx_hash_murmur(CxHashKey *key);
83
84
85
86
87
88
89
90
91
92 CX_EXPORT uint32_t cx_hash_u32(
uint32_t x);
93
94
95
96
97
98
99
100
101
102 CX_EXPORT uint64_t cx_hash_u64(
uint64_t x);
103
104
105
106
107
108
109
110 cx_attr_nodiscard
111 CX_EXPORT CxHashKey cx_hash_key_u32(
uint32_t x);
112
113
114
115
116
117
118
119 cx_attr_nodiscard
120 CX_EXPORT CxHashKey cx_hash_key_u64(
uint64_t x);
121
122
123
124
125
126
127
128
129
130 cx_attr_nodiscard cx_attr_cstr_arg(
1)
131 CX_EXPORT CxHashKey cx_hash_key_str(
const char *str);
132
133
134
135
136
137
138
139
140
141
142
143
144 cx_attr_nodiscard cx_attr_cstr_arg(
1)
145 CX_EXPORT CxHashKey cx_hash_key_ustr(
const unsigned char *str);
146
147
148
149
150
151
152
153
154 cx_attr_nodiscard cx_attr_access_r(
1,
2)
155 CX_EXPORT CxHashKey cx_hash_key_bytes(
const unsigned char *bytes,
size_t len);
156
157
158
159
160
161
162
163
164
165
166
167
168 cx_attr_nodiscard
169 cx_attr_access_r(
1,
2)
170 CX_EXPORT CxHashKey cx_hash_key(
const void *obj,
size_t len);
171
172
173
174
175
176
177
178 cx_attr_nodiscard
179 CX_EXPORT CxHashKey cx_hash_key_cxstr(cxstring str);
180
181
182
183
184
185
186
187 cx_attr_nodiscard
188 CX_EXPORT CxHashKey cx_hash_key_mutstr(cxmutstr str);
189
190
191
192
193
194
195
196
197 cx_attr_nodiscard
198 CX_INLINE CxHashKey cx_hash_key_identity(CxHashKey key) {
199 return key;
200 }
201
202 #ifndef __cplusplus
203
204
205
206
207
208
209
210
211
212
213
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
226
227
228
229
230
231
232
233
234
235
236 cx_attr_nodiscard cx_attr_nonnull
237 CX_EXPORT int cx_hash_key_cmp(
const void *left,
const void *right);
238
239 #ifdef __cplusplus
240 }
241
242
243
244
245
246 CX_CPPDECL CxHashKey
CX_HASH_KEY(CxHashKey key) {
247 return key;
248 }
249
250 CX_CPPDECL CxHashKey
CX_HASH_KEY(cxstring str) {
251 return cx_hash_key_cxstr(str);
252 }
253
254 CX_CPPDECL CxHashKey
CX_HASH_KEY(cxmutstr str) {
255 return cx_hash_key_mutstr(str);
256 }
257
258 CX_CPPDECL CxHashKey
CX_HASH_KEY(
const char *str) {
259 return cx_hash_key_str(str);
260 }
261
262 CX_CPPDECL CxHashKey
CX_HASH_KEY(
const unsigned char *str) {
263 return cx_hash_key_ustr(str);
264 }
265
266 CX_CPPDECL CxHashKey
CX_HASH_KEY(
uint32_t key) {
267 return cx_hash_key_u32(key);
268 }
269
270 CX_CPPDECL CxHashKey
CX_HASH_KEY(
uint64_t key) {
271 return cx_hash_key_u64(key);
272 }
273 #endif
274
275 #endif
276