[MPL] interest in algo that creates all possible permutations of a type?

Hi, I just joined the list and I would like to show you what I have done here. Given a type T which is a model of MPL lambda expression, permute<T, Args1, ..., ArgsN>::type is an MPL vector that contains all possible permutations of type T, where ArgsN is a model of an MPL forward sequence. Example: template<class X, class Y> struct base{}; permute< base<_1,_2>, vector<int, float>, vector<double, bool>
::type types;
types is an MPL vector containing all possible permutations of base with the supplied argument lists: vector< base<int,double>, base<int,bool>, base<float,double>, base<float,bool>
(not necessarily in this order) I believe I can easily extend this algo to n-ary lambda expressions with some preprocessor macros. The version for binary expressions is attached below because the code is short. I tested the code with gcc 3.4.4. Example usage: I use this algo to create a typelist that is given to my renderer which instantiates a 'dispatch table' (see More Effective C++, Item 31) and the appropriate render functions for these types. Then at runtime the entities to be rendered are dispatched based on their dynamic type to the specialized render functions which only work on concrete types. #ifndef permute_h #define permute_h #include <boost/mpl/apply.hpp> #include <boost/mpl/at.hpp> #include <boost/mpl/bool.hpp> #include <boost/mpl/if.hpp> #include <boost/mpl/eval_if.hpp> #include <boost/mpl/int.hpp> #include <boost/mpl/modulus.hpp> #include <boost/mpl/push_back.hpp> #include <boost/mpl/size.hpp> #include <boost/mpl/vector.hpp> template<class T, class Args1, class Args2, class Seq, int N, int K> struct permute_impl { typedef typename boost::mpl::modulus< boost::mpl::int_<N>,boost::mpl::size<Args2>
::type args2_index;
typedef typename boost::mpl::push_back< Seq , typename boost::mpl::apply< T , typename boost::mpl::at_c<Args1, K>::type , typename boost::mpl::at<Args2, args2_index>::type >::type
::type Seq2;
typedef typename boost::mpl::eval_if_c< N == 0 , Seq , permute_impl< T , Args1,Args2 , Seq2 , N-1 , boost::mpl::if_c< args2_index::value == 1 , boost::mpl::int_<K+1> , boost::mpl::int_<K> >::type::value
::type type;
}; template<class T, class Args1, class Args2> struct permute { typedef typename permute_impl< T, Args1, Args2, boost::mpl::vector<>, boost::mpl::size<Args1>::value*boost::mpl::size<Args2>::value, 0
::type type; };
#endif _________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

Kevin Sopp wrote:
Given a type T which is a model of MPL lambda expression, permute<T,
Args1,
..., ArgsN>::type is an MPL vector that contains all possible permutations of type T, where ArgsN is a model of an MPL forward sequence.
Example: template<class X, class Y> struct base{};
permute< base<_1,_2>, vector<int, float>, vector<double, bool>
::type types;
types is an MPL vector containing all possible permutations of base with the supplied argument lists: vector< base<int,double>, base<int,bool>, base<float,double>, base<float,bool>
(not necessarily in this order)
I'm sorry I'm not an MPL expert so I can't give you feedback about the usefulness of this class. It looks interesting though. What I would like to say is that you should choose a name different from "permute" because it is misleading. From the wikipedia: "The concept of a permutation expresses the idea that distinguishable objects may be arranged in various different orders." In other words "permute" conveys the concept that you already have a list of types and you provide a different re-ordering of such list (compare with std::next_permutation, std::last_permutation). In your case you are synthesizing a list of types based on a sort of "template" type and a set of sets of parameters. Moreover you say that the synthesized list has no particular order. This looks a very different operation to me. Just my opinion, Ganesh

Alberto Ganesh Barbati <abarbati <at> iaanus.com> writes:
Kevin Sopp wrote:
Given a type T which is a model of MPL lambda expression, permute<T,
Args1,
<snip>
What I would like to say is that you should choose a name different from "permute" because it is misleading.... ....In other words "permute" conveys the concept that you already have a list of types and you provide a different re-ordering of such list (compare with std::next_permutation, std::last_permutation).
The word you are looking for is combination (combine) S

From: Alberto Ganesh Barbati <abarbati@iaanus.com>
I'm sorry I'm not an MPL expert so I can't give you feedback about the usefulness of this class. It looks interesting though. What I would like to say is that you should choose a name different from "permute" because it is misleading.
I was actually looking for a better name myself but couldn't come up with one, other than 'generate' :-) Combine sounds alright to me.
Moreover you say that the synthesized list has no particular order. This looks a very different operation to me.
Of course the order of the resulting types is not random. Observing the output of my test program with the following argument lists and T = base<_1,_2>: args1 = 0,1 args2 = 0,1,2,3 the resulting vector will contain the types in the following order: base<0,0>, base<0,3>, base<0,2>, base<0,1>, base<1,0>, base<1,3>, base<1,2>, base<1,1> I uploaded the code for binary lambda expressions at http://oceanstar.sourceforge.net/combine.h Kevin Sopp _________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

On 11/11/05, Kevin Sopp <baraclese@hotmail.com> wrote: [snip]
Of course the order of the resulting types is not random. Observing the output of my test program with the following argument lists and T = base<_1,_2>: args1 = 0,1 args2 = 0,1,2,3
the resulting vector will contain the types in the following order: base<0,0>, base<0,3>, base<0,2>, base<0,1>, base<1,0>, base<1,3>, base<1,2>, base<1,1>
Any reason why it isnt in this order: base<0, 0>, base<0, 1>, base<0, 2>, base<0, 3>, base<1, 0>, base<1, 1>, base<1, 2>, base<1, 3> ?
I uploaded the code for binary lambda expressions at http://oceanstar.sourceforge.net/combine.h
Kevin Sopp
best regards, -- Felipe Magno de Almeida Developer from synergy and Computer Science student from State University of Campinas(UNICAMP). Unicamp: http://www.ic.unicamp.br Synergy: http://www.synergy.com.br "There is no dark side of the moon really. Matter of fact it's all dark."
participants (4)
-
Alberto Ganesh Barbati
-
Felipe Magno de Almeida
-
Kevin Sopp
-
Simon Carter