Boost logo

Boost Users :

Subject: Re: [Boost-users] generate structs with all combinations of members from a given struct
From: Daniel James (daniel_james_at_[hidden])
Date: 2010-01-30 04:00:30


On 30 January 2010 06:57, joel falcou <joel.falcou_at_[hidden]> wrote:
> Hicham Mouline wrote:
>>
>> Hello,
>>  struct params {
>>  double field1;
>>  double field2;
>>  double field3;
>>  double fiedl4;
>> };
>>  I would like to generate all the types that hold any combination of the
>> members of params.
>>
>
> Can't you like generate a fusion vector out of combination enumeration ?
>
> like having make_combo< params, vector_c<1,3,4> >

You'd want a map, at least. Consider:

template <typename T>
void foo(T const& x)
{
    x.field3 *= 2;
}

How would you translate that to take a vector, if 'field3' could be in
different positions? But I've never used a fusion map so I don't know
how you'd go about that.

Here is are some macros to generate the necessary subsets. The main
loop could be implement using a fold, but as there can't be more than
8 iterations it seemed easier to hard code it.

Daniel

#include <boost/preprocessor.hpp>

#define SUBSETS(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(5, (field1, field2, field3, field4, field5))


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