.
This commit is contained in:
55
lib/table.c
55
lib/table.c
@@ -9,15 +9,17 @@ KHASH_MAP_INIT_STR(TABLE_STR, VALUE)
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
struct Table {
|
||||
khash_t(TABLE_INT) *tbl_int;
|
||||
khash_t(TABLE_STR) *tbl_str;
|
||||
khash_t(TABLE_INT)* tbl_int;
|
||||
khash_t(TABLE_STR)* tbl_str;
|
||||
Heap const* heap;
|
||||
};
|
||||
|
||||
Table* table_new(void)
|
||||
Table* table_new(Heap const* heap)
|
||||
{
|
||||
Table* t = xcalloc(1, sizeof(Table));
|
||||
t->tbl_int = kh_init(TABLE_INT);
|
||||
t->tbl_str = kh_init(TABLE_STR);
|
||||
t->heap = heap;
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -33,13 +35,13 @@ size_t table_len(Table* t)
|
||||
return kh_size(t->tbl_int) + kh_size(t->tbl_str);
|
||||
}
|
||||
|
||||
static int64_t value_hash(VALUE v)
|
||||
static TABLE_HASH value_hash(VALUE v)
|
||||
{
|
||||
switch (value_type(v)) {
|
||||
case TT_NIL:
|
||||
return 0;
|
||||
case TT_INTEGER:
|
||||
return value_integer(v);
|
||||
return (uint64_t) value_integer(v);
|
||||
case TT_REAL: {
|
||||
uint32_t vv;
|
||||
float f = value_real(v);
|
||||
@@ -47,15 +49,15 @@ static int64_t value_hash(VALUE v)
|
||||
break;
|
||||
}
|
||||
case TT_STRING_CONST:
|
||||
return (int64_t) value_idx(v) | ((int64_t) 1 << 33);
|
||||
return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 33);
|
||||
case TT_ARRAY:
|
||||
return (int64_t) value_idx(v) | ((int64_t) 1 << 34);
|
||||
return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 34);
|
||||
case TT_TABLE:
|
||||
return (int64_t) value_idx(v) | ((int64_t) 1 << 35);
|
||||
return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 35);
|
||||
case TT_FUNCTION:
|
||||
return (int64_t) value_idx(v) | ((int64_t) 1 << 36);
|
||||
return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 36);
|
||||
case TT_NATIVE_PTR:
|
||||
return (int64_t) value_idx(v) | ((int64_t) 1 << 37);
|
||||
return (TABLE_HASH) value_idx(v) | ((TABLE_HASH) 1 << 37);
|
||||
case TT_STRING:
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
@@ -66,15 +68,42 @@ static int64_t value_hash(VALUE v)
|
||||
|
||||
VALUE table_get(Table const* t, VALUE key)
|
||||
{
|
||||
if (value_type(key) == TT_STRING) {
|
||||
const char* skey;
|
||||
if (heap_get_string(t->heap, value_idx(key), &skey) != T_OK)
|
||||
abort();
|
||||
khiter_t k = kh_get(TABLE_STR, t->tbl_str, skey);
|
||||
if (k == kh_end(t->tbl_str))
|
||||
__builtin_unreachable();
|
||||
return kh_value(t->tbl_str, k);
|
||||
|
||||
} else {
|
||||
TABLE_HASH hash = value_hash(key);
|
||||
khiter_t k = kh_get(TABLE_INT, t->tbl_int, hash);
|
||||
if (k == kh_end(t->tbl_int))
|
||||
__builtin_unreachable();
|
||||
return kh_value(t->tbl_int, k);
|
||||
}
|
||||
}
|
||||
|
||||
void table_set(Table* t, VALUE key, VALUE value)
|
||||
{
|
||||
/*
|
||||
int ret;
|
||||
|
||||
if (value_type(key) == TT_STRING) {
|
||||
khiter_t k = kh_get(TABLE_STR, t->tbl_str, value_);
|
||||
const char* skey;
|
||||
if (heap_get_string(t->heap, value_idx(key), &skey) != T_OK)
|
||||
abort();
|
||||
khiter_t k = kh_put(TABLE_STR, t->tbl_str, skey, &ret);
|
||||
if (ret < 0)
|
||||
out_of_memory();
|
||||
kh_value(t->tbl_str, k) = value;
|
||||
|
||||
} else {
|
||||
TABLE_HASH hash = value_hash(key);
|
||||
khiter_t k = kh_put(TABLE_INT, t->tbl_int, hash, &ret);
|
||||
if (ret < 0)
|
||||
out_of_memory();
|
||||
kh_value(t->tbl_int, k) = value;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user