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