From cb78d7e273406c4041470efbb2b0a450c003fca0 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 7 Mar 2024 20:18:18 -0800 Subject: find: use seq_iter. * lib.c (find): We retain the optimization for strings when the comparison is regular equality (eq, eql, equal). The other cases are handled by seq_iter rather than separate list-like and vector-like cases. --- lib.c | 62 +++++++++++++++++++++----------------------------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/lib.c b/lib.c index 97a36e3e..93f05e93 100644 --- a/lib.c +++ b/lib.c @@ -11677,55 +11677,35 @@ val find(val item, val seq, val testfun_in, val keyfun_in) val keyfun = default_arg(keyfun_in, identity_f); seq_info_t si = seq_info(seq); - switch (si.kind) { - case SEQ_NIL: - return nil; - case SEQ_LISTLIKE: + switch (si.type) { + case STR: + case LIT: + if (keyfun == identity_f && + (testfun == equal_f || testfun == eql_f || testfun == eq_f)) { - gc_hint(seq); + const wchar_t ch = c_chr(item); + const wchar_t *cstr = c_str(seq, self); + if (wcschr(cstr, ch)) + return item; + return nil; + } + /* fallthrough */ + default: + { + val elem; + seq_iter_t it; - for (seq = z(si.obj); seq; seq = cdr(seq)) { - val elem = car(seq); - val key = funcall1(keyfun, elem); + seq_iter_init_with_info(self, &it, si, 0); + while (seq_get(&it, &elem)) { + val key = funcall1(keyfun, elem); if (funcall2(testfun, item, key)) return elem; } } - return nil; - case SEQ_VECLIKE: - switch (si.type) { - case STR: - case LIT: - if (keyfun == identity_f && - (testfun == equal_f || testfun == eql_f || testfun == eq_f)) - { - const wchar_t ch = c_chr(item); - const wchar_t *cstr = c_str(seq, self); - if (wcschr(cstr, ch)) - return item; - return nil; - } - /* fallthrough */ - default: - { - val vec = si.obj; - cnum len = c_fixnum(length(vec), self); - cnum i; - - for (i = 0; i < len; i++) { - val elem = ref(vec, num_fast(i)); - val key = funcall1(keyfun, elem); - if (funcall2(testfun, item, key)) - return elem; - } - } - break; - } - return nil; - default: - unsup_obj(self, seq); + break; } + return nil; } val rfind(val item, val seq, val testfun_in, val keyfun_in) -- cgit v1.2.3