From 900295256d825fc323149cd223c49787f32a3696 Mon Sep 17 00:00:00 2001 From: tylov Date: Thu, 20 Jul 2023 15:09:10 +0200 Subject: Moved examples to sub-directories. Added cotask1.c cotask2.c examples. --- misc/examples/spans/printspan.c | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 misc/examples/spans/printspan.c (limited to 'misc/examples/spans/printspan.c') diff --git a/misc/examples/spans/printspan.c b/misc/examples/spans/printspan.c new file mode 100644 index 00000000..cd3c5f4f --- /dev/null +++ b/misc/examples/spans/printspan.c @@ -0,0 +1,52 @@ +// printspan.c + +#include +#define i_implement +#include +#define i_key int +#include +#define i_key int +#include +#define i_key_str +#include + +#include +using_cspan(intspan, int, 1); + +void printMe(intspan container) { + printf("%d:", (int)cspan_size(&container)); + c_foreach (e, intspan, container) + printf(" %d", *e.ref); + puts(""); +} + +int main(void) +{ + intspan sp1 = cspan_init(intspan, {1, 2}); + printMe( sp1 ); + + printMe( c_init(intspan, {1, 2, 3}) ); + + int arr[] = {1, 2, 3, 4, 5, 6}; + intspan sp2 = cspan_from_array(arr); + printMe( c_LITERAL(intspan)cspan_subspan(&sp2, 1, 4) ); + + cvec_int vec = c_init(cvec_int, {1, 2, 3, 4, 5}); + printMe( c_LITERAL(intspan)cspan_from(&vec) ); + + printMe( sp2 ); + + cstack_int stk = c_init(cstack_int, {1, 2, 3, 4, 5, 6, 7}); + printMe( c_LITERAL(intspan)cspan_from(&stk) ); + + csset_str set = c_init(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"}); + printf("%d:", (int)csset_str_size(&set)); + c_foreach (e, csset_str, set) + printf(" %s", cstr_str(e.ref)); + puts(""); + + // cleanup + cvec_int_drop(&vec); + cstack_int_drop(&stk); + csset_str_drop(&set); +} -- cgit v1.2.3 From f1f0c01e798eb3217e62a43de660723173984547 Mon Sep 17 00:00:00 2001 From: tylov Date: Mon, 24 Jul 2023 12:54:09 +0200 Subject: Improved an issue with cspan. --- docs/cspan_api.md | 10 ++++++++++ include/stc/cspan.h | 18 +++++++++++------- misc/examples/spans/printspan.c | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) (limited to 'misc/examples/spans/printspan.c') 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 +#include #include 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 #include 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 #include -using_cspan(intspan, int, 1); +using_cspan(intspan, int); void printMe(intspan container) { printf("%d:", (int)cspan_size(&container)); -- cgit v1.2.3 From b6f7896ff21002e58e9af12fd553da32bda5f6d1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 14 Aug 2023 21:19:28 +0200 Subject: Simplified printspan.c example --- misc/examples/spans/printspan.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'misc/examples/spans/printspan.c') diff --git a/misc/examples/spans/printspan.c b/misc/examples/spans/printspan.c index eb9d80e3..b6999b61 100644 --- a/misc/examples/spans/printspan.c +++ b/misc/examples/spans/printspan.c @@ -1,18 +1,16 @@ -// printspan.c +// https://www.modernescpp.com/index.php/c-20-std-span/ #include -#define i_implement -#include #define i_key int #include + #define i_key int #include -#define i_key_str -#include #include using_cspan(intspan, int); + void printMe(intspan container) { printf("%d:", (int)cspan_size(&container)); c_foreach (e, intspan, container) @@ -20,33 +18,24 @@ void printMe(intspan container) { puts(""); } + int main(void) { - intspan sp1 = cspan_init(intspan, {1, 2}); - printMe( sp1 ); + printMe( c_init(intspan, {1, 2, 3, 4}) ); - printMe( c_init(intspan, {1, 2, 3}) ); + int arr[] = {1, 2, 3, 4, 5}; + printMe( (intspan)cspan_from_array(arr) ); - int arr[] = {1, 2, 3, 4, 5, 6}; - intspan sp2 = cspan_from_array(arr); - printMe( c_LITERAL(intspan)cspan_subspan(&sp2, 1, 4) ); - - cvec_int vec = c_init(cvec_int, {1, 2, 3, 4, 5}); - printMe( c_LITERAL(intspan)cspan_from(&vec) ); - - printMe( sp2 ); + cvec_int vec = c_init(cvec_int, {1, 2, 3, 4, 5, 6}); + printMe( (intspan)cspan_from(&vec) ); cstack_int stk = c_init(cstack_int, {1, 2, 3, 4, 5, 6, 7}); - printMe( c_LITERAL(intspan)cspan_from(&stk) ); + printMe( (intspan)cspan_from(&stk) ); - csset_str set = c_init(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"}); - printf("%d:", (int)csset_str_size(&set)); - c_foreach (e, csset_str, set) - printf(" %s", cstr_str(e.ref)); - puts(""); + intspan spn = c_init(intspan, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); + printMe( (intspan)cspan_subspan(&spn, 2, 8) ); // cleanup cvec_int_drop(&vec); cstack_int_drop(&stk); - csset_str_drop(&set); } -- cgit v1.2.3