Mon, 12 Aug 2013 14:39:51 +0200
documented map.h + changed return value of ucx_map_iter_next()
olaf@2 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@103 | 4 | * Copyright 2013 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 | |
olaf@20 | 44 | #include "ucx.h" |
olaf@20 | 45 | #include "string.h" |
universe@114 | 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@138 | 62 | * @param iter an 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@138 | 70 | /** Type for a key of an UcxMap. @see UcxKey */ |
olaf@31 | 71 | typedef struct UcxKey UcxKey; |
universe@138 | 72 | /** Type for an element of an UcxMap. @see UcxMapElement */ |
olaf@31 | 73 | typedef struct UcxMapElement UcxMapElement; |
universe@138 | 74 | /** Type for an iterator over an UcxMap. @see UcxMapIterator */ |
olaf@31 | 75 | typedef struct UcxMapIterator UcxMapIterator; |
olaf@2 | 76 | |
universe@138 | 77 | /** Structure for the UCX map. */ |
olaf@20 | 78 | struct UcxMap { |
universe@138 | 79 | /** An allocator that is used for the map elements. */ |
olaf@107 | 80 | UcxAllocator *allocator; |
universe@138 | 81 | /** The array of map element lists. */ |
universe@29 | 82 | UcxMapElement **map; |
universe@138 | 83 | /** The size of the map is the length of the element list array. */ |
olaf@20 | 84 | size_t size; |
universe@138 | 85 | /** The count of elements currently stored in this map. */ |
olaf@45 | 86 | size_t count; |
olaf@20 | 87 | }; |
olaf@2 | 88 | |
universe@138 | 89 | /** Structure for a key of an UcxMap. */ |
olaf@20 | 90 | struct UcxKey { |
universe@138 | 91 | /** The key data. */ |
olaf@20 | 92 | void *data; |
universe@138 | 93 | /** The length of the key data. */ |
olaf@20 | 94 | size_t len; |
universe@138 | 95 | /** The hash value of the key data. */ |
olaf@20 | 96 | int hash; |
olaf@20 | 97 | }; |
olaf@20 | 98 | |
universe@138 | 99 | /** Structure for an element of an UcxMap. */ |
olaf@20 | 100 | struct UcxMapElement { |
universe@138 | 101 | /** The value data. */ |
olaf@20 | 102 | void *data; |
universe@138 | 103 | /** A pointer to the next element in the current list. */ |
olaf@20 | 104 | UcxMapElement *next; |
universe@138 | 105 | /** The corresponding key. */ |
olaf@20 | 106 | UcxKey key; |
olaf@20 | 107 | }; |
olaf@20 | 108 | |
universe@138 | 109 | /** Structure for an iterator over an UcxMap. */ |
olaf@31 | 110 | struct UcxMapIterator { |
universe@138 | 111 | /** The map to iterate over. */ |
olaf@31 | 112 | UcxMap *map; |
universe@138 | 113 | /** The current map element. */ |
olaf@31 | 114 | UcxMapElement *cur; |
universe@138 | 115 | /** |
universe@138 | 116 | * The current index of the element list array. |
universe@138 | 117 | * <b>Attention: </b> this is <b>NOT</b> the element index! Do <b>NOT</b> |
universe@138 | 118 | * manually iterate over the map by increasing this index. Use |
universe@138 | 119 | * ucx_map_iter_next(). |
universe@138 | 120 | * @see UcxMap.map*/ |
universe@95 | 121 | size_t index; |
olaf@31 | 122 | }; |
olaf@31 | 123 | |
universe@136 | 124 | /** |
universe@136 | 125 | * Creates a new hash map with the specified size. |
universe@136 | 126 | * @param size the size of the hash map |
universe@136 | 127 | * @return a pointer to the new hash map |
universe@136 | 128 | */ |
universe@136 | 129 | UcxMap *ucx_map_new(size_t size); |
olaf@20 | 130 | |
universe@136 | 131 | /** |
universe@136 | 132 | * Creates a new hash map with the specified size using an UcxAllocator. |
universe@136 | 133 | * @param size the size of the hash map |
universe@136 | 134 | * @param allocator the allocator to use |
universe@136 | 135 | * @return a pointer to the new hash map |
universe@136 | 136 | */ |
universe@136 | 137 | UcxMap *ucx_map_new_a(size_t size, UcxAllocator *allocator); |
universe@136 | 138 | |
universe@136 | 139 | /** |
universe@136 | 140 | * Frees a hash map. |
universe@136 | 141 | * |
universe@136 | 142 | * <b>Note:</b> the contents are <b>not</b> freed, use an UcxMempool for that |
universe@136 | 143 | * purpose. |
universe@136 | 144 | * |
universe@136 | 145 | * @param map the map to be freed |
universe@136 | 146 | */ |
universe@29 | 147 | void ucx_map_free(UcxMap *map); |
universe@136 | 148 | |
universe@138 | 149 | /** |
universe@138 | 150 | * Copies contents from a map to another map using a copy function. |
universe@138 | 151 | * |
universe@138 | 152 | * <b>Note:</b> The destination map does not need to be empty. However, if it |
universe@138 | 153 | * contains data with keys that are also present in the source map, the contents |
universe@138 | 154 | * are overwritten. |
universe@138 | 155 | * |
universe@138 | 156 | * @param from the source map |
universe@138 | 157 | * @param to the destination map |
universe@138 | 158 | * @param fnc the copy function or <code>NULL</code> if the pointer address |
universe@138 | 159 | * shall be copied |
universe@138 | 160 | * @param data additional data for the copy function |
universe@138 | 161 | * @return 0 on success or a non-zero value on memory allocation errors |
universe@138 | 162 | */ |
universe@67 | 163 | int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, |
universe@67 | 164 | copy_func fnc, void *data); |
universe@138 | 165 | |
universe@138 | 166 | /** |
universe@138 | 167 | * Clones the map and rehashes if necessary. |
universe@138 | 168 | * |
universe@138 | 169 | * <b>Note:</b> In contrast to ucx_map_rehash() the load factor is irrelevant. |
universe@138 | 170 | * This function <i>always</i> ensures a new UcxMap.size of at least |
universe@138 | 171 | * 2.5*UcxMap.count. |
universe@138 | 172 | * |
universe@138 | 173 | * @param map the map to clone |
universe@138 | 174 | * @param fnc the copy function to use or <code>NULL</code> if the new and |
universe@138 | 175 | * the old map shall share the data pointers |
universe@138 | 176 | * @param data additional data for the copy function |
universe@138 | 177 | * @return the cloned map |
universe@138 | 178 | * @see ucx_map_copy() |
universe@138 | 179 | */ |
olaf@44 | 180 | UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); |
universe@138 | 181 | |
universe@138 | 182 | /** |
universe@138 | 183 | * Increases size of the hash map, if necessary. |
universe@138 | 184 | * |
universe@138 | 185 | * The load value is 0.75*UcxMap.size. If the element count exceeds the load |
universe@138 | 186 | * value, the map needs to be rehashed. Otherwise no action is performed and |
universe@138 | 187 | * this function simply returns 0. |
universe@138 | 188 | * |
universe@138 | 189 | * The rehashing process ensures, that the UcxMap.size is at least |
universe@138 | 190 | * 2.5*UcxMap.count. So there is enough room for additional elements without |
universe@138 | 191 | * the need of another soon rehashing. |
universe@138 | 192 | * |
universe@138 | 193 | * You can use this function to dramatically increase access performance. |
universe@138 | 194 | * |
universe@138 | 195 | * @param map the map to rehash |
universe@138 | 196 | * @return 1, if a memory allocation error occurred, 0 otherwise |
universe@138 | 197 | */ |
olaf@52 | 198 | int ucx_map_rehash(UcxMap *map); |
olaf@20 | 199 | |
universe@138 | 200 | /** |
universe@138 | 201 | * Puts a key/value-pair into the map. |
universe@138 | 202 | * |
universe@138 | 203 | * @param map the map |
universe@138 | 204 | * @param key the key |
universe@138 | 205 | * @param value the value |
universe@138 | 206 | * @return 0 on success, non-zero value on failure |
universe@138 | 207 | */ |
universe@138 | 208 | int ucx_map_put(UcxMap *map, UcxKey key, void *value); |
universe@138 | 209 | |
universe@138 | 210 | /** |
universe@138 | 211 | * Retrieves a value by using a key. |
universe@138 | 212 | * |
universe@138 | 213 | * @param map the map |
universe@138 | 214 | * @param key the key |
universe@138 | 215 | * @return the value |
universe@138 | 216 | */ |
olaf@20 | 217 | void* ucx_map_get(UcxMap *map, UcxKey key); |
universe@138 | 218 | |
universe@138 | 219 | /** |
universe@138 | 220 | * Removes a key/value-pair from the map by using the key. |
universe@138 | 221 | * |
universe@138 | 222 | * @param map the map |
universe@138 | 223 | * @param key the key |
universe@138 | 224 | * @return the removed value |
universe@138 | 225 | */ |
universe@53 | 226 | void* ucx_map_remove(UcxMap *map, UcxKey key); |
olaf@20 | 227 | |
universe@136 | 228 | /** |
universe@136 | 229 | * Shorthand for putting data with a sstr_t key into the map. |
universe@136 | 230 | * @param map the map |
universe@136 | 231 | * @param key the key |
universe@136 | 232 | * @param value the value |
universe@138 | 233 | * @return 0 on success, non-zero value on failure |
universe@136 | 234 | * @see ucx_map_put() |
universe@136 | 235 | */ |
universe@136 | 236 | #define ucx_map_sstr_put(map, key, value) \ |
universe@136 | 237 | ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value) |
universe@136 | 238 | /** |
universe@136 | 239 | * Shorthand for putting data with a C string key into the map. |
universe@136 | 240 | * @param map the map |
universe@136 | 241 | * @param key the key |
universe@136 | 242 | * @param value the value |
universe@138 | 243 | * @return 0 on success, non-zero value on failure |
universe@136 | 244 | * @see ucx_map_put() |
universe@136 | 245 | */ |
universe@136 | 246 | #define ucx_map_cstr_put(map, key, value) \ |
universe@136 | 247 | ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value) |
universe@136 | 248 | /** |
universe@136 | 249 | * Shorthand for putting data with an integer key into the map. |
universe@136 | 250 | * @param map the map |
universe@136 | 251 | * @param key the key |
universe@136 | 252 | * @param value the value |
universe@138 | 253 | * @return 0 on success, non-zero value on failure |
universe@136 | 254 | * @see ucx_map_put() |
universe@136 | 255 | */ |
universe@136 | 256 | #define ucx_map_int_put(map, key, value) \ |
universe@136 | 257 | ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value) |
olaf@78 | 258 | |
olaf@78 | 259 | |
universe@136 | 260 | /** |
universe@136 | 261 | * Shorthand for getting data from the map with a sstr_t key. |
universe@136 | 262 | * @param map the map |
universe@136 | 263 | * @param key the key |
universe@138 | 264 | * @return the value |
universe@136 | 265 | * @see ucx_map_get() |
universe@136 | 266 | */ |
universe@136 | 267 | #define ucx_map_sstr_get(map, key) \ |
universe@136 | 268 | ucx_map_get(map, ucx_key(key.ptr, key.length)) |
universe@136 | 269 | /** |
universe@136 | 270 | * Shorthand for getting data from the map with a C string key. |
universe@138 | 271 | * @param map the map |
universe@138 | 272 | * @param key the key |
universe@138 | 273 | * @return the value |
universe@136 | 274 | * @see ucx_map_get() |
universe@136 | 275 | */ |
universe@136 | 276 | #define ucx_map_cstr_get(map, key) \ |
universe@136 | 277 | ucx_map_get(map, ucx_key((void*)key, strlen(key))) |
universe@136 | 278 | /** |
universe@136 | 279 | * Shorthand for getting data from the map with an integer key. |
universe@136 | 280 | * @param map the map |
universe@136 | 281 | * @param key the key |
universe@138 | 282 | * @return the value |
universe@136 | 283 | * @see ucx_map_get() |
universe@136 | 284 | */ |
universe@136 | 285 | #define ucx_map_int_get(map, key) \ |
universe@136 | 286 | ucx_map_get(map, ucx_key((void*)&key, sizeof(int))) |
universe@136 | 287 | /** |
universe@136 | 288 | * Shorthand for removing data from the map with a sstr_t key. |
universe@136 | 289 | * @param map the map |
universe@136 | 290 | * @param key the key |
universe@138 | 291 | * @return the removed value |
universe@136 | 292 | * @see ucx_map_remove() |
universe@136 | 293 | */ |
universe@136 | 294 | #define ucx_map_sstr_remove(map, key) \ |
universe@136 | 295 | ucx_map_remove(map, ucx_key(key.ptr, key.length)) |
universe@136 | 296 | /** |
universe@136 | 297 | * Shorthand for removing data from the map with a C string key. |
universe@136 | 298 | * @param map the map |
universe@136 | 299 | * @param key the key |
universe@138 | 300 | * @return the removed value |
universe@136 | 301 | * @see ucx_map_remove() |
universe@136 | 302 | */ |
universe@136 | 303 | #define ucx_map_cstr_remove(map, key) \ |
universe@136 | 304 | ucx_map_remove(map, ucx_key((void*)key, strlen(key))) |
universe@136 | 305 | /** |
universe@136 | 306 | * Shorthand for removing data from the map with an integer key. |
universe@136 | 307 | * @param map the map |
universe@136 | 308 | * @param key the key |
universe@138 | 309 | * @return the removed value |
universe@136 | 310 | * @see ucx_map_remove() |
universe@136 | 311 | */ |
universe@136 | 312 | #define ucx_map_int_remove(map, key) \ |
universe@136 | 313 | ucx_map_remove(map, ucx_key((void*)&key, sizeof(key))) |
olaf@20 | 314 | |
universe@138 | 315 | /** |
universe@138 | 316 | * Creates an UcxKey based on the given data. |
universe@138 | 317 | * |
universe@138 | 318 | * This function implicitly computes the hash. |
universe@138 | 319 | * |
universe@138 | 320 | * @param data the data for the key |
universe@138 | 321 | * @param len the length of the data |
universe@138 | 322 | * @return an UcxKey with implicitly computed hash |
universe@138 | 323 | * @see ucx_hash() |
universe@138 | 324 | */ |
olaf@20 | 325 | UcxKey ucx_key(void *data, size_t len); |
olaf@20 | 326 | |
universe@138 | 327 | /** |
universe@138 | 328 | * Computes a murmur hash-2. |
universe@138 | 329 | * |
universe@138 | 330 | * @param data the data to hash |
universe@138 | 331 | * @param len the length of the data |
universe@138 | 332 | * @return the murmur hash-2 of the data |
universe@138 | 333 | */ |
universe@67 | 334 | int ucx_hash(const char *data, size_t len); |
olaf@2 | 335 | |
universe@138 | 336 | /** |
universe@138 | 337 | * Creates an iterator for a map. |
universe@138 | 338 | * |
universe@138 | 339 | * <b>Note:</b> An UcxMapIterator iterates over all elements in all element |
universe@138 | 340 | * lists successively. Therefore the order highly depends on the key hashes and |
universe@138 | 341 | * may vary under different map sizes. So generally you may <b>NOT</b> rely on |
universe@138 | 342 | * the iteration order. |
universe@138 | 343 | * |
universe@138 | 344 | * <b>Note:</b> The iterator is <b>NOT</b> initialized. You need to call |
universe@138 | 345 | * ucx_map_iter_next() at least once before accessing any information. However, |
universe@138 | 346 | * it is not recommended to access the fields of an UcxMapIterator directly. |
universe@138 | 347 | * |
universe@138 | 348 | * @param map the map to create the iterator for |
universe@138 | 349 | * @return an iterator initialized on the first element of the |
universe@138 | 350 | * first element list |
universe@138 | 351 | * @see ucx_map_iter_next() |
universe@138 | 352 | */ |
olaf@31 | 353 | UcxMapIterator ucx_map_iterator(UcxMap *map); |
olaf@31 | 354 | |
universe@138 | 355 | /** |
universe@138 | 356 | * Proceeds to the next element of the map (if any). |
universe@138 | 357 | * |
universe@138 | 358 | * Subsequent calls on the same iterator proceed to the next element and |
universe@138 | 359 | * store the key/value-pair into the memory specified as arguments of this |
universe@138 | 360 | * function. |
universe@138 | 361 | * |
universe@138 | 362 | * If no further elements are found, this function returns zero and leaves the |
universe@138 | 363 | * last found key/value-pair in memory. |
universe@138 | 364 | * |
universe@138 | 365 | * @param iterator the iterator to use |
universe@138 | 366 | * @param key a pointer to the memory where to store the key |
universe@138 | 367 | * @param value a pointer to the memory where to store the value |
universe@138 | 368 | * @return 1, if another element was found, 0 if all elements has been processed |
universe@138 | 369 | * @see ucx_map_iterator() |
universe@138 | 370 | */ |
universe@138 | 371 | int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key, void **value); |
olaf@31 | 372 | |
universe@42 | 373 | |
olaf@2 | 374 | #ifdef __cplusplus |
olaf@2 | 375 | } |
olaf@2 | 376 | #endif |
olaf@2 | 377 | |
olaf@120 | 378 | #endif /* UCX_MAP_H */ |
olaf@2 | 379 |