diff options
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/examples/forfilter.c | 2 | ||||
| -rw-r--r-- | misc/examples/multidim.c | 49 | ||||
| -rw-r--r-- | misc/examples/prime.c | 2 |
3 files changed, 51 insertions, 2 deletions
diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index 280fdc9c..bd3d346d 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -65,7 +65,7 @@ void demo2(void) c_AUTO (IVec, vector) { puts("demo2:"); - c_FORFILTER (x, crange, crange_LITERAL(INT64_MAX) + c_FORFILTER (x, crange, crange_object(INT64_MAX) , c_FLT_SKIPWHILE(x, *x.ref != 11) && *x.ref % 2 != 0 , c_FLT_TAKE(x, 5)) diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c new file mode 100644 index 00000000..044693be --- /dev/null +++ b/misc/examples/multidim.c @@ -0,0 +1,49 @@ +// Example from https://en.cppreference.com/w/cpp/container/mdspan +#define i_val int +#include <stc/cstack.h> +#include <stc/algo/cmspan.h> +#include <stdio.h> +using_cmspan(ispan1, int, 1); +using_cmspan(ispan2, int, 2); +using_cmspan(ispan3, int, 3); + +int main() +{ + cstack_int v = {0}; + c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12}) + cstack_int_push(&v, *i.ref); + + // View data as contiguous memory representing 12 ints + ispan1 ms1 = cmspan_make(v.data, 12); + // View data as contiguous memory representing 2 rows of 6 ints each + ispan2 ms2 = cmspan_make(v.data, 2, 6); + // View the same data as a 3D array 2 x 3 x 2 + ispan3 ms3 = cmspan_make(v.data, 2, 3, 2); + + // write data using 2D view + for(unsigned i=0; i != ms2.dim[0]; i++) + for(unsigned j=0; j != ms2.dim[1]; j++) + *cmspan_at(&ms2, i, j) = i*1000 + j; + + // print data using 1D view + for(unsigned i=0; i != ms1.dim[0]; i++) + printf(" %d", *cmspan_at(&ms1, i)); + puts(""); + + c_FOREACH (i, ispan1, ms1) + printf(" %d", *i.ref); + puts(""); + + // read back using 3D view + for(unsigned i=0; i != ms3.dim[0]; i++) + { + printf("slice @ i = %u\n", i); + for(unsigned j=0; j != ms3.dim[1]; j++) + { + for(unsigned k=0; k != ms3.dim[2]; k++) + printf("%d ", *cmspan_at(&ms3, i, j, k)); + puts(""); + } + } + cstack_int_drop(&v); +} diff --git a/misc/examples/prime.c b/misc/examples/prime.c index f6e89e09..b4d81868 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -43,7 +43,7 @@ int main(void) puts(""); puts("Show the last 50 primes using a temporary crange generator:"); - c_FORFILTER (i, crange, crange_LITERAL(n - 1, 0, -2) + c_FORFILTER (i, crange, crange_object(n - 1, 0, -2) , cbits_test(&primes, *i.ref>>1) , c_FLT_TAKE(i, 50)) { printf("%lld ", *i.ref); |
