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>
Elijah Newren committed
Nov 6, 2020 at 00:24 UTC
1201eb628ac753af5751258466df5f964bdc9f17
2 files changed
+80
strmap.c
+17
@@ -143,3 +143,20 @@ void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
143
else
144
strintmap_set(map, str, map->default_value + amt);
145
}
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
+63
@@ -27,6 +27,7 @@ int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
27
.map = STRMAP_INIT, \
28
.default_value = 0, \
29
}
30
+#define STRSET_INIT { .map = STRMAP_INIT }
31
32
/*
33
* 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,
197
*/
198
void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt);
199
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
+
262
#endif /* STRMAP_H */