core: Add hash_clear!.

* src/hash.c (hash_clear_x): New builtin.
* include/mes/builtins.h: Declare it.
* src/builtins.c (mes_builtins): Register it.
* tests/hash.test: Add a test.

Co-authored-by: Timothy Sample <samplet@ngyro.com>
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2019-11-13 08:27:44 +01:00 committed by Timothy Sample
parent 858daa625d
commit 64d6dff810
4 changed files with 20 additions and 0 deletions

View File

@ -73,6 +73,7 @@ struct scm *hash_table_printer (struct scm *table);
struct scm *make_hash_table (struct scm *x);
struct scm *hash_buckets (struct scm *table);
struct scm *hash_table_p (struct scm *x);
struct scm *hash_clear_x (struct scm *table);
/* src/lib.c */
struct scm *type_ (struct scm *x);
struct scm *car_ (struct scm *x);

View File

@ -183,6 +183,7 @@ mes_builtins (struct scm *a) /*:((internal)) */
a = init_builtin (builtin_type, "hash-table?", 1, &hash_table_p, a);
a = init_builtin (builtin_type, "make-hash-table", -1, &make_hash_table, a);
a = init_builtin (builtin_type, "hash-buckets", 1, &hash_buckets, a);
a = init_builtin (builtin_type, "hash-clear!", 1, &hash_clear_x, a);
/* src/lib.c */
a = init_builtin (builtin_type, "core:type", 1, &type_, a);
a = init_builtin (builtin_type, "core:car", 1, &car_, a);

View File

@ -231,3 +231,13 @@ hash_buckets (struct scm *table)
{
return struct_ref_ (table, 4);
}
struct scm *
hash_clear_x (struct scm *table)
{
struct scm *s = struct_ref_ (table, 3);
long size = s->value;
struct scm *buckets = make_vector_ (size, cell_unspecified);
struct_set_x_ (table, 4, buckets);
return cell_unspecified;
}

View File

@ -90,4 +90,12 @@ exec ${MES-bin/mes} --no-auto-compile -L ${0%/*} -L module -C module -e '(tests
(pass-if "hash-table?"
(hash-table? (make-hash-table)))
(pass-if "hash-clear!"
(let ((t (make-hash-table)))
(hashq-set! t 'k1 'v1)
(hashq-set! t 'k2 'v2)
(hash-clear! t)
(not (or (hashq-ref t 'k1)
(hashq-ref t 'k2)))))
(result 'report)