From 770a39ff6ed39494569a411949f22edcdb7eb30c Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 15 Jan 2023 21:18:39 +0100 Subject: Large commit: - Moved stc/algo/cspan.h to stc/cspan.h - its a data view type similar to csview. +Many updates. Added docs/cspan_api.md page! - Update c11/fmt.h to VER 2.0: NEW API, see test. NOTE: fmt.h is not officially part of STC, as it is C11, and STC is C99. - Renamed crange_LITERAL() back to crange_literal(), and cspan_LITERAL() to cspan_literal(). These returns a compound literal (lvalue) that can be passed to a c_FOR*-iterator. --- misc/examples/forfilter.c | 2 +- misc/examples/multidim.c | 18 ++++++++++++------ misc/examples/prime.c | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) (limited to 'misc/examples') diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index bd3d346d..05d0bc28 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_object(INT64_MAX) + c_FORFILTER (x, crange, crange_literal(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 index 2cf16b7f..99310847 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -1,13 +1,15 @@ -// Example from https://en.cppreference.com/w/cpp/container/mdspan +// Example based on https://en.cppreference.com/w/cpp/container/mdspan #define i_val int #include -#include +#include #include +using_cspan3(ispan, int); +/* using_cspan(ispan1, int, 1); using_cspan(ispan2, int, 2); using_cspan(ispan3, int, 3); - +*/ int main() { @@ -16,7 +18,7 @@ int main() cstack_int_push(&v, *i.ref); // View data as contiguous memory representing 12 ints - ispan1 ms1 = cspan_make(v.data, 12); + ispan1 ms1 = cspan_from(&v); // View data as contiguous memory representing 2 rows of 6 ints each ispan2 ms2 = cspan_make(v.data, 2, 6); // View the same data as a 3D array 2 x 3 x 2 @@ -27,12 +29,16 @@ int main() for (unsigned j=0; j != ms2.dim[1]; j++) *cspan_at(&ms2, i, j) = i*1000 + j; - // print data using 1D view + // print all items using 1D view + printf("all: "); for (unsigned i=0; i != ms1.dim[0]; i++) printf(" %d", *cspan_at(&ms1, i)); puts(""); - c_FOREACH (i, ispan1, ms1) + // or iterate a subspan... + ispan2 sub = cspan_3to2(&ms3, 1); + printf("sub: "); + c_FOREACH (i, ispan2, sub) printf(" %d", *i.ref); puts(""); diff --git a/misc/examples/prime.c b/misc/examples/prime.c index b4d81868..40ccc299 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_object(n - 1, 0, -2) + c_FORFILTER (i, crange, crange_literal(n - 1, 0, -2) , cbits_test(&primes, *i.ref>>1) , c_FLT_TAKE(i, 50)) { printf("%lld ", *i.ref); -- cgit v1.2.3