summaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-06-28 20:35:28 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-06-28 20:35:28 -0700
commit8ee92a458a4b854b4a7e726975c1197be877c500 (patch)
treee864efd0fb99485c6c0891098854f8d64fc35603 /hash.c
parent59478a2944d8f9a06a8481bd2f746ffd7dd764c7 (diff)
downloadtxr-8ee92a458a4b854b4a7e726975c1197be877c500.tar.gz
txr-8ee92a458a4b854b4a7e726975c1197be877c500.tar.bz2
txr-8ee92a458a4b854b4a7e726975c1197be877c500.zip
New function: hash-map.
hash-map converts a function mapping over a sequence into a hash table. * hash.[ch] (hash_map): New function. * tests/010/hash.tl: Test case. * genman.txr: The hash-map identifier introduces a hash collision. We have to deal with that somehow now. (colli): We put the conflicting entries into a new hash called colli which maps them to an increment value. (hash-title): Increment the hash code h by the amount indicated in colli, if the title is found there. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index 432e8468..8932a856 100644
--- a/hash.c
+++ b/hash.c
@@ -1694,6 +1694,19 @@ val hash_from_alist_v(val alist, struct args *hashv_args)
return hash;
}
+val hash_map(val fun, val seq, struct args *hashv_args)
+{
+ val self = lit("hash-map");
+ seq_iter_t iter;
+ val hash = hashv(hashv_args), elem;
+ seq_iter_init(self, &iter, seq);
+
+ while (seq_get(&iter, &elem))
+ sethash(hash, elem, funcall1(fun, elem));
+
+ return hash;
+}
+
val hash_props(struct args *plist)
{
val self = lit("hash-props");
@@ -2222,6 +2235,7 @@ void hash_init(void)
reg_fun(hash_construct_s, func_n2(hash_construct));
reg_fun(intern(lit("hash-from-pairs"), user_package), func_n1v(hash_from_pairs_v));
reg_fun(intern(lit("hash-from-alist"), user_package), func_n1v(hash_from_alist_v));
+ reg_fun(intern(lit("hash-map"), user_package), func_n2v(hash_map));
reg_fun(intern(lit("hash-props"), user_package), func_n0v(hash_props));
reg_fun(intern(lit("hash-list"), user_package), func_n1v(hash_list));
reg_fun(intern(lit("hash-zip"), user_package), func_n2v(hash_zip));