From 3765c0b0fb5e4d37669257afdbe3756f0facaa79 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 1 Jul 2018 22:44:56 +0200 Subject: [PATCH] Add _equal_p method for OA dictionary. --- m-dict.h | 30 ++++++++++++++++++++++++++++++ tests/test-mdict.c | 10 +++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/m-dict.h b/m-dict.h index 529dc496..c2826a55 100644 --- a/m-dict.h +++ b/m-dict.h @@ -1429,6 +1429,36 @@ typedef enum { DICTI_OA_CONTRACT(dict); \ } \ \ + M_IF_METHOD(EQUAL, value_oplist)( \ + static inline bool \ + M_C(name, _equal_p)(dict_t dict1, dict_t dict2) \ + { \ + DICTI_OA_CONTRACT(dict1); \ + DICTI_OA_CONTRACT(dict2); \ + /* NOTE: Key type has mandatory equal operator */ \ + /* Easy case */ \ + if (M_LIKELY (dict1->count != dict2->count)) \ + return false; \ + if (M_UNLIKELY (dict1->count == 0)) \ + return true; \ + /* Otherwise this is the slow path */ \ + dict_it_t it; \ + for(M_C(name, _it)(it, dict1) ; \ + !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)(dict2, item->key); \ + if (ptr == NULL) \ + return false; \ + M_IF(isSet)(, \ + if (M_GET_EQUAL value_oplist (item->value, *ptr) == false) \ + return false; \ + ) \ + } \ + return true; \ + } \ + , /* no value equal */ ) \ + \ DICTI_FUNC_ADDITIONAL_DEF2(name, key_type, key_oplist, value_type, value_oplist, 0, dict_t, dict_it_t) diff --git a/tests/test-mdict.c b/tests/test-mdict.c index e3ce65cb..2b0d77a9 100644 --- a/tests/test-mdict.c +++ b/tests/test-mdict.c @@ -368,13 +368,13 @@ static void test_init_oa(void) dict_oa_int_remove (d1, 17); dict_oa_int_t d3; dict_oa_int_init_set (d3, d1); - //assert (dict_oa_int_equal_p (d3, d1)); + assert (dict_oa_int_equal_p (d3, d1)); dict_oa_int_set (d2, d1); assert (dict_oa_int_get (d2, -10) == NULL); assert (*dict_oa_int_get (d2, 10) == 11); - //assert (dict_oa_int_equal_p (d2, d1)); - //assert (dict_oa_int_equal_p (d2, d3)); + assert (dict_oa_int_equal_p (d2, d1)); + assert (dict_oa_int_equal_p (d2, d3)); dict_oa_int_clear (d3); dict_oa_int_set_at (d1, -10, -20); @@ -384,11 +384,11 @@ static void test_init_oa(void) assert (dict_oa_int_size (d1) == 101); assert (*dict_oa_int_get (d1, -10) == -22); - //assert (!dict_oa_int_equal_p (d2, d1)); + assert (!dict_oa_int_equal_p (d2, d1)); bool b = dict_oa_int_remove (d1, 0); assert (dict_oa_int_size (d1) == 100); assert (b); - //assert (!dict_oa_int_equal_p (d2, d1)); + assert (!dict_oa_int_equal_p (d2, d1)); for(size_t i = 1; i < 100; i++) { b = dict_oa_int_remove (d1, 2*i);