picrin/lib/dict.c

288 lines
6.1 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2017-03-28 10:09:40 -04:00
#include "object.h"
2014-08-25 00:38:09 -04:00
KHASH_DEFINE(dict, symbol *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
2015-06-24 16:56:15 -04:00
2016-02-19 05:08:45 -05:00
pic_value
2014-09-12 06:41:20 -04:00
pic_make_dict(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-21 06:32:00 -05:00
struct dict *dict;
2014-08-25 00:38:09 -04:00
2016-02-21 06:32:00 -05:00
dict = (struct dict *)pic_obj_alloc(pic, sizeof(struct dict), PIC_TYPE_DICT);
2015-06-24 16:56:15 -04:00
kh_init(dict, &dict->hash);
return obj_value(pic, dict);
2014-08-25 00:38:09 -04:00
}
pic_value
2016-02-20 01:31:14 -05:00
pic_dict_ref(pic_state *pic, pic_value dict, pic_value key)
2014-08-25 00:38:09 -04:00
{
2016-02-19 05:08:45 -05:00
khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash;
2016-02-20 11:52:34 -05:00
int it;
2014-08-25 00:38:09 -04:00
2016-02-20 01:31:14 -05:00
it = kh_get(dict, h, pic_sym_ptr(pic, key));
2015-06-24 16:56:15 -04:00
if (it == kh_end(h)) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "element not found for given key", 1, 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
2016-02-20 01:31:14 -05:00
pic_dict_set(pic_state *pic, pic_value dict, pic_value key, pic_value val)
2014-08-25 00:38:09 -04:00
{
2016-02-19 05:08:45 -05:00
khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash;
2015-06-24 16:56:15 -04:00
int ret;
2016-02-20 11:52:34 -05:00
int it;
2015-06-24 16:56:15 -04:00
2016-02-20 01:31:14 -05:00
it = kh_put(dict, h, pic_sym_ptr(pic, key), &ret);
2015-06-24 16:56:15 -04:00
kh_val(h, it) = val;
2014-08-25 00:38:09 -04:00
}
2015-08-26 06:04:27 -04:00
int
2016-02-21 05:19:35 -05:00
pic_dict_size(pic_state *PIC_UNUSED(pic), pic_value dict)
2014-08-25 00:38:09 -04:00
{
2016-02-19 05:08:45 -05:00
return kh_size(&pic_dict_ptr(pic, dict)->hash);
2014-08-25 00:38:09 -04:00
}
bool
2016-02-20 01:31:14 -05:00
pic_dict_has(pic_state *pic, pic_value dict, pic_value key)
2014-08-25 00:38:09 -04:00
{
2016-02-19 05:08:45 -05:00
khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash;
2016-02-20 01:31:14 -05:00
return kh_get(dict, h, pic_sym_ptr(pic, key)) != kh_end(h);
2014-08-25 00:38:09 -04:00
}
void
2016-02-20 01:31:14 -05:00
pic_dict_del(pic_state *pic, pic_value dict, pic_value key)
2014-08-25 00:38:09 -04:00
{
2016-02-19 05:08:45 -05:00
khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash;
2016-02-20 11:52:34 -05:00
int it;
2015-06-24 16:56:15 -04:00
2016-02-20 01:31:14 -05:00
it = kh_get(dict, h, pic_sym_ptr(pic, key));
2015-06-24 16:56:15 -04:00
if (it == kh_end(h)) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "element not found for given key", 1, 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
}
2016-02-18 10:39:13 -05:00
bool
2016-02-21 05:19:35 -05:00
pic_dict_next(pic_state *PIC_UNUSED(pic), pic_value dict, int *iter, pic_value *key, pic_value *val)
2016-02-18 10:39:13 -05:00
{
2016-02-19 05:08:45 -05:00
khash_t(dict) *h = &pic_dict_ptr(pic, dict)->hash;
2016-02-18 10:39:13 -05:00
int it = *iter;
for (it = *iter; it != kh_end(h); ++it) {
if (kh_exist(h, it)) {
if (key) *key = obj_value(pic, kh_key(h, it));
2016-02-18 10:39:13 -05:00
if (val) *val = kh_val(h, it);
*iter = ++it;
return true;
}
}
return false;
}
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
{
pic_get_args(pic, "");
2016-02-19 05:08:45 -05:00
return pic_make_dict(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
2014-09-13 03:44:27 -04:00
pic_dict_dictionary(pic_state *pic)
{
2016-02-19 05:08:45 -05:00
pic_value dict, *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) {
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, argv[i], sym);
2016-02-20 01:31:14 -05:00
pic_dict_set(pic, dict, argv[i], argv[i+1]);
2014-09-13 03:44:27 -04:00
}
2016-02-19 05:08:45 -05:00
return dict;
2014-09-13 03:44:27 -04:00
}
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, pic_dict_p(pic, obj));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_dict_dictionary_has_p(pic_state *pic)
{
pic_value dict, key;
pic_get_args(pic, "dm", &dict, &key);
return pic_bool_value(pic, pic_dict_has(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_ref(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-20 01:31:14 -05:00
pic_value dict, key;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "dm", &dict, &key);
2014-08-25 00:38:09 -04:00
return 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
{
2016-02-20 01:31:14 -05:00
pic_value dict, key, val;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "dmo", &dict, &key, &val);
2014-08-25 00:38:09 -04:00
pic_dict_set(pic, dict, key, val);
return pic_undef_value(pic);
}
static pic_value
pic_dict_dictionary_delete(pic_state *pic)
{
pic_value dict, key;
pic_get_args(pic, "dm", &dict, &key);
pic_dict_del(pic, dict, key);
return pic_undef_value(pic);
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
{
2016-02-19 05:08:45 -05:00
pic_value dict;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "d", &dict);
return pic_int_value(pic, pic_dict_size(pic, dict));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_dict_dictionary_map(pic_state *pic)
{
2016-02-20 01:31:14 -05:00
pic_value dict, proc, key, ret = pic_nil_value(pic);
2016-02-19 05:08:45 -05:00
int it = 0;
pic_get_args(pic, "ld", &proc, &dict);
2016-02-19 05:08:45 -05:00
while (pic_dict_next(pic, dict, &it, &key, NULL)) {
2016-02-20 01:31:14 -05:00
pic_push(pic, pic_call(pic, proc, 1, key), ret);
}
return pic_reverse(pic, ret);
}
static pic_value
pic_dict_dictionary_for_each(pic_state *pic)
{
2016-02-20 01:31:14 -05:00
pic_value dict, proc, key;
2016-02-19 05:08:45 -05:00
int it;
pic_get_args(pic, "ld", &proc, &dict);
2016-02-19 05:08:45 -05:00
while (pic_dict_next(pic, dict, &it, &key, NULL)) {
2016-02-20 01:31:14 -05:00
pic_call(pic, proc, 1, key);
}
return pic_undef_value(pic);
}
2014-09-13 03:44:27 -04:00
static pic_value
pic_dict_dictionary_to_alist(pic_state *pic)
{
2016-02-20 01:31:14 -05:00
pic_value dict, key, val, alist = pic_nil_value(pic);
2016-02-18 10:39:13 -05:00
int it = 0;
2014-09-13 03:44:27 -04:00
pic_get_args(pic, "d", &dict);
2016-02-20 01:31:14 -05:00
while (pic_dict_next(pic, dict, &it, &key, &val)) {
pic_push(pic, pic_cons(pic, key, val), alist);
2014-09-13 03:44:27 -04:00
}
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)
{
2016-02-19 05:08:45 -05:00
pic_value dict, 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) {
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, pic_car(pic, e), sym);
2016-02-20 01:31:14 -05:00
pic_dict_set(pic, dict, pic_car(pic, e), pic_cdr(pic, e));
2014-09-13 03:44:27 -04:00
}
2016-02-19 05:08:45 -05:00
return dict;
2014-09-13 03:44:27 -04:00
}
static pic_value
pic_dict_dictionary_to_plist(pic_state *pic)
{
2016-02-20 01:31:14 -05:00
pic_value dict, key, val, plist = pic_nil_value(pic);
2016-02-18 10:39:13 -05:00
int it = 0;
2014-09-13 03:44:27 -04:00
pic_get_args(pic, "d", &dict);
2016-02-20 01:31:14 -05:00
while (pic_dict_next(pic, dict, &it, &key, &val)) {
2016-02-18 10:39:13 -05:00
pic_push(pic, val, plist);
2016-02-20 01:31:14 -05:00
pic_push(pic, key, 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)
{
2016-02-19 05:08:45 -05:00
pic_value dict, plist, e;
2014-09-13 03:44:27 -04:00
pic_get_args(pic, "o", &plist);
dict = pic_make_dict(pic);
for (e = pic_reverse(pic, plist); ! pic_nil_p(pic, e); e = pic_cddr(pic, e)) {
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, pic_cadr(pic, e), sym);
2016-02-20 01:31:14 -05:00
pic_dict_set(pic, dict, pic_cadr(pic, e), pic_car(pic, e));
2014-09-13 03:44:27 -04:00
}
2016-02-19 05:08:45 -05:00
return dict;
2014-09-13 03:44:27 -04:00
}
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-has?", pic_dict_dictionary_has_p);
2014-09-13 03:44:27 -04:00
pic_defun(pic, "dictionary-ref", pic_dict_dictionary_ref);
pic_defun(pic, "dictionary-set!", pic_dict_dictionary_set);
pic_defun(pic, "dictionary-delete!", pic_dict_dictionary_delete);
2014-09-13 03:44:27 -04:00
pic_defun(pic, "dictionary-size", pic_dict_dictionary_size);
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
}