From 79d8c4cab327a7cd5879a0508f6498d6d6533399 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 23 Nov 2016 20:45:54 -0800 Subject: Adding clearhash function. This is needed for an upcoming bugfix, so why not expose it as an intrinsic. * hash.c (clearhash): New function. (hash_init): clearhash intrinsic registered. * hash.h (clearhash): Declared. * txr.1: Documented clearhash. --- hash.c | 13 +++++++++++++ hash.h | 1 + txr.1 | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/hash.c b/hash.c index b2bbe0ec..a786a9c6 100644 --- a/hash.c +++ b/hash.c @@ -754,6 +754,18 @@ val remhash(val hash, val key) return nil; } +val clearhash(val hash) +{ + struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); + val mod = num_fast(256); + val table = vector(mod, nil); + cnum oldcount = h->count; + h->modulus = c_num(mod); + h->count = 0; + h->table = table; + return oldcount ? num(oldcount) : nil; +} + val hash_count(val hash) { struct hash *h = coerce(struct hash *, cobj_handle(hash, hash_s)); @@ -1374,6 +1386,7 @@ void hash_init(void) reg_fun(intern(lit("sethash"), user_package), func_n3(sethash)); reg_fun(intern(lit("pushhash"), user_package), func_n3(pushhash)); reg_fun(intern(lit("remhash"), user_package), func_n2(remhash)); + reg_fun(intern(lit("clearhash"), user_package), func_n1(clearhash)); reg_fun(intern(lit("hash-count"), user_package), func_n1(hash_count)); reg_fun(intern(lit("get-hash-userdata"), user_package), ghu); reg_fun(intern(lit("hash-userdata"), user_package), ghu); diff --git a/hash.h b/hash.h index a0c40797..0f0db6df 100644 --- a/hash.h +++ b/hash.h @@ -39,6 +39,7 @@ val gethash_f(val hash, val key, loc found); val sethash(val hash, val key, val value); val pushhash(val hash, val key, val value); val remhash(val hash, val key); +val clearhash(val hash); val hash_count(val hash); val get_hash_userdata(val hash); val set_hash_userdata(val hash, val data); diff --git a/txr.1 b/txr.1 index 2effcf39..9376d51a 100644 --- a/txr.1 +++ b/txr.1 @@ -34998,6 +34998,27 @@ is returned. Otherwise .code nil is returned. +.coNP Function @ clearhash +.synb +.mets (clearhash << hash ) +.syne +.desc +The +.code clearhash +function removes all keys-value pairs from +.metn hash , +causing it to be empty. + +If +.meta hash +is already empty prior to the operation, then +.codn nil , +is returned. + +Otherwise an integer is returned indicating the number of entries +that were purged from +.metn hash . + .coNP Function @ hash-count .synb .mets (hash-count << hash ) -- cgit v1.2.3