]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/set.h
Merge pull request #20303 from andir/sysconfig-example
[thirdparty/systemd.git] / src / basic / set.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
60918275 3
d97c5aea 4#include "extract-word.h"
60918275 5#include "hashmap.h"
a2341f68 6#include "macro.h"
60918275 7
3fb2326f
ZJS
8#define set_free_and_replace(a, b) \
9 ({ \
10 set_free(a); \
11 (a) = (b); \
12 (b) = NULL; \
13 0; \
14 })
15
8a35af80 16Set* _set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
138f49e4 17#define set_new(ops) _set_new(ops HASHMAP_DEBUG_SRC_ARGS)
60918275 18
8a35af80 19static inline Set* set_free(Set *s) {
138f49e4 20 return (Set*) _hashmap_free(HASHMAP_BASE(s), NULL, NULL);
89439d4f 21}
60918275 22
8a35af80 23static inline Set* set_free_free(Set *s) {
138f49e4 24 return (Set*) _hashmap_free(HASHMAP_BASE(s), free, NULL);
89439d4f
MS
25}
26
27/* no set_free_free_free */
28
5ef8b072 29#define set_copy(s) ((Set*) _hashmap_copy(HASHMAP_BASE(s) HASHMAP_DEBUG_SRC_ARGS))
89439d4f 30
138f49e4
ZJS
31int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
32#define set_ensure_allocated(h, ops) _set_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
60918275 33
89439d4f
MS
34int set_put(Set *s, const void *key);
35/* no set_update */
36/* no set_replace */
f3f14c57 37static inline void *set_get(const Set *s, const void *key) {
138f49e4 38 return _hashmap_get(HASHMAP_BASE((Set *) s), key);
89439d4f
MS
39}
40/* no set_get2 */
41
3c4e3031 42static inline bool set_contains(const Set *s, const void *key) {
138f49e4 43 return _hashmap_contains(HASHMAP_BASE((Set *) s), key);
89439d4f
MS
44}
45
4a9185c4 46static inline void *set_remove(Set *s, const void *key) {
138f49e4 47 return _hashmap_remove(HASHMAP_BASE(s), key);
89439d4f
MS
48}
49
50/* no set_remove2 */
51/* no set_remove_value */
52int set_remove_and_put(Set *s, const void *old_key, const void *new_key);
53/* no set_remove_and_replace */
91cdde8a
LP
54int set_merge(Set *s, Set *other);
55
89439d4f 56static inline int set_reserve(Set *h, unsigned entries_add) {
138f49e4 57 return _hashmap_reserve(HASHMAP_BASE(h), entries_add);
89439d4f
MS
58}
59
60static inline int set_move(Set *s, Set *other) {
138f49e4 61 return _hashmap_move(HASHMAP_BASE(s), HASHMAP_BASE(other));
89439d4f
MS
62}
63
64static inline int set_move_one(Set *s, Set *other, const void *key) {
138f49e4 65 return _hashmap_move_one(HASHMAP_BASE(s), HASHMAP_BASE(other), key);
89439d4f
MS
66}
67
3c4e3031 68static inline unsigned set_size(const Set *s) {
138f49e4 69 return _hashmap_size(HASHMAP_BASE((Set *) s));
89439d4f
MS
70}
71
3c4e3031 72static inline bool set_isempty(const Set *s) {
89439d4f
MS
73 return set_size(s) == 0;
74}
75
3c4e3031 76static inline unsigned set_buckets(const Set *s) {
138f49e4 77 return _hashmap_buckets(HASHMAP_BASE((Set *) s));
89439d4f 78}
60918275 79
e4126adf
ZJS
80static inline bool set_iterate(const Set *s, Iterator *i, void **value) {
81 return _hashmap_iterate(HASHMAP_BASE((Set*) s), i, value, NULL);
82}
60918275 83
89439d4f 84static inline void set_clear(Set *s) {
138f49e4 85 _hashmap_clear(HASHMAP_BASE(s), NULL, NULL);
89439d4f
MS
86}
87
88static inline void set_clear_free(Set *s) {
138f49e4 89 _hashmap_clear(HASHMAP_BASE(s), free, NULL);
89439d4f
MS
90}
91
92/* no set_clear_free_free */
93
94static inline void *set_steal_first(Set *s) {
138f49e4 95 return _hashmap_first_key_and_value(HASHMAP_BASE(s), true, NULL);
89439d4f
MS
96}
97
38c116df 98#define set_clear_with_destructor(s, f) \
224b0e7a 99 ({ \
38c116df 100 Set *_s = (s); \
224b0e7a
ZJS
101 void *_item; \
102 while ((_item = set_steal_first(_s))) \
38c116df
YW
103 f(_item); \
104 _s; \
224b0e7a 105 })
38c116df
YW
106#define set_free_with_destructor(s, f) \
107 set_free(set_clear_with_destructor(s, f))
224b0e7a 108
89439d4f
MS
109/* no set_steal_first_key */
110/* no set_first_key */
9946996c 111
f18f809c 112static inline void *set_first(const Set *s) {
138f49e4 113 return _hashmap_first_key_and_value(HASHMAP_BASE((Set *) s), false, NULL);
89439d4f 114}
60918275 115
89439d4f
MS
116/* no set_next */
117
118static inline char **set_get_strv(Set *s) {
138f49e4 119 return _hashmap_get_strv(HASHMAP_BASE(s));
89439d4f
MS
120}
121
0f9ccd95
ZJS
122int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key HASHMAP_DEBUG_PARAMS);
123#define set_ensure_put(s, hash_ops, key) _set_ensure_put(s, hash_ops, key HASHMAP_DEBUG_SRC_ARGS)
124
fcc1d031
ZJS
125int _set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key HASHMAP_DEBUG_PARAMS);
126#define set_ensure_consume(s, hash_ops, key) _set_ensure_consume(s, hash_ops, key HASHMAP_DEBUG_SRC_ARGS)
127
89439d4f 128int set_consume(Set *s, void *value);
b8b46b1c 129
11e9fec2
YW
130int _set_put_strdup_full(Set **s, const struct hash_ops *hash_ops, const char *p HASHMAP_DEBUG_PARAMS);
131#define set_put_strdup_full(s, hash_ops, p) _set_put_strdup_full(s, hash_ops, p HASHMAP_DEBUG_SRC_ARGS)
132#define set_put_strdup(s, p) set_put_strdup_full(s, &string_hash_ops_free, p)
133int _set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l HASHMAP_DEBUG_PARAMS);
134#define set_put_strdupv_full(s, hash_ops, l) _set_put_strdupv_full(s, hash_ops, l HASHMAP_DEBUG_SRC_ARGS)
135#define set_put_strdupv(s, l) set_put_strdupv_full(s, &string_hash_ops_free, l)
b8b46b1c 136
d97c5aea 137int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags flags);
9590dfe7 138
90e74a66
ZJS
139#define _SET_FOREACH(e, s, i) \
140 for (Iterator i = ITERATOR_FIRST; set_iterate((s), &i, (void**)&(e)); )
141#define SET_FOREACH(e, s) \
142 _SET_FOREACH(e, s, UNIQ_T(i, UNIQ))
60918275 143
35aa04e9
LP
144#define SET_FOREACH_MOVE(e, d, s) \
145 for (; ({ e = set_first(s); assert_se(!e || set_move_one(d, s, e) >= 0); e; }); )
146
14bf2c9d
LP
147DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free);
148DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free_free);
89439d4f 149
dfb33a97
LP
150#define _cleanup_set_free_ _cleanup_(set_freep)
151#define _cleanup_set_free_free_ _cleanup_(set_free_freep)
4dbce717 152
8d80f275 153int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **ret);
e4304fb8
LP
154
155bool set_equal(Set *a, Set *b);