strmap: add functions facilitating use as a string->int map
[git/debian.git] / strmap.h
blob56a5cdb864a9f66bc73d6b43721d4c560a04f509
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, \
26 #define STRINTMAP_INIT { \
27 .map = STRMAP_INIT, \
28 .default_value = 0, \
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.
35 void strmap_init(struct strmap *map);
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.
41 void strmap_init_with_options(struct strmap *map,
42 int strdup_strings);
45 * Remove all entries from the map, releasing any allocated resources.
47 void strmap_clear(struct strmap *map, int free_values);
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.
53 void strmap_partial_clear(struct strmap *map, int free_values);
56 * Insert "str" into the map, pointing to "data".
58 * If an entry for "str" already exists, its data pointer is overwritten, and
59 * the original data pointer returned. Otherwise, returns NULL.
61 void *strmap_put(struct strmap *map, const char *str, void *data);
64 * Return the strmap_entry mapped by "str", or NULL if there is not such
65 * an item in map.
67 struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str);
70 * Return the data pointer mapped by "str", or NULL if the entry does not
71 * exist.
73 void *strmap_get(struct strmap *map, const char *str);
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.
79 int strmap_contains(struct strmap *map, const char *str);
82 * Remove the given entry from the strmap. If the string isn't in the
83 * strmap, the map is not altered.
85 void strmap_remove(struct strmap *map, const char *str, int free_value);
88 * Return how many entries the strmap has.
90 static inline unsigned int strmap_get_size(struct strmap *map)
92 return hashmap_get_size(&map->map);
96 * Return whether the strmap is empty.
98 static inline int strmap_empty(struct strmap *map)
100 return strmap_get_size(map) == 0;
104 * iterate through @map using @iter, @var is a pointer to a type strmap_entry
106 #define strmap_for_each_entry(mystrmap, iter, var) \
107 hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
111 * strintmap:
112 * A map of string -> int, typecasting the void* of strmap to an int.
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.
123 struct strintmap {
124 struct strmap map;
125 int default_value;
128 #define strintmap_for_each_entry(mystrmap, iter, var) \
129 strmap_for_each_entry(&(mystrmap)->map, iter, var)
131 static inline void strintmap_init(struct strintmap *map, int default_value)
133 strmap_init(&map->map);
134 map->default_value = default_value;
137 static inline void strintmap_init_with_options(struct strintmap *map,
138 int default_value,
139 int strdup_strings)
141 strmap_init_with_options(&map->map, strdup_strings);
142 map->default_value = default_value;
145 static inline void strintmap_clear(struct strintmap *map)
147 strmap_clear(&map->map, 0);
150 static inline void strintmap_partial_clear(struct strintmap *map)
152 strmap_partial_clear(&map->map, 0);
155 static inline int strintmap_contains(struct strintmap *map, const char *str)
157 return strmap_contains(&map->map, str);
160 static inline void strintmap_remove(struct strintmap *map, const char *str)
162 return strmap_remove(&map->map, str, 0);
165 static inline int strintmap_empty(struct strintmap *map)
167 return strmap_empty(&map->map);
170 static inline unsigned int strintmap_get_size(struct strintmap *map)
172 return strmap_get_size(&map->map);
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.
179 static inline int strintmap_get(struct strintmap *map, const char *str)
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;
187 static inline void strintmap_set(struct strintmap *map, const char *str,
188 intptr_t v)
190 strmap_put(&map->map, str, (void *)v);
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.
197 void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt);
199 #endif /* STRMAP_H */