*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index c861720..f8d7379 100644 (file)
--- a/MText.cs
+++ b/MText.cs
@@ -19,28 +19,34 @@ namespace M17N.Core
     }
 #endif
 
-  public class MTextProperty
+  public class MProperty
   {
     [FlagsAttribute]
     public enum Flags
     {
-      None =           0,
+      None =           0x00,
       /// A text inserted before a character that has this property
       /// inherits this property.  If the text already has properties
-      /// of the same key, they are deleted.  See the documentation of
-      /// Sensitive for exception.
-      FrontSticky =    1,
+      /// of the same key, they are deleted.
+      FrontSticky =    0x01,
       /// A text inserted after a character that has this property
       /// inherits this property.  If the text already has properties
-      /// of the same key, they are deleted.  See the documentation of
-      /// Sensitive for exception.
-      RearSticky =     2,
+      /// of the same key, they are deleted.
+      RearSticky =     0x02,
       /// 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.
+      Sensitive =      0x04,
+      /// Like Sensitive but also this property is deleted from a span
+      /// of text if a text just before the span is modified,
+      /// inserted, or deleted.
+      FrontSensitive = 0x0C,
+      /// Like Sensitive but also this property is deleted from a span
+      /// of text if a text just after the span is modified, inserted,
+      /// or deleted.
+      RearSensitive =  0x14
     };
 
     internal MSymbol key;
@@ -49,12 +55,25 @@ namespace M17N.Core
     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 = Flags.None;
       this.key = key;
       this.val = val;
     }
 
+    public MProperty (string name, object val)
+    {
+      key = MSymbol.PropertyKey (name);
+      this.val = val;
+    }
+
+    public static bool HasFlags (MSymbol key, Flags flags)
+    {
+      return ((key.flags & flags) == flags);
+    }
+
     public override string ToString ()
     {
       return key.ToString () + ":" + val;
@@ -126,27 +145,27 @@ namespace M17N.Core
     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
@@ -263,10 +282,11 @@ namespace M17N.Core
        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)
@@ -399,7 +419,12 @@ namespace M17N.Core
 
       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)
+             ((MInterval) plist.Val).MergeAfterChange (from, from);
+         }
       else
        intervals.Clear ();
       if (M17n.debug)
@@ -415,11 +440,11 @@ namespace M17N.Core
       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);
 
@@ -430,24 +455,24 @@ namespace M17N.Core
       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)
        {
@@ -471,7 +496,11 @@ namespace M17N.Core
          else
            root = (MInterval) p.Val;
 
+         if (root.isSensitive)
+           root.PopSensitive (from, to);
          root.Push (from, to, prop);
+         root.MergeAfterChange (from, to);
+         root.Balance ();
        }
     }
 
@@ -496,8 +525,15 @@ namespace M17N.Core
          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 = (MInterval) p.Val;
+             if (M17n.debug)
+               DumpPropNested ();
              root.MergeAfterChange (from, to);
+             root.Balance ();
            }
        }
     }
@@ -561,19 +597,17 @@ namespace M17N.Core
       }
 
       /// 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;
@@ -582,12 +616,12 @@ namespace M17N.Core
            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];
       }
 
@@ -604,20 +638,29 @@ namespace M17N.Core
 
       private bool isRearSticky
       {
-       get { return ((Key.TextPropertyFlags & MTextProperty.Flags.RearSticky)
-                     != MTextProperty.Flags.None); }
+       get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
       }
 
       private bool isFrontSticky
       {
-       get { return ((Key.TextPropertyFlags & MTextProperty.Flags.FrontSticky)
-                     != MTextProperty.Flags.None); }
+       get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
+      }
+
+      public bool isSensitive
+      {
+       get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
+      }
+
+      public bool isFrontSensitive
+      {
+       get { return MProperty.HasFlags (Key,
+                                        MProperty.Flags.FrontSensitive) ; }
       }
 
-      private bool isSensitive
+      public bool isRearSensitive
       {
-       get { return ((Key.TextPropertyFlags & MTextProperty.Flags.Sensitive)
-                     != MTextProperty.Flags.None); }
+       get { return MProperty.HasFlags (Key,
+                                        MProperty.Flags.RearSensitive) ; }
       }
 
       private void update_from_to ()
@@ -709,13 +752,23 @@ namespace M17N.Core
        }
       }
 
-      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;
       }
 
@@ -736,29 +789,31 @@ namespace M17N.Core
       //          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;
+
+       Parent.update_from_to ();
        return Parent;
       }
 
@@ -768,36 +823,39 @@ namespace M17N.Core
       // 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;
+
+       Parent.update_from_to ();
        return Parent;
       }
 
-      private MInterval balance ()
+      public MInterval Balance ()
       {
        MInterval i = this;
 
+       update_from_to ();
        while (true)
          {
            //       .-this-.
@@ -812,8 +870,10 @@ namespace M17N.Core
                            + 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)
              {
@@ -821,9 +881,12 @@ namespace M17N.Core
                            + 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;
       }
@@ -939,10 +1002,8 @@ namespace M17N.Core
          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))
              {
@@ -975,10 +1036,8 @@ namespace M17N.Core
          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))
              {
@@ -1012,119 +1071,137 @@ namespace M17N.Core
          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");
       }
 
@@ -1217,7 +1294,7 @@ namespace M17N.Core
          }
       }
 
-      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);
@@ -1254,14 +1331,13 @@ namespace M17N.Core
       /// intervals are mergeable.
       private static void combine (MInterval head, MInterval tail)
       {
-       M17n.DebugPrint ("merging "); head.DumpOne (true, false);
+       M17n.DebugPrint ("combining "); head.DumpOne (true, false);
        M17n.DebugPrint (" through "); tail.DumpOne (true, false);
 
        int from = head.From;
        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);
        
@@ -1282,6 +1358,7 @@ namespace M17N.Core
                else
                  prev = prev.Parent;
              }
+           root.update_from_to ();
          }
        if (root.To < to)
          {
@@ -1298,46 +1375,35 @@ namespace M17N.Core
                else
                  next = next.Parent;
              }
+           root.update_from_to ();
          }
       }
 
       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), i = head;
+       MInterval tail = find_tail (end).Next;
 
-       if (start == From && start > 0)
+       if (start == head.From && start > 0)
          {
-           i = Prev;
-           if (mergeable (i))
-             head = i;
+           i = head.Prev;
+           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);
-               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)
@@ -1375,6 +1441,30 @@ namespace M17N.Core
          }
       }
 
+      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);
@@ -1385,9 +1475,16 @@ namespace M17N.Core
        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 ();