diff options
| author | _Tradam <[email protected]> | 2023-09-08 01:29:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-08 01:29:47 +0000 |
| commit | 3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch) | |
| tree | afbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/spans/submdspan.c | |
| parent | 0841165881871ee01b782129be681209aeed2423 (diff) | |
| parent | 1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff) | |
| download | STC-modified-modified.tar.gz STC-modified-modified.zip | |
Diffstat (limited to 'misc/examples/spans/submdspan.c')
| -rw-r--r-- | misc/examples/spans/submdspan.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/misc/examples/spans/submdspan.c b/misc/examples/spans/submdspan.c new file mode 100644 index 00000000..0752dfa1 --- /dev/null +++ b/misc/examples/spans/submdspan.c @@ -0,0 +1,44 @@ +// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2630r0.html +// C99: +#include <stdio.h> +#include <stc/cspan.h> + +using_cspan3(span, double); // shorthand for defining span, span2, span3 + +// Set all elements of a rank-2 mdspan to zero. +void zero_2d(span2 grid2d) { + (void)c_static_assert(cspan_rank(&grid2d) == 2); + for (int i = 0; i < grid2d.shape[0]; ++i) { + for (int j = 0; j < grid2d.shape[1]; ++j) { + *cspan_at(&grid2d, i,j) = 0; + } + } +} + +void zero_surface(span3 grid3d) { + (void)c_static_assert(cspan_rank(&grid3d) == 3); + zero_2d(cspan_slice(span2, &grid3d, {0}, {c_ALL}, {c_ALL})); + zero_2d(cspan_slice(span2, &grid3d, {c_ALL}, {0}, {c_ALL})); + zero_2d(cspan_slice(span2, &grid3d, {c_ALL}, {c_ALL}, {0})); + zero_2d(cspan_slice(span2, &grid3d, {grid3d.shape[0]-1}, {c_ALL}, {c_ALL})); + zero_2d(cspan_slice(span2, &grid3d, {c_ALL}, {grid3d.shape[1]-1}, {c_ALL})); + zero_2d(cspan_slice(span2, &grid3d, {c_ALL}, {c_ALL}, {grid3d.shape[2]-1})); +} + +int main() { + double arr[3*4*5]; + for (int i=0; i<c_arraylen(arr); ++i) arr[i] = i + 1.0; + + span3 md = cspan_md(arr, 3, 4, 5); + + zero_surface(md); + + for (int i = 0; i < md.shape[0]; i++) { + for (int j = 0; j < md.shape[1]; j++) { + for (int k = 0; k < md.shape[2]; k++) + printf(" %2g", *cspan_at(&md, i,j,k)); + puts(""); + } + puts(""); + } +} |
