From 17a70908cab0e4064e4913d4f1a109569d24295f Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 18 Jan 2023 10:15:33 +0100 Subject: using_cspanN(S, T, R) now defines S, S1, S2.. (not S1) --- docs/cspan_api.md | 52 ++++++++++++++++++++++++------------------------ include/stc/cspan.h | 15 ++++++++++---- misc/examples/multidim.c | 6 +++--- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 15125e78..a491185e 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -13,47 +13,47 @@ See the c++ classes [std::span](https://en.cppreference.com/w/cpp/container/span using_cspan(SpanType, ValueType, Rank); // define SpanType with ValueType elements. // Rank is number of dimensions (max 4) // Shorthands: -using_cspan2(S, ValueType); // define span types S1, S2 with Ranks 1, 2. -using_cspan3(S, ValueType); // define span types S1, S2, S3 with Ranks 1, 2, 3. -using_cspan4(S, ValueType); // define span types S1.., S4 with Ranks 1, 2, 3, 4. +using_cspan2(S, ValueType); // define span types S, S2 with ranks 1, 2. +using_cspan3(S, ValueType); // define span types S, S2, S3 with ranks 1, 2, 3. +using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4. ``` ## Methods Note that `cspan_make()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type on assignment, but not on initialization of a span variable. All functions are type-safe, and arguments are side-effect safe, except for SpanType arg. which must not have side-effects. ```c -SpanType cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan +SpanType{N} cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan SpanType cspan_from(STCContainer* cnt); // create a 1D cspan from a compatible STC container SpanType cspan_from_array(ValueType array[]); // create a 1D cspan from a C array SpanType cspan_from_list(T ValueType, {val0, val1, ...}); // create a 1D cspan from an initializer list SpanType& cspan_literal(T SpanType, {val0, val1, ...}); // create a 1D cspan compound literal from init list -void cspan_resize(SpanType* self, size_t xdim, ...); // change the extent of each dimension +void cspan_resize(SpanType{N}* self, size_t xdim, ...); // change the extent of each dimension -size_t cspan_size(SpanType spn); // return number of elements -unsigned cspan_rank(SpanType spn); // return number of dimensions -size_t cspan_index(SpanType spn, size_t x, ...); // index of element +size_t cspan_size(SpanType{N} spn); // return number of elements +unsigned cspan_rank(SpanType{N} spn); // return number of dimensions +size_t cspan_index(SpanType{N} spn, size_t x, ...); // index of element + +ValueType* cspan_at(SpanType{N} spn, size_t x, ...); // at(): num of args decides input SpanType{N}. +SpanType cspan_at2(SpanType2 spn, size_t x); // return a 1D subarray cspan. +SpanType{N} cspan_at3(SpanType3 spn, size_t x, ...); // atN(): N decides input SpanType{N}, +SpanType{N} cspan_at4(SpanType4 spn, size_t x, ...); // and num of args decides returned SpanType{N}. -ValueType* cspan_at(SpanType spn, size_t x, ...); // return value pointer from a 1D, 2D, 3D or 4D cspan -SpanType1 cspan_at2(SpanType2 spn, size_t x); // return a 1D subarray cspan -SpanType cspan_at3(SpanType3 spn, size_t x, ...); // return a 2D or 1D subarray cspan -SpanType cspan_at4(SpanType4 spn, size_t x, ...); // return a 3D, 2D or 1D subarray cspan - -SpanType1 cspan_subspan1(SpanType1 spn, size_t offset, size_t count); // return a slice of a 1D cspan +SpanType cspan_subspan(SpanType spn, size_t offset, size_t count); // return a slice of a 1D cspan SpanType2 cspan_subspan2(SpanType2 spn, size_t offset, size_t count); // return a slice of a 2D cspan SpanType3 cspan_subspan3(SpanType3 spn, size_t offset, size_t count); // return a slice of a 3D cspan SpanType4 cspan_subspan4(SpanType4 spn, size_t offset, size_t count); // return a slice of a 4D cspan -SpanType_iter SpanType_begin(const SpanType* self); -SpanType_iter SpanType_end(const SpanType* self); -void SpanType_next(SpanType_iter* it); +SpanType{N}_iter SpanType_begin(const SpanType{N}* self); +SpanType{N}_iter SpanType_end(const SpanType{N}* self); +void SpanType_next(SpanType{N}_iter* it); ``` ## Types -| Type name | Type definition | Used to represent... | -|:-----------------|:-----------------------------------------------------|:---------------------| -| SpanType | `struct { ValueType *data; uint32_t dim[RANK]; }` | The SpanType | -| SpanType`_value` | `ValueType` | The ValueType | -| SpanType`_iter` | `struct { ValueType *ref; ... }` | Iterator type | +| Type name | Type definition | Used to represent... | +|:--------------------|:-----------------------------------------------|:---------------------| +| SpanType{N} | `struct { ValueType *data; uint32_t dim[N]; }` | SpanType with rank N | +| SpanType{N}`_value` | `ValueType` | The ValueType | +| SpanType{N}`_iter` | `struct { ValueType *ref; ... }` | Iterator type | ## Example ```c @@ -62,7 +62,7 @@ void SpanType_next(SpanType_iter* it); #include #include -using_cspan3(FS, float); // Shorthand to define span types FS1, FS2, and FS3. +using_cspan3(FS, float); // Shorthand to define span types FS, FS2, and FS3. int main() { @@ -76,9 +76,9 @@ int main() *cspan_at(span3, 4, 3, 2) = 3.14f; printf("index: %d", (int)cspan_index(span3, 4, 3, 2)); - FS1 span1 = cspan_at3(span3, 4, 3); + FS span1 = cspan_at3(span3, 4, 3); printf("\niterate span1: "); - c_FOREACH (i, FS1, span1) + c_FOREACH (i, FS, span1) printf("%g ", *i.ref); FS2 span2 = cspan_at3(span3, 4); @@ -90,7 +90,7 @@ int main() c_FORRANGE (i, span3.dim[0]) { c_FORRANGE (j, span3.dim[1]) { c_FORRANGE (k, span3.dim[2]) - printf(" %g", *cspan_at(span3, i, j, k)); + printf(" %2g", *cspan_at(span3, i, j, k)); printf(" |"); } puts(""); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 45e51b79..d91a3460 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -77,7 +77,7 @@ int demo2() { { if (++it->ref == it->end) it->ref = NULL; } \ struct stc_nostruct -#define using_cspan2(Self, T) using_cspan(Self##1, T, 1); using_cspan(Self##2, T, 2) +#define using_cspan2(Self, T) using_cspan(Self, T, 1); using_cspan(Self##2, T, 2) #define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan(Self##3, T, 3) #define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan(Self##4, T, 4) @@ -109,11 +109,11 @@ int demo2() { (void)memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \ sizeof((self)->dim) + cspan_rank_ok(*(self), c_NUMARGS(__VA_ARGS__))) -#define cspan_at(spn, ...) ((spn).data + cspan_index((spn), __VA_ARGS__)) +#define cspan_at(spn, ...) ((spn).data + cspan_index(spn, __VA_ARGS__)) +#define cspan_front(spn) ((spn).data) +#define cspan_back(spn) ((spn).data + cspan_size(spn) - 1) #define cspan_subspan(spn, offset, count) \ - cspan_subspan1(spn, offset, count) -#define cspan_subspan1(spn, offset, count) \ {.data=cspan_at(spn, offset), .dim={count}} #define cspan_subspan2(spn, offset, count) \ {.data=cspan_at(spn, offset, 0), .dim={count, (spn).dim[1]}} @@ -122,6 +122,13 @@ int demo2() { #define cspan_subspan4(spn, offset, count) \ {.data=cspan_at(spn, offset, 0, 0, 0), .dim={count, (spn).dim[1], (spn).dim[2], (spn).dim[3]}} +#define cspan_subspan2(spn, offset, count) \ + {.data=cspan_at(spn, offset, 0), .dim={count, (spn).dim[1]}} +#define cspan_subspan3(spn, offset, count) \ + {.data=cspan_at(spn, offset, 0, 0), .dim={count, (spn).dim[1], (spn).dim[2]}} +#define cspan_subspan4(spn, offset, count) \ + {.data=cspan_at(spn, offset, 0, 0, 0), .dim={count, (spn).dim[1], (spn).dim[2], (spn).dim[3]}} + #define cspan_at4(...) c_MACRO_OVERLOAD(cspan_at4, __VA_ARGS__) #define cspan_at3(...) c_MACRO_OVERLOAD(cspan_at3, __VA_ARGS__) #define cspan_at2(spn, x) \ diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index f3982259..11418e15 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -6,7 +6,7 @@ using_cspan3(ispan, int); /* -using_cspan(ispan1, int, 1); +using_cspan(ispan, int, 1); using_cspan(ispan2, int, 2); using_cspan(ispan3, int, 3); */ @@ -18,7 +18,7 @@ int main() cstack_int_push(&v, *i.ref); // View data as contiguous memory representing 12 ints - ispan1 ms1 = cspan_from(&v); + 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 @@ -31,7 +31,7 @@ int main() // print all items using 1D view printf("all: "); - for (unsigned i=0; i != ms1.dim[0]; i++) + for (unsigned i=0; i != cspan_size(ms1); i++) printf(" %d", *cspan_at(ms1, i)); puts(""); -- cgit v1.2.3