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-02-06 15:50:32


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:

#include <boost/preprocessor.hpp>

/////////////////////////////////////////////////////////////////////
// Generates the non-empty subsets of a sequence

#define SUBSETS(values) \
   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(
   ((char, field1))
   ((float, field2))
   ((std::string, field3))
   ((int, field4))
)


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