*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index 21d0bc9..fcf4a58 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
@@ -274,6 +275,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 +330,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 +355,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 +381,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 +393,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 +404,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);
@@ -434,10 +472,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
@@ -563,7 +607,11 @@ namespace M17N.Core
          if (Left != null)
            for (i = Left; i.Right != null; i = i.Right);
          else
-           for (i = Parent; i != null && i.Left == null; i = i.Parent);
+           {
+             MInterval child = this;
+             for (i = Parent; i != null && i.Left == child;
+                  child = i, i = i.Parent);
+           }
          return i;
        }
       }
@@ -575,7 +623,11 @@ namespace M17N.Core
          if (Right != null)
            for (i = Right; i.Left != null; i = i.Left);
          else
-           for (i = Parent; i != null && i.Right == null; i = i.Parent);
+           {
+             MInterval child = this;
+             for (i = Parent; i != null && i.Right == child;
+                  child = i, i = i.Parent);
+           }
          return i;
        }
       }
@@ -704,24 +756,32 @@ 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);
            right_copy = Right.Copy (To, end);
          }
 
-       copy = new MInterval (Key, mtext, end - start, Stack);
+       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;
       }
 
@@ -732,7 +792,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)
          {
@@ -752,7 +812,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)
          {
@@ -812,19 +872,120 @@ namespace M17N.Core
          }
       }
 
+      private MInterval delete_node_forward ()
+      {
+       if (Parent != null)
+         {
+           int len = Length - RightLength;
+
+           for (MInterval i = Parent; i != null; i = i.Parent)
+             i.Length -= len;
+           if (Parent.Left == this)
+             Parent.Left = Right;
+           else
+             Parent.Right = Right;
+         }
+
+       if (Right != null)
+         {
+           Right.Parent = Parent;
+           return Right.LeftMost;
+         }
+       return Parent;
+      }
+
+      private MInterval delete_node_backward ()
+      {
+       if (Parent != null)
+         {
+           int len = Length - RightLength;
+
+           for (MInterval i = Parent; i != null; i = i.Parent)
+             i.Length -= len;
+           if (Parent.Left == this)
+             Parent.Left = Left;
+           else
+             Parent.Right = Left;
+         }
+
+       if (Left != null)
+         {
+           Left.Parent = Parent;
+           return Left.RightMost;
+         }
+       return Parent;
+      }
+
+      private void set_mtext (MText mt)
+      {
+       mtext = mt;
+       if (Left != null)
+         Left.set_mtext (mt);
+       if (Right != null)
+         Right.set_mtext (mt);
+      }
+
+      private MInterval graft (MInterval interval, bool forward, out int len)
+      {
+       MInterval i;
+
+       len = 0;
+       if (forward)
+         {
+           i = interval.LeftMost;
+           while (i != null)
+             {
+               if (! mergeable (i))
+                 break;
+               len += i.Length - i.RightLength;
+               i = i.delete_node_forward ();
+             }
+         }
+       else
+         {
+           i = interval.RightMost;
+           while (i != null)
+             {
+               if (! mergeable (i))
+                 break;
+               len += i.Length - i.LeftLength;
+               i = i.delete_node_backward ();
+             }
+         }
+
+       Length += len;
+       To += len;
+       for (MInterval prev = this, ii = this.Parent; ii != null;
+            prev = ii, ii = ii.Parent)
+         {
+           ii.Length += len;
+           if (prev == ii.Left)
+             {
+               ii.From += len;
+               ii.To += len;;
+             }
+         }
+       if (i != null)
+         while (i.Parent != null) i = i.Parent;
+       return i;
+      }
+
       public void Insert (int pos, MInterval interval)
       {
        update_from_to ();
-       Console.Write ("insert({0}) in ", pos); DumpOne (false, true);
+       M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
+       DumpOne (false, true);
+
+       interval.set_mtext (mtext);
 
        if (pos < From)
          Prev.Insert (pos, interval);
        else if (pos == From)
          {
-           if (pos > 0)
-             {
-               MInterval prev = Prev;
+           MInterval prev = Prev;
 
+           if (prev != null)
+             {
                if (Left == null)
                  {
                    prev.Insert (pos, interval);
@@ -838,41 +999,57 @@ namespace M17N.Core
              (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
            interval.inherit_rear_properties (Stack);
 
-           // INTERVAL is ready to insert.
-           //
-           //    .-this-.   ==>          .-this-.
-           // left-.                  left-.
-           //     child                  .-interval
-           //                         child
-
-           if (pos > 0)
+           int len;
+           interval = graft (interval, false, out len);
+           if (interval != null && prev != null)
+             interval = prev.graft (interval, true, out len);
+           if (interval != null)
              {
-               MInterval i = Left.RightMost;
-       
-               i.Left = interval;
+               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
-             {
-               Left = interval;
-
-               for (MInterval i = this; i != null; i = i.Parent)
-                 i.Length += interval.Length;
-             }
          }
        else if (pos < To)
          {
            remove_properties (MTextProperty.Flag.Sensitive);
-           divide_right (pos).Insert (pos, interval);
-         }         
+
+           int len;
+           interval = graft (interval, true, out len);
+           pos += len;
+           if (interval != null)
+             interval = graft (interval, false, out len);
+           if (interval != 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)
          {
-           if (pos < mtext.Length)
-             {
-               MInterval next = Next;
+           MInterval next = Next;
 
+           if (next != null)
+             {
                if (Right == null)
                  {
                    next.Insert (pos, interval);
@@ -886,29 +1063,33 @@ namespace M17N.Core
              (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
            interval.inherit_front_properties (Stack);
 
-           // INTERVAL is ready to insert.
-           //
-           //    .-this-.   ==>          .-this-.
-           //         .-right                 .-right
-           //     child                  interval-.
-           //                                    child
-
-           if (Right != null)
+           int len;
+           interval = graft (interval, true, out len);
+           if (interval != null && next != null)
+             interval = next.graft (interval, false, out len);
+           if (interval != null)
              {
-               MInterval i = Right.LeftMost;
-       
-               i.Left = interval;
+               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;
              }
-           else
-             {
-               Right = interval;
-
-               for (MInterval i = this; i != null; i = i.Parent)
-                 i.Length += interval.Length;
-             }
          }
        else                    // (pos > To)
          Next.Insert (pos, interval);
@@ -916,12 +1097,13 @@ namespace M17N.Core
 
       private void vacate_node (MInterval interval)
       {
-       Console.WriteLine ("vacate #{0} to #{1}", ID, interval.ID);
+       M17N.DebugPrint ("vacate #{0} to #{1}", ID, interval.ID);
        if (interval != null)
          interval.Parent = Parent;
        if (Parent == null)
          {
-           mtext.intervals.Put (Key, interval);
+           if (mtext != null)
+             mtext.intervals.Put (Key, interval);
          }
        else
          {
@@ -941,7 +1123,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)
@@ -997,7 +1179,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)
@@ -1033,11 +1215,15 @@ namespace M17N.Core
        if (! mergeable (prev))
          return false;
 
+       M17N.DebugPrint ("merging "); DumpOne (false, false);
+       M17N.DebugPrint (" with prev "); prev.DumpOne (false, true);
        int len = prev.Length - prev.LeftLength;
 
        // PREV is Left, Left.Right, ..., or Left....Right.
        if (prev != Left)
          {
+           if (prev.Left != null)
+             prev.Left.Parent = prev.Parent;
            prev.Parent.Right = prev.Left;
            while (prev.Parent != Left)
              {
@@ -1047,7 +1233,11 @@ namespace M17N.Core
          }
        Left.Length -= len;
        if (Left.Length == Left.LeftLength)
-         Left = Left.Left;
+         {
+           if (Left.Left != null)
+             Left.Left.Parent = this;
+           Left = Left.Left;
+         }
        return true;
       }
 
@@ -1058,11 +1248,16 @@ namespace M17N.Core
        if (! mergeable (next))
          return false;
 
-       int len = next.Length - next.LeftLength;
+       M17N.DebugPrint ("merging "); DumpOne (false, false);
+       M17N.DebugPrint (" with next "); next.DumpOne (false, true);
+
+       int len = next.Length - next.RightLength;
 
        // NEXT is Right, Right.Left, ..., or Right....Left.
        if (next != Right)
          {
+           if (next.Right != null)
+             next.Right.Parent = next.Parent;
            next.Parent.Left = next.Right;
            while (next.Parent != Right)
              {
@@ -1071,9 +1266,12 @@ namespace M17N.Core
              }
          }
        Right.Length -= len;
-       if (Right.Length == Right.LeftLength)
-         Right = Right.Left;
-
+       if (Right.Length == Right.RightLength)
+         {
+           if (Right.Right != null)
+             Right.Right.Parent = this;
+           Right = Right.Right;
+         }
        return true;
       }
 
@@ -1081,7 +1279,7 @@ namespace M17N.Core
       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)
@@ -1114,9 +1312,9 @@ namespace M17N.Core
              divide_right (end);
            Stack.Pop ();
            if (check_prev && Left != null)
-             check_prev = try_merge_prev () && (Left == null);
+             check_prev = try_merge_prev () && (Left != null);
            if (check_next && Right != null)
-             check_next = try_merge_next () && (Right == null);
+             check_next = try_merge_next () && (Right != null);
            if (check_prev)
              {
                if (Prev.try_merge_next () && check_next)
@@ -1131,26 +1329,80 @@ namespace M17N.Core
 
       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);
+           if (Right != null)
+             {
+               if (indent_type >= 1)
+                 Right.DumpNested (indent + "  ", force);
+               else if (indent_type == 2)
+                 Right.DumpNested (indent + "| ", force);
+             }
+         }
       }
     }