diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2024-03-07 20:18:18 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2024-03-07 20:18:18 -0800 |
commit | cb78d7e273406c4041470efbb2b0a450c003fca0 (patch) | |
tree | a2daa69fbef6706c9641ab452d390c68a04cbb4b /lib.c | |
parent | 80c985bc48a448e53ee5ef9c5e4910f598f5af7e (diff) | |
download | txr-cb78d7e273406c4041470efbb2b0a450c003fca0.tar.gz txr-cb78d7e273406c4041470efbb2b0a450c003fca0.tar.bz2 txr-cb78d7e273406c4041470efbb2b0a450c003fca0.zip |
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.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 62 |
1 files changed, 21 insertions, 41 deletions
@@ -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) |