No Such Blog or Diary
そろそろ免許の更新をば
- 2005-07-12 (Tue)
- 一般
更新しなければならないが視力がやばそう.ためしに Web 上で見つけた Flashの視力測定できるやつで試してしると… 光度を落としているとはいえ,右目で0.3がはっきりしない.めがねを作るのもったいないしなぁ,どうにか一時的にでも視力回復しないもんだろうか? とりあえずブルーベリーでも食べとくか.
- Comments: 0
- TrackBack (Close): -
Minix本
- 2005-07-11 (Mon)
- 一般
輪読で読む<http://www.amazon.co.jp/exec/obidos/ASIN/4894710471/>Minix本がやっとこさ届いた.明日発表担当なのでさっさと読まねば.それにしても後ろほぼ半分がソースコードって… すげぇなぁ.
- Comments: 0
- TrackBack (Close): -
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): -
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): -
Template で条件分岐
Template でプログラム中の条件分岐を全てやってしまうことはできないだろうか? 最近こんなことを考えていたのだが,if 文だけなら何とかなるなぁと思ってみた.各処理ブロックをTemplate にして,必要な変数を全て Template 引数にして,条件分岐に Selector を使って条件に応じて適した Template クラスの calc() メソッドを呼ばせると.このとき,else 節がいらない場合には, Nothing という何もしない Template を使うと.たとえば,x が y より小さいときだけ出力する場合には,こんなかたちでできるはず.
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 << endl; } }; template<unsigned int x, unsigned int y> struct MinThenPrint<x, y >{ inline static void calc(){ Selector<(x < y), Printer<x,y>,Nothing>::val::calc(); } };
- Comments: 0
- TrackBack (Close): -