src/ucx/map.h

Thu, 21 Jun 2018 16:00:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 21 Jun 2018 16:00:37 +0200
changeset 327
fbc33813265b
parent 277
f819fe5e20f5
child 374
be77fb2da242
permissions
-rw-r--r--

UcxMap now separates internal non-const keys from public const keys

This simplifies function calls with constant keys like scstr_t or const char*.

olaf@2 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@259 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@2 27 */
olaf@2 28
universe@136 29 /**
universe@136 30 * @file map.h
universe@136 31 *
universe@136 32 * Hash map implementation.
universe@136 33 *
universe@136 34 * This implementation uses murmur hash 2 and separate chaining with linked
universe@136 35 * lists.
universe@136 36 *
universe@136 37 * @author Mike Becker
universe@136 38 * @author Olaf Wintermann
universe@136 39 */
universe@136 40
olaf@120 41 #ifndef UCX_MAP_H
olaf@120 42 #define UCX_MAP_H
olaf@2 43
universe@259 44 #include "ucx.h"
universe@259 45 #include "string.h"
universe@259 46 #include "allocator.h"
universe@41 47 #include <stdio.h>
olaf@20 48
olaf@2 49 #ifdef __cplusplus
olaf@2 50 extern "C" {
olaf@2 51 #endif
olaf@2 52
universe@138 53 /**
universe@138 54 * Loop statement for UCX maps.
universe@138 55 *
universe@138 56 * The <code>key</code> variable is implicitly defined, but the
universe@138 57 * <code>value</code> variable must be already declared as type information
universe@138 58 * cannot be inferred.
universe@138 59 *
universe@138 60 * @param key the variable name for the key
universe@138 61 * @param value the variable name for the value
universe@225 62 * @param iter a UcxMapIterator
universe@138 63 * @see ucx_map_iterator()
universe@138 64 */
universe@138 65 #define UCX_MAP_FOREACH(key,value,iter) \
universe@138 66 for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&value);)
olaf@31 67
universe@138 68 /** Type for the UCX map. @see UcxMap */
olaf@31 69 typedef struct UcxMap UcxMap;
universe@146 70
universe@225 71 /** Type for a key of a UcxMap. @see UcxKey */
olaf@31 72 typedef struct UcxKey UcxKey;
universe@146 73
universe@225 74 /** Type for an element of a UcxMap. @see UcxMapElement */
olaf@31 75 typedef struct UcxMapElement UcxMapElement;
universe@146 76
universe@225 77 /** Type for an iterator over a UcxMap. @see UcxMapIterator */
olaf@31 78 typedef struct UcxMapIterator UcxMapIterator;
olaf@2 79
universe@138 80 /** Structure for the UCX map. */
olaf@20 81 struct UcxMap {
universe@138 82 /** An allocator that is used for the map elements. */
olaf@107 83 UcxAllocator *allocator;
universe@138 84 /** The array of map element lists. */
universe@29 85 UcxMapElement **map;
universe@138 86 /** The size of the map is the length of the element list array. */
olaf@20 87 size_t size;
universe@138 88 /** The count of elements currently stored in this map. */
olaf@45 89 size_t count;
olaf@20 90 };
olaf@2 91
universe@327 92 /** Structure to publicly denote a key of a UcxMap. */
olaf@20 93 struct UcxKey {
universe@138 94 /** The key data. */
universe@327 95 const void *data;
universe@138 96 /** The length of the key data. */
universe@327 97 size_t len;
universe@327 98 /** A cache for the hash value of the key data. */
universe@327 99 int hash;
universe@327 100 };
universe@327 101
universe@327 102 /** Internal structure for a key of a UcxMap. */
universe@327 103 struct UcxMapKey {
universe@327 104 /** The key data. */
universe@327 105 void *data;
universe@327 106 /** The length of the key data. */
universe@327 107 size_t len;
universe@138 108 /** The hash value of the key data. */
universe@327 109 int hash;
olaf@20 110 };
olaf@20 111
universe@225 112 /** Structure for an element of a UcxMap. */
olaf@20 113 struct UcxMapElement {
universe@138 114 /** The value data. */
universe@327 115 void *data;
universe@146 116
universe@138 117 /** A pointer to the next element in the current list. */
universe@327 118 UcxMapElement *next;
universe@146 119
universe@138 120 /** The corresponding key. */
universe@327 121 struct UcxMapKey key;
olaf@20 122 };
olaf@20 123
universe@225 124 /** Structure for an iterator over a UcxMap. */
olaf@31 125 struct UcxMapIterator {
universe@138 126 /** The map to iterate over. */
olaf@31 127 UcxMap *map;
universe@146 128
universe@138 129 /** The current map element. */
olaf@31 130 UcxMapElement *cur;
universe@146 131
universe@138 132 /**
universe@138 133 * The current index of the element list array.
universe@138 134 * <b>Attention: </b> this is <b>NOT</b> the element index! Do <b>NOT</b>
universe@138 135 * manually iterate over the map by increasing this index. Use
universe@138 136 * ucx_map_iter_next().
universe@138 137 * @see UcxMap.map*/
universe@95 138 size_t index;
olaf@31 139 };
olaf@31 140
universe@136 141 /**
universe@136 142 * Creates a new hash map with the specified size.
universe@136 143 * @param size the size of the hash map
universe@136 144 * @return a pointer to the new hash map
universe@136 145 */
universe@136 146 UcxMap *ucx_map_new(size_t size);
olaf@20 147
universe@136 148 /**
universe@225 149 * Creates a new hash map with the specified size using a UcxAllocator.
olaf@137 150 * @param allocator the allocator to use
universe@136 151 * @param size the size of the hash map
universe@136 152 * @return a pointer to the new hash map
universe@136 153 */
olaf@137 154 UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size);
universe@136 155
universe@136 156 /**
universe@136 157 * Frees a hash map.
universe@136 158 *
universe@208 159 * <b>Note:</b> the contents are <b>not</b> freed, use ucx_map_free_content()
universe@208 160 * before calling this function to achieve that.
universe@136 161 *
universe@136 162 * @param map the map to be freed
universe@208 163 * @see ucx_map_free_content()
universe@136 164 */
universe@29 165 void ucx_map_free(UcxMap *map);
universe@136 166
universe@138 167 /**
universe@208 168 * Frees the contents of a hash map.
universe@208 169 *
universe@208 170 * This is a convenience function that iterates over the map and passes all
universe@277 171 * values to the specified destructor function.
universe@277 172 *
universe@277 173 * If no destructor is specified (<code>NULL</code>), the free() function of
universe@277 174 * the map's own allocator is used.
universe@208 175 *
universe@209 176 * You must ensure, that it is valid to pass each value in the map to the same
universe@209 177 * destructor function.
universe@208 178 *
universe@208 179 * You should free or clear the map afterwards, as the contents will be invalid.
universe@208 180 *
universe@208 181 * @param map for which the contents shall be freed
universe@277 182 * @param destr optional pointer to a destructor function
universe@208 183 * @see ucx_map_free()
universe@208 184 * @see ucx_map_clear()
universe@208 185 */
universe@209 186 void ucx_map_free_content(UcxMap *map, ucx_destructor destr);
universe@208 187
universe@208 188 /**
universe@206 189 * Clears a hash map.
universe@206 190 *
universe@208 191 * <b>Note:</b> the contents are <b>not</b> freed, use ucx_map_free_content()
universe@208 192 * before calling this function to achieve that.
universe@206 193 *
universe@208 194 * @param map the map to be cleared
universe@208 195 * @see ucx_map_free_content()
universe@206 196 */
universe@206 197 void ucx_map_clear(UcxMap *map);
universe@206 198
universe@208 199
universe@206 200 /**
universe@138 201 * Copies contents from a map to another map using a copy function.
universe@138 202 *
universe@138 203 * <b>Note:</b> The destination map does not need to be empty. However, if it
universe@138 204 * contains data with keys that are also present in the source map, the contents
universe@138 205 * are overwritten.
universe@138 206 *
universe@138 207 * @param from the source map
universe@138 208 * @param to the destination map
universe@138 209 * @param fnc the copy function or <code>NULL</code> if the pointer address
universe@138 210 * shall be copied
universe@138 211 * @param data additional data for the copy function
universe@138 212 * @return 0 on success or a non-zero value on memory allocation errors
universe@138 213 */
universe@253 214 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data);
universe@138 215
universe@138 216 /**
universe@138 217 * Clones the map and rehashes if necessary.
universe@138 218 *
universe@138 219 * <b>Note:</b> In contrast to ucx_map_rehash() the load factor is irrelevant.
universe@138 220 * This function <i>always</i> ensures a new UcxMap.size of at least
universe@138 221 * 2.5*UcxMap.count.
universe@138 222 *
universe@138 223 * @param map the map to clone
universe@138 224 * @param fnc the copy function to use or <code>NULL</code> if the new and
universe@138 225 * the old map shall share the data pointers
universe@138 226 * @param data additional data for the copy function
universe@138 227 * @return the cloned map
universe@138 228 * @see ucx_map_copy()
universe@138 229 */
olaf@44 230 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
universe@138 231
universe@138 232 /**
universe@138 233 * Increases size of the hash map, if necessary.
universe@138 234 *
universe@138 235 * The load value is 0.75*UcxMap.size. If the element count exceeds the load
universe@138 236 * value, the map needs to be rehashed. Otherwise no action is performed and
universe@138 237 * this function simply returns 0.
universe@138 238 *
universe@138 239 * The rehashing process ensures, that the UcxMap.size is at least
universe@138 240 * 2.5*UcxMap.count. So there is enough room for additional elements without
universe@138 241 * the need of another soon rehashing.
universe@138 242 *
universe@138 243 * You can use this function to dramatically increase access performance.
universe@138 244 *
universe@138 245 * @param map the map to rehash
universe@138 246 * @return 1, if a memory allocation error occurred, 0 otherwise
universe@138 247 */
olaf@52 248 int ucx_map_rehash(UcxMap *map);
olaf@20 249
universe@138 250 /**
universe@138 251 * Puts a key/value-pair into the map.
universe@138 252 *
universe@138 253 * @param map the map
universe@138 254 * @param key the key
universe@138 255 * @param value the value
universe@138 256 * @return 0 on success, non-zero value on failure
universe@138 257 */
universe@138 258 int ucx_map_put(UcxMap *map, UcxKey key, void *value);
universe@138 259
universe@138 260 /**
universe@138 261 * Retrieves a value by using a key.
universe@138 262 *
universe@138 263 * @param map the map
universe@138 264 * @param key the key
universe@138 265 * @return the value
universe@138 266 */
olaf@20 267 void* ucx_map_get(UcxMap *map, UcxKey key);
universe@138 268
universe@138 269 /**
universe@138 270 * Removes a key/value-pair from the map by using the key.
universe@138 271 *
universe@138 272 * @param map the map
universe@138 273 * @param key the key
universe@138 274 * @return the removed value
universe@138 275 */
universe@53 276 void* ucx_map_remove(UcxMap *map, UcxKey key);
olaf@20 277
universe@136 278 /**
universe@136 279 * Shorthand for putting data with a sstr_t key into the map.
universe@136 280 * @param map the map
universe@136 281 * @param key the key
universe@136 282 * @param value the value
universe@138 283 * @return 0 on success, non-zero value on failure
universe@136 284 * @see ucx_map_put()
universe@136 285 */
universe@136 286 #define ucx_map_sstr_put(map, key, value) \
universe@136 287 ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
universe@146 288
universe@136 289 /**
universe@136 290 * Shorthand for putting data with a C string key into the map.
universe@136 291 * @param map the map
universe@136 292 * @param key the key
universe@136 293 * @param value the value
universe@138 294 * @return 0 on success, non-zero value on failure
universe@136 295 * @see ucx_map_put()
universe@136 296 */
universe@136 297 #define ucx_map_cstr_put(map, key, value) \
universe@327 298 ucx_map_put(map, ucx_key(key, strlen(key)), (void*)value)
universe@146 299
universe@136 300 /**
universe@136 301 * Shorthand for putting data with an integer key into the map.
universe@136 302 * @param map the map
universe@136 303 * @param key the key
universe@136 304 * @param value the value
universe@138 305 * @return 0 on success, non-zero value on failure
universe@136 306 * @see ucx_map_put()
universe@136 307 */
universe@136 308 #define ucx_map_int_put(map, key, value) \
universe@327 309 ucx_map_put(map, ucx_key(&key, sizeof(key)), (void*)value)
olaf@78 310
universe@136 311 /**
universe@136 312 * Shorthand for getting data from the map with a sstr_t key.
universe@136 313 * @param map the map
universe@136 314 * @param key the key
universe@138 315 * @return the value
universe@136 316 * @see ucx_map_get()
universe@136 317 */
universe@136 318 #define ucx_map_sstr_get(map, key) \
universe@136 319 ucx_map_get(map, ucx_key(key.ptr, key.length))
universe@146 320
universe@136 321 /**
universe@136 322 * Shorthand for getting data from the map with a C string key.
universe@138 323 * @param map the map
universe@138 324 * @param key the key
universe@138 325 * @return the value
universe@136 326 * @see ucx_map_get()
universe@136 327 */
universe@136 328 #define ucx_map_cstr_get(map, key) \
universe@327 329 ucx_map_get(map, ucx_key(key, strlen(key)))
universe@146 330
universe@136 331 /**
universe@136 332 * Shorthand for getting data from the map with an integer key.
universe@136 333 * @param map the map
universe@136 334 * @param key the key
universe@138 335 * @return the value
universe@136 336 * @see ucx_map_get()
universe@136 337 */
universe@136 338 #define ucx_map_int_get(map, key) \
universe@327 339 ucx_map_get(map, ucx_key(&key, sizeof(int)))
universe@146 340
universe@136 341 /**
universe@136 342 * Shorthand for removing data from the map with a sstr_t key.
universe@136 343 * @param map the map
universe@136 344 * @param key the key
universe@138 345 * @return the removed value
universe@136 346 * @see ucx_map_remove()
universe@136 347 */
universe@136 348 #define ucx_map_sstr_remove(map, key) \
universe@136 349 ucx_map_remove(map, ucx_key(key.ptr, key.length))
universe@146 350
universe@136 351 /**
universe@136 352 * Shorthand for removing data from the map with a C string key.
universe@136 353 * @param map the map
universe@136 354 * @param key the key
universe@138 355 * @return the removed value
universe@136 356 * @see ucx_map_remove()
universe@136 357 */
universe@136 358 #define ucx_map_cstr_remove(map, key) \
universe@327 359 ucx_map_remove(map, ucx_key(key, strlen(key)))
universe@146 360
universe@136 361 /**
universe@136 362 * Shorthand for removing data from the map with an integer key.
universe@136 363 * @param map the map
universe@136 364 * @param key the key
universe@138 365 * @return the removed value
universe@136 366 * @see ucx_map_remove()
universe@136 367 */
universe@136 368 #define ucx_map_int_remove(map, key) \
universe@327 369 ucx_map_remove(map, ucx_key(&key, sizeof(key)))
olaf@20 370
universe@138 371 /**
universe@225 372 * Creates a UcxKey based on the given data.
universe@138 373 *
universe@138 374 * This function implicitly computes the hash.
universe@138 375 *
universe@138 376 * @param data the data for the key
universe@138 377 * @param len the length of the data
universe@225 378 * @return a UcxKey with implicitly computed hash
universe@138 379 * @see ucx_hash()
universe@138 380 */
universe@327 381 UcxKey ucx_key(const void *data, size_t len);
olaf@20 382
universe@138 383 /**
universe@138 384 * Computes a murmur hash-2.
universe@138 385 *
universe@138 386 * @param data the data to hash
universe@138 387 * @param len the length of the data
universe@138 388 * @return the murmur hash-2 of the data
universe@138 389 */
universe@67 390 int ucx_hash(const char *data, size_t len);
olaf@2 391
universe@138 392 /**
universe@138 393 * Creates an iterator for a map.
universe@138 394 *
universe@225 395 * <b>Note:</b> A UcxMapIterator iterates over all elements in all element
universe@138 396 * lists successively. Therefore the order highly depends on the key hashes and
universe@138 397 * may vary under different map sizes. So generally you may <b>NOT</b> rely on
universe@138 398 * the iteration order.
universe@138 399 *
universe@138 400 * <b>Note:</b> The iterator is <b>NOT</b> initialized. You need to call
universe@138 401 * ucx_map_iter_next() at least once before accessing any information. However,
universe@225 402 * it is not recommended to access the fields of a UcxMapIterator directly.
universe@138 403 *
universe@138 404 * @param map the map to create the iterator for
universe@138 405 * @return an iterator initialized on the first element of the
universe@138 406 * first element list
universe@138 407 * @see ucx_map_iter_next()
universe@138 408 */
olaf@31 409 UcxMapIterator ucx_map_iterator(UcxMap *map);
olaf@31 410
universe@138 411 /**
universe@138 412 * Proceeds to the next element of the map (if any).
universe@138 413 *
universe@138 414 * Subsequent calls on the same iterator proceed to the next element and
universe@138 415 * store the key/value-pair into the memory specified as arguments of this
universe@138 416 * function.
universe@138 417 *
universe@138 418 * If no further elements are found, this function returns zero and leaves the
universe@138 419 * last found key/value-pair in memory.
universe@138 420 *
universe@138 421 * @param iterator the iterator to use
universe@138 422 * @param key a pointer to the memory where to store the key
universe@138 423 * @param value a pointer to the memory where to store the value
universe@138 424 * @return 1, if another element was found, 0 if all elements has been processed
universe@138 425 * @see ucx_map_iterator()
universe@138 426 */
universe@138 427 int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key, void **value);
olaf@31 428
universe@42 429
olaf@2 430 #ifdef __cplusplus
olaf@2 431 }
olaf@2 432 #endif
olaf@2 433
olaf@120 434 #endif /* UCX_MAP_H */
olaf@2 435

mercurial