Boost logo

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-01 00:09:53


----- Original Message -----
From: "Larry Evans" <cppljevans_at_[hidden]>
To: <boost-users_at_[hidden]>
Sent: Monday, February 01, 2010 1:03 AM
Subject: Re: [Boost-users] generate structs with all combinations of members from a given struct

Hello,
Larry thanks for all the answers.

I have a little trouble following your trail of thoughts, especially step 2, but let me reread your posts a couple of times more to get it right.

I think your solution is rather build more solutions than required then remove those which are not a "combination" in the mathematical sense. It may cause problems when my structs have more than 15 members in which case we may be working with 15! to get back to 2^15 -1
again, let me reread your posts first.

In the meantime, I have written the pure runtime version of the problem which is in fact like a tree recursive descent problem.(I think), this builds and runs correctly but doesn't deal with the identical types and member names.

#include <iostream>
#include <list>

typedef std::list< std::list<size_t> > main_list_t;

void combinations(main_list_t& m, const size_t* arr, size_t arrsize)
{
  if (arrsize==0)
    return;
  
  if (m.empty()) {
    std::list<size_t> l;
    l.push_back( arr[0] );
    m.push_back(l);
  }
  else {
    std::list<size_t> l( m.back() );
    l.push_back( arr[0] );
    m.push_back( l );
  }
 
  combinations(m, arr+1, arrsize-1);
}

int main()
{
  const size_t arr[] = { 1, 2, 3, 4, 5 };
  const size_t arrsize = sizeof(arr)/sizeof(size_t);

  main_list_t m[arrsize];
  for (size_t s=0; s<arrsize; ++s)
    combinations(m[s], &arr[s], arrsize-s);

  main_list_t mm;
  for (size_t s=0; s<arrsize; ++s)
    mm.insert(mm.end(), m[s].begin(), m[s].end());

  for (main_list_t::const_iterator i = mm.begin(); i!=mm.end(); ++i) {
    const std::list<size_t>& l = *i;
    for (std::list<size_t>::const_iterator ii = l.begin(); ii!=l.end(); ++ii) {
      std::cout<< *ii << '\t';
    }
    std::cout<<std::endl;
  }
}

I will try to build a Boost.PP version of this runtime, with PP lists I think,

and finally I will have your mpl version to compare the 2.

Unfortunately, I'll have to use MSVC2008 so I don't think variadic templates are usable, but I'm sure your solution can work without variadic templates.

Again, thank you very much,



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