2005年07月10日
Template で覆面算
覆面算のすべての条件分岐を Template で処理してしまうプログラムを考えた.ソースを書くのが面倒なのでソースジェネレータを書いて生成することに.とりあえず,ジェネレータとできたソースをおいておくとして… Pen4 2.6C で8時間たってもコンパイルが終わらない… メモリもすでに 1G 食ってるし.このソースをコンパイルできる環境ってどこかにあるのだろうか? ちなみに,X*Y = ZZ という 4 進数での覆面算のプログラムは以下のとおり(答えなし)
#include <iostream>
using namespace std;
template <bool judge, typename T, typename F>
struct Selector{ typedef T val; };
template <typename T, typename F>
struct Selector<false, T, F>{ typedef F val; };
struct Nothing{ inline static void calc(){}};
template<unsigned int x, unsigned int y>
struct Printer{
inline static void calc(){
cout << x << " * " << y << " = " << x * y << endl;
cout << y << " * " << x << " = " << x * y << endl;
}
};
template<int x0, int y0>
struct Judge{
const static bool val = ((x0==y0))&&((x0==y0))&& (x0!=0);};
template<int n, unsigned int x, unsigned int y, int x0, int x1, int x2, int x3>
struct Perms{ inline static void calc(){}};
template<unsigned int x, unsigned int y, int x0, int x1, int x2, int x3>
struct Perms<0, x, y, x0, x1, x2, x3>{
inline static void calc(){
Selector<true, Perms<1, x | (x0<< 0), y, x0, x1, x2, x3>, Nothing >::val::calc();
Selector<true, Perms<1, x | (x1<< 0), y, x1, x0, x2, x3>, Nothing >::val::calc();
Selector<true, Perms<1, x | (x2<< 0), y, x2, x1, x0, x3>, Nothing >::val::calc();
Selector<true, Perms<1, x | (x3<< 0), y, x3, x1, x2, x0>, Nothing >::val::calc();
}
};
template<unsigned int x, unsigned int y, int x0, int x1, int x2, int x3>
struct Perms<1, x, y, x0, x1, x2, x3>{
inline static void calc(){
Selector<(x0 > x1), Perms<2, x, y | (x1 << 0), x0, x1, x2, x3>, Nothing >::val::calc();
Selector<(x0 > x2), Perms<2, x, y | (x2 << 0), x0, x2, x1, x3>, Nothing >::val::calc();
Selector<(x0 > x3), Perms<2, x, y | (x3 << 0), x0, x3, x2, x1>, Nothing >::val::calc();
}
};
template<unsigned int x, unsigned int y, int x0, int x1, int x2, int x3>
struct Perms<2, x, y, x0, x1, x2, x3>{
inline static void calc(){
Selector<(((x*y) >> 0)& 0x3) == x2, Perms<3, x, y, x0, x1, x2, x3>, Nothing >::val::calc();
Selector<(((x*y) >> 0)& 0x3) == x3, Perms<3, x, y, x0, x1, x3, x2>, Nothing >::val::calc();
}
};
template<unsigned int x, unsigned int y, int x0, int x1, int x2, int x3>
struct Perms<3, x, y, x0, x1, x2, x3>{
inline static void calc(){
Selector<Judge<(((x*y)>>2)&0x3), x3>::val, Printer<x,y>,Nothing>::val::calc();
}
};
int main(int argc, char *argv[])
{
cout << hex;
Perms< 0, 0, 0, 0, 1, 2, 3>::calc();
}
- Comments: 0
- TrackBack (Close): -