|
Boost Users : |
Subject: Re: [Boost-users] generate structs with all combinations of members from a given struct
From: Hicham Mouline (hicham_at_[hidden])
Date: 2010-02-07 09:16:34
> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of Daniel James
> Sent: 06 February 2010 20:51
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] generate structs with all combinations of
> members from a given struct
>
> On 6 February 2010 19:42, Hicham Mouline <hicham_at_[hidden]> wrote:
> >
> > Attached now,
>
> The code I posted for getting the subsets before should be faster,
> here it is adapted for sequences:
Brilliant!
Your code is a lot faster.
I adapted it to generate the structs.
#include <boost/preprocessor.hpp>
/////////////////////////////////////////////////////////////////////
// Generates the non-empty subsets of a sequence
#define STATEMENT_FROM_PAIR(pair) BOOST_PP_TUPLE_ELEM(2,0,pair)
BOOST_PP_TUPLE_ELEM(2,1,pair);
#define PRED(r,state) BOOST_PP_SEQ_SIZE(state)
#define OP(r,state) BOOST_PP_SEQ_POP_FRONT(state)
#define MACRO(r,state) STATEMENT_FROM_PAIR(BOOST_PP_SEQ_HEAD(state))
#define PRED1(r,state) BOOST_PP_SEQ_SIZE(BOOST_PP_TUPLE_ELEM(2,0,state))
#define OP1(r,state)
(BOOST_PP_SEQ_POP_FRONT(BOOST_PP_TUPLE_ELEM(2,0,state)),\
BOOST_PP_CAT(\
BOOST_PP_TUPLE_ELEM(2,1,state),\
BOOST_PP_CAT(\
_,\
BOOST_PP_TUPLE_ELEM(2,1,\
BOOST_PP_SEQ_HEAD(BOOST_PP_TUPLE_ELEM(2,0,state))\
)\
)\
)\
)
#define MAKE_STRUCT(r,data,elem) struct
BOOST_PP_TUPLE_ELEM(2,1,BOOST_PP_WHILE(PRED1,OP1,(elem,data))) \
{ BOOST_PP_FOR(elem, PRED, OP, MACRO) };
#define SUBSETS(struct_name,values) BOOST_PP_SEQ_FOR_EACH(MAKE_STRUCT,
struct_name, SUBSETS2(BOOST_PP_SEQ_SIZE(values),
BOOST_PP_SEQ_TO_TUPLE(values)))
#define SUBSETS2(size, values) BOOST_PP_CAT(SUBSETS_, size) values
#define SUBSETS_1(a1) ((a1))
#define SUBSETS_2(a1,a2) SUBSETS_COMBINE(a2, SUBSETS_1(a1))
#define SUBSETS_3(a1,a2,a3) SUBSETS_COMBINE(a3, SUBSETS_2(a1,a2))
#define SUBSETS_4(a1,a2,a3,a4) SUBSETS_COMBINE(a4, SUBSETS_3(a1,a2,a3))
#define SUBSETS_5(a1,a2,a3,a4,a5) SUBSETS_COMBINE(a5,
SUBSETS_4(a1,a2,a3,a4))
#define SUBSETS_6(a1,a2,a3,a4,a5,a6) SUBSETS_COMBINE(a6,
SUBSETS_5(a1,a2,a3,a4,a5))
#define SUBSETS_7(a1,a2,a3,a4,a5,a6,a7) SUBSETS_COMBINE(a7,
SUBSETS_6(a1,a2,a3,a4,a5,a6))
#define SUBSETS_8(a1,a2,a3,a4,a5,a6,a7,a8) SUBSETS_COMBINE(a8,
SUBSETS_7(a1,a2,a3,a4,a5,a6,a7))
#define SUBSETS_COMBINE(x, seq_seq) \
seq_seq \
((x)) \
BOOST_PP_SEQ_FOR_EACH(SUBSETS_COMBINE_IMPL, x, seq_seq)
#define SUBSETS_COMBINE_IMPL(r, elem, seq) (seq (elem))
/////////////////////////////////////////////////////////////////////
// Example of use:
SUBSETS(
params,
((char, field1))
((float, field2))
((std::string, field3))
((int, field4))
)
///generates this
/// struct params_field1 { char field1; }; struct params_field2 { float
field2; }; struct params_field1_field2 { char field1; float field2; };
struct params_field3 { std::string field3; }; struct params_field1_field3 {
char field1; std::string field3; }; struct params_field2_field3 { float
field2; std::string field3; }; struct params_field1_field2_field3 { char
field1; float field2; std::string field3; }; struct params_field4 { int
field4; }; struct params_field1_field4 { char field1; int field4; }; struct
params_field2_field4 { float field2; int field4; }; struct
params_field1_field2_field4 { char field1; float field2; int field4; };
struct params_field3_field4 { std::string field3; int field4; }; struct
params_field1_field3_field4 { char field1; std::string field3; int field4;
}; struct params_field2_field3_field4 { float field2; std::string field3;
int field4; }; struct params_field1_field2_field3_field4 { char field1;
float field2; std::string field3; int field4; };
How do I use BOOST_PP_ITERATE to get vertical output of the structs, ie,
each struct on a different line?
Thanks,
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net