From 2cd96058eb14978141ec0c456c016ab5d5973322 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Wed, 14 Feb 2018 13:00:43 -0500 Subject: [PATCH] Add fast assoc by cdr --- include/cyclone/runtime.h | 1 + runtime.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/cyclone/runtime.h b/include/cyclone/runtime.h index d2b89ee3..58343f1b 100644 --- a/include/cyclone/runtime.h +++ b/include/cyclone/runtime.h @@ -159,6 +159,7 @@ object memberp(void *data, object x, list l); object memqp(void *data, object x, list l); list assq(void *data, object x, list l); list assoc(void *data, object x, list l); +list assoc_cdr(void *data, object x, list l); /**@}*/ /** diff --git a/runtime.c b/runtime.c index c037be3b..826775b3 100644 --- a/runtime.c +++ b/runtime.c @@ -1174,6 +1174,22 @@ list assoc(void *data, object x, list l) } return boolean_f; } + +/** + * Same as assoc but check the cdr of each item for equality + */ +list assoc_cdr(void *data, object x, list l) +{ + if ((l == NULL) || is_value_type(l) || type_of(l) != pair_tag) + return boolean_f; + for (; (l != NULL); l = cdr(l)) { + list la = car(l); + Cyc_check_pair(data, la); + if (boolean_f != equalp(x, cdr(la))) + return la; + } + return boolean_f; +} /* END member and assoc */ // Internal function, do not use this anywhere outside the runtime