Add map iteration.
This commit is contained in:
parent
a835d115cb
commit
d1b16c8722
3 changed files with 64 additions and 4 deletions
28
src/map.c
28
src/map.c
|
@ -355,6 +355,34 @@ cfw_map_set(CFWMap *map, void *key, void *obj)
|
|||
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),
|
||||
|
|
10
src/map.h
10
src/map.h
|
@ -30,9 +30,19 @@
|
|||
#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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue