diff options
author | Elijah Newren <newren@gmail.com> | 2020-11-06 01:24:54 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-11-06 18:33:35 +0100 |
commit | 1201eb628ac753af5751258466df5f964bdc9f17 (patch) | |
tree | 4ab1943c2580973114720cad98f1b0c9a5744f69 /strmap.c | |
parent | strmap: split create_entry() out of strmap_put() (diff) | |
download | git-1201eb628ac753af5751258466df5f964bdc9f17.tar.xz git-1201eb628ac753af5751258466df5f964bdc9f17.zip |
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 <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strmap.c')
-rw-r--r-- | strmap.c | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -143,3 +143,20 @@ void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt) else strintmap_set(map, str, map->default_value + amt); } + +int strset_add(struct strset *set, const char *str) +{ + /* + * Cannot use strmap_put() because it'll return NULL in both cases: + * - cannot find str: NULL means "not found" + * - does find str: NULL is the value associated with str + */ + struct strmap_entry *entry = find_strmap_entry(&set->map, str); + + if (entry) + return 0; + + entry = create_entry(&set->map, str, NULL); + hashmap_add(&set->map.map, &entry->ent); + return 1; +} |