core: Add hash-map->list.

* src/hash.c (hash_map_to_list): New function.
* src/builtins.c (mes_builtins): Update.
* include/mes/builtins.h: Update.
This commit is contained in:
Jan Nieuwenhuizen 2019-11-10 19:51:59 +01:00 committed by Jan (janneke) Nieuwenhuizen
parent 34b4c3b1d2
commit 41ef524cfc
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
3 changed files with 28 additions and 0 deletions

View File

@ -71,6 +71,7 @@ struct scm *hashq_set_x (struct scm *table, struct scm *key, struct scm *value);
struct scm *hash_set_x (struct scm *table, struct scm *key, struct scm *value);
struct scm *hash_table_printer (struct scm *table);
struct scm *make_hash_table (struct scm *x);
struct scm *hash_map_to_list (struct scm *proc, struct scm *table);
/* src/lib.c */
struct scm *type_ (struct scm *x);
struct scm *car_ (struct scm *x);

View File

@ -181,6 +181,7 @@ mes_builtins (struct scm *a) /*:((internal)) */
a = init_builtin (builtin_type, "hash-set!", 3, &hash_set_x, a);
a = init_builtin (builtin_type, "hash-table-printer", 1, &hash_table_printer, a);
a = init_builtin (builtin_type, "make-hash-table", -1, &make_hash_table, a);
a = init_builtin (builtin_type, "hash-map->list", 2, &hash_map_to_list, 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

@ -216,3 +216,29 @@ make_hash_table (struct scm *x) /*:((arity . n)) */
}
return make_hash_table_ (size);
}
struct scm *
hash_map_to_list (struct scm *proc, struct scm *table)
{
struct scm *lst = cell_nil;
struct scm *buckets = struct_ref_ (table, 4);
int i;
struct scm *e;
struct scm *a;
struct scm *x;
for (i = 0; i < buckets->length; i = i + 1)
{
e = vector_ref_ (buckets, i);
if (e != cell_unspecified)
{
while (e->type == TPAIR)
{
a = e->car;
x = cons (a->car, cons (a->cdr, cell_nil));
lst = cons (apply (proc, x, cell_nil), lst);
e = e->cdr;
}
}
}
return lst;
}