From 8914d336a04a3ed1800884aaccfa96b26353834b Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 18 Oct 2017 23:25:39 +0200 Subject: [PATCH] dict_ref & dict_cref returns a pointer to struct pair_s instead of pair_t (which is an array of struct pair_s). This will break API compatibility. However: it makes the user code cleaner as a level of indirection is removed. it uniformizes with the OA dict version --- README.md | 8 ++++---- example/ex-dict01.c | 4 ++-- example/ex-dict02.c | 6 +++--- example/ex-dict03.c | 4 ++-- example/ex-multi02.c | 8 ++++---- m-dict.h | 31 +++++++++++++++---------------- tests/test-mdict.c | 4 ++-- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 33aefdce..899639b1 100644 --- a/README.md +++ b/README.md @@ -1418,15 +1418,15 @@ Return true if 'it' references the last element or is no longer valid. Update the iterator 'it' to the next element. -##### name\_pair\_t *name\_ref(name\_it\_t it) +##### struct name\_pair\_s *name\_ref(name\_it\_t it) -Return a pointer to the pair composed by the key and its value. +Return a pointer to the pair composed by the key ('key' field) and its value ('value' field). 'key' element can not be modified. This pointer remains valid until the dictionary is modified by another method. -##### const name\_pair\_t *name\_ref(name\_it\_t it) +##### const struct name\_pair\_s *name\_ref(name\_it\_t it) -Return a constant pointer to the pair composed by the key and its value. +Return a constant pointer to the pair composed by the key('key' field) and its value ('value' field). This pointer remains valid until the dictionary is modified by another method. ##### void name\_get\_str(string\_t str, const name\_t dict, bool append) diff --git a/example/ex-dict01.c b/example/ex-dict01.c index 7661900d..87146ba5 100644 --- a/example/ex-dict01.c +++ b/example/ex-dict01.c @@ -42,9 +42,9 @@ int main(void) dict_str_it_t it; for(dict_str_it(it, h); !dict_str_end_p(it); dict_str_next(it)) { // Get a constant reference on the pair stored in the dictionnary - const dict_str_pair_t *pair = dict_str_cref(it); + const struct dict_str_pair_s *pair = dict_str_cref(it); printf ("Key=%s Value=%d\n", - string_get_cstr((*pair)->key), (*pair)->value); + string_get_cstr(pair->key), pair->value); } // Clear variables diff --git a/example/ex-dict02.c b/example/ex-dict02.c index 6c720901..13d38ef1 100644 --- a/example/ex-dict02.c +++ b/example/ex-dict02.c @@ -18,8 +18,8 @@ int main(void) { dict_m32_remove(h, 5); // h is now empty dict_m32_it_t it; // iterate over all dictionnary for (dict_m32_it (it, h) ; !dict_m32_end_p (it); dict_m32_next(it)) { - dict_m32_pair_t *item = dict_m32_ref(it); - (*item)->value = 1; // Set its value to 1 + struct dict_m32_pair_s *item = dict_m32_ref(it); + item->value = 1; // Set its value to 1 } dict_m32_clear(h); // h is cleared return 0; @@ -35,7 +35,7 @@ int main(void) { assert (is_missing); dict_m32_remove(h, 5); // h is now empty for M_EACH(item, h, M32_OPLIST) { // traverse each item - (*item)->value = 1; // Set its value to 1 + item->value = 1; // Set its value to 1 } } // h is cleared return 0; diff --git a/example/ex-dict03.c b/example/ex-dict03.c index e98c99da..790fca0d 100644 --- a/example/ex-dict03.c +++ b/example/ex-dict03.c @@ -76,8 +76,8 @@ int main(void) printf("Displaying Map by Iterator:\n"); for (dict_map_it(it, mp); !dict_map_end_p(it); dict_map_next(it)) { - const dict_map_pair_t *cref = dict_map_cref(it); - printf ("%d : %d\n", (*cref)->key, (*cref)->value); + const struct dict_map_pair_s *cref = dict_map_cref(it); + printf ("%d : %d\n", cref->key, cref->value); } break; diff --git a/example/ex-multi02.c b/example/ex-multi02.c index 5568ad22..41bd91ec 100644 --- a/example/ex-multi02.c +++ b/example/ex-multi02.c @@ -48,13 +48,13 @@ int main(void) // Iterate over the dictionary container for M_EACH(p, *item, MY_DICT_OPLIST) { // Dictionary iterator are pair (key,value) - if (my_integer_n_p((*p)->value)) { + if (my_integer_n_p(p->value)) { printf ("It is a long long, value = %lld\n", - *my_integer_get_n((*p)->value)); + *my_integer_get_n(p->value)); } - if (my_integer_z_p((*p)->value)) { + if (my_integer_z_p(p->value)) { printf ("It is a mpz_t, value ="); - mpz_out_str(stdout, 10, *my_integer_get_z((*p)->value)); + mpz_out_str(stdout, 10, *my_integer_get_z(p->value)); printf("\n"); } } diff --git a/m-dict.h b/m-dict.h index 076a4b48..fa4c6761 100644 --- a/m-dict.h +++ b/m-dict.h @@ -414,19 +414,19 @@ it2->list_it); \ } \ \ - static inline M_C(name, _pair_t) * \ + static inline struct M_C(name, _pair_s) * \ M_C(name, _ref)(const dict_it_t it) \ { \ assert(it != NULL); \ /* NOTE: partially unsafe if the user modify the 'key'. */ \ - return M_C(name, _list_pair_ref)(it->list_it); \ + return *M_C(name, _list_pair_ref)(it->list_it); \ } \ \ - static inline const M_C(name, _pair_t) * \ + static inline const struct M_C(name, _pair_s) * \ M_C(name, _cref)(const dict_it_t it) \ { \ assert(it != NULL); \ - return M_C(name, _list_pair_cref)(it->list_it); \ + return *M_C(name, _list_pair_cref)(it->list_it); \ } \ \ M_IF_METHOD(EQUAL, value_oplist)( \ @@ -445,12 +445,12 @@ for(M_C(name, _it)(it, dict1) ; \ !M_C(name, _end_p)(it); \ M_C(name, _next)(it)) { \ - const M_C(name, _pair_t) *item = M_C(name, _cref)(it); \ - value_type *ptr = M_C(name, _get)(dict2, (*item)->key); \ + const struct M_C(name, _pair_s) *item = M_C(name, _cref)(it); \ + value_type *ptr = M_C(name, _get)(dict2, item->key); \ if (ptr == NULL) \ return false; \ M_IF(isSet)(, \ - if (M_GET_EQUAL value_oplist ((*item)->value, *ptr) == false) \ + if (M_GET_EQUAL value_oplist (item->value, *ptr) == false) \ return false; \ ) \ } \ @@ -476,11 +476,11 @@ for (M_C(name, _it)(it, dict) ; \ !M_C(name, _end_p)(it); \ M_C(name, _next)(it)){ \ - const M_C(name, _pair_t) *item = \ + const struct M_C(name, _pair_s) *item = \ M_C(name, _cref)(it); \ - M_GET_GET_STR key_oplist (str, (*item)->key, true); \ + M_GET_GET_STR key_oplist (str, item->key, true); \ string_push_back (str, ':'); \ - M_GET_GET_STR value_oplist (str, (*item)->value, true); \ + M_GET_GET_STR value_oplist (str, item->value, true); \ if (!M_C(name, _last_p)(it)) \ string_push_back (str, ','); \ } \ @@ -500,11 +500,11 @@ for (M_C(name, _it)(it, dict) ; \ !M_C(name, _end_p)(it); \ M_C(name, _next)(it)){ \ - const M_C(name, _pair_t) *item = \ + const struct M_C(name, _pair_s) *item = \ M_C(name, _cref)(it); \ - M_GET_OUT_STR key_oplist (file, (*item)->key); \ + M_GET_OUT_STR key_oplist (file, item->key); \ fputc (':', file); \ - M_GET_OUT_STR value_oplist (file, (*item)->value); \ + M_GET_OUT_STR value_oplist (file, item->value); \ if (!M_C(name, _last_p)(it)) \ fputc (',', file); \ } \ @@ -557,7 +557,7 @@ MOVE(M_C(name, _move)), \ SWAP(M_C(name, _swap)), \ TYPE(M_C(name, _t)), \ - SUBTYPE(M_C(name, _pair_t)), \ + SUBTYPE(struct M_C(name, _pair_s)), \ IT_TYPE(M_C(name, _it_t)), \ IT_FIRST(M_C(name,_it)), \ IT_SET(M_C(name, _it_set)), \ @@ -1189,8 +1189,7 @@ typedef enum { return it1->dict == it2->dict && it1->index == it2->index; \ } \ \ - /* TODO: Same level of indirection compared to std dict */ \ - static inline M_C(name, _pair_t) * \ + static inline struct M_C(name, _pair_s) * \ M_C(name, _ref)(const dict_it_t it) \ { \ assert (it != NULL); \ diff --git a/tests/test-mdict.c b/tests/test-mdict.c index 2fea9754..b725838a 100644 --- a/tests/test-mdict.c +++ b/tests/test-mdict.c @@ -256,9 +256,9 @@ static void test1(void) size_t s = 0; bool check1 = false, check2= false; for M_EACH(r, dict, DICT_OPLIST(dict_str)) { - if (string_cmp_str((*r)->key, "README") == 0) + if (string_cmp_str(r->key, "README") == 0) check1 = true; - if (string_cmp_str((*r)->value, "BSD3") == 0) + if (string_cmp_str(r->value, "BSD3") == 0) check2 = true; s++; }