running map or foreach on multiple dictionaries does not make sense.

This commit is contained in:
Yuichi Nishiwaki 2015-07-13 09:32:27 +09:00
parent f6bb2ce178
commit 0f55320a67
1 changed files with 17 additions and 64 deletions

View File

@ -155,42 +155,19 @@ static pic_value
pic_dict_dictionary_map(pic_state *pic) pic_dict_dictionary_map(pic_state *pic)
{ {
struct pic_proc *proc; struct pic_proc *proc;
size_t argc, i; struct pic_dict *dict;
pic_value *args; khiter_t it;
pic_value arg_list, ret = pic_nil_value(); khash_t(dict) *kh;
pic_value ret = pic_nil_value();
pic_get_args(pic, "l*", &proc, &argc, &args); pic_get_args(pic, "ld", &proc, &dict);
if (argc != 0) { kh = &dict->hash;
khiter_t it[argc];
khash_t(dict) *kh[argc];
for (i = 0; i < argc; ++i) { for (it = kh_begin(kh); it != kh_end(kh); ++it) {
if (! pic_dict_p(args[i])) { if (kh_exist(kh, it)) {
pic_errorf(pic, "expected dict, but got %s", pic_type_repr(pic_type(args[i]))); pic_push(pic, pic_apply1(pic, proc, pic_obj_value(kh_key(kh, it))), ret);
} }
kh[i] = &pic_dict_ptr(args[i])->hash;
it[i] = kh_begin(kh[i]);
}
do {
arg_list = pic_nil_value();
for (i = 0; i < argc; ++i) {
while (it[i] != kh_end(kh[i])) { /* find next available */
if (kh_exist(kh[i], it[i]))
break;
it[i]++;
}
if (it[i] == kh_end(kh[i])) {
break;
}
pic_push(pic, pic_obj_value(kh_key(kh[i], it[i]++)), arg_list);
}
if (i != argc) {
break;
}
pic_push(pic, pic_apply(pic, proc, pic_reverse(pic, arg_list)), ret);
} while (1);
} }
return pic_reverse(pic, ret); return pic_reverse(pic, ret);
@ -200,42 +177,18 @@ static pic_value
pic_dict_dictionary_for_each(pic_state *pic) pic_dict_dictionary_for_each(pic_state *pic)
{ {
struct pic_proc *proc; struct pic_proc *proc;
size_t argc, i; struct pic_dict *dict;
pic_value *args; khiter_t it;
pic_value arg_list; khash_t(dict) *kh;
pic_get_args(pic, "l*", &proc, &argc, &args); pic_get_args(pic, "ld", &proc, &dict);
if (argc != 0) { kh = &dict->hash;
khiter_t it[argc];
khash_t(dict) *kh[argc];
for (i = 0; i < argc; ++i) { for (it = kh_begin(kh); it != kh_end(kh); ++it) {
if (! pic_dict_p(args[i])) { if (kh_exist(kh, it)) {
pic_errorf(pic, "expected dict, but got %s", pic_type_repr(pic_type(args[i]))); pic_apply1(pic, proc, pic_obj_value(kh_key(kh, it)));
} }
kh[i] = &pic_dict_ptr(args[i])->hash;
it[i] = kh_begin(kh[i]);
}
do {
arg_list = pic_nil_value();
for (i = 0; i < argc; ++i) {
while (it[i] != kh_end(kh[i])) { /* find next available */
if (kh_exist(kh[i], it[i]))
break;
it[i]++;
}
if (it[i] == kh_end(kh[i])) {
break;
}
pic_push(pic, pic_obj_value(kh_key(kh[i], it[i]++)), arg_list);
}
if (i != argc) {
break;
}
pic_void(pic_apply(pic, proc, pic_reverse(pic, arg_list)));
} while (1);
} }
return pic_undef_value(); return pic_undef_value();