diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-08-04 08:37:51 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-08-04 08:37:51 -0700 |
commit | 4ecca7c0b2ac3d61658d749f51dc1e7fbc408ed8 (patch) | |
tree | 7d53e099bd63c1546eb91bb41eac2700b2ada339 | |
parent | 7b226277f9ab1d27c657e80e4a44c43c0bedf002 (diff) | |
download | txr-4ecca7c0b2ac3d61658d749f51dc1e7fbc408ed8.tar.gz txr-4ecca7c0b2ac3d61658d749f51dc1e7fbc408ed8.tar.bz2 txr-4ecca7c0b2ac3d61658d749f51dc1e7fbc408ed8.zip |
* hash.c (hash_revget): New function.
* hash.h (hash_revget): Declared.
* eval.c (eval_init): Registered hash-revget intrinsic.
* txr.1: Documented hash-revget.
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | eval.c | 1 | ||||
-rw-r--r-- | hash.c | 16 | ||||
-rw-r--r-- | hash.h | 1 | ||||
-rw-r--r-- | txr.1 | 44 |
5 files changed, 72 insertions, 0 deletions
@@ -1,5 +1,15 @@ 2015-08-04 Kaz Kylheku <kaz@kylheku.com> + * hash.c (hash_revget): New function. + + * hash.h (hash_revget): Declared. + + * eval.c (eval_init): Registered hash-revget intrinsic. + + * txr.1: Documented hash-revget. + +2015-08-04 Kaz Kylheku <kaz@kylheku.com> + * stream.c (indent_mode_put_string): Function removed, logic hoisted into put_string. (put_string, put_char): Always count column, indent mode or not. @@ -4373,6 +4373,7 @@ void eval_init(void) reg_fun(intern(lit("hash-update"), user_package), func_n2(hash_update)); reg_fun(intern(lit("hash-update-1"), user_package), func_n4o(hash_update_1, 3)); + reg_fun(intern(lit("hash-revget"), user_package), func_n4o(hash_revget, 2)); reg_fun(intern(lit("eval"), user_package), func_n2o(eval_intrinsic, 1)); reg_fun(intern(lit("lisp-parse"), user_package), func_n4o(lisp_parse, 0)); @@ -1149,6 +1149,22 @@ val hash_update_1(val hash, val key, val fun, val init) } } +val hash_revget(val hash, val value, val test, val keyfun) +{ + val iter = hash_begin(hash); + val cell; + + test = default_arg(test, eql_f); + keyfun = default_arg(keyfun, identity_f); + + while ((cell = hash_next(iter)) != nil) { + if (funcall2(test, value, funcall1(keyfun, cdr(cell)))) + return car(cell); + } + + return nil; +} + void hash_init(void) { weak_keys_k = intern(lit("weak-keys"), keyword_package); @@ -62,6 +62,7 @@ val hash_subset(val hash1, val hash2); val hash_proper_subset(val hash1, val hash2); val hash_update(val hash, val fun); val hash_update_1(val hash, val key, val fun, val init); +val hash_revget(val hash, val value, val test, val keyfun); void hash_process_weak(void); @@ -25301,6 +25301,50 @@ to The function returns .codn nil . +.coNP Function @ hash-revget +.synb +.mets (hash-revget < hash < value >> [ testfun <> [ keyfun ]]) +.syne +.desc +The +.code hash-revget +function performs a reverse lookup on +.metn hash . + +It searches through the entries stored in +.meta hash +for an entry whose value matches +.metn value . + +If such an entry is found, that entry's key is returned. +Otherwise +.code nil +is returned. + +If multiple matching entries exist, it is not specified which entry's +key is returned. + +The +.meta keyfun +function is applied to each value in +.meta hash +and the resulting value is compared with +.metn value . +The default +.meta keyfun +is the +.code identity +function. + +The comparison is performed using +.metn testfun . + +The default +.meta testfun +is the +.code eql +function. + .coNP Functions @ hash-eql and @ hash-equal .synb .mets (hash-eql << object ) |