ucx/map.h

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

mercurial