From 0cb51320d6b55088608277592231c6bfd225009a Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sat, 12 Jul 2014 20:43:15 +0200 Subject: Implement an iterator for `struct str_map' --- src/common.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/common.c b/src/common.c index ae5814f..283535f 100644 --- a/src/common.c +++ b/src/common.c @@ -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) { -- cgit v1.2.3-70-g09d2