add dictionary-for-each

This commit is contained in:
Yuichi Nishiwaki 2014-09-13 16:14:33 +09:00
parent 0f2a172ffc
commit d882b5fb24
1 changed files with 25 additions and 0 deletions

25
dict.c
View File

@ -5,6 +5,7 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/dict.h" #include "picrin/dict.h"
#include "picrin/cont.h" #include "picrin/cont.h"
#include "picrin/pair.h"
struct pic_dict * struct pic_dict *
pic_make_dict(pic_state *pic) pic_make_dict(pic_state *pic)
@ -137,6 +138,29 @@ pic_dict_dict_size(pic_state *pic)
return pic_int_value(pic_dict_size(pic, dict)); return pic_int_value(pic_dict_size(pic, dict));
} }
static pic_value
pic_dict_dict_for_each(pic_state *pic)
{
struct pic_proc *proc;
struct pic_dict *dict;
pic_value item;
xh_iter it;
pic_get_args(pic, "ld", &proc, &dict);
xh_begin(&it, &dict->hash);
while (xh_next(&it)) {
int ai = pic_gc_arena_preserve(pic);
item = pic_cons(pic, pic_sym_value(xh_key(it.e, pic_sym)), xh_val(it.e, pic_value));
pic_apply1(pic, proc, item);
pic_gc_arena_restore(pic, ai);
}
return pic_none_value();
}
void void
pic_init_dict(pic_state *pic) pic_init_dict(pic_state *pic)
{ {
@ -146,4 +170,5 @@ pic_init_dict(pic_state *pic)
pic_defun(pic, "dictionary-set!", pic_dict_dict_set); pic_defun(pic, "dictionary-set!", pic_dict_dict_set);
pic_defun(pic, "dictionary-delete", pic_dict_dict_del); pic_defun(pic, "dictionary-delete", pic_dict_dict_del);
pic_defun(pic, "dictionary-size", pic_dict_dict_size); pic_defun(pic, "dictionary-size", pic_dict_dict_size);
pic_defun(pic, "dictionary-for-each", pic_dict_dict_for_each);
} }