>From 167e82e93ee59ac9a0244006ae9664d68877b4c8 Mon Sep 17 00:00:00 2001 From: Nala Ginrut Date: Tue, 26 Mar 2013 16:00:54 +0800 Subject: [PATCH] Add native hashtable helper functions. libguile/hashtab.c: Add hash-items and hash-size libguile/hashtab.h module/ice-9/boot-9.scm: Add hash-keys --- libguile/hashtab.c | 19 +++++++++++++++++++ libguile/hashtab.h | 2 ++ module/ice-9/boot-9.scm | 8 ++++++++ 3 files changed, 29 insertions(+) diff --git a/libguile/hashtab.c b/libguile/hashtab.c index 88cb199..b1e59b8 100644 --- a/libguile/hashtab.c +++ b/libguile/hashtab.c @@ -383,6 +383,25 @@ scm_i_rehash (SCM table, } } +SCM_DEFINE (scm_hash_items, "hash-items", 1, 0, 0, + (SCM table), + "Get the amount of items in this hash table.") +#define FUNC_NAME s_scm_hash_items +{ + SCM_VALIDATE_HASHTABLE (1, table); + return scm_from_uint (SCM_HASHTABLE_N_ITEMS (table)); +} +#undef FUNC_NAME + +SCM_DEFINE (scm_hash_size, "hash-size", 1, 0, 0, + (SCM table), + "Get the size of this hash table.") +#define FUNC_NAME s_scm_hash_size +{ + SCM_VALIDATE_HASHTABLE (1, table); + return scm_from_uint (SCM_SIMPLE_VECTOR_LENGTH (SCM_HASHTABLE_VECTOR (table))); +} +#undef FUNC_NAME void scm_i_hashtable_print (SCM exp, SCM port, scm_print_state *pstate) diff --git a/libguile/hashtab.h b/libguile/hashtab.h index dcebcb8..227299e 100644 --- a/libguile/hashtab.h +++ b/libguile/hashtab.h @@ -96,6 +96,8 @@ typedef struct scm_t_hashtable { SCM_API SCM scm_vector_to_hash_table (SCM vector); SCM_API SCM scm_c_make_hash_table (unsigned long k); +SCM_API SCM scm_hash_table_size (SCM h); +SCM_API SCM scm_hash_table_items (SCM h); SCM_API SCM scm_make_hash_table (SCM n); SCM_API SCM scm_make_weak_key_hash_table (SCM k); SCM_API SCM scm_make_weak_value_hash_table (SCM k); diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm index ced3a28..ae1c49b 100644 --- a/module/ice-9/boot-9.scm +++ b/module/ice-9/boot-9.scm @@ -34,6 +34,14 @@ +;; Native hash-table helper functions. + +(define (hash-keys table) + "Return all the keys from hash table." + (hash-map->list (lambda (x y) x) table)) + + + ;; Before compiling, make sure any symbols are resolved in the (guile) ;; module, the primary location of those symbols, rather than in ;; (guile-user), the default module that we compile in. -- 1.7.10.4