]>
Commit | Line | Data |
---|---|---|
60918275 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | #ifndef foohashmaphfoo | |
4 | #define foohashmaphfoo | |
5 | ||
a7334b09 LP |
6 | /*** |
7 | This file is part of systemd. | |
8 | ||
9 | Copyright 2010 Lennart Poettering | |
10 | ||
11 | systemd is free software; you can redistribute it and/or modify it | |
12 | under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation; either version 2 of the License, or | |
14 | (at your option) any later version. | |
15 | ||
16 | systemd is distributed in the hope that it will be useful, but | |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
23 | ***/ | |
24 | ||
60918275 LP |
25 | #include <stdbool.h> |
26 | ||
27 | /* Pretty straightforward hash table implementation. As a minor | |
28 | * optimization a NULL hashmap object will be treated as empty hashmap | |
29 | * for all read operations. That way it is not necessary to | |
30 | * instantiate an object for each Hashmap use. */ | |
31 | ||
32 | typedef struct Hashmap Hashmap; | |
034c6ed7 LP |
33 | typedef struct _IteratorStruct _IteratorStruct; |
34 | typedef _IteratorStruct* Iterator; | |
35 | ||
36 | #define ITERATOR_FIRST ((Iterator) 0) | |
37 | #define ITERATOR_LAST ((Iterator) -1) | |
60918275 LP |
38 | |
39 | typedef unsigned (*hash_func_t)(const void *p); | |
40 | typedef int (*compare_func_t)(const void *a, const void *b); | |
41 | ||
42 | unsigned string_hash_func(const void *p); | |
43 | int string_compare_func(const void *a, const void *b); | |
44 | ||
45 | unsigned trivial_hash_func(const void *p); | |
46 | int trivial_compare_func(const void *a, const void *b); | |
47 | ||
48 | Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func); | |
91cdde8a LP |
49 | void hashmap_free(Hashmap *h); |
50 | Hashmap *hashmap_copy(Hashmap *h); | |
034c6ed7 | 51 | int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func); |
60918275 LP |
52 | |
53 | int hashmap_put(Hashmap *h, const void *key, void *value); | |
3158713e | 54 | int hashmap_replace(Hashmap *h, const void *key, void *value); |
60918275 LP |
55 | void* hashmap_get(Hashmap *h, const void *key); |
56 | void* hashmap_remove(Hashmap *h, const void *key); | |
3158713e | 57 | void* hashmap_remove_value(Hashmap *h, const void *key, void *value); |
101d8e63 | 58 | int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value); |
60918275 | 59 | |
91cdde8a | 60 | int hashmap_merge(Hashmap *h, Hashmap *other); |
101d8e63 LP |
61 | void hashmap_move(Hashmap *h, Hashmap *other); |
62 | int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key); | |
91cdde8a | 63 | |
60918275 LP |
64 | unsigned hashmap_size(Hashmap *h); |
65 | bool hashmap_isempty(Hashmap *h); | |
66 | ||
034c6ed7 LP |
67 | void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key); |
68 | void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key); | |
69 | void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i); | |
60918275 | 70 | |
11dd41ce | 71 | void hashmap_clear(Hashmap *h); |
60918275 LP |
72 | void *hashmap_steal_first(Hashmap *h); |
73 | void* hashmap_first(Hashmap *h); | |
74 | void* hashmap_last(Hashmap *h); | |
75 | ||
034c6ed7 LP |
76 | #define HASHMAP_FOREACH(e, h, i) \ |
77 | for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL)) | |
60918275 | 78 | |
034c6ed7 LP |
79 | #define HASHMAP_FOREACH_KEY(e, k, h, i) \ |
80 | for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k))) | |
11dd41ce | 81 | |
034c6ed7 LP |
82 | #define HASHMAP_FOREACH_BACKWARDS(e, h, i) \ |
83 | for ((i) = ITERATE_LAST, (e) = hashmap_iterate_backwards((h), &(i), NULL); (e); (e) = hashmap_iterate_backwards((h), &(i), NULL)) | |
60918275 LP |
84 | |
85 | #endif |