]> git.ipfire.org Git - thirdparty/git.git/blob - strmap.h
strmap: split create_entry() out of strmap_put()
[thirdparty/git.git] / strmap.h
1 #ifndef STRMAP_H
2 #define STRMAP_H
3
4 #include "hashmap.h"
5
6 struct strmap {
7 struct hashmap map;
8 unsigned int strdup_strings:1;
9 };
10
11 struct strmap_entry {
12 struct hashmap_entry ent;
13 const char *key;
14 void *value;
15 };
16
17 int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
18 const struct hashmap_entry *entry1,
19 const struct hashmap_entry *entry2,
20 const void *keydata);
21
22 #define STRMAP_INIT { \
23 .map = HASHMAP_INIT(cmp_strmap_entry, NULL), \
24 .strdup_strings = 1, \
25 }
26 #define STRINTMAP_INIT { \
27 .map = STRMAP_INIT, \
28 .default_value = 0, \
29 }
30
31 /*
32 * Initialize the members of the strmap. Any keys added to the strmap will
33 * be strdup'ed with their memory managed by the strmap.
34 */
35 void strmap_init(struct strmap *map);
36
37 /*
38 * Same as strmap_init, but for those who want to control the memory management
39 * carefully instead of using the default of strdup_strings=1.
40 */
41 void strmap_init_with_options(struct strmap *map,
42 int strdup_strings);
43
44 /*
45 * Remove all entries from the map, releasing any allocated resources.
46 */
47 void strmap_clear(struct strmap *map, int free_values);
48
49 /*
50 * Similar to strmap_clear() but leaves map->map->table allocated and
51 * pre-sized so that subsequent uses won't need as many rehashings.
52 */
53 void strmap_partial_clear(struct strmap *map, int free_values);
54
55 /*
56 * Insert "str" into the map, pointing to "data".
57 *
58 * If an entry for "str" already exists, its data pointer is overwritten, and
59 * the original data pointer returned. Otherwise, returns NULL.
60 */
61 void *strmap_put(struct strmap *map, const char *str, void *data);
62
63 /*
64 * Return the strmap_entry mapped by "str", or NULL if there is not such
65 * an item in map.
66 */
67 struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str);
68
69 /*
70 * Return the data pointer mapped by "str", or NULL if the entry does not
71 * exist.
72 */
73 void *strmap_get(struct strmap *map, const char *str);
74
75 /*
76 * Return non-zero iff "str" is present in the map. This differs from
77 * strmap_get() in that it can distinguish entries with a NULL data pointer.
78 */
79 int strmap_contains(struct strmap *map, const char *str);
80
81 /*
82 * Remove the given entry from the strmap. If the string isn't in the
83 * strmap, the map is not altered.
84 */
85 void strmap_remove(struct strmap *map, const char *str, int free_value);
86
87 /*
88 * Return how many entries the strmap has.
89 */
90 static inline unsigned int strmap_get_size(struct strmap *map)
91 {
92 return hashmap_get_size(&map->map);
93 }
94
95 /*
96 * Return whether the strmap is empty.
97 */
98 static inline int strmap_empty(struct strmap *map)
99 {
100 return strmap_get_size(map) == 0;
101 }
102
103 /*
104 * iterate through @map using @iter, @var is a pointer to a type strmap_entry
105 */
106 #define strmap_for_each_entry(mystrmap, iter, var) \
107 hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
108
109
110 /*
111 * strintmap:
112 * A map of string -> int, typecasting the void* of strmap to an int.
113 *
114 * Primary differences:
115 * 1) Since the void* value is just an int in disguise, there is no value
116 * to free. (Thus one fewer argument to strintmap_clear)
117 * 2) strintmap_get() returns an int, or returns the default_value if the
118 * key is not found in the strintmap.
119 * 3) No strmap_put() equivalent; strintmap_set() and strintmap_incr()
120 * instead.
121 */
122
123 struct strintmap {
124 struct strmap map;
125 int default_value;
126 };
127
128 #define strintmap_for_each_entry(mystrmap, iter, var) \
129 strmap_for_each_entry(&(mystrmap)->map, iter, var)
130
131 static inline void strintmap_init(struct strintmap *map, int default_value)
132 {
133 strmap_init(&map->map);
134 map->default_value = default_value;
135 }
136
137 static inline void strintmap_init_with_options(struct strintmap *map,
138 int default_value,
139 int strdup_strings)
140 {
141 strmap_init_with_options(&map->map, strdup_strings);
142 map->default_value = default_value;
143 }
144
145 static inline void strintmap_clear(struct strintmap *map)
146 {
147 strmap_clear(&map->map, 0);
148 }
149
150 static inline void strintmap_partial_clear(struct strintmap *map)
151 {
152 strmap_partial_clear(&map->map, 0);
153 }
154
155 static inline int strintmap_contains(struct strintmap *map, const char *str)
156 {
157 return strmap_contains(&map->map, str);
158 }
159
160 static inline void strintmap_remove(struct strintmap *map, const char *str)
161 {
162 return strmap_remove(&map->map, str, 0);
163 }
164
165 static inline int strintmap_empty(struct strintmap *map)
166 {
167 return strmap_empty(&map->map);
168 }
169
170 static inline unsigned int strintmap_get_size(struct strintmap *map)
171 {
172 return strmap_get_size(&map->map);
173 }
174
175 /*
176 * Returns the value for str in the map. If str isn't found in the map,
177 * the map's default_value is returned.
178 */
179 static inline int strintmap_get(struct strintmap *map, const char *str)
180 {
181 struct strmap_entry *result = strmap_get_entry(&map->map, str);
182 if (!result)
183 return map->default_value;
184 return (intptr_t)result->value;
185 }
186
187 static inline void strintmap_set(struct strintmap *map, const char *str,
188 intptr_t v)
189 {
190 strmap_put(&map->map, str, (void *)v);
191 }
192
193 /*
194 * Increment the value for str by amt. If str isn't in the map, add it and
195 * set its value to default_value + amt.
196 */
197 void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt);
198
199 #endif /* STRMAP_H */