From 4fa1d501f775253fe70bf6c6a00fe8156c8c61c7 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Thu, 5 Nov 2020 00:22:41 +0000 Subject: strmap: add functions facilitating use as a string->int map Although strmap could be used as a string->int map, one either had to allocate an int for every entry and then deallocate later, or one had to do a bunch of casting between (void*) and (intptr_t). Add some special functions that do the casting. Also, rename put->set for such wrapper functions since 'put' implied there may be some deallocation needed if the string was already found in the map, which isn't the case when we're storing an int value directly in the void* slot instead of using the void* slot as a pointer to data. Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- strmap.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'strmap.c') diff --git a/strmap.c b/strmap.c index c410c5241a..0d10a884b5 100644 --- a/strmap.c +++ b/strmap.c @@ -123,3 +123,14 @@ void strmap_remove(struct strmap *map, const char *str, int free_value) free((char*)ret->key); free(ret); } + +void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt) +{ + struct strmap_entry *entry = find_strmap_entry(&map->map, str); + if (entry) { + intptr_t *whence = (intptr_t*)&entry->value; + *whence += amt; + } + else + strintmap_set(map, str, map->default_value + amt); +} -- cgit v1.3-5-g9baa