From beec4b47ad9d02a2ca44258d9c508f78b0c0af68 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 10 Jul 2023 23:51:52 -0700 Subject: unique: use sequence iteration * lib.c (unique): Use seq_iter_t rather than dividing into list and vector cases. --- lib.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/lib.c b/lib.c index ec62119c..8ba7cd3c 100644 --- a/lib.c +++ b/lib.c @@ -11339,31 +11339,19 @@ val unique(val seq, val keyfun, struct args *hashv_args) val self = lit("unique"); val hash = hashv(hashv_args); val kf = default_arg(keyfun, identity_f); - + seq_iter_t iter; + val elem; list_collect_decl (out, ptail); - if (vectorp(seq) || stringp(seq)) { - cnum i, len; - - for (i = 0, len = c_fixnum(length(seq), self); i < len; i++) { - val new_p; - val v = ref(seq, num_fast(i)); - - (void) gethash_c(self, hash, funcall1(kf, v), mkcloc(new_p)); + seq_iter_init(self, &iter, seq); - if (new_p) - ptail = list_collect(ptail, v); - } - } else { - for (; seq; seq = cdr(seq)) { - val new_p; - val v = car(seq); + while (seq_get(&iter, &elem)) { + val new_p; - (void) gethash_c(self, hash, funcall1(kf, v), mkcloc(new_p)); + (void) gethash_c(self, hash, funcall1(kf, elem), mkcloc(new_p)); - if (new_p) - ptail = list_collect(ptail, v); - } + if (new_p) + ptail = list_collect(ptail, elem); } return make_like(out, seq); -- cgit v1.2.3