1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 #include "compat/hashtab.h"
20 string_map_hash_fn string_map_hash_helper
= htab_hash_string
;
23 string_map_equal(const void *element
, const void *map_
)
25 struct string_map
*map
= (struct string_map
*) map_
;
26 const char *key
= map
->key_fn(element
);
28 return !strcmp(key
, map
->key
);
32 string_map_get(struct string_map
*map
, const char *key
)
36 return htab_find_with_hash(map
->htab
, map
, htab_hash_string(key
));
43 string_map_get_at(struct string_map
*map
, const char *key
)
47 return htab_find_slot_with_hash(map
->htab
, map
, htab_hash_string(key
), NO_INSERT
);
54 string_map_put_to(struct string_map
*map
, const char *key
)
57 map
->htab
= htab_create_alloc(16, map
->hash_fn
, string_map_equal
, NULL
, calloc
, free
);
63 return htab_find_slot_with_hash(map
->htab
, map
, htab_hash_string(key
), INSERT
);
67 string_map_put(struct string_map
*map
, const char *key
, void *value
)
69 void **slot
= string_map_put_to(map
, key
);
79 string_map_remove(struct string_map
*map
, const char *key
)
82 void **slot
= string_map_get_at(map
, key
);
86 htab_clear_slot(map
->htab
, slot
);
93 string_map_clear(struct string_map
*map
)
96 htab_empty(map
->htab
);
99 struct string_map_iterator
{
100 string_map_iterator_fn fn
;
105 string_map_iterate(void **slot
, void *data
)
107 struct string_map_iterator
*iterator
= data
;
109 return iterator
->fn(iterator
->data
, *slot
);
113 string_map_foreach(struct string_map
*map
, string_map_iterator_fn fn
, void *data
)
116 struct string_map_iterator iterator
= { fn
, data
};
118 htab_traverse_noresize(map
->htab
, string_map_iterate
, &iterator
);
122 /* vim: set ts=8 sw=8 noexpandtab: */