Skip to content

Commit 1201eb6

Browse files
newrengitster
authored andcommitted
strmap: add a strset sub-type
Similar to adding strintmap for special-casing a string -> int mapping, add a strset type for cases where we really are only interested in using strmap for storing a set rather than a mapping. In this case, we'll always just store NULL for the value but the different struct type makes it clearer than code comments how a variable is intended to be used. The difference in usage also results in some differences in API: a few things that aren't necessary or meaningful are dropped (namely, the free_values argument to *_clear(), and the *_get() function), and strset_add() is chosen as the API instead of strset_put(). Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6abd220 commit 1201eb6

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

strmap.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,20 @@ void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
143143
else
144144
strintmap_set(map, str, map->default_value + amt);
145145
}
146+
147+
int strset_add(struct strset *set, const char *str)
148+
{
149+
/*
150+
* Cannot use strmap_put() because it'll return NULL in both cases:
151+
* - cannot find str: NULL means "not found"
152+
* - does find str: NULL is the value associated with str
153+
*/
154+
struct strmap_entry *entry = find_strmap_entry(&set->map, str);
155+
156+
if (entry)
157+
return 0;
158+
159+
entry = create_entry(&set->map, str, NULL);
160+
hashmap_add(&set->map.map, &entry->ent);
161+
return 1;
162+
}

strmap.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
2727
.map = STRMAP_INIT, \
2828
.default_value = 0, \
2929
}
30+
#define STRSET_INIT { .map = STRMAP_INIT }
3031

3132
/*
3233
* Initialize the members of the strmap. Any keys added to the strmap will
@@ -196,4 +197,66 @@ static inline void strintmap_set(struct strintmap *map, const char *str,
196197
*/
197198
void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt);
198199

200+
/*
201+
* strset:
202+
* A set of strings.
203+
*
204+
* Primary differences with strmap:
205+
* 1) The value is always NULL, and ignored. As there is no value to free,
206+
* there is one fewer argument to strset_clear
207+
* 2) No strset_get() because there is no value.
208+
* 3) No strset_put(); use strset_add() instead.
209+
*/
210+
211+
struct strset {
212+
struct strmap map;
213+
};
214+
215+
#define strset_for_each_entry(mystrset, iter, var) \
216+
strmap_for_each_entry(&(mystrset)->map, iter, var)
217+
218+
static inline void strset_init(struct strset *set)
219+
{
220+
strmap_init(&set->map);
221+
}
222+
223+
static inline void strset_init_with_options(struct strset *set,
224+
int strdup_strings)
225+
{
226+
strmap_init_with_options(&set->map, strdup_strings);
227+
}
228+
229+
static inline void strset_clear(struct strset *set)
230+
{
231+
strmap_clear(&set->map, 0);
232+
}
233+
234+
static inline void strset_partial_clear(struct strset *set)
235+
{
236+
strmap_partial_clear(&set->map, 0);
237+
}
238+
239+
static inline int strset_contains(struct strset *set, const char *str)
240+
{
241+
return strmap_contains(&set->map, str);
242+
}
243+
244+
static inline void strset_remove(struct strset *set, const char *str)
245+
{
246+
return strmap_remove(&set->map, str, 0);
247+
}
248+
249+
static inline int strset_empty(struct strset *set)
250+
{
251+
return strmap_empty(&set->map);
252+
}
253+
254+
static inline unsigned int strset_get_size(struct strset *set)
255+
{
256+
return strmap_get_size(&set->map);
257+
}
258+
259+
/* Returns 1 if str is added to the set; returns 0 if str was already in set */
260+
int strset_add(struct strset *set, const char *str);
261+
199262
#endif /* STRMAP_H */

0 commit comments

Comments
 (0)