From c9be5f66a481bd040b36a25314f6589dd939daa5 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 12 Mar 2023 13:30:15 +0100 Subject: Safer state machine in coroutine.h (internal). Removed c_forwhile() macro. Redundant, use c_forfilter(). Removed find and eq in cspan (use general c_find_if() instead for search). --- include/stc/algo/coroutine.h | 9 +++++++-- include/stc/algo/crange.h | 6 +++--- include/stc/algo/filter.h | 13 ++++++++----- include/stc/ccommon.h | 5 ----- include/stc/cspan.h | 21 +++------------------ include/stc/priv/altnames.h | 1 - 6 files changed, 21 insertions(+), 34 deletions(-) (limited to 'include') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 1c849d49..1dd82720 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -68,12 +68,11 @@ enum { #define cco_begin(ctx) \ int *_state = &(ctx)->cco_state; \ switch (*_state) { \ - case cco_state_done: \ case 0: #define cco_end(retval) \ *_state = cco_state_done; break; \ - default: goto _cco_final_; /* never happens */ \ + default: assert(0); goto _cco_final_; \ } \ return retval @@ -109,4 +108,10 @@ enum { if (*_state > 0) *_state = cco_state_final; \ } while (0) +#define cco_reset(ctx) \ + do { \ + int* _state = &(ctx)->cco_state; \ + if (*_state == cco_state_final) *_state = 0; \ + } while (0) + #endif diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h index 4fc957b6..ca06c258 100644 --- a/include/stc/algo/crange.h +++ b/include/stc/algo/crange.h @@ -34,9 +34,9 @@ int main() // use a temporary crange object. int a = 100, b = INT32_MAX; - c_forfilter (i, crange, crange_obj(a, b, 8) - , i.index > 10 - && c_flt_take(i, 3)) + c_forfilter (i, crange, crange_obj(a, b, 8), + c_flt_skip(i, 10) && + c_flt_take(i, 3)) printf(" %lld", *i.ref); puts(""); } diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h index a5e11b64..5e3125b1 100644 --- a/include/stc/algo/filter.h +++ b/include/stc/algo/filter.h @@ -35,10 +35,10 @@ int main() printf(" %d", *i.ref); puts(""); - c_forfilter (i, cstack_int, stk - , c_flt_skipwhile(i, *i.ref < 3) - && (*i.ref & 1) == 0 // even only - && c_flt_take(i, 2)) // break after 2 + c_forfilter (i, cstack_int, stk, + c_flt_skipwhile(i, *i.ref < 3) && + (*i.ref & 1) == 0 && // even only + c_flt_take(i, 2)) // break after 2 printf(" %d", *i.ref); puts(""); } @@ -57,8 +57,11 @@ int main() #define c_flt_last(i) (i).b.s1[(i).b.s1top - 1] #define c_forfilter(i, C, cnt, filter) \ + c_forfilter_it(i, C, C##_begin(&cnt), filter) + +#define c_forfilter_it(i, C, start, filter) \ for (struct {struct _flt_base b; C##_iter it; C##_value *ref;} \ - i = {.it=C##_begin(&cnt), .ref=i.it.ref} ; !i.b.done & (i.it.ref != NULL) ; \ + i = {.it=start, .ref=i.it.ref} ; !i.b.done & (i.it.ref != NULL) ; \ C##_next(&i.it), i.ref = i.it.ref, i.b.s1top=0, i.b.s2top=0) \ if (!(filter)) ; else diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index de230910..33446982 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -178,11 +178,6 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, for (C##_iter it = start, *_endref = (C##_iter*)(finish).ref \ ; it.ref != (C##_value*)_endref; C##_next(&it)) -#define c_forwhile(i, C, cnt, cond) \ - for (struct {C##_iter it; C##_value *ref; intptr_t index;} \ - i = {.it=C##_begin(&cnt), .ref=i.it.ref}; i.it.ref && (cond) \ - ; C##_next(&i.it), i.ref = i.it.ref, ++i.index) - #define c_forpair(key, val, C, cnt) /* structured binding */ \ for (struct {C##_iter it; const C##_key* key; C##_mapped* val;} _ = {.it=C##_begin(&cnt)} \ ; _.it.ref && (_.key = &_.it.ref->first, _.val = &_.it.ref->second) \ diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 00313540..d5482200 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -48,9 +48,9 @@ int demo2() { puts(""); c_forfilter (i, Intspan, span, - , c_flt_skipwhile(i, *i.ref < 25) - && (*i.ref & 1) == 0 // even only - && c_flt_take(i, 2)) // break after 2 + c_flt_skipwhile(i, *i.ref < 25) && + (*i.ref & 1) == 0 && // even only + c_flt_take(i, 2)) // break after 2 printf(" %d", *i.ref); puts(""); } @@ -65,9 +65,6 @@ int demo2() { using_cspan_3(Self, T, 1) #define using_cspan_3(Self, T, RANK) \ - using_cspan_4(Self, T, RANK, c_default_eq) - -#define using_cspan_4(Self, T, RANK, i_eq) \ typedef T Self##_value; typedef T Self##_raw; \ typedef struct { \ Self##_value *data; \ @@ -99,18 +96,6 @@ int demo2() { it->ref += _cspan_next##RANK(RANK, it->pos, it->_s->shape, it->_s->stride.d); \ if (it->pos[0] == it->_s->shape[0]) it->ref = NULL; \ } \ - STC_INLINE bool Self##_eq(const Self* x, const Self* y) { \ - if (memcmp(x->shape, y->shape, sizeof x->shape)) return false; \ - Self##_iter i = Self##_begin(x), j = Self##_begin(y); \ - for (; i.ref; Self##_next(&i), Self##_next(&j)) \ - if (!(i_eq(i.ref, j.ref))) return false; \ - return true; \ - } \ - STC_INLINE Self##_iter Self##_find(const Self* self, Self##_raw raw) { \ - Self##_iter i = Self##_begin(self); \ - for (; i.ref; Self##_next(&i)) if (i_eq(i.ref, &raw)) return i; \ - return i; \ - } \ struct stc_nostruct #define using_cspan2(Self, T) using_cspan_3(Self, T, 1); using_cspan_3(Self##2, T, 2) diff --git a/include/stc/priv/altnames.h b/include/stc/priv/altnames.h index b10c7a11..8fa326f1 100644 --- a/include/stc/priv/altnames.h +++ b/include/stc/priv/altnames.h @@ -23,7 +23,6 @@ #define c_FORLIST c_forlist #define c_FORRANGE c_forrange #define c_FOREACH c_foreach -#define c_FORWHILE c_forwhile #define c_FORPAIR c_forpair #define c_FORFILTER c_forfilter #define c_FORMATCH c_formatch -- cgit v1.2.3