ucx/cx/hash_key.h

branch
dav-2
changeset 889
42cdbf9bbd49
parent 854
1c8401ece69e
equal deleted inserted replaced
887:26541c37b619 889:42cdbf9bbd49
44 extern "C" { 44 extern "C" {
45 #endif 45 #endif
46 46
47 /** Internal structure for a key within a hash map. */ 47 /** Internal structure for a key within a hash map. */
48 struct cx_hash_key_s { 48 struct cx_hash_key_s {
49 /** The key data. */ 49 /**
50 * The key data.
51 * May be NULL when the hash is collision-free.
52 */
50 const void *data; 53 const void *data;
51 /** 54 /**
52 * The key data length. 55 * The key data length.
53 */ 56 */
54 size_t len; 57 size_t len;
55 /** The hash value of the key data. */ 58 /** The hash value of the key data. */
56 unsigned hash; 59 uint64_t hash;
57 }; 60 };
58 61
59 /** 62 /**
60 * Type for a hash key. 63 * Type for a hash key.
61 */ 64 */
74 * 77 *
75 * @param key the key, the hash shall be computed for 78 * @param key the key, the hash shall be computed for
76 * @see cx_hash_key() 79 * @see cx_hash_key()
77 */ 80 */
78 cx_attr_nonnull 81 cx_attr_nonnull
79 cx_attr_export 82 CX_EXPORT void cx_hash_murmur(CxHashKey *key);
80 void cx_hash_murmur(CxHashKey *key); 83
84 /**
85 * Mixes up a 32-bit integer to be used as a hash.
86 *
87 * This function produces no collisions and has a good statistical distribution.
88 *
89 * @param x the integer
90 * @return the hash
91 */
92 CX_EXPORT uint32_t cx_hash_u32(uint32_t x);
93
94 /**
95 * Mixes up a 64-bit integer to be used as a hash.
96 *
97 * This function produces no collisions and has a good statistical distribution.
98 *
99 * @param x the integer
100 * @return the hash
101 */
102 CX_EXPORT uint64_t cx_hash_u64(uint64_t x);
103
104 /**
105 * Computes a hash key from a 32-bit integer.
106 *
107 * @param x the integer
108 * @return the hash key
109 */
110 cx_attr_nodiscard
111 CX_EXPORT CxHashKey cx_hash_key_u32(uint32_t x);
112
113 /**
114 * Computes a hash key from a 64-bit integer.
115 *
116 * @param x the integer
117 * @return the hash key
118 */
119 cx_attr_nodiscard
120 CX_EXPORT CxHashKey cx_hash_key_u64(uint64_t x);
81 121
82 /** 122 /**
83 * Computes a hash key from a string. 123 * Computes a hash key from a string.
84 * 124 *
85 * The string needs to be zero-terminated. 125 * The string needs to be zero-terminated.
86 * 126 *
87 * @param str the string 127 * @param str the string
88 * @return the hash key 128 * @return the hash key
89 */ 129 */
90 cx_attr_nodiscard 130 cx_attr_nodiscard cx_attr_cstr_arg(1)
91 cx_attr_cstr_arg(1) 131 CX_EXPORT CxHashKey cx_hash_key_str(const char *str);
92 cx_attr_export 132
93 CxHashKey cx_hash_key_str(const char *str); 133 /**
134 * Computes a hash key from a string.
135 *
136 * Use this function when the string is represented
137 * as an unsigned char array.
138 *
139 * The string needs to be zero-terminated.
140 *
141 * @param str the string
142 * @return the hash key
143 */
144 cx_attr_nodiscard cx_attr_cstr_arg(1)
145 CX_EXPORT CxHashKey cx_hash_key_ustr(const unsigned char *str);
94 146
95 /** 147 /**
96 * Computes a hash key from a byte array. 148 * Computes a hash key from a byte array.
97 * 149 *
98 * @param bytes the array 150 * @param bytes the array
99 * @param len the length 151 * @param len the length
100 * @return the hash key 152 * @return the hash key
101 */ 153 */
102 cx_attr_nodiscard 154 cx_attr_nodiscard cx_attr_access_r(1, 2)
103 cx_attr_access_r(1, 2) 155 CX_EXPORT CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len);
104 cx_attr_export
105 CxHashKey cx_hash_key_bytes(
106 const unsigned char *bytes,
107 size_t len
108 );
109 156
110 /** 157 /**
111 * Computes a hash key for an arbitrary object. 158 * Computes a hash key for an arbitrary object.
112 * 159 *
113 * The computation uses the in-memory representation that might not be 160 * The computation uses the in-memory representation that might not be
114 * the same on different platforms. Therefore, this hash should not be 161 * the same on different platforms. Therefore, this hash should not be
115 * used for data exchange with different machines. 162 * used for data exchange with different machines.
116 * 163 *
117 * @param obj a pointer to an arbitrary object 164 * @param obj a pointer to an arbitrary object
118 * @param len the length of object in memory 165 * @param len the length of the object in memory
119 * @return the hash key 166 * @return the hash key
120 */ 167 */
121 cx_attr_nodiscard 168 cx_attr_nodiscard
122 cx_attr_access_r(1, 2) 169 cx_attr_access_r(1, 2)
123 cx_attr_export 170 CX_EXPORT CxHashKey cx_hash_key(const void *obj, size_t len);
124 CxHashKey cx_hash_key(
125 const void *obj,
126 size_t len
127 );
128 171
129 /** 172 /**
130 * Computes a hash key from a UCX string. 173 * Computes a hash key from a UCX string.
131 * 174 *
132 * @param str the string 175 * @param str the string
133 * @return the hash key 176 * @return the hash key
134 */ 177 */
135 cx_attr_nodiscard 178 cx_attr_nodiscard
136 static inline CxHashKey cx_hash_key_cxstr(cxstring str) { 179 CX_EXPORT CxHashKey cx_hash_key_cxstr(cxstring str);
137 return cx_hash_key(str.ptr, str.length);
138 }
139 180
140 /** 181 /**
141 * Computes a hash key from a UCX string. 182 * Computes a hash key from a UCX string.
142 * 183 *
143 * @param str (@c cxstring or @c cxmutstr) the string 184 * @param str the string
144 * @return (@c CxHashKey) the hash key 185 * @return the hash key
145 */ 186 */
146 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) 187 cx_attr_nodiscard
188 CX_EXPORT CxHashKey cx_hash_key_mutstr(cxmutstr str);
189
190 /**
191 * The identity function for the CX_HASH_KEY() macro.
192 * You should never need to use this manually.
193 *
194 * @param key the key
195 * @return a copy of the key
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 * Creates a hash key from any of the supported types with implicit length.
205 *
206 * Does nothing when passing a CxHashkey.
207 *
208 * Supported types are UCX strings, zero-terminated C strings,
209 * and 32-bit or 64-bit unsigned integers.
210 *
211 * @param key the key data
212 * @returns the @c CxHashKey
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 // __cplusplus
226
227 /**
228 * Compare function for hash keys.
229 *
230 * The pointers are untyped to be compatible with the cx_compare_func signature.
231 *
232 * @param left (@c CxHashKey*) the first key
233 * @param right (@c CxHashKey*) the second key
234 * @return zero when the keys equal, non-zero when they differ
235 */
236 cx_attr_nodiscard cx_attr_nonnull
237 CX_EXPORT int cx_hash_key_cmp(const void *left, const void *right);
147 238
148 #ifdef __cplusplus 239 #ifdef __cplusplus
149 } // extern "C" 240 } // extern "C"
241
242 // ----------------------------------------------------------
243 // Overloads of CX_HASH_KEY (the C++ version of a _Generic)
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 }
150 #endif 273 #endif
151 274
152 #endif // UCX_HASH_KEY_H 275 #endif // UCX_HASH_KEY_H

mercurial