Index: src/map.c ================================================================== --- src/map.c +++ src/map.c @@ -352,10 +352,38 @@ return false; } return true; } + +void +cfw_map_iter(CFWMap *map, cfw_map_iter_t *iter) +{ + iter->key = NULL; + iter->obj = NULL; + iter->_map = map; + iter->_pos = 0; +} + +void +cfw_map_iter_next(cfw_map_iter_t *iter) +{ + CFWMap *map = iter->_map; + + for (; iter->_pos < map->size && + (map->data[iter->_pos] == NULL || + map->data[iter->_pos] == &deleted); iter->_pos++); + + if (iter->_pos < map->size) { + iter->key = map->data[iter->_pos]->key; + iter->obj = map->data[iter->_pos]->obj; + iter->_pos++; + } else { + iter->key = NULL; + iter->obj = NULL; + } +} static CFWClass class = { .name = "CFWMap", .size = sizeof(CFWMap), .ctor = ctor, Index: src/map.h ================================================================== --- src/map.h +++ src/map.h @@ -28,11 +28,21 @@ #define __COREFW_MAP_H__ #include "class.h" typedef struct CFWMap CFWMap; + +typedef struct cfw_map_iter_t { + void *key, *obj; + /* private */ + CFWMap *_map; + uint32_t _pos; +} cfw_map_iter_t; + extern CFWClass *cfw_map; extern size_t cfw_map_size(CFWMap*); extern void* cfw_map_get(CFWMap*, void*); extern bool cfw_map_set(CFWMap*, void*, void*); +extern void cfw_map_iter(CFWMap*, cfw_map_iter_t*); +extern void cfw_map_iter_next(cfw_map_iter_t*); #endif Index: tests/tests.c ================================================================== --- tests/tests.c +++ tests/tests.c @@ -28,10 +28,33 @@ #include "object.h" #include "string.h" #include "array.h" #include "map.h" + +void +print_map(CFWMap *map) +{ + cfw_map_iter_t iter; + + cfw_map_iter(map, &iter); + cfw_map_iter_next(&iter); + + fputs("{\n", stdout); + + while (iter.key != NULL) { + if (iter.obj != NULL) + printf("\t%s = %s\n", cfw_string_c(iter.key), + cfw_string_c(iter.obj)); + else + printf("\t%s = NULL\n", cfw_string_c(iter.key)); + + cfw_map_iter_next(&iter); + } + + fputs("}\n", stdout); +} int main() { CFWString *s[3]; @@ -66,24 +89,23 @@ s[0] = cfw_new(cfw_string, "Hallo"); s[1] = cfw_new(cfw_string, "Welt!"); m = cfw_new(cfw_map, s[0], s[1], NULL); - cfw_unref(s[1]); - puts(cfw_string_c(cfw_map_get(m, s[0]))); + print_map(m); s[1] = cfw_new(cfw_string, "Test"); cfw_map_set(m, s[0], s[1]); cfw_unref(s[1]); - puts(cfw_string_c(cfw_map_get(m, s[0]))); + print_map(m); cfw_map_set(m, s[0], NULL); - printf("%p\n", cfw_map_get(m, s[0])); + print_map(m); cfw_unref(s[0]); cfw_unref(m); return 0; }