diff options
author | Daniel Hartwig <mandyke@gmail.com> | 2013-02-17 16:38:31 +0800 |
---|---|---|
committer | Daniel Hartwig <mandyke@gmail.com> | 2013-02-18 10:21:43 +0800 |
commit | 3330f00f54649cdd0914b6ff03c7b7bbc38ffa8d (patch) | |
tree | 300aa688b6c2a940e0cfdf05f7829321709288dd /libguile/hashtab.c | |
parent | 3d2b2676e3fc0a5b243b8a4188d07bba1b4b40a4 (diff) | |
download | guile-3330f00f54649cdd0914b6ff03c7b7bbc38ffa8d.tar.gz |
add hash-count for native tables
* libguile/hashtab.c (scm_hash_count): New function. Count the number
of elements in a hash table.
* doc/ref/api-compound.texi (Hash Tables): Update examples and
reference.
* test-suite/tests/hash.test (hash-count): New test.
Diffstat (limited to 'libguile/hashtab.c')
-rw-r--r-- | libguile/hashtab.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/libguile/hashtab.c b/libguile/hashtab.c index 0abc7dc04..88cb199c5 100644 --- a/libguile/hashtab.c +++ b/libguile/hashtab.c @@ -584,6 +584,7 @@ SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0, } #undef FUNC_NAME + /* Accessing hash table entries. */ @@ -1371,6 +1372,33 @@ SCM_DEFINE (scm_hash_map_to_list, "hash-map->list", 2, 0, 0, } #undef FUNC_NAME +static SCM +count_proc (void *pred, SCM key, SCM data, SCM value) +{ + if (scm_is_false (scm_call_2 (SCM_PACK (pred), key, data))) + return value; + else + return scm_oneplus(value); +} + +SCM_DEFINE (scm_hash_count, "hash-count", 2, 0, 0, + (SCM pred, SCM table), + "Return the number of elements in the given hash TABLE that\n" + "cause `(PRED KEY VALUE)' to return true. To quickly determine\n" + "the total number of elements, use `(const #t)' for PRED.") +#define FUNC_NAME s_scm_hash_count +{ + SCM init; + + SCM_VALIDATE_PROC (1, pred); + SCM_VALIDATE_HASHTABLE (2, table); + + init = scm_from_int (0); + return scm_internal_hash_fold ((scm_t_hash_fold_fn) count_proc, + (void *) SCM_UNPACK (pred), init, table); +} +#undef FUNC_NAME + SCM |