diff options
| author | tylov <[email protected]> | 2023-07-24 12:54:09 +0200 |
|---|---|---|
| committer | tylov <[email protected]> | 2023-07-24 12:54:09 +0200 |
| commit | f1f0c01e798eb3217e62a43de660723173984547 (patch) | |
| tree | b3ebe91d4f3a7e24e79fb325982365af89c698cf | |
| parent | 4a37879119cd4d8b92c5dc578741052dd399f53f (diff) | |
| download | STC-modified-f1f0c01e798eb3217e62a43de660723173984547.tar.gz STC-modified-f1f0c01e798eb3217e62a43de660723173984547.zip | |
Improved an issue with cspan.
| -rw-r--r-- | docs/cspan_api.md | 10 | ||||
| -rw-r--r-- | include/stc/cspan.h | 18 | ||||
| -rw-r--r-- | misc/examples/spans/printspan.c | 2 |
3 files changed, 22 insertions, 8 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 1312ae6d..51d72856 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -29,6 +29,7 @@ by default (define `STC_NDEBUG` or `NDEBUG` to disable). ```c SpanType cspan_init(TYPE SpanType, {v1, v2, ...}); // make a 1-d cspan from values SpanType cspan_from(STCContainer* cnt); // make a 1-d cspan from a cvec, cstack, cpque (heap) +SpanType cspan_from_n(ValueType* ptr, intptr_t n); // make a 1-d cspan from a pointer and length SpanType cspan_from_array(ValueType array[]); // make a 1-d cspan from a C array intptr_t cspan_size(const SpanTypeN* self); // return number of elements @@ -144,6 +145,7 @@ Slicing cspan without and with reducing the rank: ```c #define i_implement #include <c11/fmt.h> +#include <stc/algorithm.h> #include <stc/cspan.h> using_cspan3(Span, int); // Shorthand to define Span, Span2, and Span3 @@ -164,6 +166,14 @@ int main(void) fmt_print(" {}", *i.ref); puts(""); + // create span on-the-fly + int array[] = {3, 65, 4, 3, 7, 87, 45}; + c_forfilter (i, ISpan, (ISpan)cspan_from_array(array), + c_flt_skip(i, 2) && + c_flt_take(i, 3)) + fmt_print(" {}", *i.ref); + puts(""); + // slice without reducing rank: Span3 ss3 = cspan_slice(Span3, &span3, {c_ALL}, {3,4}, {c_ALL}); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 08045010..1b57d4d4 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -26,7 +26,7 @@ #include <stc/cspan.h> #include <stc/algo/filter.h> using_cspan(Span2f, float, 2); -using_cspan(Intspan, int, 1); +using_cspan(Intspan, int); int demo1() { float raw[4*5]; @@ -65,7 +65,11 @@ int demo2() { #define using_cspan(...) c_MACRO_OVERLOAD(using_cspan, __VA_ARGS__) #define using_cspan_2(Self, T) \ - using_cspan_3(Self, T, 1) + using_cspan_3(Self, T, 1); \ + STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \ + return (Self){.data=raw, .shape={(int32_t)n}, .stride={.d={1}}}; \ + } \ + struct stc_nostruct #define using_cspan_3(Self, T, RANK) \ typedef T Self##_value; typedef T Self##_raw; \ @@ -77,9 +81,6 @@ int demo2() { \ typedef struct { Self##_value *ref; int32_t pos[RANK]; const Self *_s; } Self##_iter; \ \ - STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \ - return (Self){.data=raw, .shape={(int32_t)n}}; \ - } \ STC_INLINE Self Self##_slice_(Self##_value* d, const int32_t shape[], const int32_t stri[], \ const int rank, const int32_t a[][2]) { \ Self s; int outrank; \ @@ -104,7 +105,7 @@ int demo2() { } \ struct stc_nostruct -#define using_cspan2(Self, T) using_cspan_3(Self, T, 1); using_cspan_3(Self##2, T, 2) +#define using_cspan2(Self, T) using_cspan_2(Self, T); using_cspan_3(Self##2, T, 2) #define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan_3(Self##3, T, 3) #define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan_3(Self##4, T, 4) #define using_cspan_tuple(N) typedef struct { int32_t d[N]; } cspan_tuple##N @@ -124,8 +125,11 @@ using_cspan_tuple(7); using_cspan_tuple(8); #define cspan_from(container) \ {.data=(container)->data, .shape={(int32_t)(container)->_len}, .stride={.d={1}}} +#define cspan_from_n(ptr, n) \ + {.data=(ptr), .shape={n}, .stride={.d={1}}} + #define cspan_from_array(array) \ - {.data=(array), .shape={c_arraylen(array)}, .stride={.d={1}}} + cspan_from_n(array, c_arraylen(array)) #define cspan_size(self) _cspan_size((self)->shape, cspan_rank(self)) #define cspan_rank(self) c_arraylen((self)->shape) diff --git a/misc/examples/spans/printspan.c b/misc/examples/spans/printspan.c index cd3c5f4f..eb9d80e3 100644 --- a/misc/examples/spans/printspan.c +++ b/misc/examples/spans/printspan.c @@ -11,7 +11,7 @@ #include <stc/csset.h> #include <stc/cspan.h> -using_cspan(intspan, int, 1); +using_cspan(intspan, int); void printMe(intspan container) { printf("%d:", (int)cspan_size(&container)); |
