2012/06/17(Sun)Jsonほぼ互換の似非バリアント的な何か。

はてブ数 2012/06/17 18:22 プログラミング::C# つーさ

似非バリアント もしくは、似非JSONオブジェクト。
Jsonパーサ・フォーマッタの独自実装ですはい。
ゲームとかのセーブデータをスキーマレスに読み書きしたいなという欲求があるからして。

    class VarTest
    {
        public static void TestVar()
        {
            Var v = new VarList {
                true,
                false,
                12345,
                "ほげ",
                new byte[]{0x1, 0x2, 3, 4, 5, },
                new VarDictionary() { 
                    {"あ", true}, 
                    {"い", false}, 
                    {"X", Var.Null}, 
                    {"Y", 123},
                    {"辞書", new VarDictionary() { 
                        {"あ", true}, {"い", false}, {"X", new Var()}, {"Y", 123}, 
                    } },
                    {"辞書内配列", new VarDictionary() { 
                        {"あ", new int[]{123,456 } }, 
                        {"い", new bool[]{true, true, false, false, true} }, 
                        {"X", new VarList { Var.Null, "えー", new byte[]{99, 99, 99}, } },
                        {"Y", new string[]{"う゛ぁ", "う゛ぃ",} },
                    } }
                },
                "にゃー",
            };

            int a = v[5]["辞書内配列"]["あ"][1]; // → 456
            string b = v[5]["辞書内配列"]["X"][1]; // → "えー"
            Console.WriteLine(a);
            Console.WriteLine(b);
            string serialized = v.ToFormattedString(); // JSON形式の文字列になる
            Console.WriteLine(serialized);
            Var readed = Var.FromFormattedString(serialized); // JSON形式の文字列から読み込む
            Console.WriteLine(readed.ToFormattedString()); // ちゃんと読み込めたか?
        }
    }

のような。
読み込み時に備えて、Nullとは別にundefindも定義しておくべきだったかしら。

続きを読む

2012/04/01(Sun)C# で BigInteger

はてブ数 2012/04/01 7:37 プログラミング::C# つーさ

とりあえず10進数で2300桁ぐらい扱える全然BigじゃないBigInt。負の値は扱えない。
256のとこを大きくしたら扱える桁数が増える。

割り算と剰余演算子作るのめんどくさくて作ってないけど、
たいてい必要になるのってその辺なんだよなー。もじゅろ10億7とかさー。
というわけで、longのもじゅろだけはとれるよーにしといた。

実戦で使える速度が出るかどうかは未検証。
まぁでもそういう問題は、たぶん、計算量的に無理なようにできてるでしょうね。

続きを読む

2012/04/01(Sun)C# で 素数表を得る。エラトステネスのふるい

はてブ数 2012/04/01 3:57 プログラミング::C# つーさ

素数表を得る

class PrimeTable {
    public List<int> Primes = new List<int>();
    public bool[] IsPrime;
    public PrimeTable(int max) {
        IsPrime = new bool[max + 1];
        for (int i = 2; i <= max; i++) IsPrime[i] = true;
        for (int i = 2; i <= max; i++)
            if (IsPrime[i]) {
                Primes.Add(i);
                for (int j = i * 2; j <= max; j += i)
                    IsPrime[j] = false;
            }
    }
}

public class Program {
    static void Main() {
        foreach (var p in new PrimeTable(100).Primes)
            Console.WriteLine(p);
    }
}

2012/03/31(Sat)C# で PriorityQueue を使いたいので

はてブ数 2012/03/31 22:51 プログラミング::C# つーさ

Dijkstra とか教科書通りに書きたいとき。コピペしていいプロコン用に。

public class PriorityQueue<T> {
    List<T> list = new List<T>();
    IComparer<T> comp = Comparer<T>.Default;
    class Comparer : IComparer<T> {
        Comparison<T> comparison;
        public Comparer(Comparison<T> comparison) { this.comparison = comparison; }
        public int Compare(T x, T y) { return comparison(x, y); }
    }
    public PriorityQueue() { }
    public PriorityQueue(Comparison<T> comp) { this.comp = new Comparer(comp); }
    public void Enqueue(T item) { int i = list.BinarySearch(item, comp); list.Insert(i < 0 ? ~i : i, item); }
    public T Dequeue() { T r = list[0]; list.RemoveAt(0); return r; }
    public T Peek() { return list[0]; }
    public int Count { get { return list.Count; } }
    public T this[int i] { get { return list[i]; } set { list[i] = value; } }
}

public class Program { // てすと
    class A : IComparable<A> {
        public int X;
        public A(int x) { this.X = x; }
    }

    static void Main() {
        PriorityQueue<A> Q = new PriorityQueue<A>((x, y) => y.X - x.X); // でかい順
        Q.Push(new A(1));
        Q.Push(new A(3));
        Q.Push(new A(2));
        Q.Push(new A(1));

        Console.WriteLine(Q.Pop().X);
        Console.WriteLine(Q.Pop().X);
        Console.WriteLine(Q.Pop().X);
        Console.WriteLine(Q.Pop().X);
    }
}

中の構造に単純な配列を使ってるのでPushとPopでメモリコピーが発生してO(N)なのがどうかなー。
状態数が多くなるならちゃんとヒープ作るべきなのかも。

SortedList は、キーが重複できないのでいやーんな感じ。
つーか、もうちょっとスニペットを充実させておきたいなぁ。

2011/12/26(Mon)Direct 3D の Device の Reset に失敗する。

はてブ数 2011/12/26 19:49 プログラミング::SlimDX つーさ

ウィンドウモードで起動したアプリはAlt+Enterで何度でも切り替えられるのに、フルスクリーンで起動したアプリをウィンドウモードにしてからフルスクリーンにしようとすると、IDirect3DDevice9::ResetでD3DERR_DEVICELOST が出て復帰できない。

>> スクリーンモードを変更します。
Direct3D9: (ERROR) :Exclusive Mode has been taken by other app or other device on the same adapter. SetCooperativeLevel returns D3DERR_DEVICELOST.
Direct3D9: (ERROR) :SetCooperativeLevel returned failure. CreateDevice/Reset Failed
Direct3D9: (ERROR) :Reset failed and Reset/TestCooperativeLevel/Release are the only legal APIs to be called subsequently
Direct3D9: :Window has been subclassed; cannot restore!
Direct3D9: (ERROR) :Exclusive Mode has been taken by other app or other device on the same adapter. SetCooperativeLevel returns D3DERR_DEVICELOST.

がんばってググった結果は、Direct Inputがウィンドウをサブクラス化してしまうせいだとわかった。
Direct3Dより、DirectInputを先に初期化するだけで、この不具合は直った。

覚えておくといいなのっ