strmap: add more utility functions
[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
27 /*
28  * Initialize the members of the strmap.  Any keys added to the strmap will
29  * be strdup'ed with their memory managed by the strmap.
30  */
31 void strmap_init(struct strmap *map);
32
33 /*
34  * Same as strmap_init, but for those who want to control the memory management
35  * carefully instead of using the default of strdup_strings=1.
36  */
37 void strmap_init_with_options(struct strmap *map,
38                               int strdup_strings);
39
40 /*
41  * Remove all entries from the map, releasing any allocated resources.
42  */
43 void strmap_clear(struct strmap *map, int free_values);
44
45 /*
46  * Insert "str" into the map, pointing to "data".
47  *
48  * If an entry for "str" already exists, its data pointer is overwritten, and
49  * the original data pointer returned. Otherwise, returns NULL.
50  */
51 void *strmap_put(struct strmap *map, const char *str, void *data);
52
53 /*
54  * Return the strmap_entry mapped by "str", or NULL if there is not such
55  * an item in map.
56  */
57 struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str);
58
59 /*
60  * Return the data pointer mapped by "str", or NULL if the entry does not
61  * exist.
62  */
63 void *strmap_get(struct strmap *map, const char *str);
64
65 /*
66  * Return non-zero iff "str" is present in the map. This differs from
67  * strmap_get() in that it can distinguish entries with a NULL data pointer.
68  */
69 int strmap_contains(struct strmap *map, const char *str);
70
71 /*
72  * Remove the given entry from the strmap.  If the string isn't in the
73  * strmap, the map is not altered.
74  */
75 void strmap_remove(struct strmap *map, const char *str, int free_value);
76
77 /*
78  * Return how many entries the strmap has.
79  */
80 static inline unsigned int strmap_get_size(struct strmap *map)
81 {
82         return hashmap_get_size(&map->map);
83 }
84
85 /*
86  * Return whether the strmap is empty.
87  */
88 static inline int strmap_empty(struct strmap *map)
89 {
90         return strmap_get_size(map) == 0;
91 }
92
93 /*
94  * iterate through @map using @iter, @var is a pointer to a type strmap_entry
95  */
96 #define strmap_for_each_entry(mystrmap, iter, var)      \
97         hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
98
99 #endif /* STRMAP_H */