From c2bb1aabdbd8d636fa21bb0c8e7f26291ff23332 Mon Sep 17 00:00:00 2001 From: handa Date: Mon, 14 Aug 2006 01:07:24 +0000 Subject: [PATCH 1/1] C# version of m17n-lib --- .cvsignore | 1 + MSymbol.cs | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 5 ++ 3 files changed, 267 insertions(+) create mode 100644 .cvsignore create mode 100644 MSymbol.cs create mode 100644 Makefile diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..b883f1f --- /dev/null +++ b/.cvsignore @@ -0,0 +1 @@ +*.exe diff --git a/MSymbol.cs b/MSymbol.cs new file mode 100644 index 0000000..14e3e3b --- /dev/null +++ b/MSymbol.cs @@ -0,0 +1,261 @@ +using System; +using M17N; + +namespace M17N +{ + public class MPlist + { + private MSymbol key; + private object val; + private MPlist next; + + public MPlist () + { + key = null; + val = null; + next = null; + } + + public bool tailp { get { return (object) key == null; } } + + public new string ToString () + { + string str = ""; + + for (MPlist p = this; ! p.tailp; p = p.next) + { + str += (p == this ? "(" : " ") + p.key.ToString () + ":"; + if (p.val is MSymbol) + str += ((MSymbol) p.val).ToString (); + else if (p.val is MPlist) + str += ((MPlist) p.val).ToString (); + } + return str + ")"; + } + + public object get (MSymbol key) + { + if ((object) key == null) + return null; + for (MPlist p = this; ! p.tailp; p = p.next) + if (p.key == key) + return p.val; + return null; + } + + public object put (MSymbol key, object val) + { + MPlist p; + + for (p = this; ! p.tailp; p = p.next) + if (p.key == key) + { + if (val != null) + p.val = val; + else + p.pop (); + return val; + } + if (val != null) + { + p.key = key; + p.val = val; + p.next = new MPlist (); + } + return val; + } + + public object push (MSymbol key, object val) + { + MPlist p = new MPlist (); + + p.key = this.key; + p.val = this.val; + p.next = this.next; + this.key = key; + this.val = val; + this.next = p; + + return val; + } + + public object pop () + { + if (tailp) + return null; + + object val = this.val; + + this.key = this.next.key; + this.val = this.next.val; + this.next = this.next.next; + return val; + } + + public object add (MSymbol key, object val) + { + MPlist p; + + for (p = this; ! p.tailp; p = p.next); + if (val != null) + { + p.key = key; + p.val = val; + p.next = new MPlist (); + } + return val; + } + } + + public class MSymbol + { + internal class MSymbolData + { + public string name; + public MPlist plist; + public MSymbolData next; + + public MSymbolData (string name, MSymbolData next) + { + this.name = name; + this.next = next; + } + } + + static internal class MSymbolPool + { + const int MSYMBOL_POOL_SIZE = 1024; + static MSymbolData[] pool = new MSymbolData[MSYMBOL_POOL_SIZE]; + static int[] used = new int[MSYMBOL_POOL_SIZE]; + + static public MSymbolData get_data (string name) + { + MSymbolData data; + + if (used[0] > 0) + { + data = pool[0]; + for (int i = 0; i < used[0]; i++, data = data.next) + if (data.name == name) + return data; + } + pool[0] = data = new MSymbolData (name, pool[0]); + used[0]++; + return data; + } + } + + static public MSymbol nil = new MSymbol ("nil"); + static public MSymbol t = new MSymbol ("t"); + + private MSymbolData data; + + public MSymbol (string name) + { + data = MSymbolPool.get_data (name); + } + + public new string ToString () { return data.name; } + + public new bool Equals (object sym) { return ((object) this == sym); } + + public new int GetHashCode () { return 0; } + + public static bool operator== (MSymbol sym1, MSymbol sym2) + { + return ((object) sym1.data == (object) sym2.data); + } + + public static bool operator!= (MSymbol sym1, MSymbol sym2) + { + return ((object) sym1.data != (object) sym2.data); + } + + public object get (MSymbol key) + { + return (data.plist == null ? null : data.plist.get (key)); + } + + public object put (MSymbol key, object val) + { + if (data.plist == null) + data.plist = new MPlist (); + return data.plist.put (key, val); + } + } + + public class MTextProperty + { + } + + public enum MTextFormat + { + MTEXT_FORMAT_US_ASCII, + MTEXT_FORMAT_UTF_8, + MTEXT_FORMAT_UTF_16BE, + MTEXT_FORMAT_UTF_16LE, + MTEXT_FORMAT_UTF_32BE, + MTEXT_FORMAT_UTF_32LE, + } + + public class MText + { + private class MTextPlist : MPlist + { + public class MInterval + { + public MPlist stack; + public int nprops; + public int start, end; + public MInterval prev, next; + } + + MInterval head, tail; + + public MTextPlist (MText mt) + { + head = tail = new MInterval (); + head.start = 0; + head.end = mt.nchars; + } + } + + private string str; + private int nchars; + private int nunits; + private int cache_pos; + private int cache_idx; + private MTextPlist plist; + + public MText (byte str, MTextFormat format) + { + + } + } + + + public class Test + { + public static void Main() + { + MSymbol sym1, sym2; + + sym1 = new MSymbol ("abc"); + Console.WriteLine (sym1.ToString ()); + sym2 = new MSymbol ("abc"); + Console.WriteLine (sym2.ToString ()); + Console.WriteLine (sym1 == sym2 ? "OK" : "NO"); + sym1.put (MSymbol.nil, MSymbol.t); + + MPlist p = new MPlist (); + + p.put (MSymbol.t, sym1); + p.push (MSymbol.t, sym2); + + MPlist pp = new MPlist (); + pp.put (MSymbol.t, p); + Console.WriteLine (pp.ToString ()); + Console.WriteLine (p.get (MSymbol.t)); + } + } +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3454d54 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: MSymbol.exe + + +%.exe: %.cs + mcs $< -- 1.7.10.4