summaryrefslogtreecommitdiff
path: root/src/runtime/c
diff options
context:
space:
mode:
authorkrangelov <kr.angelov@gmail.com>2019-08-30 08:12:15 +0200
committerkrangelov <kr.angelov@gmail.com>2019-08-30 08:12:15 +0200
commit72cfc1f48a2cbae9e51102f78868cf6c1efa4550 (patch)
tree89754f34fd97531b7e6b9d9b738eb860cd7afc2d /src/runtime/c
parent394d033d194df8c63eea7a0eca444168ae74844e (diff)
a more reasonable API to iterate over a map
Diffstat (limited to 'src/runtime/c')
-rw-r--r--src/runtime/c/gu/map.c49
-rw-r--r--src/runtime/c/gu/map.h9
2 files changed, 16 insertions, 42 deletions
diff --git a/src/runtime/c/gu/map.c b/src/runtime/c/gu/map.c
index 7b61fa096..be7228b74 100644
--- a/src/runtime/c/gu/map.c
+++ b/src/runtime/c/gu/map.c
@@ -321,47 +321,26 @@ gu_map_iter(GuMap* map, GuMapItor* itor, GuExn* err)
}
}
-typedef struct {
- GuEnum en;
- GuMap* ht;
- size_t i;
- GuMapKeyValue x;
-} GuMapEnum;
-
-static void
-gu_map_enum_next(GuEnum* self, void* to, GuPool* pool)
+GU_API bool
+gu_map_next(GuMap* map, size_t i, const void** pkey, void** pvalue)
{
- *((GuMapKeyValue**) to) = NULL;
-
- size_t i;
- GuMapEnum* en = (GuMapEnum*) self;
- for (i = en->i; i < en->ht->data.n_entries; i++) {
- if (gu_map_entry_is_free(en->ht, &en->ht->data, i)) {
+ while (i < map->data.n_entries) {
+ if (gu_map_entry_is_free(map, &map->data, i)) {
+ i++;
continue;
}
- en->x.key = &en->ht->data.keys[i * en->ht->key_size];
- en->x.value = &en->ht->data.values[i * en->ht->cell_size];
- if (en->ht->hasher == gu_addr_hasher) {
- en->x.key = *(const void* const*) en->x.key;
- } else if (en->ht->hasher == gu_string_hasher) {
- en->x.key = *(GuString*) en->x.key;
- }
- *((GuMapKeyValue**) to) = &en->x;
- break;
+ *pkey = &map->data.keys[i * map->key_size];
+ *pvalue = &map->data.values[i * map->cell_size];
+ if (map->hasher == gu_addr_hasher) {
+ *pkey = *(const void* const*) *pkey;
+ } else if (map->hasher == gu_string_hasher) {
+ *pkey = *(GuString*) *pkey;
+ }
+ return true;
}
- en->i = i+1;
-}
-
-GU_API GuEnum*
-gu_map_enum(GuMap* ht, GuPool* pool)
-{
- GuMapEnum* en = gu_new(GuMapEnum, pool);
- en->en.next = gu_map_enum_next;
- en->ht = ht;
- en->i = 0;
- return &en->en;
+ return false;
}
GU_API size_t
diff --git a/src/runtime/c/gu/map.h b/src/runtime/c/gu/map.h
index aaea06a08..b0fc17303 100644
--- a/src/runtime/c/gu/map.h
+++ b/src/runtime/c/gu/map.h
@@ -74,13 +74,8 @@ gu_map_delete(GuMap* ht, const void* key);
GU_API_DECL void
gu_map_iter(GuMap* ht, GuMapItor* itor, GuExn* err);
-typedef struct {
- const void* key;
- void* value;
-} GuMapKeyValue;
-
-GU_API_DECL GuEnum*
-gu_map_enum(GuMap* ht, GuPool* pool);
+GU_API bool
+gu_map_next(GuMap* ht, size_t i, const void** pkey, void** pvalue);
typedef GuMap GuIntMap;