summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/multidim.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-15 21:18:39 +0100
committerTyge Løvset <[email protected]>2023-01-15 21:18:39 +0100
commit770a39ff6ed39494569a411949f22edcdb7eb30c (patch)
treeddc38f64ed90eceff9571c1c2753b4425947629e /misc/examples/multidim.c
parentc5e071c622d6f460aa1ad07b6543ceaaed5209fc (diff)
downloadSTC-modified-770a39ff6ed39494569a411949f22edcdb7eb30c.tar.gz
STC-modified-770a39ff6ed39494569a411949f22edcdb7eb30c.zip
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.
Diffstat (limited to 'misc/examples/multidim.c')
-rw-r--r--misc/examples/multidim.c18
1 files changed, 12 insertions, 6 deletions
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 <stc/cstack.h>
-#include <stc/algo/cspan.h>
+#include <stc/cspan.h>
#include <stdio.h>
+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("");