diff options
| author | Tyge Løvset <[email protected]> | 2022-10-04 16:38:41 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-10-04 20:05:49 +0200 |
| commit | 5daba7ad2291dd7f02299eabeb650d0d0b77a1a6 (patch) | |
| tree | d56fa2a14c5f2a84022c67a6268574ef4f1241a7 | |
| parent | 6bfc24241e2dea76dc14e3de771d98ecf6bb698e (diff) | |
| download | STC-modified-5daba7ad2291dd7f02299eabeb650d0d0b77a1a6.tar.gz STC-modified-5daba7ad2291dd7f02299eabeb650d0d0b77a1a6.zip | |
- Removed deprecated c_forrange() (replaced by c_forloop + crange type)
- Removed csview_new(literal) macro. Use c_sv(literal) instead.
- Added stc/views.h: moved crange from ccommon.h and added templated type c_listview. Instantiate by: using_listview(ViewName, ValueType) after #include, does not use #define i_val .... See examples in views.h.
| -rw-r--r-- | docs/ccommon_api.md | 11 | ||||
| -rw-r--r-- | docs/csview_api.md | 3 | ||||
| -rw-r--r-- | examples/forfilter.c | 20 | ||||
| -rw-r--r-- | examples/prime.c | 3 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 27 | ||||
| -rw-r--r-- | include/stc/csview.h | 2 | ||||
| -rw-r--r-- | include/stc/views.h | 109 |
7 files changed, 129 insertions, 46 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 912601e7..1cdc56cb 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -263,17 +263,16 @@ Note that `c_flt_take()` is given as an optional argument, which makes the loop ### crange **crange** is a number sequence generator type. The **crange_value** type is `long long`. Below, *start*, *stop*, *step* are type *crange_value*: ```c -crange crange_init(void); // will generate 0, 1, 2, ... -crange crange_make(stop); // will generate 0, 1, ..., stop-1 -crange crange_make(start, stop); // will generate start, start+1, ... stop-1 -crange crange_make(start, stop, step); // will generate start, start+step, ... upto-not-including stop +crange crange_init(stop); // will generate 0, 1, ..., stop-1 +crange crange_init(start, stop); // will generate start, start+1, ... stop-1 +crange crange_init(start, stop, step); // will generate start, start+step, ... upto-not-including stop // note that step may be negative. crange_iter crange_begin(crange* self); crange_iter crange_end(crange* self); void crange_next(crange_iter* it); // 1. All primes less than 32: -crange r1 = crange_make(3, 32, 2); +crange r1 = crange_init(3, 32, 2); printf("2"); // first prime c_forfilter (i, crange, r1 , isPrime(*i.ref)) @@ -281,7 +280,7 @@ c_forfilter (i, crange, r1 // 2 3 5 7 11 13 17 19 23 29 31 // 2. The 11 first primes: -crange r2 = crange_make(3, INTMAX_MAX, 2); +crange r2 = crange_init(3, crange_MAX, 2); printf("2"); c_forfilter (i, crange, r2 , isPrime(*i.ref) diff --git a/docs/csview_api.md b/docs/csview_api.md index 70587911..25bb8c84 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -26,9 +26,8 @@ All csview definitions and prototypes are available by including a single header ## Methods ```c -csview c_sv(const char literal_only[]); // alias for csview_new +csview c_sv(const char literal_only[]); // construct from literal, no strlen() csview c_sv(const char* str, size_t n); // overloaded csview constructor. -csview csview_new(const char literal_only[]); // construct from literal, no strlen() csview csview_from(const char* str); // construct from const char* void csview_clear(csview* self); diff --git a/examples/forfilter.c b/examples/forfilter.c index 2957fe63..336407de 100644 --- a/examples/forfilter.c +++ b/examples/forfilter.c @@ -2,6 +2,7 @@ #define i_implement #include <stc/cstr.h> #include <stc/csview.h> +#include <stc/views.h> #define i_type IVec #define i_val int @@ -29,8 +30,8 @@ void demo1(void) puts(""); int res, sum = 0; - c_forfilter (i, IVec, vec, - c_flt_skipwhile(i, *i.ref != 80) + c_forfilter (i, IVec, vec + , c_flt_skipwhile(i, *i.ref != 80) && c_flt_skip(i, 1) && c_flt_skipwhile(i, *i.ref != 80) && flt_isEven(i) @@ -59,9 +60,10 @@ fn main() { void demo2(void) { c_auto (IVec, vector) { - crange rv = crange_make(1, INTMAX_MAX); - c_forfilter (x, crange, rv, - flt_isOdd(x) + crange rv = crange_init(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)); @@ -121,12 +123,12 @@ 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, INTMAX_MAX); - c_forfilter (i, crange, r1, - c_flt_skip(i,15) + crange r1 = crange_init(1963, crange_MAX); + c_forfilter (i, crange, r1 + , c_flt_skip(i,15) && c_flt_skipwhile(i, flt_mid_decade(i)) && c_flt_skip(i,30) - && flt_isEven(i) + && flt_even(i) , c_flt_take(i,10)) printf(" %lld", *i.ref); puts(""); diff --git a/examples/prime.c b/examples/prime.c index 613048bf..3f0c4f6e 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -2,6 +2,7 @@ #include <math.h> #include <time.h> #include <stc/cbits.h> +#include <stc/views.h> cbits sieveOfEratosthenes(size_t n) { @@ -40,7 +41,7 @@ int main(void) puts(""); puts("Show the last 50 primes using a temporary crange generator:"); - c_forfilter (i, crange, *(crange[]){crange_make(n - 1, 0, -2)} + c_forfilter (i, crange, c_range(n - 1, 0, -2) , cbits_test(&primes, *i.ref>>1) , c_flt_take(i, 50)) { printf("%lld ", *i.ref); diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index fc164e28..c93c41b9 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -196,17 +196,6 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, ; _.it.ref && (_.key = &_.it.ref->first, _.val = &_.it.ref->second) \ ; C##_next(&_.it)) -// [deprecated]: -#define c_forrange(...) c_MACRO_OVERLOAD(c_forrange, __VA_ARGS__) -#define c_forrange1(stop) c_forrange4(_c_i, size_t, 0, stop) -#define c_forrange2(i, stop) c_forrange4(i, size_t, 0, stop) -#define c_forrange3(i, itype, stop) c_forrange4(i, itype, 0, stop) -#define c_forrange4(i, itype, start, stop) \ - for (itype i=start, _end=stop; i < _end; ++i) -#define c_forrange5(i, itype, start, stop, step) \ - for (itype i=start, _inc=step, _end=(stop) - (_inc > 0) \ - ; (_inc > 0) ^ (i > _end); i += _inc) -// [replacement]: #define c_forloop(...) c_MACRO_OVERLOAD(c_forloop, __VA_ARGS__) #define c_forloop1(stop) for (long long _i=0, _end=stop; _i < _end; ++_i) #define c_forloop2(i, stop) c_forloop4(i, 0, stop, 1) @@ -215,22 +204,6 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, for (long long i=start, _inc=step, _end=(stop) - (_inc > 0) \ ; (_inc > 0) ^ (i > _end); i += _inc) -typedef long long crange_value; -struct {crange_value start, end, step, val; } typedef crange; -struct {crange_value *ref, end, step; } typedef crange_iter; -#define crange_make(...) c_MACRO_OVERLOAD(crange_make, __VA_ARGS__) -#define crange_make1(stop) crange_make3(0, stop, 1) -#define crange_make2(start, stop) crange_make3(start, stop, 1) -#define c_range(...) (*(crange[]){crange_make(__VA_ARGS__)}) -STC_INLINE crange crange_make3(crange_value start, crange_value stop, crange_value step) - { crange r = {start, stop - (step > 0), step}; return r; } -STC_INLINE crange_iter crange_begin(crange* self) - { self->val = self->start; crange_iter it = {&self->val, self->end, self->step}; return it; } -STC_INLINE crange_iter crange_end(crange* self) - { crange_iter it = {NULL}; return it; } -STC_INLINE void crange_next(crange_iter* it) - { *it->ref += it->step; if ((it->step > 0) == (*it->ref > it->end)) it->ref = NULL; } - #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)} \ diff --git a/include/stc/csview.h b/include/stc/csview.h index 2be30cb0..3d8bd044 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -28,12 +28,12 @@ #include "utf8.h" #define csview_null c_sv("") -#define csview_new(literal) c_sv(literal) #define csview_npos (SIZE_MAX >> 1) #define csview_init() csview_null #define csview_drop c_default_drop #define csview_clone c_default_clone +#define csview_from_n c_sv STC_INLINE csview csview_from(const char* str) { return c_make(csview){str, strlen(str)}; } diff --git a/include/stc/views.h b/include/stc/views.h new file mode 100644 index 00000000..d4186caf --- /dev/null +++ b/include/stc/views.h @@ -0,0 +1,109 @@ +/* MIT License + * + * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ +/* +#include <stdio.h> +#include <stc/views.h> +using_listview(IView, int); + +int main() +{ + int array[] = {1, 2, 3, 4, 5}; + IView iv = IView_init(array, c_arraylen(array)); + + c_foreach (i, IView, iv) printf(" %d", *i.ref); + puts(""); + + c_forfilter (i, IView, c_listview(IView, {10, 20, 30, 22, 23}) + , c_flt_skipwhile(i, *i.ref < 25) + , c_flt_take(i, 2)) + printf(" %d", *i.ref); + puts(""); + + crange r1 = crange_init(80, 90); + c_foreach (i, crange, r1) printf(" %lld", *i.ref); + puts(""); + + c_foreach (i, crange, c_range(0, 100, 8)) printf(" %lld", *i.ref); + puts(""); +} +*/ +#ifndef STC_VIEWS_H_INCLUDED +#define STC_VIEWS_H_INCLUDED + +#include <stc/ccommon.h> + +#define c_listview(C, ...) \ + ((C){.data = (C##_value[])__VA_ARGS__, \ + .size = sizeof((C##_value[])__VA_ARGS__)/sizeof(C##_value)}) + +#define using_listview(Self, T) \ +typedef T Self##_raw; typedef const Self##_raw Self##_value; \ +typedef struct { Self##_value *data; size_t size; } Self; \ +typedef struct { Self##_value *ref, *end; } Self##_iter; \ + \ +STC_INLINE Self Self##_init(Self##_value* data, size_t size) \ + { Self me = {.data=data, .size=size}; return me; } \ + \ +STC_INLINE Self##_value* Self##_at(const Self* self, size_t idx) \ + { assert(idx < self->size); return self->data + idx; } \ + \ +STC_INLINE Self##_iter Self##_begin(const Self* self) { \ + Self##_iter it = {self->data, self->data + self->size}; \ + return it; \ +} \ + \ +STC_INLINE Self##_iter Self##_end(const Self* self) { \ + Self##_iter it = {NULL, self->data + self->size}; \ + return it; \ +} \ + \ +STC_INLINE void Self##_next(Self##_iter* it) \ + { if (++it->ref == it->end) it->ref = NULL; } \ +struct stc_nostruct + + +#define c_range(...) \ + (*(crange[]){crange_init(__VA_ARGS__)}) +#define crange_MAX INT64_MAX + +typedef long long crange_value; +typedef struct { crange_value start, end, step, val; } crange; +typedef struct { crange_value *ref, end, step; } crange_iter; + +#define crange_init(...) c_MACRO_OVERLOAD(crange_init, __VA_ARGS__) +#define crange_init1(stop) crange_init3(0, stop, 1) +#define crange_init2(start, stop) crange_init3(start, stop, 1) + +STC_INLINE crange crange_init3(crange_value start, crange_value stop, crange_value step) + { crange r = {start, stop - (step > 0), step}; return r; } + +STC_INLINE crange_iter crange_begin(crange* self) + { self->val = self->start; crange_iter it = {&self->val, self->end, self->step}; return it; } + +STC_INLINE crange_iter crange_end(crange* self) + { crange_iter it = {NULL}; return it; } + +STC_INLINE void crange_next(crange_iter* it) + { *it->ref += it->step; if ((it->step > 0) == (*it->ref > it->end)) it->ref = NULL; } + +#endif |
