summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/spans/mdspan.c
blob: 630ffddb48d8dca6969cb99e1986f553ba070e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stc/cspan.h>
#include <stdlib.h>

using_cspan3(DSpan, double);

int main(void) {
    const int nx=5, ny=4, nz=3;
    double* data = c_new_n(double, nx*ny*nz);

    printf("\nMultidim span ms[5, 4, 3], fortran ordered");
    DSpan3 ms = cspan_md_layout(c_COLMAJOR, data, nx, ny, nz);

    int idx = 0;
    for (int i = 0; i < ms.shape[0]; ++i)
        for (int j = 0; j < ms.shape[1]; ++j)
            for (int k = 0; k < ms.shape[2]; ++k)
                *cspan_at(&ms, i, j, k) = ++idx;

    cspan_transpose(&ms);

    printf(", transposed:\n\n");
    for (int i = 0; i < ms.shape[0]; ++i) {
        for (int j = 0; j < ms.shape[1]; ++j) {
            for (int k = 0; k < ms.shape[2]; ++k)
                printf(" %3g", *cspan_at(&ms, i, j, k));
            puts("");
        }
        puts("");
    }

    DSpan2 sub;

    puts("Slicing:");
    printf("\nms[0, :, :] ");
    sub = cspan_slice(DSpan2, &ms, {0}, {c_ALL}, {c_ALL});
    c_foreach (i, DSpan2, sub) printf(" %g", *i.ref);
    puts("");

    printf("\nms[:, 0, :] ");
    sub = cspan_slice(DSpan2, &ms, {c_ALL}, {0}, {c_ALL});
    c_foreach (i, DSpan2, sub) printf(" %g", *i.ref);
    puts("");

    sub = cspan_slice(DSpan2, &ms, {c_ALL}, {c_ALL}, {0});
    printf("\nms[:, :, 0] ");
    c_foreach (i, DSpan2, sub) printf(" %g", *i.ref);
    puts("");

    free(data);
}