ucx/map.h

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

mercurial