]>
Commit | Line | Data |
---|---|---|
03467c88 | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 | 2 | |
c2f1db8f | 3 | #pragma once |
60918275 | 4 | |
a7334b09 LP |
5 | /*** |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2010 Lennart Poettering | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
11 | under the terms of the GNU Lesser General Public License as published by |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
13 | (at your option) any later version. |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 18 | Lesser General Public License for more details. |
a7334b09 | 19 | |
5430f7f2 | 20 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
22 | ***/ | |
23 | ||
60918275 LP |
24 | #include <stdbool.h> |
25 | ||
44a6b1b6 | 26 | #include "macro.h" |
5e2f14e6 | 27 | #include "util.h" |
44a6b1b6 | 28 | |
60918275 LP |
29 | /* Pretty straightforward hash table implementation. As a minor |
30 | * optimization a NULL hashmap object will be treated as empty hashmap | |
31 | * for all read operations. That way it is not necessary to | |
32 | * instantiate an object for each Hashmap use. */ | |
33 | ||
34 | typedef struct Hashmap Hashmap; | |
034c6ed7 LP |
35 | typedef struct _IteratorStruct _IteratorStruct; |
36 | typedef _IteratorStruct* Iterator; | |
37 | ||
38 | #define ITERATOR_FIRST ((Iterator) 0) | |
39 | #define ITERATOR_LAST ((Iterator) -1) | |
60918275 LP |
40 | |
41 | typedef unsigned (*hash_func_t)(const void *p); | |
42 | typedef int (*compare_func_t)(const void *a, const void *b); | |
43 | ||
44a6b1b6 ZJS |
44 | unsigned string_hash_func(const void *p) _pure_; |
45 | int string_compare_func(const void *a, const void *b) _pure_; | |
60918275 | 46 | |
1210bc66 LP |
47 | /* This will compare the passed pointers directly, and will not |
48 | * dereference them. This is hence not useful for strings or | |
49 | * suchlike. */ | |
44a6b1b6 ZJS |
50 | unsigned trivial_hash_func(const void *p) _const_; |
51 | int trivial_compare_func(const void *a, const void *b) _const_; | |
60918275 | 52 | |
44a6b1b6 ZJS |
53 | unsigned uint64_hash_func(const void *p) _pure_; |
54 | int uint64_compare_func(const void *a, const void *b) _pure_; | |
a4bcff5b | 55 | |
60918275 | 56 | Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func); |
91cdde8a | 57 | void hashmap_free(Hashmap *h); |
449ddb2d | 58 | void hashmap_free_free(Hashmap *h); |
fabe5c0e | 59 | void hashmap_free_free_free(Hashmap *h); |
91cdde8a | 60 | Hashmap *hashmap_copy(Hashmap *h); |
034c6ed7 | 61 | int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func); |
60918275 LP |
62 | |
63 | int hashmap_put(Hashmap *h, const void *key, void *value); | |
d99ae53a | 64 | int hashmap_update(Hashmap *h, const void *key, void *value); |
3158713e | 65 | int hashmap_replace(Hashmap *h, const void *key, void *value); |
2f79c10e DB |
66 | void *hashmap_get(Hashmap *h, const void *key); |
67 | void *hashmap_get2(Hashmap *h, const void *key, void **rkey); | |
96342de6 | 68 | bool hashmap_contains(Hashmap *h, const void *key); |
2f79c10e DB |
69 | void *hashmap_remove(Hashmap *h, const void *key); |
70 | void *hashmap_remove_value(Hashmap *h, const void *key, void *value); | |
101d8e63 | 71 | int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value); |
8fe914ec | 72 | int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value); |
60918275 | 73 | |
91cdde8a | 74 | int hashmap_merge(Hashmap *h, Hashmap *other); |
101d8e63 LP |
75 | void hashmap_move(Hashmap *h, Hashmap *other); |
76 | int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key); | |
91cdde8a | 77 | |
44a6b1b6 ZJS |
78 | unsigned hashmap_size(Hashmap *h) _pure_; |
79 | bool hashmap_isempty(Hashmap *h) _pure_; | |
45fa9e29 | 80 | unsigned hashmap_buckets(Hashmap *h) _pure_; |
60918275 | 81 | |
034c6ed7 LP |
82 | void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key); |
83 | void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key); | |
84 | void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i); | |
60918275 | 85 | |
11dd41ce | 86 | void hashmap_clear(Hashmap *h); |
9946996c | 87 | void hashmap_clear_free(Hashmap *h); |
fabe5c0e | 88 | void hashmap_clear_free_free(Hashmap *h); |
9946996c | 89 | |
60918275 | 90 | void *hashmap_steal_first(Hashmap *h); |
22be093f | 91 | void *hashmap_steal_first_key(Hashmap *h); |
44a6b1b6 ZJS |
92 | void *hashmap_first(Hashmap *h) _pure_; |
93 | void *hashmap_first_key(Hashmap *h) _pure_; | |
94 | void *hashmap_last(Hashmap *h) _pure_; | |
60918275 | 95 | |
3c1668da LP |
96 | void *hashmap_next(Hashmap *h, const void *key); |
97 | ||
db1413d7 KS |
98 | char **hashmap_get_strv(Hashmap *h); |
99 | ||
034c6ed7 | 100 | #define HASHMAP_FOREACH(e, h, i) \ |
aed5e44d | 101 | for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL)) |
60918275 | 102 | |
034c6ed7 | 103 | #define HASHMAP_FOREACH_KEY(e, k, h, i) \ |
aed5e44d | 104 | for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k))) |
11dd41ce | 105 | |
034c6ed7 | 106 | #define HASHMAP_FOREACH_BACKWARDS(e, h, i) \ |
aed5e44d | 107 | for ((i) = ITERATOR_LAST, (e) = hashmap_iterate_backwards((h), &(i), NULL); (e); (e) = hashmap_iterate_backwards((h), &(i), NULL)) |
5e2f14e6 LP |
108 | |
109 | DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free); | |
110 | DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free); | |
111 | DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free_free); | |
112 | #define _cleanup_hashmap_free_ _cleanup_(hashmap_freep) | |
113 | #define _cleanup_hashmap_free_free_ _cleanup_(hashmap_free_freep) | |
114 | #define _cleanup_hashmap_free_free_free_ _cleanup_(hashmap_free_free_freep) |