Change table removal function to use values instead of references

This commit is contained in:
Bruce Hill 2024-08-08 13:20:38 -04:00
parent c045c54309
commit f78cd6357a
3 changed files with 8 additions and 2 deletions

View File

@ -38,6 +38,7 @@ void Table$set(table_t *t, const void *key, const void *value, const TypeInfo *t
Table$set(t, &k, &v, type); })
#define Table$reserve_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$reserve(t, &k, NULL, type); })
void Table$remove(table_t *t, const void *key, const TypeInfo *type);
#define Table$remove_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$remove(t, &k, type); })
void Table$clear(table_t *t);
table_t Table$sorted(table_t t, const TypeInfo *type);
void Table$mark_copy_on_write(table_t *t);

View File

@ -1976,8 +1976,8 @@ CORD compile(env_t *env, ast_t *ast)
compile_type_info(env, self_value_t), ")");
} else if (streq(call->name, "remove")) {
CORD self = compile_to_pointer_depth(env, call->self, 1, false);
arg_t *arg_spec = new(arg_t, .name="key", .type=Type(PointerType, .pointed=table->key_type, .is_stack=true, .is_readonly=true));
return CORD_all("Table$remove(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
arg_t *arg_spec = new(arg_t, .name="key", .type=table->key_type);
return CORD_all("Table$remove_value(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
compile_type_info(env, self_value_t), ")");
} else if (streq(call->name, "clear")) {
CORD self = compile_to_pointer_depth(env, call->self, 1, false);

View File

@ -56,3 +56,8 @@ func main():
= {1:10, 3:30, 5:50}
>> {x:10*x for x in y if x > 1 for y in [3, 4, 5] if y < 5}
= {2:20, 3:30, 4:40}
>> t3 := {1:10, 2:20, 3:30}
>> t3:remove(3)
>> t3
= {1:10, 2:20}