]> git.ipfire.org Git - people/ms/systemd.git/blame - hashmap.h
don't allow comments at the end of lines
[people/ms/systemd.git] / hashmap.h
CommitLineData
60918275
LP
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
13typedef struct Hashmap Hashmap;
034c6ed7
LP
14typedef struct _IteratorStruct _IteratorStruct;
15typedef _IteratorStruct* Iterator;
16
17#define ITERATOR_FIRST ((Iterator) 0)
18#define ITERATOR_LAST ((Iterator) -1)
60918275
LP
19
20typedef unsigned (*hash_func_t)(const void *p);
21typedef int (*compare_func_t)(const void *a, const void *b);
22
23unsigned string_hash_func(const void *p);
24int string_compare_func(const void *a, const void *b);
25
26unsigned trivial_hash_func(const void *p);
27int trivial_compare_func(const void *a, const void *b);
28
29Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func);
91cdde8a
LP
30void hashmap_free(Hashmap *h);
31Hashmap *hashmap_copy(Hashmap *h);
034c6ed7 32int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func);
60918275
LP
33
34int hashmap_put(Hashmap *h, const void *key, void *value);
3158713e 35int hashmap_replace(Hashmap *h, const void *key, void *value);
60918275
LP
36void* hashmap_get(Hashmap *h, const void *key);
37void* hashmap_remove(Hashmap *h, const void *key);
3158713e 38void* hashmap_remove_value(Hashmap *h, const void *key, void *value);
60918275 39
91cdde8a
LP
40int hashmap_merge(Hashmap *h, Hashmap *other);
41
60918275
LP
42unsigned hashmap_size(Hashmap *h);
43bool hashmap_isempty(Hashmap *h);
44
034c6ed7
LP
45void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key);
46void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key);
47void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i);
60918275 48
11dd41ce 49void hashmap_clear(Hashmap *h);
60918275
LP
50void *hashmap_steal_first(Hashmap *h);
51void* hashmap_first(Hashmap *h);
52void* hashmap_last(Hashmap *h);
53
034c6ed7
LP
54#define HASHMAP_FOREACH(e, h, i) \
55 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL))
60918275 56
034c6ed7
LP
57#define HASHMAP_FOREACH_KEY(e, k, h, i) \
58 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
11dd41ce 59
034c6ed7
LP
60#define HASHMAP_FOREACH_BACKWARDS(e, h, i) \
61 for ((i) = ITERATE_LAST, (e) = hashmap_iterate_backwards((h), &(i), NULL); (e); (e) = hashmap_iterate_backwards((h), &(i), NULL))
60918275
LP
62
63#endif