summaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-06-11 19:27:18 -0700
committerKaz Kylheku <kaz@kylheku.com>2019-06-11 19:27:18 -0700
commit2ec0f6e63f9c42750f09af4a7f03be832a2ca28a (patch)
tree8b5307e3a3a2d0eb800adb2a45bb6f6c1885a690 /hash.c
parent04c2f66cae1015899cb690189aaa4162330737f2 (diff)
downloadtxr-2ec0f6e63f9c42750f09af4a7f03be832a2ca28a.tar.gz
txr-2ec0f6e63f9c42750f09af4a7f03be832a2ca28a.tar.bz2
txr-2ec0f6e63f9c42750f09af4a7f03be832a2ca28a.zip
New function: hash-peek.
* hash.c (hash_peek): New function. (hash_init): hash-peek intrinsic registered. * hash.h (hash_peek): Declared. * txr.1: Documented.
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index fcabb5bd..0d8aaef9 100644
--- a/hash.c
+++ b/hash.c
@@ -973,6 +973,31 @@ val hash_next(val iter)
return us_car(hi->cons);
}
+val hash_peek(val iter)
+{
+ val self = lit("hash-peek");
+ struct hash_iter *hi = coerce(struct hash_iter *,
+ cobj_handle(self, iter, hash_iter_s));
+ val hash = hi->hash;
+ struct hash *h = hash ? coerce(struct hash *, hash->co.handle) : 0;
+ cnum chain = hi->chain;
+ val cell = hi->cons;
+
+ if (!h)
+ return nil;
+ if (cell) {
+ val peek = us_cdr(cell);
+ if (peek)
+ return us_car(peek);
+ }
+ do {
+ if (++chain >= h->modulus)
+ return nil;
+ cell = vecref(h->table, num_fast(chain));
+ } while (!cell);
+ return us_car(cell);
+}
+
val maphash(val fun, val hash)
{
val iter = hash_begin(hash);
@@ -1651,6 +1676,7 @@ void hash_init(void)
reg_fun(intern(lit("hash-revget"), user_package), func_n4o(hash_revget, 2));
reg_fun(intern(lit("hash-begin"), user_package), func_n1(hash_begin));
reg_fun(intern(lit("hash-next"), user_package), func_n1(hash_next));
+ reg_fun(intern(lit("hash-peek"), user_package), func_n1(hash_peek));
reg_fun(intern(lit("set-hash-str-limit"), system_package),
func_n1(set_hash_str_limit));
reg_fun(intern(lit("set-hash-rec-limit"), system_package),