From 5724a053262cc324515206180b6640ecde12addb Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 13 Feb 2019 06:27:33 -0800 Subject: Framework for iterating over sequences. This has been needed for a while. While we have seq_info for classifying sequences to nicely dispatch code into various cases, those cases duplicate code. The code base could benefit from generic traversal. * lib.c (seq_iter_get_nil, seq_iter_get_list, seq_iter_get_vec, set_iter_get_hash): New static functions. (seq_iter_rewind, seq_iter_init): New functions. * lib.h (struct seq_iter, seq_iter_t): New struct type and its typedef name. (seq_iter_init, seq_iter_rewind): Declared. (seq_get): New inline function. --- lib.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib.h') diff --git a/lib.h b/lib.h index aab0083d..6d706b3d 100644 --- a/lib.h +++ b/lib.h @@ -369,6 +369,18 @@ typedef struct seq_info { seq_kind_t kind; } seq_info_t; +typedef struct seq_iter { + seq_info_t inf; + union { + val iter; + cnum index; + } ui; + union { + cnum len; + } ul; + int (*get)(struct seq_iter *, val *pval); +} seq_iter_t; + extern const seq_kind_t seq_kind_tab[MAXTYPE+1]; #define SEQ_KIND_PAIR(A, B) ((A) << 3 | (B)) @@ -517,6 +529,9 @@ val typeof(val obj); val subtypep(val sub, val sup); val typep(val obj, val type); seq_info_t seq_info(val cobj); +void seq_iter_init(val self, seq_iter_t *it, val obj); +void seq_iter_rewind(val self, seq_iter_t *it); +INLINE int seq_get(seq_iter_t *it, val *pval) { return it->get(it, pval); } val throw_mismatch(val self, val obj, type_t); INLINE val type_check(val self, val obj, type_t typecode) { -- cgit v1.2.3