*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index ff29503..cfa688b 100644 (file)
--- a/MText.cs
+++ b/MText.cs
@@ -22,27 +22,55 @@ namespace M17N.Core
   {
     internal MSymbol key;
     internal object val;
-    private bool front_sticky;
-    private bool rear_sticky;
+
+    [FlagsAttribute]
+    internal enum Flag : byte
+      {
+       None =          0,
+       FrontSticky =   1,
+       RearSticky =    2,
+       Sensitive =     4
+      };
+    internal Flag flags;
 
     public MSymbol Key { get { return key; } }
     public object Val { get { return val; } }
-    public bool FrontSticky { get { return front_sticky; } }
-    public bool RearSticky { get { return rear_sticky; } }
+    public bool FrontSticky
+    {
+      get { return (flags & Flag.FrontSticky) != Flag.None; }
+    }
+    public bool RearSticky
+    {
+      get { return (flags & Flag.RearSticky) != Flag.None; }
+    }
+    public bool Sensitive
+    {
+      get { return (flags & Flag.Sensitive) != Flag.None; }
+    }
 
     public MTextProperty (MSymbol key, object val)
     {
       this.key = key;
       this.val = val;
+      flags |= Flag.RearSticky;
     }
 
     public MTextProperty (MSymbol key, object val,
-                         bool front_sticky, bool rear_sticky)
+                         bool front_sticky, bool rear_sticky, bool sensitive)
     {
       this.key = key;
       this.val = val;
-      this.front_sticky = front_sticky;
-      this.rear_sticky = rear_sticky;
+      if (front_sticky)
+       flags |= Flag.FrontSticky;
+      if (rear_sticky)
+       flags |= Flag.RearSticky;
+      if (sensitive)
+       flags |= Flag.Sensitive;
+    }
+
+    public override string ToString ()
+    {
+      return key.ToString () + ":" + val;
     }
   }
 
@@ -136,7 +164,7 @@ namespace M17N.Core
       return this.sb.ToString ().CompareTo (other.sb.ToString ());
     }
 
-    public override String ToString () { return sb.ToString (); }
+    public override String ToString () { return "\"" + sb.ToString () + "\""; }
 
     private static bool surrogate_high_p (char c)
     {
@@ -205,8 +233,23 @@ namespace M17N.Core
       return i;
     }
 
+    private void check_pos (int pos, bool tail_ok)
+    {
+      if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
+       throw new Exception ("Invalid MText position:" + pos);
+    }
+
+    private void check_range (int from, int to, bool zero_ok)
+    {
+      if (from < 0 || (zero_ok ? from > to : from >= to)
+         || to > nchars)
+       throw new Exception ("Invalid MText range");
+    }
+
     private void insert (int pos, MText mt2, int from, int to)
     {
+      check_pos (pos, true);
+
       int pos_idx = pos_to_idx (this, pos);
       int from_idx = pos_to_idx (mt2, from);
       int to_idx = pos_to_idx (mt2, to);
@@ -215,18 +258,18 @@ namespace M17N.Core
       nchars += to - from;
 
       foreach (MPlist plist in mt2.intervals)
-       if (intervals.find (plist.Key) == null)
-         intervals.push (plist.Key, new MInterval (plist.Key, this));
+       if (intervals.Find (plist.Key) == null)
+         intervals.Push (plist.Key, new MInterval (plist.Key, this));
       foreach (MPlist plist in intervals)
        {
-         MPlist p = mt2.intervals.find (plist.Key);
+         MPlist p = mt2.intervals.Find (plist.Key);
          MInterval interval;
 
          if (p.IsEmpty)
            interval = new MInterval (plist.Key, to - from);
          else
            interval = ((MInterval) p.Val).copy (from, to);
-         ((MInterval) plist.Val).insert (pos, interval);
+         ((MInterval) plist.Val).Insert (pos, interval);
        }
     }
 
@@ -259,90 +302,111 @@ namespace M17N.Core
       }
     }
 
-    public MText dup ()
+    public MText Dup ()
     {
       return (new MText (sb.ToString ()));
     }
 
-    public MText ins (int pos, MText mt)
+    public MText Ins (int pos, MText mt)
     {
       insert (pos, mt, 0, mt.nchars);
       return this;
     }
 
-    public MText ins (int pos, MText mt, int from, int to)
+    public MText Ins (int pos, MText mt, int from, int to)
     {
       insert (pos, mt, from, to);
       return this;
     }
 
-    public MText del (int from, int to)
+    public MText Del (int from, int to)
     {
+      check_range (from, to, true);
+
       sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
       nchars -= to - from;
 
-      foreach (MPlist plist in intervals)
-       ((MInterval) plist.Val).delete (from, to);
+      if (nchars > 0)
+       foreach (MPlist plist in intervals)
+         ((MInterval) plist.Val).Delete (from, to);
+      else
+       intervals = new MPlist ();
       return this;
     }
 
-    public object get_prop (int pos, MSymbol key)
+    public object GetProp (int pos, MSymbol key)
     {
-      MInterval i = (MInterval) intervals.find (key).Val;
+      check_pos (pos, false);
+
+      MInterval i = (MInterval) intervals.Find (key).Val;
 
       if (i == null)
        return null;
 
-      MTextProperty prop = i.get (pos);
+      MTextProperty prop = i.Get (pos);
       return (prop != null ? prop.Val : null);
     }
 
-    public object get_prop (int pos, MSymbol key, out MTextProperty prop)
+    public object GetProp (int pos, MSymbol key, out MTextProperty prop)
     {
-      MInterval i = (MInterval) intervals.find (key).Val;
+      check_pos (pos, false);
+
+      MInterval i = (MInterval) intervals.Find (key).Val;
 
       if (i == null)
        return (prop = null);
-      prop = i.get (pos);
+      prop = i.Get (pos);
       return (prop != null ? prop.Val : null);
     }
 
-    public object get_prop (int pos, MSymbol key, out MTextProperty[] array)
+    public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
     {
-      MInterval i = (MInterval) intervals.find (key).Val;
+      check_pos (pos, false);
+
+      MInterval i = (MInterval) intervals.Find (key).Val;
 
       if (i == null)
        return (array = null);
-      MTextProperty prop = i.get (pos, out array);
+      MTextProperty prop = i.Get (pos, out array);
       return (prop != null ? prop.Val : null);
     }
 
-    public void push_prop (int from, int to, MSymbol key, object val)
+    public void PushProp (int from, int to, MSymbol key, object val)
     {
-      push_prop (from, to, new MTextProperty (key, val));
+      check_range (from, to, false);
+
+      PushProp (from, to, new MTextProperty (key, val));
     }
 
-    public void push_prop (int from, int to, MTextProperty prop)
+    public void PushProp (int from, int to, MTextProperty prop)
     {
       if (from < 0)
        {
          if (default_property == null)
            default_property = new MPlist ();
-         default_property.push (prop.key, prop.val);
+         default_property.Push (prop.key, prop.val);
        }
       else
        {
-         MInterval root = (MInterval) intervals.find (prop.key).Val;
+         MInterval root = (MInterval) intervals.Find (prop.key).Val;
 
          if (root == null)
            {
              root = new MInterval (prop.key, this);
-             intervals.push (prop.key, root);
+             intervals.Push (prop.key, root);
            }
          root.Push (from, to, prop);
        }
     }
 
+    public void DumpProp ()
+    {
+      Console.Write ("(");
+      foreach (MPlist p in intervals)
+       ((MInterval) p.Val).Dump ();
+      Console.WriteLine (")");
+    }
+
     private class MInterval
     {
       // position: 0   1   2   3   4   5   6   7
@@ -354,10 +418,13 @@ namespace M17N.Core
       //                      [7 (3 4)]
       //              [3 (1 2)]       [3 (4 6)]
       //         [1 (0 1)] [2 (2 3)]      [1 (6 7)]
+      //
+      private static int count = 0;
+      private int id;
       private int total_length;
       private int from, to;
       private MSymbol key;
-      private Stack<MTextProperty> stack;
+      private MPlist stack;
       private MInterval left, right, parent;
       private MText mtext;
 
@@ -367,7 +434,8 @@ namespace M17N.Core
          throw new Exception ("Invalid interval length");
        this.key = key;
        total_length = length;
-       stack = new Stack<MTextProperty> ();
+       stack = new MPlist ();
+       id = count++;
       }
 
       public MInterval (MSymbol key, MText mt)
@@ -377,46 +445,62 @@ namespace M17N.Core
        total_length = mt.sb.Length;
        from = 0;
        to = total_length;
-       stack = new Stack<MTextProperty> ();
+       stack = new MPlist ();
+       id = count++;
       }
 
-      public MTextProperty get (int pos)
+      public MTextProperty Get (int pos)
       {
        MInterval i = find (pos);
 
-       return (i.stack.Count > 0 ? i.stack.Peek () : null);
+       return (i.stack.IsEmpty ? null : (MTextProperty) i.stack.Val);
       }
 
-      public MTextProperty get (int pos, out MTextProperty[] array)
+      public MTextProperty Get (int pos, out MTextProperty[] array)
       {
        MInterval i = find (pos);
 
-       if (i.stack.Count == 0)
+       if (i.stack.IsEmpty)
          {
            array = null;
            return null;
          }
-       array = i.stack.ToArray ();
-       return i.stack.Peek ();
+       array = new MTextProperty[i.stack.Count];
+
+       int idx;
+       MPlist p;
+       for (idx = 0, p = i.stack; ! p.IsEmpty; idx++, p = p.Next)
+         array[idx] = (MTextProperty) p.Val;
+       return array[idx - 1];
       }
 
-      private MInterval (MSymbol key, int length, Stack<MTextProperty> stack)
+      private MInterval (MSymbol key, int length, MPlist stack)
       {
        this.key = key;
        total_length = length;
        from = 0;
        to = total_length;
-       stack = new Stack<MTextProperty> (stack);
+       this.stack = stack.Clone ();
+       id = count++;
       }
 
-
       private void update_from_to ()
       {
-       if (parent != null)
+       if (parent == null)
+         {
+           from = LeftLength;
+           to = total_length - RightLength;
+         }
+       else if (parent.left == this)
          {
            from = parent.from - total_length + LeftLength;
            to = parent.from - RightLength;
          }
+       else
+         {
+           from = parent.to + LeftLength;
+           to = parent.to + total_length - RightLength;
+         }
       }
 
       private int LeftLength
@@ -429,17 +513,17 @@ namespace M17N.Core
        get { return (right == null ? 0 : right.total_length); }
       }
 
-      private MInterval LeftMostNode
+      private MInterval left_most_node
       {
-       get { return (left == null ? this : left.LeftMostNode); }
+       get { return (left == null ? this : left.left_most_node); }
       }
 
-      private MInterval RightMostNode
+      private MInterval right_most_node
       {
-       get { return (right == null ? this : right.RightMostNode); }
+       get { return (right == null ? this : right.right_most_node); }
       }
 
-      private MInterval LeftNode {
+      private MInterval prev {
        get {
          MInterval i;
 
@@ -451,7 +535,7 @@ namespace M17N.Core
        }
       }
 
-      private MInterval RightNode {
+      private MInterval next {
        get {
          MInterval i;
 
@@ -483,7 +567,7 @@ namespace M17N.Core
        MInterval c1;
 
        if (parent == null)
-         mtext.intervals.put (key, right);
+         mtext.intervals.Put (key, right);
        else if (parent.left == this)
          parent.left = right;
        else
@@ -515,7 +599,7 @@ namespace M17N.Core
        MInterval c1;
 
        if (parent == null)
-         mtext.intervals.put (key, left);
+         mtext.intervals.Put (key, left);
        else if (parent.left == this)
          parent.left = left;
        else
@@ -600,13 +684,13 @@ namespace M17N.Core
       //                            right
       private MInterval divide_right (int pos)
       {
-       update_from_to ();
-
        MInterval interval = new MInterval (key, to - pos, stack);
 
-       total_length -= to - pos;
+       Console.Write ("divide-right({0}) at ", pos); DumpOne (false, true);
+       to = pos;
        if (right != null)
          {
+           interval.right = right;
            right.parent = interval;
            interval.total_length += right.total_length;
          }
@@ -620,13 +704,13 @@ namespace M17N.Core
       //                 left
       private MInterval divide_left (int pos)
       {
-       update_from_to ();
-
-       MInterval interval = new MInterval (key, to - pos, stack);
+       MInterval interval = new MInterval (key, pos - from, stack);
 
-       total_length -= to - pos;
+       Console.Write ("divide-reft({0}) at ", pos); DumpOne (false, true);
+       from = pos;
        if (left != null)
          {
+           interval.left = left;
            left.parent = interval;
            interval.total_length += left.total_length;
          }
@@ -635,145 +719,218 @@ namespace M17N.Core
        return interval;
       }
 
-      public void insert (int pos, MInterval interval)
+      private void remove_properties (MTextProperty.Flag flags)
+      {
+       for (MPlist p = stack; ! p.IsEmpty;)
+         {
+           MTextProperty prop = (MTextProperty) p.Val;
+
+           if ((prop.flags & flags) == flags)
+             p.Pop ();
+           else
+             p = p.Next;
+         }
+      }
+
+      private void merge_front_properties (MPlist plist)
+      {
+       for (MInterval i = left_most_node; i != null; i = i.next)
+         {
+           if (! stack.IsEmpty)
+             break;
+           for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
+             {
+               MTextProperty prop = (MTextProperty) p.Val;
+
+               if ((prop.flags & MTextProperty.Flag.RearSticky)
+                   == MTextProperty.Flag.RearSticky)
+                 i.stack.Push (prop.key, prop);
+             }
+         }
+      }
+
+      private void merge_rear_properties (MPlist plist)
+      {
+       for (MInterval i = right_most_node; i != null; i = i.prev)
+         {
+           if (! stack.IsEmpty)
+             break;
+           for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
+             {
+               MTextProperty prop = (MTextProperty) p.Val;
+
+               if ((prop.flags & MTextProperty.Flag.FrontSticky)
+                   == MTextProperty.Flag.FrontSticky)
+                 i.stack.Push (prop.key, prop);
+             }
+         }
+      }
+
+      public void Insert (int pos, MInterval interval)
       {
        update_from_to ();
-       if (pos < from)
+       Console.Write ("insert({0}) at ", pos); DumpOne (false, true);
+       if (pos < from || (pos == from && left == null && pos > 0))
          {
-           LeftNode.insert (pos, interval);
+           prev.Insert (pos, interval);
            return;
          }
-       if (pos >= to)
+       if (pos > to || (pos == to && right == null && next != null))
          {
-           RightNode.insert (pos, interval);
+           next.Insert (pos, interval);
            return;
          }
-       if (pos > from)
+       if (pos > from && pos < to)
          {
-           divide_right (pos).insert (pos, interval);
+           remove_properties (MTextProperty.Flag.Sensitive);
+           divide_right (pos).Insert (pos, interval);
            return;
          }         
-
-       // POS == FROM
-       if (left != null && LeftNode.stack.Count > 0)
+       if (pos == from)
          {
-           Stack<MTextProperty> s = new Stack<MTextProperty> ();
-
-           foreach (MTextProperty p in LeftNode.stack)
-             if (p.RearSticky)
-               s.Push (p);
-           if (s.Count > 0)
+           if (pos > 0)
              {
-               for (MInterval i = interval.LeftMostNode;
-                    i != null && i.stack.Count == 0;
-                    i = i.LeftNode)
-                 foreach (MTextProperty p in s)
-                   i.stack.Push (p);
+               prev.remove_properties
+                 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
+               interval.merge_front_properties (prev.stack);
              }
-         }
-       if (stack.Count > 0)
-         {
-           Stack<MTextProperty> s = new Stack<MTextProperty> ();
-
-           foreach (MTextProperty p in stack)
-             if (p.FrontSticky)
-               s.Push (p);
-           if (s.Count > 0)
+           remove_properties
+             (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
+           interval.merge_rear_properties (stack);
+
+           // INTERVAL is ready to insert.
+           //
+           //    .-this-.   ==>          .-this-.
+           // left-.                  left-.
+           //     child                  .-interval
+           //                         child
+
+           if (pos > 0)
              {
-               for (MInterval i = interval.RightMostNode;
-                    i != null && i.stack.Count == 0;
-                    i = i.RightNode)
-                 foreach (MTextProperty p in s)
-                   i.stack.Push (p);
+               MInterval i = left.right_most_node;
+       
+               i.left = interval;
+               interval.parent = i;
+               for (; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
              }
-         }
-
-       // INTERVAL is ready to insert.
-       //
-       //    .-this-.   ==>          .-this-.
-       // left-.                  left-.
-       //     child                  interval
-       //                         child
+           else
+             {
+               left = interval;
 
-       if (left != null)
-         {
-           MInterval i = left.RightMostNode;
-       
-           i.left = interval;
-           interval.parent = i;
-           for (; i != null; i = i.parent)
-             i.total_length += interval.total_length;
+               for (MInterval i = this; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
+             }
          }
-       else
+       else                    // pos == to
          {
-           left = interval;
+           if (right != null)
+             {
+               MInterval left_most = right.left_most_node;
 
-           for (MInterval i = this; i != null; i = i.parent)
-             i.total_length += interval.total_length;
+               left_most.remove_properties
+                 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
+               interval.merge_rear_properties (left_most.stack);
+             }
+           remove_properties
+             (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
+           interval.merge_front_properties (stack);
+
+           // INTERVAL is ready to insert.
+           //
+           //    .-this-.   ==>          .-this-.
+           //         .-right                 .-right
+           //     child                  interval-.
+           //                                    child
+
+           if (right != null)
+             {
+               MInterval i = right.left_most_node;
+       
+               i.left = interval;
+               interval.parent = i;
+               for (; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
+             }
+           else
+             {
+               right = interval;
+
+               for (MInterval i = this; i != null; i = i.parent)
+                 i.total_length += interval.total_length;
+             }
          }
       }
 
-      private void update_parent (MInterval i)
+      private void vacate_node (MInterval interval)
       {
+       Console.WriteLine ("vacate #{0} to #{1}", id, interval.id);
+       if (interval != null)
+         interval.parent = parent;
        if (parent == null)
-         mtext.intervals.put (key, i);
+         {
+           mtext.intervals.Put (key, interval);
+         }
        else
          {
-           int diff;
-
-           if (parent.right == i)
-             {
-               diff = parent.right.total_length - i.total_length;
-               parent.right = i;
-             }
+           if (this == parent.right)
+             parent.right = interval;
            else
-             {
-               diff = parent.left.total_length - i.total_length;
-               parent.left = i;
-             }
-           for (i = parent; i != null; i = i.parent)
-             i.total_length += diff;
+             parent.left = interval;
+
+           int diff = total_length;
+           if (interval != null)
+             diff -= interval.total_length;
+           for (MInterval i = parent; i != null; i = i.parent)
+             i.total_length -= diff;
          }
       }
 
-      public void delete (int start, int end)
+      public void Delete (int start, int end)
       {
        update_from_to ();
+       Console.Write ("delete({0} {1}) at ", start, end); DumpOne (false, true);
        if (start < from)
          {
            if (end <= from)
              {
-               left.delete (start, end);
+               left.Delete (start, end);
                return;
              }
-           left.delete (start, from);
-           start = from;
+           left.Delete (start, from);
+           to -= from - start;
+           end -= from - start;
+           from = start;
          }
-       else if (end > to)
+       if (end > to)
          {
            if (start >= to)
              {
-               right.delete (start, end);
+               right.Delete (start, end);
                return;
              }
-           right.delete (to, end);
+           right.Delete (to, end);
            end = to;
          }
        if (start == from && end == to)
          {
-           if (left == null)
-             update_parent (right);
-           else if (right == null)
-             update_parent (left);
+           if (right == null)
+             {
+               vacate_node (left);
+             }
            else
              {
-               MInterval i;
+               if (left != null)
+                 {
+                   MInterval i;
                
-               for (i = right; i.left != null; i = i.left)
-                 i.total_length += left.total_length;
-               i.total_length += left.total_length;
-               i.left = left;
-               update_parent (right);
+                   for (i = right; i.left != null; i = i.left)
+                     i.total_length += left.total_length;
+                   i.total_length += left.total_length;
+                   i.left = left;
+                   left.parent = i;
+                 }
+               vacate_node (right);
              }
          }
        else
@@ -785,30 +942,60 @@ namespace M17N.Core
          }
       }
 
-      public MTextProperty Push (int start, int end, MTextProperty prop)
+      public void Push (int start, int end, MTextProperty prop)
       {
        update_from_to ();
+       Console.Write ("push({0} {1}) at ", start, end); DumpOne (false, true);
        if (start < from)
          {
-           left.Push (start, end, prop);
            if (end <= from)
-             return prop;
+             {
+               left.Push (start, end, prop);
+               return;
+             }
+           left.Push (start, from, prop);
            start = from;
          }
-       else if (end > to)
+       if (end > to)
          {
-           right.Push (start, end, prop);
            if (start >= to)
-             return prop;
+             {
+               right.Push (start, end, prop);
+               return;
+             }
+           right.Push (to, end, prop);
            end = to;
          }
 
        if (start > from)
-         divide_left (from);
+         divide_left (start);
        if (end < to)
          divide_right (end);
-       stack.Push (prop);
-       return prop;
+       stack.Push (prop.key, prop);
+      }
+
+      private void DumpOne (bool with_prop, bool newline)
+      {
+       Console.Write ("#{0}({1} {2} {3}", id, total_length, from, to);
+       if (with_prop)
+         foreach (MPlist p in stack)
+           Console.Write (" " + p.Val);
+       Console.Write (")");
+       if (newline)
+         Console.WriteLine ();
+      }
+
+      public void Dump ()
+      {
+       update_from_to ();
+
+       if (left != null)
+         left.Dump ();
+       if (from > 0)
+         Console.Write (" ");
+       DumpOne (true, false);
+       if (right != null)
+         right.Dump ();
       }
     }