chiark / gitweb /
parse socket files properly
[elogind.git] / hashmap.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef foohashmaphfoo
4 #define foohashmaphfoo
5
6 #include <stdbool.h>
7
8 /* Pretty straightforward hash table implementation. As a minor
9  * optimization a NULL hashmap object will be treated as empty hashmap
10  * for all read operations. That way it is not necessary to
11  * instantiate an object for each Hashmap use. */
12
13 typedef struct Hashmap Hashmap;
14
15 typedef unsigned (*hash_func_t)(const void *p);
16 typedef int (*compare_func_t)(const void *a, const void *b);
17
18 unsigned string_hash_func(const void *p);
19 int string_compare_func(const void *a, const void *b);
20
21 unsigned trivial_hash_func(const void *p);
22 int trivial_compare_func(const void *a, const void *b);
23
24 Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func);
25 void hashmap_free(Hashmap *h);
26 Hashmap *hashmap_copy(Hashmap *h);
27
28 int hashmap_put(Hashmap *h, const void *key, void *value);
29 void* hashmap_get(Hashmap *h, const void *key);
30 void* hashmap_remove(Hashmap *h, const void *key);
31
32 int hashmap_merge(Hashmap *h, Hashmap *other);
33
34 unsigned hashmap_size(Hashmap *h);
35 bool hashmap_isempty(Hashmap *h);
36
37 void *hashmap_iterate(Hashmap *h, void **state, const void **key);
38 void *hashmap_iterate_backwards(Hashmap *h, void **state, const void **key);
39
40 void *hashmap_steal_first(Hashmap *h);
41 void* hashmap_first(Hashmap *h);
42 void* hashmap_last(Hashmap *h);
43
44 #define HASHMAP_FOREACH(e, h, state) \
45         for ((state) = NULL, (e) = hashmap_iterate((h), &(state), NULL); (e); (e) = hashmap_iterate((h), &(state), NULL))
46
47 #define HASHMAP_FOREACH_BACKWARDS(e, h, state) \
48         for ((state) = NULL, (e) = hashmap_iterate_backwards((h), &(state), NULL); (e); (e) = hashmap_iterate_backwards((h), &(state), NULL))
49
50 #endif