From cc33079d430d0d1921cb6b9772e2493c3c89b5a2 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 30 May 2020 23:38:31 +0200 Subject: [PATCH] Add comments --- example/ex-algo02.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/example/ex-algo02.c b/example/ex-algo02.c index 4be9258d..7115a835 100644 --- a/example/ex-algo02.c +++ b/example/ex-algo02.c @@ -4,33 +4,49 @@ /* Define a dynamic array of int */ ARRAY_DEF(vector_int, int) +/* Register the oplist of this dynamic array globally */ #define M_OPL_vector_int_t() ARRAY_OPLIST(vector_int) +/* Define some standards algorithms on this dynamic array */ ALGO_DEF(vector_int, vector_int_t) /* Define a dynamic array of bool */ ARRAY_DEF(vector_bool, bool) +/* Register the oplist of this dynamic array globally */ #define M_OPL_vector_bool_t() ARRAY_OPLIST(vector_bool) +/* Define some standards algorithms on this dynamic array */ ALGO_DEF(vector_bool, vector_bool_t) +/* ID function */ static bool id(bool x) { return x ; } int main(void) { + // Define, init (& later clear) myvector & tmp as vector_int_t M_LET(myvector, tmp, vector_int_t) { + // Fill myvector with 3 int equal to 20 vector_int_fill_n(myvector, 3, 20); + // Fill tmp with 3 int equal to 33 vector_int_fill_n(tmp, 3, 33); + // Append all tmp in myvector vector_int_splice(myvector, tmp); + // Fill tmp with 2 int equal to 10 vector_int_fill_n(tmp, 2, 10); + // Append all tmp in myvector vector_int_splice(myvector, tmp); + // Display the result printf("myvector contains:"); // In C11, the for loop can be simplified as : - // ALGO_FOR_EACH(myvector, vector_int_t, M_PRINT, " "); +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) + ALGO_FOR_EACH(myvector, vector_int_t, M_PRINT, " "); +#else for M_EACH(it, myvector, vector_int_t) { printf(" %d", *it); } +#endif printf("\n"); } + // Same thing but with booleans M_LET(myvector, tmp, vector_bool_t) { vector_bool_fill_n(myvector, 3, false); vector_bool_fill_n(tmp, 3, true); @@ -38,8 +54,6 @@ int main(void) vector_bool_fill_n(tmp, 2, false); vector_bool_splice(myvector, tmp); printf("myvector contains:"); - // In C11, the for loop can be simplified as : - // ALGO_FOR_EACH(myvector, vector_bool_t, M_PRINT, " "); for M_EACH(it, myvector, vector_bool_t) { printf(" %d", (int)*it); } @@ -49,5 +63,6 @@ int main(void) vector_bool_any_of_p(myvector, id), vector_bool_none_of_p(myvector, id) ); } + return 0; }