From 0d77236aa87a69d31a9aaf697473f42ad3f0805f Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 10 Nov 2019 19:51:59 +0100 Subject: [PATCH] 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. --- include/mes/builtins.h | 1 + src/builtins.c | 1 + src/hash.c | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/include/mes/builtins.h b/include/mes/builtins.h index 37da93c0..41fa68ee 100644 --- a/include/mes/builtins.h +++ b/include/mes/builtins.h @@ -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); diff --git a/src/builtins.c b/src/builtins.c index b9fdcc98..e7c679d6 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -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); diff --git a/src/hash.c b/src/hash.c index ee574c03..a7977ec6 100644 --- a/src/hash.c +++ b/src/hash.c @@ -216,3 +216,26 @@ 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; + for (i = 0; i < buckets->length; i = i + 1) + { + struct scm *e = vector_ref_ (buckets, i); + if (e != cell_unspecified) + { + while (e->type == TPAIR) + { + struct scm *a = e->car; + struct scm *x = cons (a->car, cons (a->cdr, cell_nil)); + lst = cons (apply (proc, x, cell_nil), lst); + e = e->cdr; + } + } + } + return lst; +}