From 6bb94db22f317dde7045845b70271c607eca0693 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 15 Apr 2018 21:02:14 +0200 Subject: [PATCH] Add function dict_splice --- m-dict.h | 29 ++++++++++++++++++++++++++++- tests/test-mdict.c | 14 ++++---------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/m-dict.h b/m-dict.h index 8a64f936..6ae6c015 100644 --- a/m-dict.h +++ b/m-dict.h @@ -488,7 +488,7 @@ \ DICTI_FUNC_ADDITIONAL_DEF2(name, key_type, key_oplist, value_type, value_oplist, isSet, dict_t, dict_it_t) - +// FIXME: Seems there are some issues with set and oa (contract / fields ?) /* Define additional functions for dictionnary. Do not used any fields of the dictionnary */ #define DICTI_FUNC_ADDITIONAL_DEF2(name, key_type, key_oplist, value_type, value_oplist, isSet, dict_t, dict_it_t) \ @@ -573,6 +573,27 @@ return c == '}'; \ } \ , /* no IN_STR */ ) \ + \ + M_IF_METHOD(UPDATE, value_oplist)( \ + static inline void \ + M_C(name, _splice)(dict_t d1, dict_t d2) \ + { \ + dict_it_t it; \ + /* NOTE: Despite using set_at, the accessing of the item in d1 \ + is not as random as other uses of the HASH table as d2 \ + uses the same order than d1 */ \ + for (M_C(name, _it)(it, d2); !M_C(name, _end_p)(it); M_C(name, _next)(it)){ \ + const struct M_C(name, _pair_s) *item = M_C(name, _cref)(it); \ + value_type *ptr = M_C(name, _get)(d1, item->key); \ + if (ptr == NULL) { \ + M_C(name, _set_at)(d1, item->key, item->value); \ + } else { \ + M_GET_UPDATE value_oplist (*ptr, item->value); \ + } \ + } \ + M_C(name, _clean)(d2); \ + } \ + , /* NO UPDATE */) \ /* Define the oplist of a dictionnary */ @@ -1234,6 +1255,12 @@ typedef enum { return &it->dict->data[i]; \ } \ \ + static inline const struct M_C(name, _pair_s) * \ + M_C(name, _cref)(const dict_it_t it) \ + { \ + return M_CONST_CAST(struct M_C(name, _pair_s), M_C(name, _ref)(it)); \ + } \ + \ static inline void \ M_C(name,_reserve)(dict_t dict, size_t capacity) \ { \ diff --git a/tests/test-mdict.c b/tests/test-mdict.c index 114741aa..925cadcd 100644 --- a/tests/test-mdict.c +++ b/tests/test-mdict.c @@ -27,19 +27,13 @@ #include "m-string.h" #include "mympz.h" -static inline bool oor_equal_p(int k, unsigned char n) -{ - return k == (int)-n-1; -} -static inline void oor_set(int *k, unsigned char n) -{ - *k = (int)-n-1; -} - +static inline bool oor_equal_p(int k, unsigned char n) { return k == (int)-n-1; } +static inline void oor_set(int *k, unsigned char n) { *k = (int)-n-1; } +static inline void update_value (int *p, const int val) { *p += val; } #include "coverage.h" START_COVERAGE DICT_DEF2(dict_str, string_t, STRING_OPLIST, string_t, STRING_OPLIST) -DICT_OA_DEF2(dict_oa_int, int, M_OPEXTEND(M_DEFAULT_OPLIST, OOR_EQUAL(oor_equal_p), OOR_SET(oor_set M_IPTR)), int, M_DEFAULT_OPLIST) +DICT_OA_DEF2(dict_oa_int, int, M_OPEXTEND(M_DEFAULT_OPLIST, OOR_EQUAL(oor_equal_p), OOR_SET(oor_set M_IPTR)), int, M_OPEXTEND(M_DEFAULT_OPLIST, UPDATE(update_value M_IPTR))) END_COVERAGE DICT_STOREHASH_DEF2(dict_str2, string_t, STRING_OPLIST, string_t, STRING_OPLIST)