namespace M17N.Core
{
- public delegate object MDatabaseLoader (MDatabaseTag tag,
- object extra_info);
-
- public struct MDatabaseTag
+ public class MDatabase
{
- public MSymbol Tag0, Tag1, Tag2, Tag3;
+ public struct Tag
+ {
+ public MSymbol Tag0, Tag1, Tag2, Tag3;
- public MDatabaseTag (MSymbol tag0)
- {
- Tag0 = tag0; Tag1 = Tag2 = Tag3 = MSymbol.nil;
- }
+ public Tag (MSymbol tag0)
+ {
+ Tag0 = tag0; Tag1 = Tag2 = Tag3 = MSymbol.nil;
+ }
- public MDatabaseTag (MSymbol tag0, MSymbol tag1)
- {
- Tag0 = tag0; Tag1 = tag1; Tag2 = Tag3 = MSymbol.nil;
- }
+ public Tag (MSymbol tag0, MSymbol tag1)
+ {
+ Tag0 = tag0; Tag1 = tag1; Tag2 = Tag3 = MSymbol.nil;
+ }
- public MDatabaseTag (MSymbol tag0, MSymbol tag1, MSymbol tag2)
- {
- Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = MSymbol.nil;
- }
+ public Tag (MSymbol tag0, MSymbol tag1, MSymbol tag2)
+ {
+ Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = MSymbol.nil;
+ }
- public MDatabaseTag (MSymbol tag0, MSymbol tag1,
- MSymbol tag2, MSymbol tag3)
- {
- Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = tag3;
- }
- }
+ public Tag (MSymbol tag0, MSymbol tag1, MSymbol tag2, MSymbol tag3)
+ {
+ Tag0 = tag0; Tag1 = tag1; Tag2 = tag2; Tag3 = tag3;
+ }
+ }
+
+ public delegate object Loader (Tag tag, object extra_info);
- public class MDatabase
- {
private class MDatabaseDir
{
private const string ListFileName = "mdb.dir";
internal MPlist Props;
}
- private static Dictionary<MDatabaseTag, MDatabase> DBDict
- = new Dictionary<MDatabaseTag, MDatabase> ();
+ private static Dictionary<MDatabase.Tag, MDatabase> DBDict
+ = new Dictionary<MDatabase.Tag, MDatabase> ();
private static MDatabaseDir[] DBDirs = new MDatabaseDir[3];
private const string SystemDirectory = "/usr/share/m17n";
- private readonly MSymbol Mversion = MSymbol.Of ("version");
+ private static readonly MSymbol Mversion = MSymbol.Of ("version");
/// Type of database
private enum MDBType
/// of the same kind.
MULTIPLE,
/// The database was defined explicitely by MDatabase.Define
- /// without a special loader.
+ /// to use the normal loader.
EXPLICIT,
/// The database was defined explicitely by MDatabase.Define
- /// with a special loader.
+ /// to use a special loader.
UNKNOWN,
};
INVALID,
};
- public readonly MDatabaseTag Tag;
- private MDatabaseLoader Loader;
+ public readonly Tag tag;
+ private Loader loader;
private object ExtraInfo;
private MDBType DBType;
private MDBStatus DBStatus;
public static string ApplicationDirectory;
- private MDatabase (MDatabaseTag tag, MDatabaseLoader loader,
- object extra_info)
+ static MDatabase ()
{
- Tag = tag;
- Loader = loader;
+ string share_dir = (Environment.GetFolderPath
+ (Environment.SpecialFolder.CommonApplicationData));
+ string usr_dir = (Environment.GetFolderPath
+ (Environment.SpecialFolder.ApplicationData));
+
+ try {
+ DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, ".m17n.d"));
+ } catch (ArgumentException) {
+ DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, "_m17n_d"));
+ }
+ DBDirs[1] = null;
+ DBDirs[2] = new MDatabaseDir (Path.Combine (share_dir, "m17n"));
+ }
+
+ private MDatabase (Tag tag, Loader loader, object extra_info)
+ {
+ this.tag = tag;
+ this.loader = loader;
ExtraInfo = extra_info;
}
- private MDatabase (MDatabaseTag tag, string filename)
+ private MDatabase (Tag tag, string filename)
{
- Tag = tag;
+ this.tag = tag;
Info = new MDatabaseInfo ();
Info.Filename = new MText (filename);
}
tags[i] = plist.Symbol;
while (i < 4)
tags[i++] = MSymbol.nil;
- Tag = new MDatabaseTag (tags[0], tags[1], tags[2], tags[3]);
+ tag = new Tag (tags[0], tags[1], tags[2], tags[3]);
if (plist.IsMText)
{
Info.Filename = plist.Text;
return ((major << 16) | (minor << 8) | release);
}
- public static MDatabase Define (MDatabaseTag tag, MDatabaseLoader loader,
- object extra_info)
+ public static MDatabase Define (Tag tag, Loader loader, object extra_info)
{
MDatabase db = MDatabase.Find (tag);
if (db != null)
{
- db.Loader = loader;
+ db.loader = loader;
db.ExtraInfo = extra_info;
db.DBType = MDBType.EXPLICIT;
db.DBStatus = MDBStatus.OUTDATED;
return new MDatabase (tag, loader, extra_info);
}
- public static MDatabase Define (MDatabaseTag tag, string filename)
+ public static MDatabase Define (Tag tag, string filename)
{
MDatabase db = MDatabase.Find (tag);
if (db != null)
{
- db.Loader = null;
+ db.loader = null;
db.DBType = MDBType.EXPLICIT;
db.DBStatus = MDBStatus.OUTDATED;
db.Info = new MDatabaseInfo ();
return new MDatabase (tag, filename);
}
- static MDatabase ()
+ private void update ()
{
- string share_dir = (Environment.GetFolderPath
- (Environment.SpecialFolder.CommonApplicationData));
- string usr_dir = (Environment.GetFolderPath
- (Environment.SpecialFolder.ApplicationData));
-
- try {
- DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, ".m17n.d"));
- } catch (ArgumentException) {
- DBDirs[0] = new MDatabaseDir (Path.Combine (usr_dir, "_m17n_d"));
- }
- DBDirs[1] = null;
- DBDirs[2] = new MDatabaseDir (Path.Combine (share_dir, "m17n"));
+ for (int i = 0; i < 3; i++)
+ {
+ if (DBDirs[0].StatusChanged)
+ break;
+ }
}
- public static MDatabase Find (MDatabaseTag tag)
+ public static MDatabase Find (Tag tag)
{
MDatabase db;
public object Load ()
{
- return (Loader != null ? Loader (Tag, ExtraInfo)
+ return (loader != null ? loader (tag, ExtraInfo)
: load (MSymbol.nil, MSymbol.nil));
}
public object Load (MSymbol key, MSymbol stop)
{
- if (Loader != null)
+ if (loader != null)
return null;
return load (key, stop);
}
return null;
}
-
}
-
}
\ No newline at end of file
public MProperty (MSymbol key, object val)
{
if (key.flags == null)
- key.flags = MProperty.Flags.None;
+ key.flags = Flags.None;
this.key = key;
this.val = val;
}
this.val = val;
}
+ public static bool HasFlags (MSymbol key, Flags flags)
+ {
+ return ((key.flags & flags) == flags);
+ }
+
public override string ToString ()
{
return key.ToString () + ":" + val;
public static MText operator+ (object obj, MText mt)
{
if (obj is string)
- return new MText ((string) obj) + mt;
+ {
+ MText mtnew = new MText ((string) obj);
+ return mtnew.Ins (mtnew.Length, mt);
+ }
throw new Exception ("Unknown object type: " + obj.GetType());
}
- public static MText operator+ (MText mt1, MText mt2)
+ public static MText operator+ (MText mt, object obj)
{
- MText mt = new MText ();
-
- mt.sb.Append (mt1.sb);
- mt.sb.Append (mt2.sb);
- mt.nchars = mt1.nchars + mt2.nchars;
- return mt;
+ if (obj is string)
+ return mt + new MText ((string) obj);
+ if (obj is int)
+ return mt.Dup ().Ins (mt.Length, (int) obj);
+ if (obj is char)
+ return mt.Dup ().Ins (mt.Length, (int) ((char) obj));
+ throw new Exception ("Unknown object type: " + obj.GetType());
}
- public static MText operator+ (string str, MText mt)
+ public static MText operator+ (MText mt1, MText mt2)
{
- MText mtnew = new MText (str);
-
- mtnew.sb.Append (mt.sb);
- mtnew.nchars += mt.nchars;
- return mtnew;
+ return mt1.Dup ().Ins (mt1.Length, mt2);
}
// Public properties
private bool isRearSticky
{
- get { return ((Key.flags & MProperty.Flags.RearSticky)
- != MProperty.Flags.None); }
+ get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
}
private bool isFrontSticky
{
- get { return ((Key.flags & MProperty.Flags.FrontSticky)
- != MProperty.Flags.None); }
+ get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
}
public bool isSensitive
{
- get { return ((Key.flags & MProperty.Flags.Sensitive)
- != MProperty.Flags.None); }
+ get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
+ }
+
+ public bool isFrontSensitive
+ {
+ get { return MProperty.HasFlags (Key,
+ (MProperty.Flags.Sensitive
+ | MProperty.Flags.FrontSticky)); }
+ }
+
+ public bool isRearSensitive
+ {
+ get { return MProperty.HasFlags (Key,
+ (MProperty.Flags.Sensitive
+ | MProperty.Flags.RearSticky)); }
}
private void update_from_to ()
Left.Insert (pos, interval, start, end);
else if (pos == From)
{
- if (Left != null)
- {
- MInterval prev = Prev;
+ MInterval prev = Left != null ? Prev : null;
- if (prev.isSensitive && prev.isRearSticky)
- prev.Stack.Clear ();
- start += prev.graft_forward (interval, start, end);
- }
- if (isSensitive && isFrontSticky)
+ if (isFrontSensitive)
Stack.Clear ();
- if (start < end)
+ if (prev != null && isRearSensitive)
+ prev.Stack.Clear ();
+ if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
{
- end -= graft_backward (interval, start, end);
- if (start < end)
- {
- if (interval != null)
- interval = interval.Copy (mtext, start, end);
- else
- interval = new MInterval (Key, mtext, end - start, null);
+ prev.enlarge (end - start);
+ return;
+ }
+ if (isFrontSticky && ! Stack.IsEmpty)
+ {
+ enlarge (end - start);
+ return;
+ }
+ if (prev != null)
+ {
+ start += prev.graft_forward (interval, start, end);
+ if (start == end)
+ return;
+ }
+ if ((end -= graft_backward (interval, start, end)) == start)
+ return;
- MInterval i;
- if (Left != null)
- {
- // .-this-. ==> .-this-.
- // left-. .-left-.
- // child child-.
- // interval
- i = Left.RightMost;
- i.Right = interval;
- }
- else
- {
- Left = interval;
- i = this;
- }
- interval.Parent = i;
- for (; i != null; i = i.Parent)
- i.Length += interval.Length;
- }
+ if (interval != null)
+ interval = interval.Copy (mtext, start, end);
+ else
+ interval = new MInterval (Key, mtext, end - start, null);
+
+ MInterval i;
+ if (Left != null)
+ {
+ // .-this-. ==> .-this-.
+ // left-. .-left-.
+ // child child-.
+ // interval
+ i = Left.RightMost;
+ i.Right = interval;
}
+ else
+ {
+ Left = interval;
+ i = this;
+ }
+ interval.Parent = i;
+ for (; i != null; i = i.Parent)
+ i.Length += interval.Length;
}
else if (pos < To)
{
if (isSensitive)
Stack.Clear ();
-
+ else if (! Stack.IsEmpty && (isFrontSticky || isRearSticky))
+ {
+ enlarge (end - start);
+ return;
+ }
int len = graft_forward (interval, start, end);
start += len;
+ if (start == end)
+ return;
+ if ((end -= graft_backward (interval, start, end)) == start)
+ return;
pos += len;
- if (start < end)
+ if (interval != null)
+ interval = interval.Copy (mtext, start, end);
+ else
+ interval = new MInterval (Key, mtext, end - start, null);
+
+ divide_right (pos);
+ Right.Left = interval;
+ interval.Parent = Right;
+ for (MInterval i = Right; i != null; i = i.Parent)
+ i.Length += interval.Length;
+ }
+ else if (pos == To)
+ {
+ MInterval next = Right != null ? Next : null;
+
+ if (isRearSensitive)
+ Stack.Clear ();
+ if (next != null && isFrontSensitive)
+ next.Stack.Clear ();
+ if (isRearSticky && ! Stack.IsEmpty)
+ {
+ enlarge (end - start);
+ return;
+ }
+ if (next != null)
{
- end -= graft_backward (interval, start, end);
- if (start < end)
+ if (isFrontSticky && ! next.Stack.IsEmpty)
{
- if (interval != null)
- interval = interval.Copy (mtext, start, end);
- else
- interval = new MInterval (Key, mtext, end - start, null);
-
- divide_right (pos);
- Right.Left = interval;
- interval.Parent = Right;
- for (MInterval i = Right; i != null; i = i.Parent)
- i.Length += interval.Length;
+ next.enlarge (end - start);
+ return;
}
+ end -= next.graft_backward (interval, start, end);
+ if (start == end)
+ return;
}
- }
- else if (pos == To)
- {
+
+ if ((start += graft_forward (interval, start, end)) == end)
+ return;
+
+ if (interval != null)
+ interval = interval.Copy (mtext, start, end);
+ else
+ interval = new MInterval (Key, mtext, end - start, null);
+
+ MInterval i;
if (Right != null)
{
- MInterval next = Next;
+ // .-this-. ==> .-this-.
+ // .-right .-right
+ // child .-child
+ // interval
- if (next.isSensitive && next.isFrontSticky)
- next.Stack.Clear ();
- end -= next.graft_backward (interval, start, end);
+ i = Right.LeftMost;
+ i.Left = interval;
}
- if (isSensitive && isRearSticky)
- Stack.Clear ();
- if (start < end)
+ else
{
- start += graft_forward (interval, start, end);
- if (start < end)
- {
- if (interval != null)
- interval = interval.Copy (mtext, start, end);
- else
- interval = new MInterval (Key, mtext, end - start, null);
-
- MInterval i;
- if (Right != null)
- {
- // .-this-. ==> .-this-.
- // .-right .-right
- // child .-child
- // interval
-
- i = Right.LeftMost;
- i.Left = interval;
- }
- else
- {
- Right = interval;
- i = this;
- }
- interval.Parent = i;
- for (; i != null; i = i.Parent)
- i.Length += interval.Length;
- }
+ Right = interval;
+ i = this;
}
+ interval.Parent = i;
+ for (; i != null; i = i.Parent)
+ i.Length += interval.Length;
}
else // (pos > To)
- Next.Insert (pos, interval, start, end);
+ Right.Insert (pos, interval, start, end);
M17n.DebugPrint (" done\n");
}
int to = tail.To;
// The nearest common parent of HEAD and TAIL.
MInterval root;
-
for (root = head; root.To + root.RightLength < to;
root = root.Parent);
}
}
+ private void check_from_to (string str)
+ {
+ int save_from = From;
+ int save_to = To;
+ update_from_to ();
+ if (save_from != From || save_to != To)
+ throw new Exception (str + ":incorrect from or to");
+ }
+
public void MergeAfterChange (int start, int end)
{
update_from_to ();
- MInterval head = find_head (start), tail = head, i;
+ MInterval head = find_head (start), i = head;
+ MInterval tail = find_tail (end).Next;
if (start == head.From && start > 0)
{
i = head.Prev;
- if (head.mergeable (i))
- head = i;
+ if (! head.mergeable (i))
+ i = head;
}
- while (tail.To < end)
+ while (i != tail)
{
- i = tail.Next;
- if (! tail.mergeable (i))
+ MInterval next = i.Next;
+
+ if (next == null || ! i.mergeable (next))
{
- if (head != tail)
- combine (head, tail);
- i.update_from_to ();
- head = i;
+ if (head != i)
+ combine (head, i);
+ head = next;
}
- tail = i;
- }
- while (true)
- {
- i = tail.Next;
- if (i == null || ! tail.mergeable (i))
- break;
- tail = i;
+ i = next;
}
- if (head != tail)
- combine (head, tail);
}
public void Pop (int start, int end)
if (force || M17n.debug)
{
Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
- if (with_prop)
- foreach (MPlist p in Stack)
- Console.Write (" " + p.Val);
+ if (with_prop && ! Stack.IsEmpty)
+ {
+ string prepend = " [";
+ foreach (MPlist p in Stack)
+ {
+ Console.Write (prepend + ((MProperty) p.Val).Val);
+ prepend = " ";
+ }
+ Console.Write ("]");
+ }
Console.Write (")");
if (newline)
Console.WriteLine ();