2015-06-09 05:31:46 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
KHASH_DEFINE(weak, void *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
|
2015-06-24 18:14:29 -04:00
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
struct pic_weak *
|
|
|
|
pic_make_weak(pic_state *pic)
|
2015-06-09 05:31:46 -04:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
struct pic_weak *weak;
|
2015-06-09 05:31:46 -04:00
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
weak = (struct pic_weak *)pic_obj_alloc(pic, sizeof(struct pic_weak), PIC_TT_WEAK);
|
|
|
|
weak->prev = NULL;
|
|
|
|
kh_init(weak, &weak->hash);
|
2015-06-09 05:31:46 -04:00
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
return weak;
|
2015-06-09 05:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_ref(pic_state *pic, struct pic_weak *weak, void *key)
|
2015-06-09 05:31:46 -04:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
khash_t(weak) *h = &weak->hash;
|
2015-06-24 18:14:29 -04:00
|
|
|
khiter_t it;
|
2015-06-09 05:31:46 -04:00
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
it = kh_get(weak, h, key);
|
2015-06-24 18:14:29 -04:00
|
|
|
if (it == kh_end(h)) {
|
2015-06-09 05:31:46 -04:00
|
|
|
pic_errorf(pic, "element not found for a key: ~s", pic_obj_value(key));
|
|
|
|
}
|
2015-06-24 18:14:29 -04:00
|
|
|
return kh_val(h, it);
|
2015-06-09 05:31:46 -04:00
|
|
|
}
|
|
|
|
|
2015-12-26 22:19:05 -05:00
|
|
|
void *
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_rev_ref(pic_state *pic, struct pic_weak *weak, pic_value val)
|
2015-12-26 22:19:05 -05:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
khash_t(weak) *h = &weak->hash;
|
2015-12-26 22:19:05 -05:00
|
|
|
|
2015-12-27 10:11:47 -05:00
|
|
|
if (h->n_buckets) {
|
|
|
|
khint_t i = 0;
|
|
|
|
while ((i < h->n_buckets) && (ac_iseither(h->flags, i) || !pic_eq_p(h->vals[i], val))) {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
if (i < h->n_buckets) return kh_key(h, i);
|
2015-12-26 22:19:05 -05:00
|
|
|
}
|
2015-12-27 10:11:47 -05:00
|
|
|
pic_errorf(pic, "key not found for an element: ~s", val);
|
|
|
|
return NULL;
|
2015-12-26 22:19:05 -05:00
|
|
|
}
|
|
|
|
|
2015-06-09 05:31:46 -04:00
|
|
|
void
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_set(pic_state PIC_UNUSED(*pic), struct pic_weak *weak, void *key, pic_value val)
|
2015-06-09 05:31:46 -04:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
khash_t(weak) *h = &weak->hash;
|
2015-06-24 18:14:29 -04:00
|
|
|
int ret;
|
|
|
|
khiter_t it;
|
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
it = kh_put(weak, h, key, &ret);
|
2015-06-24 18:14:29 -04:00
|
|
|
kh_val(h, it) = val;
|
2015-06-09 05:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_has(pic_state PIC_UNUSED(*pic), struct pic_weak *weak, void *key)
|
2015-06-09 05:31:46 -04:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
return kh_get(weak, &weak->hash, key) != kh_end(&weak->hash);
|
2015-06-09 05:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_del(pic_state *pic, struct pic_weak *weak, void *key)
|
2015-06-09 05:31:46 -04:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
khash_t(weak) *h = &weak->hash;
|
2015-06-24 18:14:29 -04:00
|
|
|
khiter_t it;
|
|
|
|
|
2016-02-10 07:50:39 -05:00
|
|
|
it = kh_get(weak, h, key);
|
2015-06-24 18:14:29 -04:00
|
|
|
if (it == kh_end(h)) {
|
2016-02-10 07:57:20 -05:00
|
|
|
pic_errorf(pic, "no slot named ~s found in ephemeron", pic_obj_value(key));
|
2015-06-09 05:31:46 -04:00
|
|
|
}
|
2016-02-10 07:50:39 -05:00
|
|
|
kh_del(weak, h, it);
|
2015-06-09 05:31:46 -04:00
|
|
|
}
|
2015-06-09 05:50:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
static pic_value
|
2016-02-10 07:50:39 -05:00
|
|
|
weak_get(pic_state *pic, struct pic_weak *weak, void *key)
|
2015-06-09 05:50:46 -04:00
|
|
|
{
|
2016-02-10 07:50:39 -05:00
|
|
|
if (! pic_weak_has(pic, weak, key)) {
|
2015-07-18 02:28:53 -04:00
|
|
|
return pic_false_value();
|
2015-06-09 05:50:46 -04:00
|
|
|
}
|
2016-02-10 07:50:39 -05:00
|
|
|
return pic_cons(pic, pic_obj_value(key), pic_weak_ref(pic, weak, key));
|
2015-06-09 05:50:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2016-02-10 07:50:39 -05:00
|
|
|
weak_set(pic_state *pic, struct pic_weak *weak, void *key, pic_value val)
|
2015-06-09 05:50:46 -04:00
|
|
|
{
|
|
|
|
if (pic_undef_p(val)) {
|
2016-02-10 07:50:39 -05:00
|
|
|
if (pic_weak_has(pic, weak, key)) {
|
|
|
|
pic_weak_del(pic, weak, key);
|
2015-06-09 05:50:46 -04:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_weak_set(pic, weak, key, val);
|
2015-06-09 05:50:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pic_undef_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2016-02-10 07:50:39 -05:00
|
|
|
weak_call(pic_state *pic)
|
2015-06-09 05:50:46 -04:00
|
|
|
{
|
2016-02-06 10:48:02 -05:00
|
|
|
struct pic_proc *self;
|
2016-02-10 07:50:39 -05:00
|
|
|
struct pic_weak *weak;
|
2015-06-09 05:50:46 -04:00
|
|
|
pic_value key, val;
|
|
|
|
int n;
|
|
|
|
|
2016-02-06 10:48:02 -05:00
|
|
|
n = pic_get_args(pic, "&o|o", &self, &key, &val);
|
2015-06-09 05:50:46 -04:00
|
|
|
|
|
|
|
if (! pic_obj_p(key)) {
|
2016-02-10 07:57:20 -05:00
|
|
|
pic_errorf(pic, "attempted to set a non-object key '~s' in an ephemeron", key);
|
2015-06-09 05:50:46 -04:00
|
|
|
}
|
|
|
|
|
2016-02-14 08:23:14 -05:00
|
|
|
weak = pic_weak_ptr(pic_closure_ref(pic, 0));
|
2015-06-09 05:50:46 -04:00
|
|
|
|
|
|
|
if (n == 1) {
|
2016-02-10 07:50:39 -05:00
|
|
|
return weak_get(pic, weak, pic_obj_ptr(key));
|
2015-06-09 05:50:46 -04:00
|
|
|
} else {
|
2016-02-10 07:50:39 -05:00
|
|
|
return weak_set(pic, weak, pic_obj_ptr(key), val);
|
2015-06-09 05:50:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
2016-02-10 07:57:20 -05:00
|
|
|
pic_weak_make_ephemeron(pic_state *pic)
|
2015-06-09 05:50:46 -04:00
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
pic_get_args(pic, "");
|
|
|
|
|
2016-02-14 08:23:14 -05:00
|
|
|
proc = pic_lambda(pic, weak_call, 1, pic_obj_value(pic_make_weak(pic)));
|
2015-06-09 05:50:46 -04:00
|
|
|
|
|
|
|
return pic_obj_value(proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-10 07:50:39 -05:00
|
|
|
pic_init_weak(pic_state *pic)
|
2015-06-09 05:50:46 -04:00
|
|
|
{
|
2016-02-10 07:57:20 -05:00
|
|
|
pic_defun(pic, "make-ephemeron", pic_weak_make_ephemeron);
|
2015-06-09 05:50:46 -04:00
|
|
|
}
|