strmap: add more utility functions
[git/debian.git] / strmap.h
blobf74bc582e4d0d6486868445982aff631aa629f9c
1 #ifndef STRMAP_H
2 #define STRMAP_H
4 #include "hashmap.h"
6 struct strmap {
7 struct hashmap map;
8 unsigned int strdup_strings:1;
9 };
11 struct strmap_entry {
12 struct hashmap_entry ent;
13 const char *key;
14 void *value;
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);
22 #define STRMAP_INIT { \
23 .map = HASHMAP_INIT(cmp_strmap_entry, NULL), \
24 .strdup_strings = 1, \
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.
31 void strmap_init(struct strmap *map);
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.
37 void strmap_init_with_options(struct strmap *map,
38 int strdup_strings);
41 * Remove all entries from the map, releasing any allocated resources.
43 void strmap_clear(struct strmap *map, int free_values);
46 * Insert "str" into the map, pointing to "data".
48 * If an entry for "str" already exists, its data pointer is overwritten, and
49 * the original data pointer returned. Otherwise, returns NULL.
51 void *strmap_put(struct strmap *map, const char *str, void *data);
54 * Return the strmap_entry mapped by "str", or NULL if there is not such
55 * an item in map.
57 struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str);
60 * Return the data pointer mapped by "str", or NULL if the entry does not
61 * exist.
63 void *strmap_get(struct strmap *map, const char *str);
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.
69 int strmap_contains(struct strmap *map, const char *str);
72 * Remove the given entry from the strmap. If the string isn't in the
73 * strmap, the map is not altered.
75 void strmap_remove(struct strmap *map, const char *str, int free_value);
78 * Return how many entries the strmap has.
80 static inline unsigned int strmap_get_size(struct strmap *map)
82 return hashmap_get_size(&map->map);
86 * Return whether the strmap is empty.
88 static inline int strmap_empty(struct strmap *map)
90 return strmap_get_size(map) == 0;
94 * iterate through @map using @iter, @var is a pointer to a type strmap_entry
96 #define strmap_for_each_entry(mystrmap, iter, var) \
97 hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
99 #endif /* STRMAP_H */