Implement an iterator for `struct str_map'

This commit is contained in:
Přemysl Eric Janouch 2014-07-12 20:43:15 +02:00
parent 1edcbc5f3d
commit 0cb51320d6
1 changed files with 33 additions and 0 deletions

View File

@ -566,6 +566,16 @@ struct str_map
void (*free) (void *); ///< Callback to destruct the payload
};
// As long as you don't remove the current entry, you can modify the map.
// Use `link' directly to access the data.
struct str_map_iter
{
struct str_map *map; ///< The map we're iterating
size_t next_index; ///< Next table index to search
struct str_map_link *link; ///< Current link
};
#define STR_MAP_MIN_ALLOC 16
typedef void (*str_map_free_func) (void *);
@ -598,6 +608,29 @@ str_map_free (struct str_map *self)
self->map = NULL;
}
static void
str_map_iter_init (struct str_map_iter *self, struct str_map *map)
{
self->map = map;
self->next_index = 0;
self->link = NULL;
}
static bool
str_map_iter_next (struct str_map_iter *self)
{
struct str_map *map = self->map;
if (self->link)
self->link = self->link->next;
while (!self->link)
{
if (self->next_index >= map->len)
return false;
self->link = map->map[self->next_index++];
}
return true;
}
static uint64_t
str_map_hash (const char *s, size_t len)
{