From 50b0e2f192ecbb9abbb12c16db2bb276f72553b7 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 25 Nov 2023 13:16:11 +0100 Subject: [PATCH] WIP --- m-generic.h | 55 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/m-generic.h b/m-generic.h index 8867041b..32d847d4 100644 --- a/m-generic.h +++ b/m-generic.h @@ -81,7 +81,7 @@ struct m_g3neric_dummys; #define M_G3N_TYPE_1_func_test(gentype, op, x, oplist) \ M_IF_METHOD(op, oplist())(M_G3N_TYPE_1_func_expand, M_EAT)(gentype, op, x, oplist) #define M_G3N_TYPE_1_func_expand(gentype, op, x, oplist) \ - gentype: ( M_C(M_GET_, op)(oplist() )) { 0 }, \ + gentype: ( M_C(M_GET_, op)(oplist() )) { 0 }, \ const gentype: ( M_C(M_GET_, op)(oplist() ) ) { 0 }, // Call the OPERATOR call of the oplist registered to the variable 'x' @@ -139,10 +139,10 @@ struct m_g3neric_dummys; #define M_G3N_CALL_21_func(x, oplist) \ M_G3N_CALL_21_func_test(M_GET_GENTYPE oplist(), \ M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) -#define M_G3N_CALL_21_func_test(gentype, op, x, y, oplist) \ +#define M_G3N_CALL_21_func_test(gentype, op, x, y, oplist) \ M_IF_METHOD(op, oplist())(M_G3N_CALL_21_func_expand, M_EAT)(gentype, op, x, y, oplist) -#define M_G3N_CALL_21_func_expand(gentype, op, x, y, oplist) \ - gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(gentype, x), y), \ +#define M_G3N_CALL_21_func_expand(gentype, op, x, y, oplist) \ + gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(gentype, x), y), \ const gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(const gentype, x), y), // Call the OPERATOR call of the oplist registered to the variable 'x' @@ -154,10 +154,10 @@ struct m_g3neric_dummys; #define M_G3N_CALL_22_func(x, oplist) \ M_G3N_CALL_22_func_test(M_GET_GENTYPE oplist(), \ M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) -#define M_G3N_CALL_22_func_test(gentype, op, x, y, oplist) \ +#define M_G3N_CALL_22_func_test(gentype, op, x, y, oplist) \ M_IF_METHOD(op, oplist())(M_G3N_CALL_22_func_expand, M_EAT)(gentype, op, x, y, oplist) -#define M_G3N_CALL_22_func_expand(gentype, op, x, y, oplist) \ - gentype: M_C(M_CALL_, op)(oplist(), x, M_AS_TYPE(gentype, y)), \ +#define M_G3N_CALL_22_func_expand(gentype, op, x, y, oplist) \ + gentype: M_C(M_CALL_, op)(oplist(), x, M_AS_TYPE(gentype, y)), \ const gentype: M_C(M_CALL_, op)(oplist(), x, M_AS_TYPE(const gentype, y)), // Call the OPERATOR call of the oplist registered to the variable 'x' @@ -170,31 +170,52 @@ struct m_g3neric_dummys; M_G3N_CALL_2k_func_test(M_GET_GENTYPE oplist(), \ M_IF_METHOD(GENTYPE, M_GET_KEY_OPLIST oplist())(M_GET_GENTYPE M_GET_KEY_OPLIST oplist(), M_GET_KEY_TYPE oplist()), \ M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) -#define M_G3N_CALL_2k_func_test(gentype, keytype, op, x, y, oplist) \ +#define M_G3N_CALL_2k_func_test(gentype, keytype, op, x, y, oplist) \ M_IF_METHOD(op, oplist())(M_G3N_CALL_2k_func_expand, M_EAT)(gentype, keytype, op, x, y, oplist) -#define M_G3N_CALL_2k_func_expand(gentype, keytype, op, x, y, oplist) \ +#define M_G3N_CALL_2k_func_expand(gentype, keytype, op, x, y, oplist) \ gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(gentype, x), M_AS_LINKED_TYPE(gentype, x, keytype, y)), \ const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, x, gentype, x), M_AS_LINKED_TYPE(const gentype, x, keytype, y)), +// Call the OPERATOR call of the oplist registered to the variable 'x' +// which takes two argument (it type, type) +#define M_G3N_CALL_i2(op, x, y) \ + _Generic( ((void)0, (y)), \ + M_MAP2(M_G3N_CALL_i2_func, (op, x, y) M_G3N_REGISTERED_ITEMS() ) \ + struct m_g3neric_dummys *: /* cannot happen */ (void) 0) +#define M_G3N_CALL_i2_func(x, oplist) \ + M_G3N_CALL_i2_func_test(M_GET_GENTYPE oplist(), \ + M_GET_IT_TYPE oplist(), \ + M_TRIPLE_1 x, M_TRIPLE_2 x, M_TRIPLE_3 x, oplist) +#define M_G3N_CALL_i2_func_test(gentype, ittype, op, x, y, oplist) \ + M_IF_METHOD(op, oplist())(M_G3N_CALL_i2_func_expand, M_EAT)(gentype, ittype, op, x, y, oplist) +#define M_G3N_CALL_i2_func_expand(gentype, ittype, op, x, y, oplist) \ + gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(gentype, y, ittype, x), M_AS_TYPE(gentype, y)), \ + const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, y, ittype, x), M_AS_LINKED_TYPE(const gentype, y, gentype, y)), + // Call the OPERATOR call of the oplist registered to the variable 'x' // which takes two argument (type, keytype, valuetype) -#define M_G3N_CALL_3kv(op, x, y, z) \ +#define M_G3N_CALL_3kv(op, x, y, z) \ _Generic( ((void)0, (x)), \ - M_MAP2(M_G3N_CALL_3kv_func, (op, x, y, z) M_G3N_REGISTERED_ITEMS() ) \ + M_MAP2(M_G3N_CALL_3kv_func, (op, x, y, z) M_G3N_REGISTERED_ITEMS() ) \ struct m_g3neric_dummys *: /* cannot happen */ (void) 0) -#define M_G3N_CALL_3kv_func(x, oplist) \ - M_G3N_CALL_3kv_func_test(M_GET_GENTYPE oplist(), \ +#define M_G3N_CALL_3kv_func(x, oplist) \ + M_G3N_CALL_3kv_func_test(M_GET_GENTYPE oplist(), \ M_IF_METHOD(GENTYPE, M_GET_KEY_OPLIST oplist())(M_GET_GENTYPE M_GET_KEY_OPLIST oplist(), M_GET_KEY_TYPE oplist()), \ M_IF_METHOD(GENTYPE, M_GET_VALUE_OPLIST oplist())(M_GET_GENTYPE M_GET_VALUE_OPLIST oplist(), M_GET_VALUE_TYPE oplist()), \ M_QUAD_1 x, M_QUAD_2 x, M_QUAD_3 x, M_QUAD_4 x, oplist) -#define M_G3N_CALL_3kv_func_test(gentype, keytype, valuetype, op, x, y, z, oplist) \ +#define M_G3N_CALL_3kv_func_test(gentype, keytype, valuetype, op, x, y, z, oplist) \ M_IF_METHOD(op, oplist())(M_G3N_CALL_3kv_func_expand, M_EAT)(gentype, keytype, valuetype, op, x, y, z, oplist) -#define M_G3N_CALL_3kv_func_expand(gentype, keytype, valuetype, op, x, y, z, oplist) \ +#define M_G3N_CALL_3kv_func_expand(gentype, keytype, valuetype, op, x, y, z, oplist) \ gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(gentype, x), M_AS_LINKED_TYPE(gentype, x, keytype, y), M_AS_LINKED_TYPE(gentype, x, valuetype, z)), \ const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, x, gentype, x), M_AS_LINKED_TYPE(const gentype, x, keytype, y), M_AS_LINKED_TYPE(const gentype, x, valuetype, z) ), #define M_AS_LINKED_TYPE(typex, x, typey, y) _Generic(((void)0,(x)), typex: (y), default: (typey) {0}) +// Advantage: ??? +#define M_AS_IF(typex, x, typey, y) ((typey) {_Generic((void)0,x), typex: y, default: 0)}) + +#define M_GEN_IT_TYPE(it_type) typeof( ((void)0, (it_type){0})) + // Define generic functions // TODO: m_ prefix? #define init(x) M_G3N_CALL_1(INIT, x) @@ -212,7 +233,9 @@ struct m_g3neric_dummys; // splice_at(cont, it, cont, it) #define it_type(x) m_typeof(M_G3N_TYPE_1(IT_TYPE, x)) #define sub_type(x) m_typeof(M_G3N_TYPE_1(SUBTYPE, x)) -// it_first(it_type, cont) + it_last + it_end +#define it_first(x, y) M_G3N_CALL_i2(IT_FIST, x, y) +#define it_last(x, y) M_G3N_CALL_i2(IT_LAST, x, y) +#define it_end(x, y) M_G3N_CALL_i2(IT_END, x, y) // it_set(it_type, it_type) // it_end_p(it) + it_last_p(it) + it_next(it) + it_previous(it) + it_ref(it) + it_cref(it) // it_equal_p(it, it)