This commit is contained in:
2026-05-13 11:40:19 -05:00
parent aefc2ff9ad
commit bb8d5bfe34
4 changed files with 53 additions and 8 deletions

View File

@@ -66,7 +66,7 @@ static TABLE_HASH value_hash(VALUE v)
return 0;
}
VALUE table_get(Table const* t, VALUE key)
TYC_RESULT table_get(Table const* t, VALUE key, VALUE* value)
{
if (value_type(key) == TT_STRING) {
const char* skey;
@@ -74,16 +74,18 @@ VALUE table_get(Table const* t, VALUE key)
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);
return T_ERR_TABLE_KEY_NOT_FOUND;
*value = 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);
return T_ERR_TABLE_KEY_NOT_FOUND;
*value = kh_value(t->tbl_int, k);
}
return T_OK;
}
void table_set(Table* t, VALUE key, VALUE value)
@@ -107,3 +109,23 @@ void table_set(Table* t, VALUE key, VALUE value)
kh_value(t->tbl_int, k) = value;
}
}
void table_del(Table* 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))
return;
kh_del(TABLE_STR, 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))
return;
kh_del(TABLE_INT, t->tbl_int, k);
}
}