Boost logo

Boost Users :

Subject: Re: [Boost-users] generate structs with all combinations of members from a given struct
From: Larry Evans (cppljevans_at_[hidden])
Date: 2010-02-01 14:35:38


On 02/01/10 03:08, Hicham Mouline wrote:
>
>
> ----- Original Message -----
> *From:* Hicham Mouline <mailto:hicham_at_[hidden]>
> *To:* boost-users_at_[hidden] <mailto:boost-users_at_[hidden]>
> *Sent:* Monday, February 01, 2010 12:09 PM
> *Subject:* Re: [Boost-users] generate structs with all combinations
> of members from a given struct
>
>
> My previous runtime solution was wrong.
> I just fixed it now.

I had a hard time understanding how it worked.
A simpler solution would be to just generate the
characteristic functions for sets of a given size.
The characteristic function could then be used
to select elements from the set:

http://en.wikipedia.org/wiki/Characteristic_function

The structure of such a program is very similar to yours
in that it has a recursive call and a for loop. However, with
this method, the for loop just has size 2. The depth of
recursion is n, where n is size of the set. I think your
method had recursion depth of n-1.

The attached illustrates the method.
Output is:

size=32
  0 0 0 0 0
  1 0 0 0 0
  0 1 0 0 0
  1 1 0 0 0
  0 0 1 0 0
  1 0 1 0 0
  0 1 1 0 0
  1 1 1 0 0
  0 0 0 1 0
  1 0 0 1 0
  0 1 0 1 0
  1 1 0 1 0
  0 0 1 1 0
  1 0 1 1 0
  0 1 1 1 0
  1 1 1 1 0
  0 0 0 0 1
  1 0 0 0 1
  0 1 0 0 1
  1 1 0 0 1
  0 0 1 0 1
  1 0 1 0 1
  0 1 1 0 1
  1 1 1 0 1
  0 0 0 1 1
  1 0 0 1 1
  0 1 0 1 1
  1 1 0 1 1
  0 0 1 1 1
  1 0 1 1 1
  0 1 1 1 1
  1 1 1 1 1

-regards,
Larry


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