src/ucx/map.h

changeset 327
fbc33813265b
parent 277
f819fe5e20f5
child 374
be77fb2da242
equal deleted inserted replaced
326:3dd7d21fb76b 327:fbc33813265b
87 size_t size; 87 size_t size;
88 /** The count of elements currently stored in this map. */ 88 /** The count of elements currently stored in this map. */
89 size_t count; 89 size_t count;
90 }; 90 };
91 91
92 /** Structure for a key of a UcxMap. */ 92 /** Structure to publicly denote a key of a UcxMap. */
93 struct UcxKey { 93 struct UcxKey {
94 /** The key data. */ 94 /** The key data. */
95 void *data; 95 const void *data;
96 /** The length of the key data. */ 96 /** The length of the key data. */
97 size_t len; 97 size_t len;
98 /** A cache for the hash value of the key data. */
99 int hash;
100 };
101
102 /** Internal structure for a key of a UcxMap. */
103 struct UcxMapKey {
104 /** The key data. */
105 void *data;
106 /** The length of the key data. */
107 size_t len;
98 /** The hash value of the key data. */ 108 /** The hash value of the key data. */
99 int hash; 109 int hash;
100 }; 110 };
101 111
102 /** Structure for an element of a UcxMap. */ 112 /** Structure for an element of a UcxMap. */
103 struct UcxMapElement { 113 struct UcxMapElement {
104 /** The value data. */ 114 /** The value data. */
105 void *data; 115 void *data;
106 116
107 /** A pointer to the next element in the current list. */ 117 /** A pointer to the next element in the current list. */
108 UcxMapElement *next; 118 UcxMapElement *next;
109 119
110 /** The corresponding key. */ 120 /** The corresponding key. */
111 UcxKey key; 121 struct UcxMapKey key;
112 }; 122 };
113 123
114 /** Structure for an iterator over a UcxMap. */ 124 /** Structure for an iterator over a UcxMap. */
115 struct UcxMapIterator { 125 struct UcxMapIterator {
116 /** The map to iterate over. */ 126 /** The map to iterate over. */
283 * @param value the value 293 * @param value the value
284 * @return 0 on success, non-zero value on failure 294 * @return 0 on success, non-zero value on failure
285 * @see ucx_map_put() 295 * @see ucx_map_put()
286 */ 296 */
287 #define ucx_map_cstr_put(map, key, value) \ 297 #define ucx_map_cstr_put(map, key, value) \
288 ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value) 298 ucx_map_put(map, ucx_key(key, strlen(key)), (void*)value)
289 299
290 /** 300 /**
291 * Shorthand for putting data with an integer key into the map. 301 * Shorthand for putting data with an integer key into the map.
292 * @param map the map 302 * @param map the map
293 * @param key the key 303 * @param key the key
294 * @param value the value 304 * @param value the value
295 * @return 0 on success, non-zero value on failure 305 * @return 0 on success, non-zero value on failure
296 * @see ucx_map_put() 306 * @see ucx_map_put()
297 */ 307 */
298 #define ucx_map_int_put(map, key, value) \ 308 #define ucx_map_int_put(map, key, value) \
299 ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value) 309 ucx_map_put(map, ucx_key(&key, sizeof(key)), (void*)value)
300 310
301 /** 311 /**
302 * Shorthand for getting data from the map with a sstr_t key. 312 * Shorthand for getting data from the map with a sstr_t key.
303 * @param map the map 313 * @param map the map
304 * @param key the key 314 * @param key the key
314 * @param key the key 324 * @param key the key
315 * @return the value 325 * @return the value
316 * @see ucx_map_get() 326 * @see ucx_map_get()
317 */ 327 */
318 #define ucx_map_cstr_get(map, key) \ 328 #define ucx_map_cstr_get(map, key) \
319 ucx_map_get(map, ucx_key((void*)key, strlen(key))) 329 ucx_map_get(map, ucx_key(key, strlen(key)))
320 330
321 /** 331 /**
322 * Shorthand for getting data from the map with an integer key. 332 * Shorthand for getting data from the map with an integer key.
323 * @param map the map 333 * @param map the map
324 * @param key the key 334 * @param key the key
325 * @return the value 335 * @return the value
326 * @see ucx_map_get() 336 * @see ucx_map_get()
327 */ 337 */
328 #define ucx_map_int_get(map, key) \ 338 #define ucx_map_int_get(map, key) \
329 ucx_map_get(map, ucx_key((void*)&key, sizeof(int))) 339 ucx_map_get(map, ucx_key(&key, sizeof(int)))
330 340
331 /** 341 /**
332 * Shorthand for removing data from the map with a sstr_t key. 342 * Shorthand for removing data from the map with a sstr_t key.
333 * @param map the map 343 * @param map the map
334 * @param key the key 344 * @param key the key
344 * @param key the key 354 * @param key the key
345 * @return the removed value 355 * @return the removed value
346 * @see ucx_map_remove() 356 * @see ucx_map_remove()
347 */ 357 */
348 #define ucx_map_cstr_remove(map, key) \ 358 #define ucx_map_cstr_remove(map, key) \
349 ucx_map_remove(map, ucx_key((void*)key, strlen(key))) 359 ucx_map_remove(map, ucx_key(key, strlen(key)))
350 360
351 /** 361 /**
352 * Shorthand for removing data from the map with an integer key. 362 * Shorthand for removing data from the map with an integer key.
353 * @param map the map 363 * @param map the map
354 * @param key the key 364 * @param key the key
355 * @return the removed value 365 * @return the removed value
356 * @see ucx_map_remove() 366 * @see ucx_map_remove()
357 */ 367 */
358 #define ucx_map_int_remove(map, key) \ 368 #define ucx_map_int_remove(map, key) \
359 ucx_map_remove(map, ucx_key((void*)&key, sizeof(key))) 369 ucx_map_remove(map, ucx_key(&key, sizeof(key)))
360 370
361 /** 371 /**
362 * Creates a UcxKey based on the given data. 372 * Creates a UcxKey based on the given data.
363 * 373 *
364 * This function implicitly computes the hash. 374 * This function implicitly computes the hash.
366 * @param data the data for the key 376 * @param data the data for the key
367 * @param len the length of the data 377 * @param len the length of the data
368 * @return a UcxKey with implicitly computed hash 378 * @return a UcxKey with implicitly computed hash
369 * @see ucx_hash() 379 * @see ucx_hash()
370 */ 380 */
371 UcxKey ucx_key(void *data, size_t len); 381 UcxKey ucx_key(const void *data, size_t len);
372 382
373 /** 383 /**
374 * Computes a murmur hash-2. 384 * Computes a murmur hash-2.
375 * 385 *
376 * @param data the data to hash 386 * @param data the data to hash

mercurial