*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index dddb24d..98cb0c5 100644 (file)
--- a/MText.cs
+++ b/MText.cs
@@ -2,6 +2,7 @@ using System;
 using System.Text;
 using System.Collections;
 using System.Collections.Generic;
+using M17N;
 using M17N.Core;
 
 namespace M17N.Core
@@ -52,7 +53,6 @@ namespace M17N.Core
     {
       this.key = key;
       this.val = val;
-      flags |= Flag.RearSticky;
     }
 
     public MTextProperty (MSymbol key, object val,
@@ -251,6 +251,8 @@ namespace M17N.Core
     {
       check_pos (pos, true);
 
+      if (from == to)
+       return;
       int pos_idx = pos_to_idx (this, pos);
       int from_idx = pos_to_idx (mt2, from);
       int to_idx = pos_to_idx (mt2, to);
@@ -274,6 +276,30 @@ namespace M17N.Core
        }
     }
 
+    private void insert (int pos, int c)
+    {
+      check_pos (pos, true);
+
+      int pos_idx = pos_to_idx (this, pos);
+
+      if (c < 0x10000)
+       {
+         char ch = (char) c;
+         sb.Insert (pos_idx, ch);
+       }
+      else
+       {
+         char high = (char) (0xD800 + ((c - 0x10000) >> 10));
+         char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
+         sb.Insert (pos_idx, low);
+         sb.Insert (pos_idx, high);
+       }
+      nchars++;
+      foreach (MPlist plist in intervals)
+       ((MInterval) plist.Val).Insert (pos,
+                                       new MInterval (plist.Key, this, 1));
+    }
+
     public int this[int i]
     {
       set {
@@ -305,7 +331,17 @@ namespace M17N.Core
 
     public MText Dup ()
     {
-      return (new MText (sb.ToString ()));
+      MText mt = new MText (sb.ToString ());
+
+      foreach (MPlist p in intervals)
+       mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (0, Length));
+      return mt;
+    }
+
+    public MText Ins (int pos, int c)
+    {
+      insert (pos, c);
+      return this;
     }
 
     public MText Ins (int pos, MText mt)
@@ -320,6 +356,12 @@ namespace M17N.Core
       return this;
     }
 
+    public MText Cat (int c)
+    {
+      insert (nchars, c);
+      return this;
+    }
+
     public MText Del (int from, int to)
     {
       if (check_range (from, to, true))
@@ -340,8 +382,7 @@ namespace M17N.Core
     {
       check_pos (pos, false);
 
-      MInterval i = (MInterval) intervals.Find (key).Val;
-
+      MInterval i = (MInterval) intervals.Get (key);
       if (i == null)
        return null;
 
@@ -353,8 +394,7 @@ namespace M17N.Core
     {
       check_pos (pos, false);
 
-      MInterval i = (MInterval) intervals.Find (key).Val;
-
+      MInterval i = (MInterval) intervals.Get (key);
       if (i == null)
        return (prop = null);
       prop = i.Get (pos);
@@ -365,8 +405,7 @@ namespace M17N.Core
     {
       check_pos (pos, false);
 
-      MInterval i = (MInterval) intervals.Find (key).Val;
-
+      MInterval i = (MInterval) intervals.Get (key);
       if (i == null)
        return (array = null);
       MTextProperty prop = i.Get (pos, out array);
@@ -426,7 +465,11 @@ namespace M17N.Core
          MPlist p = intervals.Find (key);
 
          if (p != null)
-           ((MInterval) p.Val).Pop (from, to);
+           {
+             MInterval root = (MInterval) p.Val;
+             root.Pop (from, to);
+             root.MergeAfterChange (from, to);
+           }
        }
     }
 
@@ -434,10 +477,16 @@ namespace M17N.Core
     {
       Console.Write ("(");
       foreach (MPlist p in intervals)
-       ((MInterval) p.Val).Dump ();
+       ((MInterval) p.Val).Dump (true);
       Console.WriteLine (")");
     }
 
+    public void DumpPropNested ()
+    {
+      foreach (MPlist p in intervals)
+       ((MInterval) p.Val).DumpNested (true);
+    }
+
     private class MInterval
     {
       // position: 0   1   2   3   4   5   6   7
@@ -503,7 +552,7 @@ namespace M17N.Core
        MPlist p;
        for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
          array[idx] = (MTextProperty) p.Val;
-       return array[idx - 1];
+       return array[0];
       }
 
       private MInterval (MSymbol key, MText mt, int length, MPlist stack)
@@ -548,12 +597,22 @@ namespace M17N.Core
 
       private MInterval LeftMost
       {
-       get { return (Left == null ? this : Left.LeftMost); }
+       get {
+         if (Left == null)
+           return this;
+         Left.update_from_to ();
+         return Left.LeftMost;
+       }
       }
 
       private MInterval RightMost
       {
-       get { return (Right == null ? this : Right.RightMost); }
+       get {
+         if (Right == null)
+           return this;
+         Right.update_from_to ();
+         return Right.RightMost;
+       }
       }
 
       private MInterval Prev {
@@ -561,12 +620,14 @@ namespace M17N.Core
          MInterval i;
 
          if (Left != null)
-           for (i = Left; i.Right != null; i = i.Right);
+           for (i = Left; i.Right != null; i = i.Right)
+             i.update_from_to ();
          else
            {
              MInterval child = this;
              for (i = Parent; i != null && i.Left == child;
-                  child = i, i = i.Parent);
+                  child = i, i = i.Parent)
+               i.update_from_to ();
            }
          return i;
        }
@@ -577,12 +638,14 @@ namespace M17N.Core
          MInterval i;
 
          if (Right != null)
-           for (i = Right; i.Left != null; i = i.Left);
+           for (i = Right; i.Left != null; i = i.Left)
+             i.update_from_to ();
          else
            {
              MInterval child = this;
              for (i = Parent; i != null && i.Right == child;
-                  child = i, i = i.Parent);
+                  child = i, i = i.Parent)
+               i.update_from_to ();
            }
          return i;
        }
@@ -712,13 +775,14 @@ namespace M17N.Core
        MInterval copy, left_copy = null, right_copy = null;
 
        update_from_to ();
+
        if (start < From)
          {
            if (end <= From)
              return Left.Copy (start, end);
            left_copy = Left.Copy (start, From);
          }
-       else if (end > To)
+       if (end > To)
          {
            if (start >= To)
              return Right.Copy (start, end);
@@ -727,9 +791,16 @@ namespace M17N.Core
 
        copy = new MInterval (Key, null, end - start, Stack);
        remove_properties (MTextProperty.Flag.Sensitive);
-       copy.Left = left_copy;
-       copy.Right = right_copy;
-
+       if (left_copy != null)
+         {
+           copy.Left = left_copy;
+           left_copy.Parent = copy;
+         }
+       if (right_copy != null)
+         {
+           copy.Right = right_copy;
+           right_copy.Parent = copy;
+         }
        return copy;
       }
 
@@ -740,7 +811,7 @@ namespace M17N.Core
       {
        MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
 
-       Console.Write ("divide-right({0}) at ", pos); DumpOne (false, true);
+       M17N.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
        To = pos;
        if (Right != null)
          {
@@ -760,7 +831,7 @@ namespace M17N.Core
       {
        MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
 
-       Console.Write ("divide-left({0}) at ", pos); DumpOne (false, true);
+       M17N.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
        From = pos;
        if (Left != null)
          {
@@ -921,8 +992,8 @@ namespace M17N.Core
       public void Insert (int pos, MInterval interval)
       {
        update_from_to ();
-       Console.Write ("insert({0}) at {1} in ", interval.Length, pos);
-       DumpOne (false, true);
+       M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
+       DumpOne (false, false);
 
        interval.set_mtext (mtext);
 
@@ -1041,11 +1112,20 @@ namespace M17N.Core
          }
        else                    // (pos > To)
          Next.Insert (pos, interval);
+       M17N.DebugPrint (" done\n");
       }
 
       private void vacate_node (MInterval interval)
       {
-       Console.WriteLine ("vacate #{0} to #{1}", ID, interval.ID);
+       vacate_node (interval, null);
+      }
+
+      private void vacate_node (MInterval interval, MInterval stop)
+      {
+       if (interval != null)
+         M17N.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
+       else
+         M17N.DebugPrint ("vacate #{0} to null\n", ID);
        if (interval != null)
          interval.Parent = Parent;
        if (Parent == null)
@@ -1063,7 +1143,7 @@ namespace M17N.Core
            int diff = Length;
            if (interval != null)
              diff -= interval.Length;
-           for (MInterval i = Parent; i != null; i = i.Parent)
+           for (MInterval i = Parent; i != stop; i = i.Parent)
              i.Length -= diff;
          }
       }
@@ -1071,7 +1151,7 @@ namespace M17N.Core
       public void Delete (int start, int end)
       {
        update_from_to ();
-       Console.Write ("delete({0} {1}) from ", start, end); DumpOne (false, true);
+       M17N.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
        if (start < From)
          {
            if (end <= From)
@@ -1127,7 +1207,7 @@ namespace M17N.Core
       public void Push (int start, int end, MTextProperty prop)
       {
        update_from_to ();
-       Console.Write ("push({0} {1}) at ", start, end); DumpOne (false, true);
+       M17N.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
        if (start < From)
          {
            if (end <= From)
@@ -1156,62 +1236,97 @@ namespace M17N.Core
        Stack.Push (prop.key, prop);
       }
 
-      private bool try_merge_prev ()
+      private static void merge_nodes (MInterval head, MInterval tail)
       {
-       MInterval prev = Prev;
+       M17N.DebugPrint ("merging "); head.DumpOne (true, false);
+       M17N.DebugPrint (" through "); tail.DumpOne (true, false);
 
-       if (! mergeable (prev))
-         return false;
+       int from = head.From;
+       int to = tail.To;
+       MInterval root;
 
-       int len = prev.Length - prev.LeftLength;
+       for (root = head; root.To + root.RightLength < to;
+            root = root.Parent);
+       
+       M17N.DebugPrint (" common root is "); root.DumpOne (false, true);
 
-       // PREV is Left, Left.Right, ..., or Left....Right.
-       if (prev != Left)
+       if (from < root.From)
+         {
+           MInterval prev = root.Prev;
+
+           while (true)
+             {
+               M17N.DebugPrint ("merging "); prev.DumpOne (false, true);
+               prev.vacate_node (prev.Left, root);
+               if (prev == head)
+                 break;
+               if (prev.Left != null)
+                 prev = prev.Left.RightMost;
+               else
+                 prev = prev.Parent;
+             }
+         }
+       if (root.To < to)
          {
-           prev.Parent.Right = prev.Left;
-           while (prev.Parent != Left)
+           MInterval next = root.Next;
+
+           while (true)
              {
-               prev.Length -= len;
-               prev = prev.Parent;
+               M17N.DebugPrint ("merging "); next.DumpOne (false, true);
+               next.vacate_node (next.Right, root);
+               if (next == tail)
+                 break;
+               if (next.Right != null)
+                 next = next.Right.LeftMost;
+               else
+                 next = next.Parent;
              }
          }
-       Left.Length -= len;
-       if (Left.Length == Left.LeftLength)
-         Left = Left.Left;
-       return true;
       }
 
-      private bool try_merge_next ()
+      public void MergeAfterChange (int start, int end)
       {
-       MInterval next = Next;
-
-       if (! mergeable (next))
-         return false;
+       update_from_to ();
+       if (start < From)
+         {
+           Prev.MergeAfterChange (start, end);
+           return;
+         }
 
-       int len = next.Length - next.LeftLength;
+       MInterval head = this, tail = this, i;
 
-       // NEXT is Right, Right.Left, ..., or Right....Left.
-       if (next != Right)
+       if (start == From && start > 0)
          {
-           next.Parent.Left = next.Right;
-           while (next.Parent != Right)
+           i = Prev;
+           if (mergeable (i))
+             head = i;
+         }
+       while (tail.To < end)
+         {
+           i = tail.Next;
+           if (! tail.mergeable (i))
              {
-               next.Length -= len;
-               next = next.Parent;
+               if (head != tail)
+                 merge_nodes (head, tail);
+               head = i;
              }
+           tail = i;
          }
-       Right.Length -= len;
-       if (Right.Length == Right.LeftLength)
-         Right = Right.Left;
-
-       return true;
+       while (true)
+         {
+           i = tail.Next;
+           if (i == null || ! tail.mergeable (i))
+             break;
+           tail = i;
+         }
+       if (head != tail)
+         merge_nodes (head, tail);
       }
 
-
       public void Pop (int start, int end)
       {
        update_from_to ();
-       Console.Write ("pop({0} {1}) at ", start, end); DumpOne (false, true);
+       M17N.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
        if (start < From)
          {
            if (end <= From)
@@ -1235,52 +1350,90 @@ namespace M17N.Core
 
        if (! Stack.IsEmpty)
          {
-           bool check_prev = start == From && start > 0;
-           bool check_next = end == To && end < mtext.Length;
-
-           if (! check_prev)
+           if (start > From)
              divide_left (start);
-           if (! check_next)
+           if (end < To)
              divide_right (end);
            Stack.Pop ();
-           if (check_prev && Left != null)
-             check_prev = try_merge_prev () && (Left != null);
-           if (check_next && Right != null)
-             check_next = try_merge_next () && (Right != null);
-           if (check_prev)
-             {
-               if (Prev.try_merge_next () && check_next)
-                 Prev.try_merge_next ();
-             }
-           else if (check_next)
-             {
-               Next.try_merge_prev ();
-             }
          }
       }
 
       private void DumpOne (bool with_prop, bool newline)
       {
-       Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
-       if (with_prop)
-         foreach (MPlist p in Stack)
-           Console.Write (" " + p.Val);
-       Console.Write (")");
-       if (newline)
-         Console.WriteLine ();
+       DumpOne (with_prop, newline, false);
       }
 
-      public void Dump ()
+      private void DumpOne (bool with_prop, bool newline, bool force)
       {
-       update_from_to ();
+       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);
+           Console.Write (")");
+           if (newline)
+             Console.WriteLine ();
+           if (Length <= 0)
+             throw new Exception ("Invalid interval length");
+         }
+      }
 
-       if (Left != null)
-         Left.Dump ();
-       if (From > 0)
-         Console.Write (" ");
-       DumpOne (true, false);
-       if (Right != null)
-         Right.Dump ();
+      public void Dump () { Dump (false); }
+
+      public void Dump (bool force)
+      {
+       if (force || M17N.debug)
+         {
+           update_from_to ();
+
+           if (Left != null)
+             Left.Dump (force);
+           if (From > 0)
+             Console.Write (" ");
+           DumpOne (true, false, force);
+           if (Right != null)
+             Right.Dump (force);
+         }
+      }
+
+      private int Depth {
+       get { return (Parent == null ? 0 : Parent.Depth + 1); }
+      }
+
+      public void DumpNested (bool force)
+      {
+       DumpNested ("", force);
+      }
+
+      public void DumpNested (string indent, bool force)
+      {
+       if (force || M17N.debug)
+         {
+           int indent_type = (Parent == null ? 1
+                              : Parent.Left == this ? 0 : 2);
+
+           update_from_to ();
+           if (Left != null)
+             {
+               if (indent_type <= 1)
+                 Left.DumpNested (indent + "  ", force);
+               else
+                 Left.DumpNested (indent + "| ", force);
+             }
+           if (indent_type == 0)
+             Console.Write (indent + ".-");
+           else if (indent_type == 2)
+             Console.Write (indent + "`-");
+           DumpOne (true, true, true);
+           if (Right != null)
+             {
+               if (indent_type >= 1)
+                 Right.DumpNested (indent + "  ", force);
+               else
+                 Right.DumpNested (indent + "| ", force);
+             }
+         }
       }
     }