aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2014-07-12 20:43:15 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2014-07-12 21:19:44 +0200
commit0cb51320d6b55088608277592231c6bfd225009a (patch)
treec6a9b93620c5121079dd0e7255a7da90bd07cea1
parent1edcbc5f3d92acccd701ef144d6b6b513a913166 (diff)
downloadxK-0cb51320d6b55088608277592231c6bfd225009a.tar.gz
xK-0cb51320d6b55088608277592231c6bfd225009a.tar.xz
xK-0cb51320d6b55088608277592231c6bfd225009a.zip
Implement an iterator for `struct str_map'
-rw-r--r--src/common.c33
1 files changed, 33 insertions, 0 deletions
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)
{