Add support for custom str_map key comp. fun.
That wasn't hard.
This commit is contained in:
parent
3206c86430
commit
c7cd0c40e0
|
@ -571,6 +571,9 @@ struct str_map
|
||||||
size_t alloc; ///< Number of allocated entries
|
size_t alloc; ///< Number of allocated entries
|
||||||
size_t len; ///< Number of entries in the table
|
size_t len; ///< Number of entries in the table
|
||||||
void (*free) (void *); ///< Callback to destruct the payload
|
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.
|
// 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->alloc = STR_MAP_MIN_ALLOC;
|
||||||
self->len = 0;
|
self->len = 0;
|
||||||
self->free = NULL;
|
self->free = NULL;
|
||||||
|
self->key_cmp = strcmp;
|
||||||
self->map = xcalloc (self->alloc, sizeof *self->map);
|
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];
|
struct str_map_link *iter = self->map[pos];
|
||||||
for (; iter; iter = iter->next)
|
for (; iter; iter = iter->next)
|
||||||
{
|
{
|
||||||
if (strcmp (key, iter->key))
|
if (self->key_cmp (key, iter->key))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Storing the same data doesn't destroy it
|
// 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)];
|
struct str_map_link *iter = self->map[str_map_pos (self, key)];
|
||||||
for (; iter; iter = iter->next)
|
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 iter->data;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue