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;
- public readonly MTextProperty.Flags Flags;
- public object Value;
- public MPlist Plist;
-
- public MSymbolData (string name, MTextProperty.Flags flags)
- {
- Name = name;
- Flags = flags;
- }
- }
+ public readonly string Name;
+ private MPlist Plist;
+ internal MProperty.Flags? flags;
- private MSymbolData data;
+ 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");
- 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");
+ private MSymbol (string name)
+ {
+ Name = name;
+ }
- public MSymbol (string name)
+ public static MSymbol Of (string name)
{
- if (! pool.ContainsKey (name))
+ lock (pool)
{
- data = new MSymbolData (name, MTextProperty.Flags.None);
- pool.Add (name, data);
+ MSymbol sym;
+
+ if (! pool.TryGetValue (name, out sym))
+ {
+ sym = new MSymbol (name);
+ pool[name] = sym;
+ }
+ return sym;
}
- else
- data = (MSymbolData) pool[name];
}
- public MSymbol (string name, MTextProperty.Flags flags)
+ public static MSymbol PropertyKey (string name)
{
- if (! pool.ContainsKey (name))
- {
- data = new MSymbolData (name, flags);
- pool.Add (name, data);
- }
- else
- {
- if (((MSymbolData) pool[name]).Flags != flags)
- throw new ArgumentException ("Invalid MTextProperty.Flags");
- }
+ MSymbol sym = MSymbol.Of (name);
+
+ if (sym.flags == null)
+ sym.flags = MProperty.Flags.None;;
+ return sym;
}
- public MTextProperty.Flags TextPropertyFlags { get { return data.Flags; } }
+ public static MSymbol PropertyKey (string name, MProperty.Flags flags)
+ {
+ 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);
}
}
}
}
#endif
- public class MTextProperty
+ public class MProperty
{
[FlagsAttribute]
public enum Flags
RearSticky = 2,
/// This property is deleted from a span of text if the span is
/// modified (i.e. a character is changed, a text is inserted,
- /// some part is deleted). If this property is also FrontSticky
- /// (or RearSticky), text insertion just before (or after) the
- /// span also deletes this property from the span of text.
- Sensitive = 4
+ /// some part is deleted). This propery is also deleted if a
+ /// property of the same key is added, which means that this
+ /// property is not stackable. If this property is also
+ /// FrontSticky (or RearSticky), text insertion just before (or
+ /// after) the span also deletes this property from the span of
+ /// text.
+ Sensitive = 4,
};
internal MSymbol key;
public MSymbol Key { get { return key; } }
public object Val { get { return val; } }
- public MTextProperty (MSymbol key, object val)
+ public MProperty (MSymbol key, object val)
{
+ if (key.flags == null)
+ key.flags = MProperty.Flags.None;
this.key = key;
this.val = val;
}
+ public MProperty (string name, object val)
+ {
+ key = MSymbol.PropertyKey (name);
+ this.val = val;
+ }
+
public override string ToString ()
{
return key.ToString () + ":" + val;
return;
foreach (MPlist plist in intervals)
{
+ MInterval root = (MInterval) plist.Val;
MPlist p = mt2.intervals.Find (plist.Key);
MInterval i = p == null ? null : (MInterval) p.Val;
- ((MInterval) plist.Val).Insert (pos, i, from, to);
+ root.Insert (pos, i, from, to);
}
foreach (MPlist plist in mt2.intervals)
if (intervals.Find (plist.Key) == null)
if (nchars > 0)
foreach (MPlist plist in intervals)
- ((MInterval) plist.Val).Delete (from, to);
+ {
+ MInterval root = (MInterval) plist.Val;
+ root.Delete (from, to);
+ if (from > 0 && from < nchars)
+ root.MergeAfterChange (from, from);
+ }
else
intervals.Clear ();
if (M17n.debug)
if (i == null)
return null;
- MTextProperty prop = i.Get (pos);
+ MProperty prop = i.Get (pos);
return (prop != null ? prop.Val : null);
}
- public object GetProp (int pos, MSymbol key, out MTextProperty prop)
+ public object GetProp (int pos, MSymbol key, out MProperty prop)
{
check_pos (pos, false);
return (prop != null ? prop.Val : null);
}
- public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
+ public object GetProp (int pos, MSymbol key, out MProperty[] array)
{
check_pos (pos, false);
MInterval i = (MInterval) intervals.Get (key);
if (i == null)
return (array = null);
- MTextProperty prop = i.Get (pos, out array);
+ MProperty prop = i.Get (pos, out array);
return (prop != null ? prop.Val : null);
}
public void PushProp (int from, int to, MSymbol key, object val)
{
if (! check_range (from, to, true))
- PushProp (from, to, new MTextProperty (key, val));
+ PushProp (from, to, new MProperty (key, val));
}
- public void PushProp (int from, int to, MTextProperty prop)
+ public void PushProp (int from, int to, MProperty prop)
{
if (from < 0)
{
else
root = (MInterval) p.Val;
+ if (root.isSensitive)
+ root.PopSensitive (from, to);
root.Push (from, to, prop);
+ root.MergeAfterChange (from, to);
+ root.Balance ();
}
}
if (p != null)
{
MInterval root = (MInterval) p.Val;
- root.Pop (from, to);
+ if (root.isSensitive)
+ root.PopSensitive (from, to);
+ else
+ root.Pop (from, to);
root.MergeAfterChange (from, to);
+ root.Balance ();
}
}
}
}
/// POS must be smaller than Length;
- public MTextProperty Get (int pos)
+ public MProperty Get (int pos)
{
- MInterval i = find (pos);
+ MInterval i = find_head (pos);
- if (i.To == pos)
- i = i.Next;
- return (i.Stack.IsEmpty ? null : (MTextProperty) i.Stack.Val);
+ return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
}
/// POS must be smaller than Length;
- public MTextProperty Get (int pos, out MTextProperty[] array)
+ public MProperty Get (int pos, out MProperty[] array)
{
- MInterval i = find (pos);
+ MInterval i = find_head (pos);
if (i.To == pos)
i = i.Next;
array = null;
return null;
}
- array = new MTextProperty[i.Stack.Count];
+ array = new MProperty[i.Stack.Count];
int idx;
MPlist p;
for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
- array[idx] = (MTextProperty) p.Val;
+ array[idx] = (MProperty) p.Val;
return array[0];
}
private bool isRearSticky
{
- get { return ((Key.TextPropertyFlags & MTextProperty.Flags.RearSticky)
- != MTextProperty.Flags.None); }
+ get { return ((Key.flags & MProperty.Flags.RearSticky)
+ != MProperty.Flags.None); }
}
private bool isFrontSticky
{
- get { return ((Key.TextPropertyFlags & MTextProperty.Flags.FrontSticky)
- != MTextProperty.Flags.None); }
+ get { return ((Key.flags & MProperty.Flags.FrontSticky)
+ != MProperty.Flags.None); }
}
- private bool isSensitive
+ public bool isSensitive
{
- get { return ((Key.TextPropertyFlags & MTextProperty.Flags.Sensitive)
- != MTextProperty.Flags.None); }
+ get { return ((Key.flags & MProperty.Flags.Sensitive)
+ != MProperty.Flags.None); }
}
private void update_from_to ()
}
}
- private MInterval find (int pos)
+ private MInterval find_head (int pos)
{
update_from_to ();
if (pos < From)
- return Left.find (pos);
+ return Left.find_head (pos);
+ if (pos >= To)
+ return Right.find_head (pos);
+ return this;
+ }
+
+ private MInterval find_tail (int pos)
+ {
+ update_from_to ();
+ if (pos <= From)
+ return Left.find_tail (pos);
if (pos > To)
- return Right.find (pos);
+ return Right.find_tail (pos);
return this;
}
// c1 c2 left c1
private MInterval promote_right ()
{
- int right_length = Right.Length;
- MInterval c1;
+ MInterval c1 = Right.Left;
+ // Update Parent.
if (Parent == null)
mtext.intervals.Put (Key, Right);
else if (Parent.Left == this)
Parent.Left = Right;
else
Parent.Right = Right;
+
+ // Update Right.
Right.Parent = Parent;
- c1 = Right.Left;
Right.Left = this;
+ Right.Length += LeftLength + (To - From);
+ // Update this.
Parent = Right;
Right = c1;
- Parent.Length += Length;
- Length -= right_length;
+ Length = LeftLength + (To - From) + RightLength;
+
+ // Update C1 if necessary.
if (c1 != null)
- {
- c1.Parent = this;
- Parent.Length -= c1.Length;
- Length += c1.Length;
- }
+ c1.Parent = this;
+
return Parent;
}
// c1 c2 c2 right
private MInterval promote_left ()
{
- int left_length = Left.Length;
- MInterval c1;
+ MInterval c2 = Left.Right;
+ // Update Parent.
if (Parent == null)
mtext.intervals.Put (Key, Left);
else if (Parent.Left == this)
Parent.Left = Left;
else
Parent.Right = Left;
+
+ // Update Left.
Left.Parent = Parent;
- c1 = Left.Left;
Left.Right = this;
+ Left.Length += (To - From) + RightLength;
+ // Update this.
Parent = Left;
- Left = c1;
- Parent.Length += Length;
- Length -= left_length;
- if (c1 != null)
- {
- c1.Parent = this;
- Parent.Length -= c1.Length;
- Length += c1.Length;
- }
+ Left = c2;
+ Length = LeftLength + (To - From) + RightLength;
+
+ // Update C2 if necessary.
+ if (c2 != null)
+ c2.Parent = this;
+
return Parent;
}
- private MInterval balance ()
+ public MInterval Balance ()
{
MInterval i = this;
+ i.Left.RightLength - i.Left.LeftLength);
if (Math.Abs (new_diff) >= diff)
break;
+ M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
i = i.promote_left ();
- i.Right.balance ();
+ M17n.DebugPrint ("done\n");
+ i.Right.Balance ();
}
else if (diff < 0)
{
+ i.Right.LeftLength - i.Right.RightLength);
if (Math.Abs (new_diff) >= diff)
break;
+ M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
i = i.promote_right ();
- i.Left.balance ();
+ i.Left.Balance ();
}
+ else
+ break;
}
return i;
}
len = Stack.IsEmpty ? end - start : 0;
else
{
- MInterval i = interval.find (start);
+ MInterval i = interval.find_head (start);
- if (i.To == start && i.Right != null)
- i = i.Next;
len = 0;
while (i != null && mergeable (i))
{
len = Stack.IsEmpty ? end - start : 0;
else
{
- MInterval i = interval.find (end);
+ MInterval i = interval.find_tail (end);
- if (i.From == end && i.Left != null)
- i = i.Prev;
len = 0;
while (i != null && mergeable (i))
{
}
}
- public void Push (int start, int end, MTextProperty prop)
+ public void Push (int start, int end, MProperty prop)
{
update_from_to ();
M17n.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
public void MergeAfterChange (int start, int end)
{
update_from_to ();
- if (start < From)
- {
- Prev.MergeAfterChange (start, end);
- return;
- }
- MInterval head = this, tail = this, i;
+ MInterval head = find_head (start), tail = head, i;
- if (start == From && start > 0)
+ if (start == head.From && start > 0)
{
- i = Prev;
- if (mergeable (i))
+ i = head.Prev;
+ if (head.mergeable (i))
head = i;
}
while (tail.To < end)
}
}
+ public void PopSensitive (int start, int end)
+ {
+ update_from_to ();
+ MInterval head = find_head (start);
+ MInterval tail = find_tail (end);
+ while (! head.Stack.IsEmpty && head.From > 0)
+ {
+ MInterval prev = head.Prev;
+
+ if (prev.Stack.IsEmpty || head.Stack.Val != prev.Stack.Val)
+ break;
+ head = head.Prev;
+ }
+ while (! tail.Stack.IsEmpty && tail.To < mtext.Length)
+ {
+ MInterval next = tail.Next;
+
+ if (next.Stack.IsEmpty || tail.Stack.Val != next.Stack.Val)
+ break;
+ tail = tail.Next;
+ }
+ Pop (head.From, tail.To);
+ }
+
private void DumpOne (bool with_prop, bool newline)
{
DumpOne (with_prop, newline, false);