diff --git a/m-array.h b/m-array.h index 301e8463..dd83b647 100644 --- a/m-array.h +++ b/m-array.h @@ -37,12 +37,23 @@ M_END_PROTECTED_CODE +/* Define a dynamic array of the given type and its associated functions + as the provided type name_t with the iterator named it_t + USAGE: ARRAY_DEF_AS(name, name_t, it_t, type [, oplist_of_the_type]) */ +#define ARRAY_DEF_AS(name, name_t, it_t, ...) \ + M_BEGIN_PROTECTED_CODE \ + ARRAYI_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_t, it_t ), \ + (name, __VA_ARGS__, name_t, it_t ))) \ + M_END_PROTECTED_CODE + + /* Define the oplist of a dynamic array given its name and its oplist. If no oplist is given it is assumed to be M_DEFAULT_OPLIST USAGE: ARRAY_OPLIST(name[, oplist of the type]) */ #define ARRAY_OPLIST(...) \ ARRAYI_OPLIST_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ - ((__VA_ARGS__, M_DEFAULT_OPLIST), \ + ((__VA_ARGS__, M_DEFAULT_OPLIST), \ (__VA_ARGS__ ))) diff --git a/m-list.h b/m-list.h index 91d66f89..d36e538a 100644 --- a/m-list.h +++ b/m-list.h @@ -33,7 +33,18 @@ M_BEGIN_PROTECTED_CODE \ LISTI_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), M_C(name,_t), M_C(name, _it_t) ), \ - (name, __VA_ARGS__, M_C(name,_t), M_C(name, _it_t) ))) \ + (name, __VA_ARGS__, M_C(name,_t), M_C(name, _it_t) ))) \ + M_END_PROTECTED_CODE + + +/* Define a singly linked list of a given type + as the provided type name_t with the iterator named it_t + USAGE: LIST_DEF_AS(name, name_t, it_t, type [, oplist_of_the_type]) */ +#define LIST_DEF_AS(name, name_t, it_t, ...) \ + M_BEGIN_PROTECTED_CODE \ + LISTI_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_t, it_t ), \ + (name, __VA_ARGS__, name_t, it_t ))) \ M_END_PROTECTED_CODE @@ -47,6 +58,17 @@ M_END_PROTECTED_CODE +/* Define a singly linked list of a given type allowing both push. + as the provided type name_t with the iterator named it_t + USAGE: LIST_DUAL_PUSH_DEF(name, type [, oplist_of_the_type]) */ +#define LIST_DUAL_PUSH_DEF_AS(name, name_t, it_t, ...) \ + M_BEGIN_PROTECTED_CODE \ + LISTI_DUAL_PUSH_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_t, it_t ), \ + (name, __VA_ARGS__, name_t, it_t ))) \ + M_END_PROTECTED_CODE + + /* Define the oplist of a list of the given type. USAGE: LIST_OPLIST(name [, oplist_of_the_type]) */ #define LIST_OPLIST(...) \ @@ -659,7 +681,7 @@ { \ assert (str != NULL && list != NULL); \ (append ? string_cat_str : string_set_str) (str, "["); \ - M_C(name, _it_t) it; \ + it_t it; \ for (M_C(name, _it)(it, list) ; \ !M_C(name, _end_p)(it); \ M_C(name, _next)(it)){ \ @@ -804,8 +826,8 @@ M_C(name, _equal_p)(const list_t list1, const list_t list2) \ { \ assert (list1 != NULL && list2 != NULL); \ - M_C(name, _it_t) it1; \ - M_C(name, _it_t) it2; \ + it_t it1; \ + it_t it2; \ if (list1 == list2) return true; \ M_C(name, _it)(it1, list1); \ M_C(name, _it)(it2, list2); \ @@ -829,7 +851,7 @@ { \ assert (list != NULL); \ M_HASH_DECL(hash); \ - M_C(name, _it_t) it; \ + it_t it; \ for(M_C(name, _it)(it, list) ; \ !M_C(name, _end_p)(it); \ M_C(name, _next)(it)) { \ diff --git a/tests/test-marray.c b/tests/test-marray.c index c03a3e5e..0f298e9e 100644 --- a/tests/test-marray.c +++ b/tests/test-marray.c @@ -38,6 +38,9 @@ ARRAY_DEF(array_min3_z, testobj_t, (CLEAR(testobj_clear))) ARRAY_DEF(array_ulong, uint64_t) +ARRAY_DEF_AS(array_double, ArrayDouble, ArrayDoubleIt, double) +#define M_OPL_ArrayDouble() ARRAY_OPLIST(array_double, M_DEFAULT_OPLIST) + static void test_uint(void) { array_uint_t v; @@ -350,10 +353,22 @@ static void test_d(void) array_uint_clear(a2); } +static void test_double(void) +{ + M_LET( (tab, 0.0, 1.0, 2.0, 3.0), ArrayDouble) { + double ref = 0.0; + for M_EACH(i, tab, ArrayDouble) { + assert (*i == ref); + ref += 1.0; + } + } +} + int main(void) { test_uint(); test_mpz(); test_d(); + test_double(); exit(0); } diff --git a/tests/test-mlist.c b/tests/test-mlist.c index c7918937..1a5f49cd 100644 --- a/tests/test-mlist.c +++ b/tests/test-mlist.c @@ -40,6 +40,12 @@ LIST_DEF(list_min_z, testobj_t, (INIT_SET(testobj_init_set), SET(testobj_set), C LIST_DUAL_PUSH_DEF(list2_min_z, testobj_t, (INIT_SET(testobj_init_set), SET(testobj_set), CLEAR(testobj_clear))) +LIST_DEF_AS(list_double, ListDouble, ListDoubleIt, double) +#define M_OPL_ListDouble() LIST_OPLIST(list_double, M_DEFAULT_OPLIST) + +LIST_DUAL_PUSH_DEF_AS(list_doubleDP, ListDoubleDP, ListDoubleDPIt, double) +#define M_OPL_ListDoubleDP() LIST_OPLIST(list_doubleDP, M_DEFAULT_OPLIST) + static void test_uint(void) { list_uint_t v; @@ -635,6 +641,25 @@ static void test_out_default_oplist(void) } #endif +static void test_double(void) +{ + M_LET( (tab, 0.0, 1.0, 2.0, 3.0), ListDouble) { + double ref = 0.0; + for M_EACH(i, tab, ListDouble) { + assert (*i == ref); + ref += 1.0; + } + } + + M_LET( (tab, 0.0, 1.0, 2.0, 3.0), ListDoubleDP) { + double ref = 0.0; + for M_EACH(i, tab, ListDoubleDP) { + assert (*i == ref); + ref += 1.0; + } + } +} + int main(void) { test_uint(); @@ -642,6 +667,7 @@ int main(void) test_dual_push1(); test_dual_it1(); test_out_default_oplist(); + test_double(); exit(0); }