From a23df528e493203c3eca02049d2c7edc6316ed48 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 15 Oct 2020 22:05:18 +0200 Subject: [PATCH] Add DICT_*_AS macros. --- m-dict.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test-mdict.c | 8 +++++ 2 files changed, 84 insertions(+) diff --git a/m-dict.h b/m-dict.h index 520aadc8..d779204b 100644 --- a/m-dict.h +++ b/m-dict.h @@ -44,6 +44,21 @@ M_END_PROTECTED_CODE +/* Define a dictionary associating the key key_type to the value value_type and its associated functions. + as the given name name_t with its associated functions. + USAGE: + DICT_DEF2_AS(name, name_t, it_t, itref_t, key_type, key_oplist, value_type, value_oplist) + OR + DICT_DEF2_AS(name, name_t, it_t, itref_t, key_type, value_type) +*/ +#define DICT_DEF2_AS(name, name_it, it_t, itref_t, key_type, ...) \ + M_BEGIN_PROTECTED_CODE \ + DICTI_DEF2_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, key_type, M_GLOBAL_OPLIST_OR_DEF(key_type)(), __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_t, it_t, itref_t ), \ + (name, key_type, __VA_ARGS__, name_t, it_t, itref_t ) )) \ + M_END_PROTECTED_CODE + + /* Define a dictionary asssociating the key key_type to the value value_type and its associated functions. It stores the computed hash value, avoiding the need of recomputing it but increasing memory consumption. @@ -60,6 +75,23 @@ M_END_PROTECTED_CODE +/* Define a dictionary asssociating the key key_type to the value value_type and its associated functions. + as the given name name_t with its associated functions. + It stores the computed hash value, avoiding the need of recomputing it but increasing memory + consumption. + USAGE: + DICT_STOREHASH_DEF2_AS(name, name_it, it_t, itref_t, key_type[, key_oplist], value_type[, value_oplist]) + OR + DICT_STOREHASH_DEF2_AS(name, name_it, it_t, itref_t, key_type[, key_oplist], value_type[, value_oplist]) +*/ +#define DICT_STOREHASH_DEF2_AS(name, name_it, it_t, itref_t, key_type, ...) \ + M_BEGIN_PROTECTED_CODE \ + DICTI_SHASH_DEF2_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, key_type, M_GLOBAL_OPLIST_OR_DEF(key_type)(), __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_it, it_t, itref_t ), \ + (name, key_type, __VA_ARGS__, name_it, it_t, itref_t ))) \ + M_END_PROTECTED_CODE + + /* Define a dictionary associating the key key_type to the value value_type with an Open Addressing implementation and its associated functions. KEY_OPLIST needs the operators OOR_EQUAL & OOR_SET. @@ -76,6 +108,23 @@ M_END_PROTECTED_CODE +/* Define a dictionary associating the key key_type to the value value_type + with an Open Addressing implementation and its associated functions. + as the given name name_t with its associated functions. + KEY_OPLIST needs the operators OOR_EQUAL & OOR_SET. + USAGE: + DICT_OA_DEF2_AS(name, name_it, it_t, itref_t, key_type, key_oplist, value_type, value_oplist) + OR + DICT_OA_DEF2_AS(name, name_it, it_t, itref_t, key_type, value_type) +*/ +#define DICT_OA_DEF2_AS(name, name_it, it_t, itref_t, key_type, ...) \ + M_BEGIN_PROTECTED_CODE \ + DICTI_OA_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, key_type, M_GLOBAL_OPLIST_OR_DEF(key_type)(), __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_it, it_t, itref_t ), \ + (name, key_type, __VA_ARGS__, name_it, it_t, itref_t ))) \ + M_END_PROTECTED_CODE + + /* Define a set of the key key_type and its associated functions. The set is unordered. USAGE: DICT_SET_DEF(name, key_type[, key_oplist]) @@ -88,6 +137,19 @@ M_END_PROTECTED_CODE +/* Define a set of the key key_type and its associated functions. + as the given name name_t with its associated functions. + The set is unordered. + USAGE: DICT_SET_DEF_AS(name, name_it, it_t, itref_t, key_type[, key_oplist]) +*/ +#define DICT_SET_DEF_AS(name, name_it, it_t, itref_t, ...) \ + M_BEGIN_PROTECTED_CODE \ + DICTI_SET_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_it, it_t, itref_t ), \ + (name, __VA_ARGS__, name_it, it_t, itref_t ))) \ + M_END_PROTECTED_CODE + + /* Define a set of the key key_type with an Open Addressing implementation and its associated functions. The set is unordered. @@ -101,6 +163,20 @@ M_END_PROTECTED_CODE +/* Define a set of the key key_type + with an Open Addressing implementation and its associated functions. + as the given name name_t with its associated functions. + The set is unordered. + USAGE: DICT_OASET_DEF_AS(name, name_it, it_t, itref_t, key_type[, key_oplist]) +*/ +#define DICT_OASET_DEF_AS(name, name_it, it_t, itref_t, ...) \ + M_BEGIN_PROTECTED_CODE \ + DICTI_OASET_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_it, it_t, itref_t ), \ + (name, __VA_ARGS__, name_it, it_t, itref_t ))) \ + M_END_PROTECTED_CODE + + /* Define the oplist of a dictionnary (DICT_DEF2, DICT_STOREHASH_DEF2 or DICT_OA_DEF2). USAGE: DICT_OPLIST(name, oplist of the key type, oplist of the value type) diff --git a/tests/test-mdict.c b/tests/test-mdict.c index bbe6777a..2f48de16 100644 --- a/tests/test-mdict.c +++ b/tests/test-mdict.c @@ -48,6 +48,14 @@ DICT_OA_DEF2(dict_oa_str, symbol_t, BOUNDED_STRING_OPLIST(symbol), int, M_DEFAUL DICT_OA_DEF2(dict_oa_bstr, string_t, STRING_OPLIST, int, M_DEFAULT_OPLIST) DICT_OASET_DEF(dict_oa_setstr, string_t, STRING_OPLIST) + +DICT_DEF2_AS(dictas_int, DictInt, DictIntIt, DictIntItRef, int, M_DEFAULT_OPLIST, int, M_DEFAULT_OPLIST) +DICT_STOREHASH_DEF2_AS(dictas_str2, DictSInt, DictSIntIt, DictSIntItRef, string_t, STRING_OPLIST, string_t, STRING_OPLIST) +DICT_SET_DEF_AS(dictas_setstr, DictStr, DictStrIt, DictStrItRef, string_t, STRING_OPLIST) +DICT_OA_DEF2_AS(dictas_oa_bstr, DictOAStr, DictOAStrIt, DictOAStrItRef, string_t, STRING_OPLIST, int, M_DEFAULT_OPLIST) +DICT_OASET_DEF_AS(dictas_oa_setstr, DictOASStr, DictOASStrIt, DictOASStrItRef, string_t, STRING_OPLIST) + + /* Helper structure */ ARRAY_DEF(array_string, string_t, STRING_OPLIST) array_string_t v_str;