strmap: add functions facilitating use as a string->int map
[git] / strmap.c
1 #include "git-compat-util.h"
2 #include "strmap.h"
3
4 int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
5                      const struct hashmap_entry *entry1,
6                      const struct hashmap_entry *entry2,
7                      const void *keydata)
8 {
9         const struct strmap_entry *e1, *e2;
10
11         e1 = container_of(entry1, const struct strmap_entry, ent);
12         e2 = container_of(entry2, const struct strmap_entry, ent);
13         return strcmp(e1->key, e2->key);
14 }
15
16 static struct strmap_entry *find_strmap_entry(struct strmap *map,
17                                               const char *str)
18 {
19         struct strmap_entry entry;
20         hashmap_entry_init(&entry.ent, strhash(str));
21         entry.key = str;
22         return hashmap_get_entry(&map->map, &entry, ent, NULL);
23 }
24
25 void strmap_init(struct strmap *map)
26 {
27         strmap_init_with_options(map, 1);
28 }
29
30 void strmap_init_with_options(struct strmap *map,
31                               int strdup_strings)
32 {
33         hashmap_init(&map->map, cmp_strmap_entry, NULL, 0);
34         map->strdup_strings = strdup_strings;
35 }
36
37 static void strmap_free_entries_(struct strmap *map, int free_values)
38 {
39         struct hashmap_iter iter;
40         struct strmap_entry *e;
41
42         if (!map)
43                 return;
44
45         /*
46          * We need to iterate over the hashmap entries and free
47          * e->key and e->value ourselves; hashmap has no API to
48          * take care of that for us.  Since we're already iterating over
49          * the hashmap, though, might as well free e too and avoid the need
50          * to make some call into the hashmap API to do that.
51          */
52         hashmap_for_each_entry(&map->map, &iter, e, ent) {
53                 if (free_values)
54                         free(e->value);
55                 if (map->strdup_strings)
56                         free((char*)e->key);
57                 free(e);
58         }
59 }
60
61 void strmap_clear(struct strmap *map, int free_values)
62 {
63         strmap_free_entries_(map, free_values);
64         hashmap_clear(&map->map);
65 }
66
67 void strmap_partial_clear(struct strmap *map, int free_values)
68 {
69         strmap_free_entries_(map, free_values);
70         hashmap_partial_clear(&map->map);
71 }
72
73 void *strmap_put(struct strmap *map, const char *str, void *data)
74 {
75         struct strmap_entry *entry = find_strmap_entry(map, str);
76         void *old = NULL;
77
78         if (entry) {
79                 old = entry->value;
80                 entry->value = data;
81         } else {
82                 const char *key = str;
83
84                 entry = xmalloc(sizeof(*entry));
85                 hashmap_entry_init(&entry->ent, strhash(str));
86
87                 if (map->strdup_strings)
88                         key = xstrdup(str);
89                 entry->key = key;
90                 entry->value = data;
91                 hashmap_add(&map->map, &entry->ent);
92         }
93         return old;
94 }
95
96 struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)
97 {
98         return find_strmap_entry(map, str);
99 }
100
101 void *strmap_get(struct strmap *map, const char *str)
102 {
103         struct strmap_entry *entry = find_strmap_entry(map, str);
104         return entry ? entry->value : NULL;
105 }
106
107 int strmap_contains(struct strmap *map, const char *str)
108 {
109         return find_strmap_entry(map, str) != NULL;
110 }
111
112 void strmap_remove(struct strmap *map, const char *str, int free_value)
113 {
114         struct strmap_entry entry, *ret;
115         hashmap_entry_init(&entry.ent, strhash(str));
116         entry.key = str;
117         ret = hashmap_remove_entry(&map->map, &entry, ent, NULL);
118         if (!ret)
119                 return;
120         if (free_value)
121                 free(ret->value);
122         if (map->strdup_strings)
123                 free((char*)ret->key);
124         free(ret);
125 }
126
127 void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
128 {
129         struct strmap_entry *entry = find_strmap_entry(&map->map, str);
130         if (entry) {
131                 intptr_t *whence = (intptr_t*)&entry->value;
132                 *whence += amt;
133         }
134         else
135                 strintmap_set(map, str, map->default_value + amt);
136 }