Add support for custom str_map key comp. fun.

That wasn't hard.
This commit is contained in:
Přemysl Eric Janouch 2014-07-14 02:06:02 +02:00
parent 3206c86430
commit c7cd0c40e0

View File

@ -571,6 +571,9 @@ struct str_map
size_t alloc; ///< Number of allocated entries
size_t len; ///< Number of entries in the table
void (*free) (void *); ///< Callback to destruct the payload
/// Callback to compare keys for equivalence
int (*key_cmp) (const char *, const char *);
};
// As long as you don't remove the current entry, you can modify the map.
@ -593,6 +596,7 @@ str_map_init (struct str_map *self)
self->alloc = STR_MAP_MIN_ALLOC;
self->len = 0;
self->free = NULL;
self->key_cmp = strcmp;
self->map = xcalloc (self->alloc, sizeof *self->map);
}
@ -692,7 +696,7 @@ str_map_set (struct str_map *self, const char *key, void *value)
struct str_map_link *iter = self->map[pos];
for (; iter; iter = iter->next)
{
if (strcmp (key, iter->key))
if (self->key_cmp (key, iter->key))
continue;
// Storing the same data doesn't destroy it
@ -741,7 +745,7 @@ str_map_find (struct str_map *self, const char *key)
{
struct str_map_link *iter = self->map[str_map_pos (self, key)];
for (; iter; iter = iter->next)
if (!strcmp (key, (char *) iter + sizeof *iter))
if (!self->key_cmp (key, (const char *) iter + sizeof *iter))
return iter->data;
return NULL;
}