summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/multidim.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-22 16:22:51 +0100
committerTyge Løvset <[email protected]>2023-01-22 16:22:51 +0100
commitaf74e5f44af8f03ec3b0b2ffd3fd577210707e49 (patch)
tree5905f4c80190161cdc9f814e78a7a87030cae1c7 /misc/examples/multidim.c
parent01bffb4d3b6b175db264c5cc30b1b7e756560639 (diff)
downloadSTC-modified-af74e5f44af8f03ec3b0b2ffd3fd577210707e49.tar.gz
STC-modified-af74e5f44af8f03ec3b0b2ffd3fd577210707e49.zip
Added cspan_init() for static initializing and a minor fix. Added cregex replace tests.
Diffstat (limited to 'misc/examples/multidim.c')
-rw-r--r--misc/examples/multidim.c73
1 files changed, 34 insertions, 39 deletions
diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c
index e882d62a..69f818cc 100644
--- a/misc/examples/multidim.c
+++ b/misc/examples/multidim.c
@@ -5,53 +5,48 @@
#include <stdio.h>
using_cspan3(ispan, int);
-/*
-using_cspan(ispan, int, 1);
-using_cspan(ispan2, int, 2);
-using_cspan(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);
+ 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
- ispan 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
- ispan3 ms3 = cspan_make(v.data, 2, 3, 2);
+ // View data as contiguous memory representing 12 ints
+ ispan 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
+ ispan3 ms3 = cspan_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++)
- *cspan_at(&ms2, i, j) = i*1000 + j;
+ // write data using 2D view
+ for (unsigned i=0; i != ms2.dim[0]; i++)
+ for (unsigned j=0; j != ms2.dim[1]; j++)
+ *cspan_at(&ms2, i, j) = i*1000 + j;
- // print all items using 1D view
- printf("all: ");
- for (unsigned i=0; i != cspan_size(&ms1); i++)
- printf(" %d", *cspan_at(&ms1, i));
- puts("");
+ // print all items using 1D view
+ printf("all: ");
+ for (unsigned i=0; i != cspan_size(&ms1); i++)
+ printf(" %d", *cspan_at(&ms1, i));
+ puts("");
- // or iterate a subspan...
- ispan2 sub = cspan_subdim3(&ms3, 1);
- printf("sub: ");
- c_FOREACH (i, ispan2, sub)
- printf(" %d", *i.ref);
- puts("");
+ // or iterate a subspan...
+ ispan2 sub = cspan_subdim3(&ms3, 1);
+ printf("sub: ");
+ c_FOREACH (i, ispan2, sub)
+ 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++)
+ // read back using 3D view
+ for (unsigned i=0; i != ms3.dim[0]; i++)
{
- for (unsigned k=0; k != ms3.dim[2]; k++)
- printf("%d ", *cspan_at(&ms3, i, j, k));
- puts("");
+ 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 ", *cspan_at(&ms3, i, j, k));
+ puts("");
+ }
}
- }
- cstack_int_drop(&v);
+ cstack_int_drop(&v);
}