ref: 12c9d2fc728b51aa1eb9a70d0d331eb9464912d9
dir: /src/table.c/
#include "sl.h"
#include "equalhash.h"
#include "cvalues.h"
#include "types.h"
#include "print.h"
#include "table.h"
#define inline_space sizeof(((sl_htable*)nil)->_space)
static sl_type *sl_tabletype;
static void
print_htable(sl_v v, sl_ios *f)
{
sl_htable *h = cvalue_data(v);
int first = 1;
sl_print_str(f, "#table(");
for(int i = 0; i < h->size; i += 2){
if(h->table[i+1] != HT_NOTFOUND){
if(!first)
sl_print_str(f, " ");
sl_print_child(f, (sl_v)h->table[i]);
sl_print_chr(f, ' ');
sl_print_child(f, (sl_v)h->table[i+1]);
first = 0;
}
}
sl_print_chr(f, ')');
}
static void
print_traverse_htable(sl_v self)
{
sl_htable *h = cvalue_data(self);
for(int i = 0; i < h->size; i += 2){
if(h->table[i+1] != HT_NOTFOUND){
print_traverse((sl_v)h->table[i]);
print_traverse((sl_v)h->table[i+1]);
}
}
}
static void
free_htable(sl_v self)
{
sl_htable *h = cvalue_data(self);
htable_free(h);
}
static void
relocate_htable(sl_v oldv, sl_v newv)
{
sl_htable *oldh = cvalue_data(oldv);
sl_htable *h = cvalue_data(newv);
if(oldh->table == &oldh->_space[0])
h->table = &h->_space[0];
h->i = oldh->i;
for(int i = 0; i < h->size; i++){
if(h->table[i] != HT_NOTFOUND)
h->table[i] = (void*)sl_relocate((sl_v)h->table[i]);
}
}
static sl_cvtable table_vtable = {
print_htable,
relocate_htable,
free_htable,
print_traverse_htable,
};
bool
ishashtable(sl_v v)
{
return iscvalue(v) && cv_class(ptr(v)) == sl_tabletype;
}
sl_purefn
BUILTIN("table?", tablep)
{
argcount(nargs, 1);
return ishashtable(args[0]) ? sl_t : sl_nil;
}
sl_htable *
totable(sl_v v)
{
if(!ishashtable(v))
type_error("table", v);
return cvalue_data(v);
}
BUILTIN("table", table)
{
int cnt = nargs;
if(cnt & 1)
lerrorf(sl_errarg, "arguments must come in pairs");
sl_v nt;
// prevent small tables from being added to finalizer list
if(cnt <= HT_N_INLINE)
nt = cvalue_nofinalizer(sl_tabletype, sizeof(sl_htable));
else
nt = cvalue(sl_tabletype, sizeof(sl_htable)-inline_space);
sl_htable *h = cvalue_data(nt);
htable_new(h, cnt/2);
sl_v k = sl_nil, arg;
int i;
FOR_ARGS(i, 0, arg, args){
if(i & 1)
equalhash_put(h, (void*)k, (void*)arg);
else
k = arg;
}
assert(h->table == &h->_space[0] || cnt > HT_N_INLINE);
return nt;
}
// (put! table key value)
BUILTIN("put!", put)
{
argcount(nargs, 3);
sl_htable *h = totable(args[0]);
void **table0 = h->table;
equalhash_put(h, (void*)args[1], (void*)args[2]);
// register finalizer if we outgrew inline space
if(table0 == &h->_space[0] && h->table != &h->_space[0]){
csl_v *cv = ptr(args[0]);
add_finalizer(cv);
cv->len = sizeof(sl_htable) - inline_space;
}
return args[0];
}
_Noreturn
static void
key_error(sl_v key)
{
lerrorf(mk_list2(sl_errkey, key), "key not found");
}
// (get table key [default])
sl_purefn
BUILTIN("get", get)
{
if(nargs != 3)
argcount(nargs, 2);
sl_htable *h = totable(args[0]);
sl_v v = (sl_v)equalhash_get(h, (void*)args[1]);
if(v == (sl_v)HT_NOTFOUND){
if(nargs == 3)
return args[2];
key_error(args[1]);
}
return v;
}
// (has? table key)
sl_purefn
BUILTIN("has?", has)
{
argcount(nargs, 2);
sl_htable *h = totable(args[0]);
return equalhash_has(h, (void*)args[1]) ? sl_t : sl_nil;
}
// (del! table key)
BUILTIN("del!", del)
{
argcount(nargs, 2);
sl_htable *h = totable(args[0]);
if(!equalhash_remove(h, (void*)args[1]))
key_error(args[1]);
return args[0];
}
BUILTIN("table-foldl", table_foldl)
{
argcount(nargs, 3);
sl_v f = args[0], zero = args[1], t = args[2];
sl_htable *h = totable(t);
int n = h->size;
void **table = h->table;
sl_gc_handle(&f);
sl_gc_handle(&zero);
sl_gc_handle(&t);
for(int i = 0; i < n; i += 2){
if(table[i+1] == HT_NOTFOUND)
continue;
zero = sl_applyn(3, f, (sl_v)table[i], (sl_v)table[i+1], zero);
// reload pointer
h = cvalue_data(t);
n = h->size;
table = h->table;
}
sl_free_gc_handles(3);
return zero;
}
void
table_init(void)
{
sl_tabletype = define_opaque_type(symbol("table", false), sizeof(sl_htable), &table_vtable, nil);
}