diff options
Diffstat (limited to 'libguile/hashtab.c')
-rw-r--r-- | libguile/hashtab.c | 178 |
1 files changed, 88 insertions, 90 deletions
diff --git a/libguile/hashtab.c b/libguile/hashtab.c index 059be6f11..9cb75f234 100644 --- a/libguile/hashtab.c +++ b/libguile/hashtab.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -1067,7 +1067,93 @@ SCM_DEFINE (scm_hashx_remove_x, "hashx-remove!", 4, 0, 0, /* Hash table iterators */ -static const char s_scm_hash_fold[]; +SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0, + (SCM proc, SCM init, SCM table), + "An iterator over hash-table elements.\n" + "Accumulates and returns a result by applying PROC successively.\n" + "The arguments to PROC are \"(key value prior-result)\" where key\n" + "and value are successive pairs from the hash table TABLE, and\n" + "prior-result is either INIT (for the first application of PROC)\n" + "or the return value of the previous application of PROC.\n" + "For example, @code{(hash-fold acons '() tab)} will convert a hash\n" + "table into an a-list of key-value pairs.") +#define FUNC_NAME s_scm_hash_fold +{ + SCM_VALIDATE_PROC (1, proc); + if (!SCM_HASHTABLE_P (table)) + SCM_VALIDATE_VECTOR (3, table); + return scm_internal_hash_fold ((scm_t_hash_fold_fn) scm_call_3, + (void *) SCM_UNPACK (proc), init, table); +} +#undef FUNC_NAME + +static SCM +for_each_proc (void *proc, SCM handle) +{ + return scm_call_2 (SCM_PACK (proc), SCM_CAR (handle), SCM_CDR (handle)); +} + +SCM_DEFINE (scm_hash_for_each, "hash-for-each", 2, 0, 0, + (SCM proc, SCM table), + "An iterator over hash-table elements.\n" + "Applies PROC successively on all hash table items.\n" + "The arguments to PROC are \"(key value)\" where key\n" + "and value are successive pairs from the hash table TABLE.") +#define FUNC_NAME s_scm_hash_for_each +{ + SCM_VALIDATE_PROC (1, proc); + if (!SCM_HASHTABLE_P (table)) + SCM_VALIDATE_VECTOR (2, table); + + scm_internal_hash_for_each_handle (for_each_proc, + (void *) SCM_UNPACK (proc), + table); + return SCM_UNSPECIFIED; +} +#undef FUNC_NAME + +SCM_DEFINE (scm_hash_for_each_handle, "hash-for-each-handle", 2, 0, 0, + (SCM proc, SCM table), + "An iterator over hash-table elements.\n" + "Applies PROC successively on all hash table handles.") +#define FUNC_NAME s_scm_hash_for_each_handle +{ + SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, 1, FUNC_NAME); + if (!SCM_HASHTABLE_P (table)) + SCM_VALIDATE_VECTOR (2, table); + + scm_internal_hash_for_each_handle ((scm_t_hash_handle_fn) scm_call_1, + (void *) SCM_UNPACK (proc), + table); + return SCM_UNSPECIFIED; +} +#undef FUNC_NAME + +static SCM +map_proc (void *proc, SCM key, SCM data, SCM value) +{ + return scm_cons (scm_call_2 (SCM_PACK (proc), key, data), value); +} + +SCM_DEFINE (scm_hash_map_to_list, "hash-map->list", 2, 0, 0, + (SCM proc, SCM table), + "An iterator over hash-table elements.\n" + "Accumulates and returns as a list the results of applying PROC successively.\n" + "The arguments to PROC are \"(key value)\" where key\n" + "and value are successive pairs from the hash table TABLE.") +#define FUNC_NAME s_scm_hash_map_to_list +{ + SCM_VALIDATE_PROC (1, proc); + if (!SCM_HASHTABLE_P (table)) + SCM_VALIDATE_VECTOR (2, table); + return scm_internal_hash_fold (map_proc, + (void *) SCM_UNPACK (proc), + SCM_EOL, + table); +} +#undef FUNC_NAME + + SCM scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure, @@ -1132,8 +1218,6 @@ scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure, scm_internal_hash_fold_handles, but we don't want to promote such an API. */ -static const char s_scm_hash_for_each[]; - void scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn, void *closure, SCM table) @@ -1163,92 +1247,6 @@ scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn, void *closure, } } -SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0, - (SCM proc, SCM init, SCM table), - "An iterator over hash-table elements.\n" - "Accumulates and returns a result by applying PROC successively.\n" - "The arguments to PROC are \"(key value prior-result)\" where key\n" - "and value are successive pairs from the hash table TABLE, and\n" - "prior-result is either INIT (for the first application of PROC)\n" - "or the return value of the previous application of PROC.\n" - "For example, @code{(hash-fold acons '() tab)} will convert a hash\n" - "table into an a-list of key-value pairs.") -#define FUNC_NAME s_scm_hash_fold -{ - SCM_VALIDATE_PROC (1, proc); - if (!SCM_HASHTABLE_P (table)) - SCM_VALIDATE_VECTOR (3, table); - return scm_internal_hash_fold ((scm_t_hash_fold_fn) scm_call_3, - (void *) SCM_UNPACK (proc), init, table); -} -#undef FUNC_NAME - -static SCM -for_each_proc (void *proc, SCM handle) -{ - return scm_call_2 (SCM_PACK (proc), SCM_CAR (handle), SCM_CDR (handle)); -} - -SCM_DEFINE (scm_hash_for_each, "hash-for-each", 2, 0, 0, - (SCM proc, SCM table), - "An iterator over hash-table elements.\n" - "Applies PROC successively on all hash table items.\n" - "The arguments to PROC are \"(key value)\" where key\n" - "and value are successive pairs from the hash table TABLE.") -#define FUNC_NAME s_scm_hash_for_each -{ - SCM_VALIDATE_PROC (1, proc); - if (!SCM_HASHTABLE_P (table)) - SCM_VALIDATE_VECTOR (2, table); - - scm_internal_hash_for_each_handle (for_each_proc, - (void *) SCM_UNPACK (proc), - table); - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - -SCM_DEFINE (scm_hash_for_each_handle, "hash-for-each-handle", 2, 0, 0, - (SCM proc, SCM table), - "An iterator over hash-table elements.\n" - "Applies PROC successively on all hash table handles.") -#define FUNC_NAME s_scm_hash_for_each_handle -{ - SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, 1, FUNC_NAME); - if (!SCM_HASHTABLE_P (table)) - SCM_VALIDATE_VECTOR (2, table); - - scm_internal_hash_for_each_handle ((scm_t_hash_handle_fn) scm_call_1, - (void *) SCM_UNPACK (proc), - table); - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - -static SCM -map_proc (void *proc, SCM key, SCM data, SCM value) -{ - return scm_cons (scm_call_2 (SCM_PACK (proc), key, data), value); -} - -SCM_DEFINE (scm_hash_map_to_list, "hash-map->list", 2, 0, 0, - (SCM proc, SCM table), - "An iterator over hash-table elements.\n" - "Accumulates and returns as a list the results of applying PROC successively.\n" - "The arguments to PROC are \"(key value)\" where key\n" - "and value are successive pairs from the hash table TABLE.") -#define FUNC_NAME s_scm_hash_map_to_list -{ - SCM_VALIDATE_PROC (1, proc); - if (!SCM_HASHTABLE_P (table)) - SCM_VALIDATE_VECTOR (2, table); - return scm_internal_hash_fold (map_proc, - (void *) SCM_UNPACK (proc), - SCM_EOL, - table); -} -#undef FUNC_NAME - |