diff --git a/m-algo.h b/m-algo.h index 30762028..cf8fb12a 100644 --- a/m-algo.h +++ b/m-algo.h @@ -75,13 +75,30 @@ /* Define, initialize, set & clear a container with a variable array list. USAGE: - ALGO_LET_INIT_VA(container, containerOplist, param1[, param2[, ...]]) { - // Stuff with container + ALGO_LET_INIT_VA(container, containerOplist|type, param1[, param2[, ...]]) { + // Stuff with container } */ #define ALGO_LET_INIT_VA(dest, contOp, ...) \ ALGO_LET_INIT_VAI(dest, M_GLOBAL_OPLIST(contOp), __VA_ARGS__) +#define ALGO_INSERT_AT(contDst, contDstOp, position, contSrc, contSrcOp) \ + ALGOI_INSERT_AT(contDst, M_GLOBAL_OPLIST(contDstOp), position, contSrc, M_GLOBAL_OPLIST(contSrcOp) ) + +#define ALGOI_INSERT_AT(contDst, contDstOp, position, contSrc, contSrcOp) do { \ + M_GET_IT_TYPE contSrcOp itSrc; \ + M_GET_IT_TYPE contDstOp itDst; \ + M_GET_IT_SET contDstOp (itDst, position); \ + for (M_GET_IT_FIRST contSrcOp (itSrc, contSrc) ; \ + !M_GET_IT_END_P contSrcOp (itSrc) ; \ + M_GET_IT_NEXT contSrcOp (itSrc) ) { \ + M_GET_IT_INSERT contDstOp (contDst, itDst, \ + *M_GET_IT_CREF contSrcOp (itSrc)); \ + M_GET_IT_NEXT contDstOp (itDst); \ + } \ + } while (0) + + /********************************** INTERNAL ************************************/ #define ALGOI_DEF(name, cont_oplist) \ @@ -668,7 +685,7 @@ } \ } \ while (!M_GET_IT_END_P cont_oplist (itSrc)) { \ - type *objSrc = M_GET_REF cont_oplist (itSrc); \ + type_t *objSrc = M_GET_IT_REF cont_oplist (itSrc); \ M_GET_PUSH cont_oplist (dst, *objSrc); \ M_GET_IT_NEXT cont_oplist (itSrc); \ } \ diff --git a/tests/test-malgo.c b/tests/test-malgo.c index b630d125..a3b49fa2 100644 --- a/tests/test-malgo.c +++ b/tests/test-malgo.c @@ -386,11 +386,31 @@ static void test_extract(void) list_int_clear(l); } +ARRAY_DEF(aint, int) +LIST_DEF(lint, int) +#define M_OPL_aint_t() ARRAY_OPLIST(aint) +#define M_OPL_lint_t() LIST_OPLIST(lint) + +static void test_insert(void) +{ + ALGO_LET_INIT_VA(a, lint_t, 1, 2, 3, 4) + ALGO_LET_INIT_VA(b, aint_t, -1, -2, -3) { + lint_it_t i; + lint_it(i, a); + ALGO_INSERT_AT(a, lint_t, i, b, aint_t); + // TBC: order ok for the list (the reverse is confusing...)? + ALGO_LET_INIT_VA(c, lint_t, 1, 2, 3, -3, -2, -1, 4) { + assert (lint_equal_p (c, a)); + } + } +} + int main(void) { test_list(); test_array(); test_string(); test_extract(); + test_insert(); exit(0); }