From acb886c3913429bd8ea126bd9b5ae439c63ddfab Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Fri, 14 Aug 2020 23:34:59 +0200 Subject: [PATCH] Add macro RBTREE_DEF_AS --- m-rbtree.h | 11 +++++++++++ tests/test-mrbtree.c | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/m-rbtree.h b/m-rbtree.h index 634189ec..8b33162a 100644 --- a/m-rbtree.h +++ b/m-rbtree.h @@ -39,6 +39,17 @@ M_END_PROTECTED_CODE +/* Define a Red/Black binary tree of a given type + as the name name_t and the iterator it_t. + USAGE: RBTREE_DEF_AS(name, name_t, it_t, type [, oplist_of_the_type]) */ +#define RBTREE_DEF_AS(name, name_t, it_t, ...) \ + M_BEGIN_PROTECTED_CODE \ + RBTREEI_DEF_P1(M_IF_NARGS_EQ1(__VA_ARGS__) \ + ((name, __VA_ARGS__, M_GLOBAL_OPLIST_OR_DEF(__VA_ARGS__)(), name_t, M_C(name, _node_ct), it_t ), \ + (name, __VA_ARGS__, name_t, M_C(name, _node_ct), it_t ))) \ + M_END_PROTECTED_CODE + + /* Define the oplist of a rbtree of type. USAGE: RBTREE_OPLIST(name [, oplist_of_the_type]) */ #define RBTREE_OPLIST(...) \ diff --git a/tests/test-mrbtree.c b/tests/test-mrbtree.c index 50361cd0..9c43e182 100644 --- a/tests/test-mrbtree.c +++ b/tests/test-mrbtree.c @@ -60,6 +60,9 @@ RBTREE_DEF(rbtree_mpz, testobj_t, TESTOBJ_OPLIST) #define UINT_OPLIST RBTREE_OPLIST(rbtree_uint) #define FLOAT_OP RBTREE_OPLIST(rbtree_float) +RBTREE_DEF_AS(TreeDouble, TreeDouble, TreeDoubleIt, double, M_DEFAULT_OPLIST) +#define M_OPL_TreeDouble() RBTREE_OPLIST(TreeDouble, M_DEFAULT_OPLIST) + static void test_uint(void) { rbtree_uint_t tree, tree2, tree3; @@ -257,8 +260,18 @@ static void test_io(void) assert(b); assert(*sp == 0); assert(rbtree_uint_equal_p(tree1, tree2)); + } +} + +static void test_double(void) +{ + M_LET( (tree, 0.0, 1.0, 2.0), TreeDouble) { + double ref = 0.0; + for M_EACH(i, tree, TreeDouble) { + assert (*i == ref); + ref += 1.0; + } } - } int main(void) @@ -267,5 +280,6 @@ int main(void) test_uint_permut(); test_float(); test_io(); + test_double(); exit(0); }