2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
2015-06-24 16:56:15 -04:00
|
|
|
KHASH_DEFINE(dict, pic_sym *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
struct pic_dict *
|
2014-09-12 06:41:20 -04:00
|
|
|
pic_make_dict(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
|
|
|
|
|
|
|
dict = (struct pic_dict *)pic_obj_alloc(pic, sizeof(struct pic_dict), PIC_TT_DICT);
|
2015-06-24 16:56:15 -04:00
|
|
|
kh_init(dict, &dict->hash);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_sym *key)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-24 16:56:15 -04:00
|
|
|
khash_t(dict) *h = &dict->hash;
|
|
|
|
khiter_t it;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-24 16:56:15 -04:00
|
|
|
it = kh_get(dict, h, key);
|
|
|
|
if (it == kh_end(h)) {
|
2015-01-18 21:08:27 -05:00
|
|
|
pic_errorf(pic, "element not found for a key: ~s", pic_obj_value(key));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-06-24 16:56:15 -04:00
|
|
|
return kh_val(h, it);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-28 04:06:41 -04:00
|
|
|
pic_dict_set(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key, pic_value val)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-24 16:56:15 -04:00
|
|
|
khash_t(dict) *h = &dict->hash;
|
|
|
|
int ret;
|
|
|
|
khiter_t it;
|
|
|
|
|
|
|
|
it = kh_put(dict, h, key, &ret);
|
|
|
|
kh_val(h, it) = val;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:04:27 -04:00
|
|
|
int
|
2015-05-28 04:06:41 -04:00
|
|
|
pic_dict_size(pic_state PIC_UNUSED(*pic), struct pic_dict *dict)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-24 16:56:15 -04:00
|
|
|
return kh_size(&dict->hash);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-05-28 04:06:41 -04:00
|
|
|
pic_dict_has(pic_state PIC_UNUSED(*pic), struct pic_dict *dict, pic_sym *key)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-24 16:56:15 -04:00
|
|
|
return kh_get(dict, &dict->hash, key) != kh_end(&dict->hash);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym *key)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2015-06-24 16:56:15 -04:00
|
|
|
khash_t(dict) *h = &dict->hash;
|
|
|
|
khiter_t it;
|
|
|
|
|
|
|
|
it = kh_get(dict, h, key);
|
|
|
|
if (it == kh_end(h)) {
|
2015-01-18 21:08:27 -05:00
|
|
|
pic_errorf(pic, "no slot named ~s found in dictionary", pic_obj_value(key));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
2015-06-24 16:56:15 -04:00
|
|
|
kh_del(dict, h, it);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_dict_make_dictionary(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
|
|
|
|
|
|
|
pic_get_args(pic, "");
|
|
|
|
|
2014-09-12 06:41:20 -04:00
|
|
|
dict = pic_make_dict(pic);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
return pic_obj_value(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_dict_dictionary(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
|
|
|
pic_value *argv;
|
2015-08-26 06:04:27 -04:00
|
|
|
int argc, i;
|
2014-09-13 03:44:27 -04:00
|
|
|
|
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
|
|
|
|
|
|
|
dict = pic_make_dict(pic);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i += 2) {
|
2015-01-17 07:03:08 -05:00
|
|
|
pic_assert_type(pic, argv[i], sym);
|
2015-01-20 01:31:17 -05:00
|
|
|
pic_dict_set(pic, dict, pic_sym_ptr(argv[i]), argv[i+1]);
|
2014-09-13 03:44:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pic_obj_value(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_dict_dictionary_p(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
pic_value obj;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &obj);
|
|
|
|
|
|
|
|
return pic_bool_value(pic_dict_p(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_dict_dictionary_ref(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *key;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-17 07:03:08 -05:00
|
|
|
pic_get_args(pic, "dm", &dict, &key);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-09 04:06:19 -04:00
|
|
|
if (! pic_dict_has(pic, dict, key)) {
|
2015-07-18 02:22:33 -04:00
|
|
|
return pic_false_value();
|
2014-08-30 13:39:09 -04:00
|
|
|
}
|
2015-07-18 02:22:33 -04:00
|
|
|
return pic_cons(pic, pic_obj_value(key), pic_dict_ref(pic, dict, key));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_dict_dictionary_set(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
2015-01-20 02:02:28 -05:00
|
|
|
pic_sym *key;
|
2015-01-17 07:03:08 -05:00
|
|
|
pic_value val;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-01-17 07:03:08 -05:00
|
|
|
pic_get_args(pic, "dmo", &dict, &key, &val);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-06-09 04:06:19 -04:00
|
|
|
if (pic_undef_p(val)) {
|
|
|
|
if (pic_dict_has(pic, dict, key)) {
|
|
|
|
pic_dict_del(pic, dict, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pic_dict_set(pic, dict, key, val);
|
|
|
|
}
|
2015-06-09 03:34:45 -04:00
|
|
|
return pic_undef_value();
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_dict_dictionary_size(pic_state *pic)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
|
|
|
|
|
|
|
pic_get_args(pic, "d", &dict);
|
|
|
|
|
2015-01-17 07:03:08 -05:00
|
|
|
return pic_int_value(pic_dict_size(pic, dict));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-17 09:26:35 -05:00
|
|
|
static pic_value
|
|
|
|
pic_dict_dictionary_map(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
2015-07-12 20:32:27 -04:00
|
|
|
struct pic_dict *dict;
|
|
|
|
khiter_t it;
|
|
|
|
khash_t(dict) *kh;
|
|
|
|
pic_value ret = pic_nil_value();
|
2015-01-17 09:26:35 -05:00
|
|
|
|
2015-07-12 20:32:27 -04:00
|
|
|
pic_get_args(pic, "ld", &proc, &dict);
|
2015-01-17 09:26:35 -05:00
|
|
|
|
2015-07-12 20:32:27 -04:00
|
|
|
kh = &dict->hash;
|
2015-06-24 16:56:15 -04:00
|
|
|
|
2015-07-12 20:32:27 -04:00
|
|
|
for (it = kh_begin(kh); it != kh_end(kh); ++it) {
|
|
|
|
if (kh_exist(kh, it)) {
|
2016-02-13 23:46:55 -05:00
|
|
|
pic_push(pic, pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it))), ret);
|
2015-01-17 09:26:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pic_reverse(pic, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_dict_dictionary_for_each(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
2015-07-12 20:32:27 -04:00
|
|
|
struct pic_dict *dict;
|
|
|
|
khiter_t it;
|
|
|
|
khash_t(dict) *kh;
|
2015-01-17 09:26:35 -05:00
|
|
|
|
2015-07-12 20:32:27 -04:00
|
|
|
pic_get_args(pic, "ld", &proc, &dict);
|
2015-01-17 09:26:35 -05:00
|
|
|
|
2015-07-12 20:32:27 -04:00
|
|
|
kh = &dict->hash;
|
2015-06-24 16:56:15 -04:00
|
|
|
|
2015-07-12 20:32:27 -04:00
|
|
|
for (it = kh_begin(kh); it != kh_end(kh); ++it) {
|
|
|
|
if (kh_exist(kh, it)) {
|
2016-02-13 23:46:55 -05:00
|
|
|
pic_call(pic, proc, 1, pic_obj_value(kh_key(kh, it)));
|
2015-01-17 09:26:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 03:34:45 -04:00
|
|
|
return pic_undef_value();
|
2015-01-17 09:26:35 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 03:44:27 -04:00
|
|
|
static pic_value
|
|
|
|
pic_dict_dictionary_to_alist(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
|
|
|
pic_value item, alist = pic_nil_value();
|
2015-06-23 12:13:18 -04:00
|
|
|
pic_sym *sym;
|
2015-06-24 16:56:15 -04:00
|
|
|
khiter_t it;
|
2014-09-13 03:44:27 -04:00
|
|
|
|
|
|
|
pic_get_args(pic, "d", &dict);
|
|
|
|
|
2015-06-23 12:13:18 -04:00
|
|
|
pic_dict_for_each (sym, dict, it) {
|
|
|
|
item = pic_cons(pic, pic_obj_value(sym), pic_dict_ref(pic, dict, sym));
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_push(pic, item, alist);
|
|
|
|
}
|
|
|
|
|
2015-06-25 09:29:27 -04:00
|
|
|
return alist;
|
2014-09-13 03:44:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_dict_alist_to_dictionary(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
2015-01-22 05:28:31 -05:00
|
|
|
pic_value alist, e, it;
|
2014-09-13 03:44:27 -04:00
|
|
|
|
|
|
|
pic_get_args(pic, "o", &alist);
|
|
|
|
|
|
|
|
dict = pic_make_dict(pic);
|
|
|
|
|
2015-01-22 05:28:31 -05:00
|
|
|
pic_for_each (e, pic_reverse(pic, alist), it) {
|
2015-01-17 07:03:08 -05:00
|
|
|
pic_assert_type(pic, pic_car(pic, e), sym);
|
2015-01-20 01:31:17 -05:00
|
|
|
pic_dict_set(pic, dict, pic_sym_ptr(pic_car(pic, e)), pic_cdr(pic, e));
|
2014-09-13 03:44:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pic_obj_value(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_dict_dictionary_to_plist(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
|
|
|
pic_value plist = pic_nil_value();
|
2015-06-23 12:13:18 -04:00
|
|
|
pic_sym *sym;
|
2015-06-24 16:56:15 -04:00
|
|
|
khiter_t it;
|
2014-09-13 03:44:27 -04:00
|
|
|
|
|
|
|
pic_get_args(pic, "d", &dict);
|
|
|
|
|
2015-06-23 12:13:18 -04:00
|
|
|
pic_dict_for_each (sym, dict, it) {
|
|
|
|
pic_push(pic, pic_dict_ref(pic, dict, sym), plist);
|
2015-06-25 09:29:27 -04:00
|
|
|
pic_push(pic, pic_obj_value(sym), plist);
|
2014-09-13 03:44:27 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 09:29:27 -04:00
|
|
|
return plist;
|
2014-09-13 03:44:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_dict_plist_to_dictionary(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_dict *dict;
|
|
|
|
pic_value plist, e;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &plist);
|
|
|
|
|
|
|
|
dict = pic_make_dict(pic);
|
|
|
|
|
|
|
|
for (e = pic_reverse(pic, plist); ! pic_nil_p(e); e = pic_cddr(pic, e)) {
|
2015-01-17 07:03:08 -05:00
|
|
|
pic_assert_type(pic, pic_cadr(pic, e), sym);
|
2015-01-20 01:31:17 -05:00
|
|
|
pic_dict_set(pic, dict, pic_sym_ptr(pic_cadr(pic, e)), pic_car(pic, e));
|
2014-09-13 03:44:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pic_obj_value(dict);
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
void
|
|
|
|
pic_init_dict(pic_state *pic)
|
|
|
|
{
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_defun(pic, "make-dictionary", pic_dict_make_dictionary);
|
|
|
|
pic_defun(pic, "dictionary?", pic_dict_dictionary_p);
|
|
|
|
pic_defun(pic, "dictionary", pic_dict_dictionary);
|
|
|
|
pic_defun(pic, "dictionary-ref", pic_dict_dictionary_ref);
|
|
|
|
pic_defun(pic, "dictionary-set!", pic_dict_dictionary_set);
|
|
|
|
pic_defun(pic, "dictionary-size", pic_dict_dictionary_size);
|
2015-01-17 09:26:35 -05:00
|
|
|
pic_defun(pic, "dictionary-map", pic_dict_dictionary_map);
|
|
|
|
pic_defun(pic, "dictionary-for-each", pic_dict_dictionary_for_each);
|
2014-09-13 03:44:27 -04:00
|
|
|
pic_defun(pic, "dictionary->alist", pic_dict_dictionary_to_alist);
|
|
|
|
pic_defun(pic, "alist->dictionary", pic_dict_alist_to_dictionary);
|
|
|
|
pic_defun(pic, "dictionary->plist", pic_dict_dictionary_to_plist);
|
|
|
|
pic_defun(pic, "plist->dictionary", pic_dict_plist_to_dictionary);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|