diff options
| author | Tyge Løvset <[email protected]> | 2022-11-02 12:25:32 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-11-02 12:25:32 +0100 |
| commit | 0a9ab178aad191dc3a394e5fa8aca860da9ee9b0 (patch) | |
| tree | 02e2ab718b40b6d2b8d3a11e8d66b6e74837c35c | |
| parent | a5ea027efc8b3d1e43df65dce042e945c2b48a52 (diff) | |
| download | STC-modified-0a9ab178aad191dc3a394e5fa8aca860da9ee9b0.tar.gz STC-modified-0a9ab178aad191dc3a394e5fa8aca860da9ee9b0.zip | |
Change to c_forwhile: takes start iter, not container.
Other minor updates.
| -rw-r--r-- | docs/ccommon_api.md | 10 | ||||
| -rw-r--r-- | examples/forfilter.c | 18 | ||||
| -rw-r--r-- | examples/forloops.c | 2 | ||||
| -rw-r--r-- | examples/list.c | 4 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 86 | ||||
| -rw-r--r-- | include/stc/views.h | 1 |
6 files changed, 59 insertions, 62 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 3a97361e..7721a38d 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -216,11 +216,11 @@ c_forrange (i, 30, 0, -5) printf(" %lld", i); ### c_forwhile, c_forfilter Iterate containers with stop-criteria and chained range filtering. -| Usage | Description | -|:--------------------------------------------------------------|:-------------------------------------| -| `c_forwhile (it, ctype, container, shortcircuit)` | Iterate until shortcircuit is false | -| `c_forfilter (it, ctype, container, filter(s))` | Filter out items in chain | -| `c_forfilter (it, ctype, container, filter(s), shortcircuit)` | Add a "short-circuit" pred/filter | +| Usage | Description | +|:----------------------------------------------------|:---------------------------------------| +| `c_forwhile (it, ctype, start, pred)` | Iterate until pred is false | +| `c_forfilter (it, ctype, container, filter)` | Filter out items in chain with && | +| `c_forfilter (it, ctype, container, filter, pred)` | Filter and iterate until pred is false | | Built-in filter | Description | |:----------------------------------|:-------------------------------------| diff --git a/examples/forfilter.c b/examples/forfilter.c index 9079d88e..3c30b976 100644 --- a/examples/forfilter.c +++ b/examples/forfilter.c @@ -49,6 +49,7 @@ void demo1(void) /* Rust: fn main() { let vector = (1..) // Infinite range of integers + .skip_while(|x| *x != 11) // Skip initial numbers unequal 11 .filter(|x| x % 2 != 0) // Collect odd numbers .take(5) // Only take five numbers .map(|x| x * x) // Square each number @@ -60,15 +61,14 @@ fn main() { void demo2(void) { c_auto (IVec, vector) { - crange rv = crange_make(crange_MAX); - c_forfilter (x, crange, rv - , flt_isOdd(x) - && c_flt_skipwhile(x, *x.ref != 11) - , c_flt_take(x, 5)) - IVec_push(&vector, flt_square(x)); - puts("demo2:"); - c_foreach (i, IVec, vector) printf(" %d", *i.ref); + + c_forfilter (x, crange, crange_literal(INT64_MAX) + , c_flt_skipwhile(x, *x.ref != 11) + && *x.ref % 2 != 0 + , c_flt_take(x, 5)) + IVec_push(&vector, *x.ref * *x.ref); + c_foreach (x, IVec, vector) printf(" %d", *x.ref); puts(""); } } @@ -123,7 +123,7 @@ void demo5(void) #define flt_even(i) ((*i.ref & 1) == 0) #define flt_mid_decade(i) ((*i.ref % 10) != 0) puts("demo5:"); - crange r1 = crange_make(1963, crange_MAX); + crange r1 = crange_make(1963, INT32_MAX); c_forfilter (i, crange, r1 , c_flt_skip(i,15) && c_flt_skipwhile(i, flt_mid_decade(i)) diff --git a/examples/forloops.c b/examples/forloops.c index 2c604877..d016cb66 100644 --- a/examples/forloops.c +++ b/examples/forloops.c @@ -60,7 +60,7 @@ int main() printf(" (%d %d)", *_.key, *_.val);
puts("\n\nc_forwhile:");
- c_forwhile (i, IVec, vec, i.index < 3)
+ c_forwhile (i, IVec, IVec_begin(&vec), i.index < 3)
printf(" %d", *i.ref);
#define isOdd(i) (*i.ref & 1)
diff --git a/examples/list.c b/examples/list.c index e94a9f18..a538d93c 100644 --- a/examples/list.c +++ b/examples/list.c @@ -25,14 +25,14 @@ int main() { sum += *i.ref; printf("sum %f\n\n", sum); - c_forwhile (i, clist_fx, list, i.index < 10) + c_forwhile (i, clist_fx, clist_fx_begin(&list), i.index < 10) printf("%8d: %10f\n", (int)i.index, *i.ref); puts("sort"); clist_fx_sort(&list); // mergesort O(n*log n) puts("sorted"); - c_forwhile (i, clist_fx, list, i.index < 10) + c_forwhile (i, clist_fx, clist_fx_begin(&list), i.index < 10) printf("%8d: %10f\n", (int)i.index, *i.ref); puts(""); diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index b2e40cb3..14f4978b 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -31,18 +31,18 @@ #include <assert.h> #if SIZE_MAX == UINT32_MAX -# define c_ZU PRIu32 + #define c_ZU PRIu32 #elif SIZE_MAX == UINT64_MAX -# define c_ZU PRIu64 + #define c_ZU PRIu64 #endif #if defined(_MSC_VER) -# pragma warning(disable: 4116 4996) // unnamed type definition in parentheses -# define STC_FORCE_INLINE static __forceinline + #pragma warning(disable: 4116 4996) // unnamed type definition in parentheses + #define STC_FORCE_INLINE static __forceinline #elif defined(__GNUC__) || defined(__clang__) -# define STC_FORCE_INLINE static inline __attribute((always_inline)) + #define STC_FORCE_INLINE static inline __attribute((always_inline)) #else -# define STC_FORCE_INLINE static inline + #define STC_FORCE_INLINE static inline #endif #define STC_INLINE static inline @@ -67,22 +67,22 @@ ((T*)((char*)(p) + 0*sizeof((p) == &((T*)0)->m) - offsetof(T, m))) #ifndef __cplusplus -# define c_alloc(T) c_malloc(sizeof(T)) -# define c_alloc_n(T, n) c_malloc(sizeof(T)*(n)) -# define c_init(T) (T) -# define c_new(T, ...) ((T*)memcpy(c_alloc(T), (T[]){__VA_ARGS__}, sizeof(T))) + #define c_alloc(T) c_malloc(sizeof(T)) + #define c_alloc_n(T, n) c_malloc(sizeof(T)*(n)) + #define c_init(T) (T) + #define c_new(T, ...) ((T*)memcpy(c_alloc(T), (T[]){__VA_ARGS__}, sizeof(T))) #else -# include <new> -# define c_alloc(T) static_cast<T*>(c_malloc(sizeof(T))) -# define c_alloc_n(T, n) static_cast<T*>(c_malloc(sizeof(T)*(n))) -# define c_init(T) T -# define c_new(T, ...) new (c_alloc(T)) T(__VA_ARGS__) + #include <new> + #define c_alloc(T) static_cast<T*>(c_malloc(sizeof(T))) + #define c_alloc_n(T, n) static_cast<T*>(c_malloc(sizeof(T)*(n))) + #define c_init(T) T + #define c_new(T, ...) new (c_alloc(T)) T(__VA_ARGS__) #endif #ifndef c_malloc -# define c_malloc(sz) malloc(sz) -# define c_calloc(n, sz) calloc(n, sz) -# define c_realloc(p, sz) realloc(p, sz) -# define c_free(p) free(p) + #define c_malloc(sz) malloc(sz) + #define c_calloc(n, sz) calloc(n, sz) + #define c_realloc(p, sz) realloc(p, sz) + #define c_free(p) free(p) #endif #define c_delete(T, ptr) do { T *_c_p = ptr; T##_drop(_c_p); c_free(_c_p); } while (0) @@ -168,7 +168,7 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, ; it.ref != (C##_value*)_endref; C##_next(&it)) #ifndef c_FLT_STACK -#define c_FLT_STACK 14 // 22, 30, .. + #define c_FLT_STACK 14 /* 22, 30, .. */ #endif #define c_flt_take(i, n) (++(i).s1[(i).s1top++] <= (n)) #define c_flt_skip(i, n) (++(i).s1[(i).s1top++] > (n)) @@ -176,23 +176,21 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, #define c_flt_takewhile(i, pred) !c_flt_skipwhile(i, pred) #define c_forfilter(...) c_MACRO_OVERLOAD(c_forfilter, __VA_ARGS__) -#define c_forfilter4(it, C, cnt, filter) \ - c_forfilter_s(it, C, C##_begin(&cnt), filter) -#define c_forfilter5(it, C, cnt, filter, whilepred) \ - c_forfilter_s(it, C, C##_begin(&cnt), filter) if (!(whilepred)) break; else -#define c_forfilter_s(it, C, start, filter) \ - c_foreach_s(it, C, start) if (!((filter) && ++it.count)) ; else - -#define c_foreach_s(i, C, start) \ +#define c_forfilter4(i, C, cnt, filter) \ + c_forfilter_s(i, C, C##_begin(&cnt), filter) +#define c_forfilter5(i, C, cnt, filter, cond) \ + c_forfilter_s(i, C, C##_begin(&cnt), filter) if (!(cond)) break; else +#define c_forfilter_s(i, C, start, filter) \ for (struct {C##_iter it; C##_value *ref; \ uint32_t s1[c_FLT_STACK], index, count; \ uint8_t s2[c_FLT_STACK], s1top, s2top;} \ i = {.it=start, .ref=i.it.ref}; i.it.ref \ - ; C##_next(&i.it), i.ref = i.it.ref, ++i.index, i.s1top=0, i.s2top=0) + ; C##_next(&i.it), i.ref = i.it.ref, ++i.index, i.s1top=0, i.s2top=0) \ + if (!((filter) && ++i.count)) ; else -#define c_forwhile(i, C, cnt, cond) \ +#define c_forwhile(i, C, start, cond) \ for (struct {C##_iter it; C##_value *ref; size_t index;} \ - i = {.it=C##_begin(&cnt), .ref=i.it.ref}; i.it.ref && (cond) \ + i = {.it=start, .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 */ \ @@ -202,7 +200,7 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, #define c_forloop c_forrange // [deprecated] #define c_forrange(...) c_MACRO_OVERLOAD(c_forrange, __VA_ARGS__) -#define c_forrange1(stop) c_forrange3(_i, 0, stop) +#define c_forrange1(stop) c_forrange3(_c_i, 0, stop) #define c_forrange2(i, stop) c_forrange3(i, 0, stop) #define c_forrange3(i, start, stop) \ for (long long i=start, _end=stop; i < _end; ++i) @@ -210,15 +208,15 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, for (long long i=start, _inc=step, _end=(stop) - (_inc > 0) \ ; (_inc > 0) ^ (i > _end); i += _inc) #ifndef __cplusplus -# define c_forlist(it, T, ...) \ + #define c_forlist(it, T, ...) \ for (struct {T* data; T* ref; int size, index;} \ it = {.data=(T[])__VA_ARGS__, .ref=it.data, .size=sizeof((T[])__VA_ARGS__)/sizeof(T)} \ ; it.index < it.size; ++it.ref, ++it.index) #else -# include <initializer_list> -# define c_forlist(it, T, ...) \ - for (struct {std::initializer_list<T> list; std::initializer_list<T>::iterator ref; size_t size, index;} \ - it = {.list=__VA_ARGS__, .ref=it.list.begin(), .size=it.list.size()} \ + #include <initializer_list> + #define c_forlist(it, T, ...) \ + for (struct {std::initializer_list<T> _il; std::initializer_list<T>::iterator data, ref; size_t size, index;} \ + it = {._il=__VA_ARGS__, .data=it._il.begin(), .ref=it.data, .size=it._il.size()} \ ; it.index < it.size; ++it.ref, ++it.index) #endif #define c_with(...) c_MACRO_OVERLOAD(c_with, __VA_ARGS__) @@ -277,16 +275,16 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, #if !defined(i_static) && !defined(STC_STATIC) && (defined(i_header) || defined(STC_HEADER) || \ defined(i_implement) || defined(STC_IMPLEMENT)) -# define STC_API extern -# define STC_DEF + #define STC_API extern + #define STC_DEF #else -# define i_static -# define STC_API static inline -# define STC_DEF static inline + #define i_static + #define STC_API static inline + #define STC_DEF static inline #endif #if defined(STC_EXTERN) -# define i_extern + #define i_extern #endif #if defined(i_static) || defined(STC_IMPLEMENT) -# define i_implement + #define i_implement #endif diff --git a/include/stc/views.h b/include/stc/views.h index 7ac50957..d8193c05 100644 --- a/include/stc/views.h +++ b/include/stc/views.h @@ -96,7 +96,6 @@ struct stc_nostruct #define crange_literal(...) \ (*(crange[]){crange_make(__VA_ARGS__)}) -#define crange_MAX INT64_MAX typedef long long crange_value; typedef struct { crange_value start, end, step, value; } crange; |
