Mon, 12 Aug 2013 14:39:51 +0200
documented map.h + changed return value of ucx_map_iter_next()
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 /**
30 * @file map.h
31 *
32 * Hash map implementation.
33 *
34 * This implementation uses murmur hash 2 and separate chaining with linked
35 * lists.
36 *
37 * @author Mike Becker
38 * @author Olaf Wintermann
39 */
41 #ifndef UCX_MAP_H
42 #define UCX_MAP_H
44 #include "ucx.h"
45 #include "string.h"
46 #include "allocator.h"
47 #include <stdio.h>
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
53 /**
54 * Loop statement for UCX maps.
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);)
68 /** Type for the UCX map. @see UcxMap */
69 typedef struct UcxMap UcxMap;
70 /** Type for a key of an UcxMap. @see UcxKey */
71 typedef struct UcxKey UcxKey;
72 /** Type for an element of an UcxMap. @see UcxMapElement */
73 typedef struct UcxMapElement UcxMapElement;
74 /** Type for an iterator over an UcxMap. @see UcxMapIterator */
75 typedef struct UcxMapIterator UcxMapIterator;
77 /** Structure for the UCX map. */
78 struct UcxMap {
79 /** An allocator that is used for the map elements. */
80 UcxAllocator *allocator;
81 /** The array of map element lists. */
82 UcxMapElement **map;
83 /** The size of the map is the length of the element list array. */
84 size_t size;
85 /** The count of elements currently stored in this map. */
86 size_t count;
87 };
89 /** Structure for a key of an UcxMap. */
90 struct UcxKey {
91 /** The key data. */
92 void *data;
93 /** The length of the key data. */
94 size_t len;
95 /** The hash value of the key data. */
96 int hash;
97 };
99 /** Structure for an element of an UcxMap. */
100 struct UcxMapElement {
101 /** The value data. */
102 void *data;
103 /** A pointer to the next element in the current list. */
104 UcxMapElement *next;
105 /** The corresponding key. */
106 UcxKey key;
107 };
109 /** Structure for an iterator over an UcxMap. */
110 struct UcxMapIterator {
111 /** The map to iterate over. */
112 UcxMap *map;
113 /** The current map element. */
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*/
121 size_t index;
122 };
124 /**
125 * Creates a new hash map with the specified size.
126 * @param size the size of the hash map
127 * @return a pointer to the new hash map
128 */
129 UcxMap *ucx_map_new(size_t size);
131 /**
132 * Creates a new hash map with the specified size using an UcxAllocator.
133 * @param size the size of the hash map
134 * @param allocator the allocator to use
135 * @return a pointer to the new hash map
136 */
137 UcxMap *ucx_map_new_a(size_t size, UcxAllocator *allocator);
139 /**
140 * Frees a hash map.
141 *
142 * <b>Note:</b> the contents are <b>not</b> freed, use an UcxMempool for that
143 * purpose.
144 *
145 * @param map the map to be freed
146 */
147 void ucx_map_free(UcxMap *map);
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 */
163 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
164 copy_func fnc, void *data);
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 */
180 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
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 */
198 int ucx_map_rehash(UcxMap *map);
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);
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 */
217 void* ucx_map_get(UcxMap *map, UcxKey key);
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 */
226 void* ucx_map_remove(UcxMap *map, UcxKey key);
228 /**
229 * Shorthand for putting data with a sstr_t key into the map.
230 * @param map the map
231 * @param key the key
232 * @param value the value
233 * @return 0 on success, non-zero value on failure
234 * @see ucx_map_put()
235 */
236 #define ucx_map_sstr_put(map, key, value) \
237 ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
238 /**
239 * Shorthand for putting data with a C string key into the map.
240 * @param map the map
241 * @param key the key
242 * @param value the value
243 * @return 0 on success, non-zero value on failure
244 * @see ucx_map_put()
245 */
246 #define ucx_map_cstr_put(map, key, value) \
247 ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
248 /**
249 * Shorthand for putting data with an integer key into the map.
250 * @param map the map
251 * @param key the key
252 * @param value the value
253 * @return 0 on success, non-zero value on failure
254 * @see ucx_map_put()
255 */
256 #define ucx_map_int_put(map, key, value) \
257 ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value)
260 /**
261 * Shorthand for getting data from the map with a sstr_t key.
262 * @param map the map
263 * @param key the key
264 * @return the value
265 * @see ucx_map_get()
266 */
267 #define ucx_map_sstr_get(map, key) \
268 ucx_map_get(map, ucx_key(key.ptr, key.length))
269 /**
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
274 * @see ucx_map_get()
275 */
276 #define ucx_map_cstr_get(map, key) \
277 ucx_map_get(map, ucx_key((void*)key, strlen(key)))
278 /**
279 * Shorthand for getting data from the map with an integer key.
280 * @param map the map
281 * @param key the key
282 * @return the value
283 * @see ucx_map_get()
284 */
285 #define ucx_map_int_get(map, key) \
286 ucx_map_get(map, ucx_key((void*)&key, sizeof(int)))
287 /**
288 * Shorthand for removing data from the map with a sstr_t key.
289 * @param map the map
290 * @param key the key
291 * @return the removed value
292 * @see ucx_map_remove()
293 */
294 #define ucx_map_sstr_remove(map, key) \
295 ucx_map_remove(map, ucx_key(key.ptr, key.length))
296 /**
297 * Shorthand for removing data from the map with a C string key.
298 * @param map the map
299 * @param key the key
300 * @return the removed value
301 * @see ucx_map_remove()
302 */
303 #define ucx_map_cstr_remove(map, key) \
304 ucx_map_remove(map, ucx_key((void*)key, strlen(key)))
305 /**
306 * Shorthand for removing data from the map with an integer key.
307 * @param map the map
308 * @param key the key
309 * @return the removed value
310 * @see ucx_map_remove()
311 */
312 #define ucx_map_int_remove(map, key) \
313 ucx_map_remove(map, ucx_key((void*)&key, sizeof(key)))
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 */
325 UcxKey ucx_key(void *data, size_t len);
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 */
334 int ucx_hash(const char *data, size_t len);
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 */
353 UcxMapIterator ucx_map_iterator(UcxMap *map);
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);
374 #ifdef __cplusplus
375 }
376 #endif
378 #endif /* UCX_MAP_H */