2005年07月09日
Template でPermutation前計算
順列を生成して出力するプログラムをかんがえる.んで,コンパイル時に順列を全生成して,その結果のみを出力するプログラムが最速である.なので,Template で順列を全生成して出力するプログラムを作ってみた.
とりあえず,ソースジェネレータと作られたソースをおいてみる… コンパイルが激遅い… ちなみに,生成された 3 の permutation のコードはこんなもん.
#include <iostream>
using namespace std;
template<int n, int x0, int x1, int x2>
struct Perms{ inline static void calc(){}
};
template<int x0, int x1, int x2>
struct Perms<0, x0, x1, x2>{
inline static void calc(){
Perms<1, x0, x1, x2>::calc();
Perms<1, x1, x0, x2>::calc();
Perms<1, x2, x1, x0>::calc();
}
};
template<int x0, int x1, int x2>
struct Perms<1, x0, x1, x2>{
inline static void calc(){
Perms<2, x0, x1, x2>::calc();
Perms<2, x0, x2, x1>::calc();
}
};
template<int x0, int x1, int x2>
struct Perms<2, x0, x1, x2>{
inline static void calc(){
Perms<3, x0, x1, x2>::calc();
}
};
template<int x0, int x1, int x2>
struct Perms<3, x0, x1, x2>{
inline static void calc(){
cout << x0 << x1 << x2<<endl;
}
};
int main(int argc, char *argv[])
{
cout << hex;
Perms< 0, 0, 1, 2>::calc();
}
- Comments: 0
- TrackBack (Close): -