From 90aab650b31febadcea45cfa60d0620b4d807ee9 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 18 Jan 2023 15:41:12 +0100 Subject: Added cspan (1D) example. --- misc/examples/printspan.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 misc/examples/printspan.c diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c new file mode 100644 index 00000000..a897e669 --- /dev/null +++ b/misc/examples/printspan.c @@ -0,0 +1,42 @@ +// printspan.c + +#include +#define i_val int +#include +#define i_val int +#include +#define i_val int +#include +#include + +using_cspan(ispan, int, 1); + +void printMe(ispan container) { + printf("%d\n", (int)cspan_size(&container)); + c_FOREACH (e, ispan, container) printf("%d ", *e.ref); + puts(""); +} + +int main() { + c_AUTO (cvec_int, vec) + c_AUTO (cstack_int, stk) + c_AUTO (cdeq_int, deq) + { + int arr[] = {1, 2, 3, 4, 5}; + ispan sp = cspan_from_array(arr); + printMe((ispan)cspan_subspan(&sp, 1, 3)); + + printMe((ispan)cspan_from_list(int, {1, 2, 3, 4})); + + printMe((ispan)cspan_from_array(arr)); + + c_FORLIST (i, int, {1, 2, 3, 4, 5, 6}) cvec_int_push(&vec, *i.ref); + printMe((ispan)cspan_from(&vec)); + + c_FORLIST (i, int, {1, 2, 3, 4, 5, 6, 7}) cstack_int_push(&stk, *i.ref); + printMe((ispan)cspan_from(&stk)); + + c_FORLIST (i, int, {1, 2, 3, 4, 5, 6, 7, 8}) cdeq_int_push_front(&deq, *i.ref); + printMe((ispan)cspan_from(&deq)); + } +} -- cgit v1.2.3