]> git.ipfire.org Git - thirdparty/git.git/blame - strmap.h
strmap: enable allocations to come from a mem_pool
[thirdparty/git.git] / strmap.h
CommitLineData
ae20bf1a
EN
1#ifndef STRMAP_H
2#define STRMAP_H
3
4#include "hashmap.h"
5
a208ec1f 6struct mem_pool;
ae20bf1a
EN
7struct strmap {
8 struct hashmap map;
a208ec1f 9 struct mem_pool *pool;
ae20bf1a
EN
10 unsigned int strdup_strings:1;
11};
12
13struct strmap_entry {
14 struct hashmap_entry ent;
15 const char *key;
16 void *value;
17};
18
19int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
20 const struct hashmap_entry *entry1,
21 const struct hashmap_entry *entry2,
22 const void *keydata);
23
24#define STRMAP_INIT { \
25 .map = HASHMAP_INIT(cmp_strmap_entry, NULL), \
26 .strdup_strings = 1, \
27 }
4fa1d501
EN
28#define STRINTMAP_INIT { \
29 .map = STRMAP_INIT, \
30 .default_value = 0, \
31 }
1201eb62 32#define STRSET_INIT { .map = STRMAP_INIT }
ae20bf1a
EN
33
34/*
35 * Initialize the members of the strmap. Any keys added to the strmap will
36 * be strdup'ed with their memory managed by the strmap.
37 */
38void strmap_init(struct strmap *map);
39
40/*
41 * Same as strmap_init, but for those who want to control the memory management
a208ec1f 42 * carefully instead of using the default of strdup_strings=1 and pool=NULL.
ae20bf1a
EN
43 */
44void strmap_init_with_options(struct strmap *map,
a208ec1f 45 struct mem_pool *pool,
ae20bf1a
EN
46 int strdup_strings);
47
48/*
49 * Remove all entries from the map, releasing any allocated resources.
50 */
51void strmap_clear(struct strmap *map, int free_values);
52
6ccdfc2a
EN
53/*
54 * Similar to strmap_clear() but leaves map->map->table allocated and
55 * pre-sized so that subsequent uses won't need as many rehashings.
56 */
57void strmap_partial_clear(struct strmap *map, int free_values);
58
ae20bf1a
EN
59/*
60 * Insert "str" into the map, pointing to "data".
61 *
62 * If an entry for "str" already exists, its data pointer is overwritten, and
63 * the original data pointer returned. Otherwise, returns NULL.
64 */
65void *strmap_put(struct strmap *map, const char *str, void *data);
66
b70c82e6
EN
67/*
68 * Return the strmap_entry mapped by "str", or NULL if there is not such
69 * an item in map.
70 */
71struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str);
72
ae20bf1a
EN
73/*
74 * Return the data pointer mapped by "str", or NULL if the entry does not
75 * exist.
76 */
77void *strmap_get(struct strmap *map, const char *str);
78
79/*
80 * Return non-zero iff "str" is present in the map. This differs from
81 * strmap_get() in that it can distinguish entries with a NULL data pointer.
82 */
83int strmap_contains(struct strmap *map, const char *str);
84
b70c82e6
EN
85/*
86 * Remove the given entry from the strmap. If the string isn't in the
87 * strmap, the map is not altered.
88 */
89void strmap_remove(struct strmap *map, const char *str, int free_value);
90
91/*
92 * Return how many entries the strmap has.
93 */
94static inline unsigned int strmap_get_size(struct strmap *map)
95{
96 return hashmap_get_size(&map->map);
97}
98
99/*
100 * Return whether the strmap is empty.
101 */
102static inline int strmap_empty(struct strmap *map)
103{
104 return strmap_get_size(map) == 0;
105}
106
107/*
108 * iterate through @map using @iter, @var is a pointer to a type strmap_entry
109 */
110#define strmap_for_each_entry(mystrmap, iter, var) \
111 hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
112
4fa1d501
EN
113
114/*
115 * strintmap:
116 * A map of string -> int, typecasting the void* of strmap to an int.
117 *
118 * Primary differences:
119 * 1) Since the void* value is just an int in disguise, there is no value
120 * to free. (Thus one fewer argument to strintmap_clear)
121 * 2) strintmap_get() returns an int, or returns the default_value if the
122 * key is not found in the strintmap.
123 * 3) No strmap_put() equivalent; strintmap_set() and strintmap_incr()
124 * instead.
125 */
126
127struct strintmap {
128 struct strmap map;
129 int default_value;
130};
131
132#define strintmap_for_each_entry(mystrmap, iter, var) \
133 strmap_for_each_entry(&(mystrmap)->map, iter, var)
134
135static inline void strintmap_init(struct strintmap *map, int default_value)
136{
137 strmap_init(&map->map);
138 map->default_value = default_value;
139}
140
141static inline void strintmap_init_with_options(struct strintmap *map,
142 int default_value,
a208ec1f 143 struct mem_pool *pool,
4fa1d501
EN
144 int strdup_strings)
145{
a208ec1f 146 strmap_init_with_options(&map->map, pool, strdup_strings);
4fa1d501
EN
147 map->default_value = default_value;
148}
149
150static inline void strintmap_clear(struct strintmap *map)
151{
152 strmap_clear(&map->map, 0);
153}
154
155static inline void strintmap_partial_clear(struct strintmap *map)
156{
157 strmap_partial_clear(&map->map, 0);
158}
159
160static inline int strintmap_contains(struct strintmap *map, const char *str)
161{
162 return strmap_contains(&map->map, str);
163}
164
165static inline void strintmap_remove(struct strintmap *map, const char *str)
166{
167 return strmap_remove(&map->map, str, 0);
168}
169
170static inline int strintmap_empty(struct strintmap *map)
171{
172 return strmap_empty(&map->map);
173}
174
175static inline unsigned int strintmap_get_size(struct strintmap *map)
176{
177 return strmap_get_size(&map->map);
178}
179
180/*
181 * Returns the value for str in the map. If str isn't found in the map,
182 * the map's default_value is returned.
183 */
184static inline int strintmap_get(struct strintmap *map, const char *str)
185{
186 struct strmap_entry *result = strmap_get_entry(&map->map, str);
187 if (!result)
188 return map->default_value;
189 return (intptr_t)result->value;
190}
191
192static inline void strintmap_set(struct strintmap *map, const char *str,
193 intptr_t v)
194{
195 strmap_put(&map->map, str, (void *)v);
196}
197
198/*
199 * Increment the value for str by amt. If str isn't in the map, add it and
200 * set its value to default_value + amt.
201 */
202void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt);
203
1201eb62
EN
204/*
205 * strset:
206 * A set of strings.
207 *
208 * Primary differences with strmap:
209 * 1) The value is always NULL, and ignored. As there is no value to free,
210 * there is one fewer argument to strset_clear
211 * 2) No strset_get() because there is no value.
212 * 3) No strset_put(); use strset_add() instead.
213 */
214
215struct strset {
216 struct strmap map;
217};
218
219#define strset_for_each_entry(mystrset, iter, var) \
220 strmap_for_each_entry(&(mystrset)->map, iter, var)
221
222static inline void strset_init(struct strset *set)
223{
224 strmap_init(&set->map);
225}
226
227static inline void strset_init_with_options(struct strset *set,
a208ec1f 228 struct mem_pool *pool,
1201eb62
EN
229 int strdup_strings)
230{
a208ec1f 231 strmap_init_with_options(&set->map, pool, strdup_strings);
1201eb62
EN
232}
233
234static inline void strset_clear(struct strset *set)
235{
236 strmap_clear(&set->map, 0);
237}
238
239static inline void strset_partial_clear(struct strset *set)
240{
241 strmap_partial_clear(&set->map, 0);
242}
243
244static inline int strset_contains(struct strset *set, const char *str)
245{
246 return strmap_contains(&set->map, str);
247}
248
249static inline void strset_remove(struct strset *set, const char *str)
250{
251 return strmap_remove(&set->map, str, 0);
252}
253
254static inline int strset_empty(struct strset *set)
255{
256 return strmap_empty(&set->map);
257}
258
259static inline unsigned int strset_get_size(struct strset *set)
260{
261 return strmap_get_size(&set->map);
262}
263
264/* Returns 1 if str is added to the set; returns 0 if str was already in set */
265int strset_add(struct strset *set, const char *str);
266
ae20bf1a 267#endif /* STRMAP_H */