using System;
-using System.Collections;
+using System.Collections.Generic;
using M17N.Core;
namespace M17N.Core
{
- public class MSymbol
+ public sealed class MSymbol
{
- static private Hashtable pool = new Hashtable ();
+ private static Dictionary<string, MSymbol> pool
+ = new Dictionary<string, MSymbol> ();
- private class MSymbolData
+ public readonly string Name;
+ private MPlist Plist;
+ internal MProperty.Flags? flags;
+
+ public static MSymbol nil = MSymbol.Of ("nil");
+ public static MSymbol t = MSymbol.Of ("t");
+ public static MSymbol symbol = MSymbol.Of ("symbol");
+ public static MSymbol mtext = MSymbol.Of ("mtext");
+ public static MSymbol plist = MSymbol.Of ("plist");
+ public static MSymbol integer = MSymbol.Of ("integer");
+
+ private MSymbol (string name)
+ {
+ Name = name;
+ }
+
+ public static MSymbol Of (string name)
{
- public string name;
- public object value;
- public MPlist plist;
+ lock (pool)
+ {
+ MSymbol sym;
- public MSymbolData (string name)
- {
- this.name = name;
- }
+ if (! pool.TryGetValue (name, out sym))
+ {
+ sym = new MSymbol (name);
+ pool[name] = sym;
+ }
+ return sym;
+ }
}
- private MSymbolData data;
+ public static MSymbol PropertyKey (string name)
+ {
+ MSymbol sym = MSymbol.Of (name);
- public static MSymbol nil = new MSymbol ("nil");
- public static MSymbol t = new MSymbol ("t");
- public static MSymbol symbol = new MSymbol ("symbol");
- public static MSymbol mtext = new MSymbol ("mtext");
- public static MSymbol plist = new MSymbol ("plist");
- public static MSymbol integer = new MSymbol ("integer");
+ if (sym.flags == null)
+ sym.flags = MProperty.Flags.None;;
+ return sym;
+ }
- public MSymbol (string name)
+ public static MSymbol PropertyKey (string name, MProperty.Flags flags)
{
- if (! pool.ContainsKey (name))
- {
- data = new MSymbolData (name);
- pool.Add (name, data);
- }
- else
- data = (MSymbolData) pool[name];
+ MSymbol sym = MSymbol.Of (name);
+
+ if (sym.flags == null)
+ sym.flags = flags;
+ else if (sym.flags != flags)
+ throw new Exception ("Flags of PropertyKey mismatch");
+ return sym;
}
public override string ToString ()
{
string str = "";
- foreach (char c in data.name)
+ foreach (char c in Name)
{
if (c == '\\' || c == ' ' || c == '\'' || c == '\"' || c == ':')
str += "\\";
return str;
}
- public static bool operator== (MSymbol sym1, MSymbol sym2)
- {
- if (System.Object.ReferenceEquals(sym1, sym2))
- return true;
- if (((object) sym1 == null) || ((object) sym2 == null))
- return false;
- return sym1.data == sym2.data;
- }
-
- public static bool operator!= (MSymbol sym1, MSymbol sym2)
- {
- return ! (sym1 == sym2);
- }
-
- public override bool Equals (object sym)
- {
- if (sym == null)
- return false;
- return (this.data == ((MSymbol) sym).data);
- }
-
- public override int GetHashCode ()
- {
- return (data.name.GetHashCode ());
- }
-
public MPlist Find (MSymbol key)
{
- return (data.plist == null ? null : data.plist.Find (key));
+ return (Plist == null ? null : Plist.Find (key));
}
public object Get (MSymbol key)
{
- return (data.plist == null ? null : data.plist.Get (key));
+ return (Plist == null ? null : Plist.Get (key));
}
public object Put (MSymbol key, object val)
{
- if (data.plist == null)
- data.plist = new MPlist ();
- return data.plist.Put (key, val);
+ if (Plist == null)
+ Plist = new MPlist ();
+ return Plist.Put (key, val);
}
}
}