using System.Text;
using System.Collections;
using System.Collections.Generic;
+using M17N;
using M17N.Core;
namespace M17N.Core
}
#endif
- public class MTextProperty
+ public class MProperty
{
- internal MProperty prop;
- internal bool front_sticky;
- internal bool rear_sticky;
- internal bool merginable;
- public MProperty Prop { get { return prop; } }
- public bool FrontSticky { get { return front_sticky; } }
- public bool RearSticky { get { return rear_sticky; } }
- public bool Merginable { get { return merginable; } }
+ [FlagsAttribute]
+ public enum Flags
+ {
+ 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.
+ 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.
+ 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). 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 = 0x08,
+ /// 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 = 0x10
+ };
+
+ internal MSymbol key;
+ internal object val;
+
+ public MSymbol Key { get { return key; } }
+ public object Val { get { return val; } }
+
+ public MProperty (MSymbol key, object val)
+ {
+ if (key.flags == null)
+ key.flags = Flags.None;
+ this.key = key;
+ this.val = val;
+ }
- public MTextProperty (bool front_sticky, bool rear_sticky)
+ public MProperty (string name, object val)
{
- this.front_sticky = front_sticky;
- this.rear_sticky = rear_sticky;
+ key = MSymbol.PropertyKey (name);
+ this.val = val;
}
- public MTextProperty (bool front_sticky, bool rear_sticky, bool merginable)
+
+ public static bool HasFlags (MSymbol key, Flags flags)
+ {
+ return ((key.flags & flags) != Flags.None);
+ }
+
+ public override string ToString ()
{
- this.front_sticky = front_sticky;
- this.rear_sticky = rear_sticky;
- this.merginable = merginable;
+ return key.ToString () + ":" + val;
}
}
#if false
public enum MTextFormat format;
#endif
-
private StringBuilder sb;
private int nchars;
private int cache_pos;
private int cache_idx;
- private MInterval root_interval;
+ private MPlist intervals;
+ private MPlist default_property;
private bool read_only;
private static UTF8Encoding utf8 = new UTF8Encoding ();
public MText ()
{
sb = new StringBuilder ();
+ intervals = new MPlist ();
}
public MText (byte[] str)
{
sb = new StringBuilder (utf8.GetString (str));
nchars = count_chars (sb);
+ intervals = new MPlist ();
}
public MText (String str)
{
sb = new StringBuilder (str);
nchars = count_chars (str);
+ intervals = new MPlist ();
}
public MText (StringBuilder str)
{
sb = str;
nchars = count_chars (str);
+ intervals = new MPlist ();
}
- public static MText operator+ (MText mt1, MText mt2)
+ public static MText operator+ (object obj, MText mt)
{
- MText mt = new MText ();
+ if (obj is string)
+ {
+ MText mtnew = new MText ((string) obj);
+ return mtnew.Ins (mtnew.Length, mt);
+ }
+ throw new Exception ("Unknown object type: " + obj.GetType());
+ }
- mt.sb.Append (mt1.sb);
- mt.sb.Append (mt2.sb);
- mt.nchars = mt1.nchars + mt2.nchars;
- return mt;
+ public static MText operator+ (MText mt, object obj)
+ {
+ 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+ (MText mt1, MText mt2)
+ {
+ return mt1.Dup ().Ins (mt1.Length, mt2);
}
// Public properties
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)
{
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 bool 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");
+ return (from == to);
+ }
+
private void insert (int pos, MText mt2, int from, int to)
{
+ check_pos (pos, true);
+
+ if (M17n.debug)
+ {
+ Console.Write ("inserting {0} to {1} of ", from, to);
+ mt2.DumpPropNested ();
+ }
+ if (from == to)
+ 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;
+
+ root.Insert (pos, i, from, to);
+ }
+ foreach (MPlist plist in mt2.intervals)
+ if (intervals.Find (plist.Key) == null)
+ {
+ MInterval root;
+
+ if (nchars == 0)
+ root = ((MInterval) plist.Val).Copy (this, from, to);
+ else
+ {
+ root = new MInterval (plist.Key, this);
+ root.Insert (pos, (MInterval) plist.Val, from, to);
+ }
+ intervals.Push (plist.Key, root);
+ }
+
int pos_idx = pos_to_idx (this, pos);
int from_idx = pos_to_idx (mt2, from);
int to_idx = pos_to_idx (mt2, to);
sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
nchars += to - from;
- if (root_interval != null)
+ }
+
+ 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
{
- MInterval interval = (mt2.root_interval == null
- ? new MInterval (0, false, to - from, false)
- : mt2.root_interval.CopyTree (from, to));
- root_interval.Insert (pos, interval);
+ 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, null, 0, 1);
}
public int this[int i]
}
}
- public MText dup ()
+ public MText Dup ()
+ {
+ MText mt = new MText (sb.ToString ());
+
+ foreach (MPlist p in intervals)
+ mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
+ return mt;
+ }
+
+ public MText Dup (int from, int to)
{
- return (new MText (sb.ToString ()));
+ if (check_range (from, to, true))
+ return new MText ();
+ int from_idx = pos_to_idx (this, from);
+ int len = pos_to_idx (this, to) - from_idx;
+ MText mt = new MText (sb.ToString ().Substring (from_idx, len));
+
+ foreach (MPlist p in intervals)
+ mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, from, to));
+ return mt;
+ }
+
+ public MText Ins (int pos, int c)
+ {
+ insert (pos, c);
+ return this;
}
- 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 Cat (int c)
{
+ insert (nchars, c);
+ return this;
+ }
+
+ public MText Del (int from, int to)
+ {
+ if (check_range (from, to, true))
+ return this;
+
sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
nchars -= to - from;
+
+ if (nchars > 0)
+ foreach (MPlist plist in intervals)
+ {
+ 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)
+ DumpPropNested ();
return this;
}
- public void PushProp (int from, int to, MTextProperty prop)
+ public object GetProp (int pos, MSymbol key)
+ {
+ check_pos (pos, false);
+
+ MInterval i = (MInterval) intervals.Get (key);
+ if (i == null)
+ return null;
+
+ MProperty prop = i.Get (pos);
+ return (prop != null ? prop.Val : null);
+ }
+
+ public object GetProp (int pos, MSymbol key, out MProperty prop)
+ {
+ check_pos (pos, false);
+
+ MInterval i = (MInterval) intervals.Get (key);
+ if (i == null)
+ return (prop = null);
+ prop = i.Get (pos);
+ return (prop != null ? prop.Val : null);
+ }
+
+ 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);
+ 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 MProperty (key, val));
+ }
+
+ public void PushProp (int from, int to, MProperty prop)
+ {
+ if (from < 0)
+ {
+ if (default_property == null)
+ default_property = new MPlist ();
+ default_property.Push (prop.key, prop.val);
+ }
+ else
+ {
+ if (check_range (from, to, true))
+ return;
+
+ MPlist p = intervals.Find (prop.key);
+ MInterval root;
+
+ if (p == null)
+ {
+ root = new MInterval (prop.key, this);
+ intervals.Push (prop.key, root);
+ }
+ else
+ root = (MInterval) p.Val;
+
+ if (root.isSensitive)
+ root.PopSensitive (from, to);
+ root.Push (from, to, prop);
+ root.MergeAfterChange (from, to);
+ root.Balance ();
+ }
+ }
+
+ public void PopProp (int from, int to, MSymbol key)
+ {
+ if (from < 0)
+ {
+ if (default_property == null)
+ return;
+ MPlist p = default_property.Find (key);
+
+ if (p != null)
+ p.Pop ();
+ }
+ else
+ {
+ if (check_range (from, to, true))
+ return;
+
+ MPlist p = intervals.Find (key);
+
+ if (p != null)
+ {
+ MInterval root = (MInterval) p.Val;
+ 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 ();
+ }
+ }
+ }
+
+ public void DumpProp ()
+ {
+ Console.Write ("(");
+ foreach (MPlist p in intervals)
+ ((MInterval) p.Val).Dump (true);
+ Console.WriteLine (")");
+ }
+
+ public void DumpPropNested ()
{
- if (root_interval == null)
- root_interval = new MInterval (this);
- root_interval.Push (from, to, prop);
+ Console.WriteLine ("total length = {0}", Length);
+ foreach (MPlist p in intervals)
+ ((MInterval) p.Val).DumpNested (true);
}
private class MInterval
{
- // position: 0 1 2 3
- // index: -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13
- // | | A | | B | | C | |
- // interval |<--------->|<--->|<--------------->|<------
+ // position: 0 1 2 3 4 5 6 7
+ // | A | B | C | D | E F | G |
+ // interval |---|---|---|<->|-------|---|
+ // |---|<->|---| |<----->|---|
+ // |<->| |<->| |<->|
//
- // [-1 99 (9 89)]
- // [0 10 (-1 9)] [-10 0 (89 99)]
- // [0 4 (-1 3)] [-4 0 (5 9)] [0 4 (89 93)] [-2 0 (97 99)]
+ // [7 (3 4)]
+ // [3 (1 2)] [3 (4 6)]
+ // [1 (0 1)] [2 (2 3)] [1 (6 7)]
//
- // Start and end positions of this interval and its children.
- // If this is the left node, the values are relative to the
- // parent's total_start. Otherwise, the values are relative to
- // the parent's total_end. So:
- // total_start total_end
- // left-side interval 0 positive
- // right-side interval negative 0
- // top-most interval -1 positive
- private int total_start, total_end;
- // Stack of MTextProperty
- private Stack<MTextProperty> stack;
- private MInterval left, right, parent;
- private MText mt;
+ private static int count = 0;
+ private int ID;
+ private int Length;
+ private int From, To;
+ private MSymbol Key;
+ private MPlist Stack;
+ private MInterval Left, Right, Parent;
+ private MText mtext;
+
+ public MInterval (MSymbol key, MText mt, int length)
+ {
+ if (length <= 0)
+ throw new Exception ("Invalid interval length");
+ Key = key;
+ mtext = mt;
+ Length = length;
+ Stack = new MPlist ();
+ ID = count++;
+ }
+
+ public MInterval (MSymbol key, MText mt)
+ {
+ Key = key;
+ mtext = mt;
+ Length = mt.sb.Length;
+ From = 0;
+ To = Length;
+ Stack = new MPlist ();
+ ID = count++;
+ }
+
+ /// POS must be smaller than Length;
+ public MProperty Get (int pos)
+ {
+ MInterval i = find_head (pos);
+
+ return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
+ }
+
+ /// POS must be smaller than Length;
+ public MProperty Get (int pos, out MProperty[] array)
+ {
+ MInterval i = find_head (pos);
+
+ if (i.To == pos)
+ i = i.Next;
+ if (i.Stack.IsEmpty)
+ {
+ array = null;
+ return null;
+ }
+ array = new MProperty[i.Stack.Count];
+
+ int idx;
+ MPlist p;
+ for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
+ array[idx] = (MProperty) p.Val;
+ return array[0];
+ }
+
+ private MInterval (MSymbol key, MText mt, int length, MPlist stack)
+ {
+ Key = key;
+ mtext = mt;
+ Length = length;
+ From = 0;
+ To = Length;
+ Stack = stack == null ? new MPlist () : stack.Clone ();
+ ID = count++;
+ }
- private static int adjust_position (int pos, bool front_inclusive)
+ private bool isRearSticky
{
- return (pos << 2) + (front_inclusive ? -1 : 1);
+ get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
}
- private static bool before_point_p (int pos)
+ private bool isFrontSticky
{
- return ((pos + 1) % 4) == 0;
+ get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
}
- public MInterval (int start, bool front_inclusive,
- int end, bool rear_inclusive)
+ public bool isSensitive
{
- if (start > end)
- throw new Exception ("Invalid Interval Range");
- this.total_start = (start << 2) + (front_inclusive ? -1 : 1);
- this.total_end = (end << 2) + (rear_inclusive ? 1 : -1);
- this.stack = new Stack<MTextProperty> ();
+ get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
}
- public MInterval (MText mt)
+ public bool isFrontSensitive
{
- this.mt = mt;
- this.total_start = -1;
- this.total_end = (mt.sb.Length << 2) + 1;
- this.stack = new Stack<MTextProperty> ();
+ get { return MProperty.HasFlags (Key,
+ MProperty.Flags.FrontSensitive) ; }
}
- private MInterval () { }
+ public bool isRearSensitive
+ {
+ get { return MProperty.HasFlags (Key,
+ MProperty.Flags.RearSensitive) ; }
+ }
- private MInterval (int start, int end, Stack<MTextProperty> stack)
+ public bool isAnySensitive
{
- this.total_start = start;
- this.total_end = end;
- this.stack = new Stack<MTextProperty> (stack);
+ get { return MProperty.HasFlags (Key,
+ (MProperty.Flags.Sensitive
+ | MProperty.Flags.RearSensitive
+ | MProperty.Flags.FrontSensitive)) ; }
}
- private MInterval find (int pos, out int offset)
+ private void update_from_to ()
{
- if (pos < total_start || total_end < pos)
+ if (Parent == null)
{
- offset = 0;
- return null;
+ From = LeftLength;
+ To = Length - RightLength;
+ }
+ else if (Parent.Left == this)
+ {
+ From = Parent.From - Length + LeftLength;
+ To = Parent.From - RightLength;
+ }
+ else
+ {
+ From = Parent.To + LeftLength;
+ To = Parent.To + Length - RightLength;
}
- if (pos < Start)
- return left.find (pos, out offset);
- if (End < pos)
- return right.find (pos - total_end, out offset);
- offset = pos - Start;
+ }
+
+ private int LeftLength
+ {
+ get { return (Left == null ? 0 : Left.Length); }
+ }
+
+ private int RightLength
+ {
+ get { return (Right == null ? 0 : Right.Length); }
+ }
+
+ private MInterval LeftMost
+ {
+ get {
+ update_from_to ();
+ if (Left == null)
+ return this;
+ return Left.LeftMost;
+ }
+ }
+
+ private MInterval RightMost
+ {
+ get {
+ update_from_to ();
+ if (Right == null)
+ return this;
+ return Right.RightMost;
+ }
+ }
+
+ private MInterval Prev {
+ get {
+ MInterval i;
+
+ if (Left != null)
+ {
+ for (i = Left; i.Right != null; i = i.Right)
+ i.update_from_to ();
+ i.update_from_to ();
+ }
+ else
+ {
+ MInterval child = this;
+ for (i = Parent; i != null && i.Left == child;
+ child = i, i = i.Parent);
+ }
+ return i;
+ }
+ }
+
+ private MInterval Next {
+ get {
+ MInterval i;
+
+ if (Right != null)
+ {
+ for (i = Right; i.Left != null; i = i.Left)
+ i.update_from_to ();
+ i.update_from_to ();
+ }
+ else
+ {
+ MInterval child = this;
+ for (i = Parent; i != null && i.Right == child;
+ child = i, i = i.Parent);
+ }
+ return i;
+ }
+ }
+
+ private MInterval find_head (int pos)
+ {
+ update_from_to ();
+ if (pos < From)
+ 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_tail (pos);
return this;
}
+ private bool mergeable (MInterval i)
+ {
+ MPlist p1, p2;
+
+ for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
+ p1 = p1.Next, p2 = p2.Next)
+ if (p1.Val != p2.Val)
+ return false;
+ return (p1.IsEmpty && p2.IsEmpty);
+ }
+
// p-. or .-p p-. or .-p
// .-this-. .-right-.
// left .-right-. -> .-this-. c2
// c1 c2 left c1
private MInterval promote_right ()
{
- int right_length = - right.total_start;
- MInterval c1;
+ MInterval c1 = Right.Left;
- if (parent == null)
- mt.root_interval = right;
- else if (total_start == 0)
- parent.left = right;
+ // Update Parent.
+ if (Parent == null)
+ mtext.intervals.Put (Key, Right);
+ else if (Parent.Left == this)
+ Parent.Left = Right;
else
- parent.right = right;
- right.parent = parent;
- c1 = right.left;
- right.left = this;
+ Parent.Right = Right;
- parent = right;
- right = c1;
- if (c1 != null)
- c1.parent = this;
+ // Update Right.
+ Right.Parent = Parent;
+ Right.Left = this;
+ Right.Length += LeftLength + (To - From);
- parent.total_start = total_start;
- parent.total_end = total_end;
- total_start = 0;
- total_end -= right_length - (c1 == null ? 0 : c1.total_end);
-
+ // Update this.
+ Parent = Right;
+ Right = c1;
+ Length = LeftLength + (To - From) + RightLength;
+
+ // Update C1 if necessary.
if (c1 != null)
- {
- c1.total_end = - c1.total_start;
- c1.total_start = 0;
- }
- return parent;
+ c1.Parent = this;
+
+ Parent.update_from_to ();
+ return Parent;
}
// p-. or .-p p-. or .-p
// c1 c2 c2 right
private MInterval promote_left ()
{
- int left_length = left.total_end;
- MInterval c2;
+ MInterval c2 = Left.Right;
- if (parent == null)
- mt.root_interval = left;
- else if (total_start == 0)
- parent.left = left;
+ // Update Parent.
+ if (Parent == null)
+ mtext.intervals.Put (Key, Left);
+ else if (Parent.Left == this)
+ Parent.Left = Left;
else
- parent.right = left;
- left.parent = parent;
- c2 = left.right;
- left.right = this;
+ Parent.Right = Left;
- parent = left;
- left = c2;
- if (c2 != null)
- c2.parent = this;
+ // Update Left.
+ Left.Parent = Parent;
+ Left.Right = this;
+ Left.Length += (To - From) + RightLength;
- parent.total_start = total_start;
- parent.total_end = total_end;
- total_start -= left_length + (c2 == null ? 0 : c2.total_end);;
- total_end = 0;
-
+ // Update this.
+ Parent = Left;
+ Left = c2;
+ Length = LeftLength + (To - From) + RightLength;
+
+ // Update C2 if necessary.
if (c2 != null)
- {
- c2.total_start = - c2.total_end;
- c2.total_end = 0;
- }
- return parent;
+ c2.Parent = this;
+
+ Parent.update_from_to ();
+ return Parent;
}
- private MInterval balance ()
+ public MInterval Balance ()
{
- MInterval interval = this;
+ MInterval i = this;
+ update_from_to ();
while (true)
{
- int this_start = interval.Start;
- int this_end = interval.End;
- int diff = ((this_start - interval.total_start)
- - (interval.total_end - this_end));
- int abs = Math.Abs (diff);
+ // .-this-.
+ // .-left-. .-right-.
+ // c1 c2 c3 c4
+ int diff = i.LeftLength - i.RightLength;
+ int new_diff;
- if (abs < this_end - this_start)
- break;
- if (diff < 0)
+ if (diff > 0)
{
- interval = interval.promote_right ();
- interval.left.balance ();
+ new_diff = (i.Length - i.LeftLength
+ + 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 ();
+ M17n.DebugPrint ("done\n");
+ i.Right.Balance ();
}
- else
+ else if (diff < 0)
{
- interval = interval.promote_left ();
- interval.right.balance ();
+ new_diff = (i.Length - i.RightLength
+ + 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 ();
}
+ else
+ break;
}
- return interval;
+ return i;
}
- public MInterval CopyTree (int start, int end)
+ public MInterval Copy (MText mt, int start, int end)
{
- MInterval interval_start, interval_end, interval;
- int offset_start, offset_end;
+ MInterval copy, left_copy = null, right_copy = null;
- start <<= 2;
- end <<= 2;
- interval_start = find (start, out offset_start);
- interval_end = find (end, out offset_end);
+ update_from_to ();
- interval = new MInterval ();
- interval.total_start = 0;
- interval.total_end = end - start;
- interval.stack = new Stack<MTextProperty> (interval_start.stack);
- interval = interval.divide_right (offset_start);
- while (interval_start != interval_end)
+ if (start < From)
{
- interval_start = interval_start.Right;
- interval = interval.divide_right (interval_start.End
- - interval_start.Start);
+ if (end <= From)
+ return Left.Copy (mt, start, end);
+ left_copy = Left.Copy (mt, start, From);
}
- return interval;
+ if (end > To)
+ {
+ if (start >= To)
+ return Right.Copy (mt, start, end);
+ right_copy = Right.Copy (mt, To, end);
+ }
+
+ copy = new MInterval (Key, null, end - start, Stack);
+ copy.mtext = mt;
+ if (isSensitive && (From < start || end < To))
+ copy.Stack.Clear ();
+ 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;
}
- private MInterval CopyNode ()
+ public MInterval Copy (MText mt, int start, int end,
+ bool first, bool last)
{
- return new MInterval (total_start, total_end, stack);
- }
+ MInterval copy = Copy (mt, start, end);
+ MInterval head = find_head (start);
+ MInterval tail = find_tail (end);
- private int Start {
- get {
- return (left == null ? total_start : total_start + left.total_end);
- }
+ if (! head.Stack.IsEmpty
+ && (isAnySensitive && head.From < start
+ || isFrontSensitive && ! first))
+ {
+ head = copy.find_head (0);
+ head.Stack.Clear ();
+ }
+ if (! tail.Stack.IsEmpty
+ && (isAnySensitive && end < head.To
+ || isRearSensitive && ! last))
+ {
+ tail = copy.find_tail (copy.Length);
+ tail.Stack.Clear ();
+ }
+ return copy;
}
- private int End {
- get {
- return (right == null ? total_end : total_end + right.total_start);
- }
+ // this-. ==> this-.
+ // right newright-.
+ // right
+ private MInterval divide_right (int pos)
+ {
+ MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
+
+ M17n.DebugPrint ("divide-right({0}) at {1}\n", pos, this);
+ To = pos;
+ if (Right != null)
+ {
+ interval.Right = Right;
+ Right.Parent = interval;
+ interval.Length += Right.Length;
+ }
+ interval.Parent = this;
+ Right = interval;
+ return interval;
}
- private MInterval Left {
- get {
- MInterval interval;
+ // .-this ==> .-this
+ // left .-newleft
+ // left
+ private MInterval divide_left (int pos)
+ {
+ MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
- if (left != null)
- for (interval = left;
- interval.right != null;
- interval = interval.right);
- else
- for (interval = parent;
- interval != null && interval.total_start == 0;
- interval = interval.parent);
+ M17n.DebugPrint ("divide-left({0}) at {1}\n", pos, this);
+ From = pos;
+ if (Left != null)
+ {
+ interval.Left = Left;
+ Left.Parent = interval;
+ interval.Length += Left.Length;
+ }
+ interval.Parent = this;
+ Left = interval;
+ return interval;
+ }
- return interval;
- }
+ private void set_mtext (MText mt)
+ {
+ mtext = mt;
+ if (Left != null)
+ Left.set_mtext (mt);
+ if (Right != null)
+ Right.set_mtext (mt);
}
- private MInterval Right {
- get {
- MInterval interval;
+ private void enlarge (int len)
+ {
+ Length += len;
+ To += len;
+ for (MInterval prev = this, i = this.Parent; i != null;
+ prev = i, i = i.Parent)
+ {
+ i.Length += len;
+ if (prev == i.Left)
+ {
+ i.From += len;
+ i.To += len;;
+ }
+ }
+ }
- if (right != null)
- for (interval = right;
- interval.left != null;
- interval = interval.left);
- else
- for (interval = parent;
- interval != null && interval.total_start < 0;
- interval = interval.parent);
+ private int graft_forward (MInterval interval, int start, int end)
+ {
+ int len;
- return interval;
- }
+ if (! Stack.IsEmpty && isRearSticky)
+ len = end - start;
+ else if (interval == null)
+ len = Stack.IsEmpty ? end - start : 0;
+ else
+ {
+ MInterval i = interval.find_head (start);
+
+ len = 0;
+ if (Stack.IsEmpty
+ && (isFrontSensitive
+ || ((isSensitive || isRearSensitive) && i.From < start)))
+ {
+ M17n.DebugPrint (" grafting {0}", i);
+ if (i.To < end)
+ len = i.To - start;
+ else
+ len = end - start;
+ i = i.Next;
+ }
+ while (i != null && i.From < end && mergeable (i))
+ {
+ M17n.DebugPrint (" grafting {0}", i);
+ len += i.To - i.From;
+ if (i.From < start)
+ len -= start - i.From;
+ if (i.To >= end)
+ {
+ len -= i.To - end;
+ break;
+ }
+ i = i.Next;
+ }
+ }
+
+ M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
+ if (len > 0)
+ enlarge (len);
+ return len;
}
- public void Push (int start, int end, MTextProperty prop)
+ private int graft_backward (MInterval interval, int start, int end)
{
- start <<= 2;
- if (prop.FrontSticky)
- start--;
- else
- start++;
- end <<= 2;
- if (prop.RearSticky)
- end++;
+ int len;
+
+ if (! Stack.IsEmpty && isFrontSticky)
+ len = end - start;
+ else if (interval == null)
+ len = Stack.IsEmpty ? end - start : 0;
else
- end--;
- if (start >= end)
- throw new Exception ("Invalid Text Property Range");
+ {
+ MInterval i = interval.find_tail (end);
+
+ len = 0;
+ if (Stack.IsEmpty
+ && (isRearSensitive
+ || ((isSensitive || isFrontSensitive) && end < i.To)))
+ {
+ M17n.DebugPrint (" grafting {0}", i);
+ if (i.From <= start)
+ len = end - start;
+ else
+ len = end - i.From;
+ i = i.Prev;
+ }
+ while (i != null && i.To <= start && mergeable (i))
+ {
+ M17n.DebugPrint (" grafting {0}", i);
+ len += i.To - i.From;
+ if (end < i.To)
+ len -= i.To - end;
+ if (i.From <= start)
+ {
+ len -= start - i.From;
+ break;
+ }
+ i = i.Prev;
+ }
+ }
- push (start, end, prop);
+ M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
+ if (len > 0)
+ enlarge (len);
+ return len;
}
- public void Insert (int pos, MInterval interval)
+ public void Insert (int pos, MInterval interval, int start, int end)
{
- if (pos < Start)
- Left.Insert (pos - total_start, interval);
- else if (pos > End)
- Right.Insert (pos - total_end, interval);
- else
+ update_from_to ();
+
+ M17n.DebugPrint ("insert({0} to {1}) at {2} in {3}",
+ start, end, pos, this);
+
+ if (pos < From)
+ Left.Insert (pos, interval, start, end);
+ else if (pos == From)
{
- // position: 0 1 2 3
- // index: -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13
- // this |<----------<-------------->->|
- // |<--------->| |<>|
- //
- // interval |<->----->->|
- //
- // new |<----------<----->----->-->------------>->|
- // |<--------->| |<---->-->------------>->|
- // |<----------->->|
- // |<>|
- int len = interval.total_end - interval.total_start;
- MInterval temp;
+ MInterval prev = Left != null ? Prev : null;
- total_end += len;
- for (temp = this; temp.parent != null;
- temp = temp.parent)
- temp.parent.total_end += len;
- if (End - pos < interval.End)
+ if (isFrontSensitive)
+ Stack.Clear ();
+ if (prev != null && isRearSensitive)
+ prev.Stack.Clear ();
+ if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
{
+ prev.enlarge (end - start);
+ return;
+ }
+ if (isFrontSticky && ! Stack.IsEmpty)
+ {
+ enlarge (end - start);
+ return;
+ }
+ bool front_grafted = false, rear_grafted = false;
+ int grafted;
+ if (prev != null
+ && (grafted = prev.graft_forward (interval, start, end)) > 0)
+ {
+ start += grafted;
+ if (start == end)
+ return;
+ front_grafted = true;
+ }
+ if ((grafted = graft_backward (interval, start, end)) > 0)
+ {
+ end -= grafted;
+ if (start == end)
+ return;
+ rear_grafted = true;
+ }
+ if (interval != null)
+ interval = interval.Copy (mtext, start, end,
+ front_grafted || prev == null,
+ rear_grafted);
+ 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 (! Stack.IsEmpty)
+ {
+ if (isSensitive || isFrontSensitive || isRearSensitive)
+ Stack.Clear ();
+ else if (isFrontSticky || isRearSticky)
+ {
+ enlarge (end - start);
+ return;
+ }
+ }
+ bool front_grafted = false, rear_grafted = false;
+ int grafted;
+ if ((grafted = graft_forward (interval, start, end)) > 0)
+ {
+ start += grafted;
+ if (start == end)
+ return;
+ front_grafted = true;
+ pos += grafted;
+ }
+ if ((grafted = graft_backward (interval, start, end)) > 0)
+ {
+ end -= grafted;
+ if (start == end)
+ return;
+ rear_grafted = true;
}
+ if (interval != null)
+ interval = interval.Copy (mtext, start, end,
+ front_grafted, rear_grafted);
+ else
+ interval = new MInterval (Key, mtext, end - start, null);
- temp = divide_right (Start + 2);
- temp.left = interval;
- right = interval;
- }
-
+ 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 && isFrontSticky && ! next.Stack.IsEmpty)
+ {
+ next.enlarge (end - start);
+ return;
+ }
+ bool front_grafted = false, rear_grafted = false;
+ int grafted;
+ if (next != null
+ && (grafted = next.graft_backward (interval, start, end)) > 0)
+ {
+ end -= grafted;
+ if (start == end)
+ return;
+ rear_grafted = true;
+ }
+ if ((grafted = graft_forward (interval, start, end)) > 0)
+ {
+ start += grafted;
+ if (start == end)
+ return;
+ front_grafted = true;
+ }
+ if (interval != null)
+ interval = interval.Copy (mtext, start, end,
+ front_grafted,
+ rear_grafted || next == null);
+ 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;
+ }
+ else // (pos > To)
+ Right.Insert (pos, interval, start, end);
+ M17n.DebugPrint (" done\n");
}
- private MInterval divide_right (int pos)
+ private void vacate_node (MInterval interval)
{
- MInterval interval = CopyNode ();
- int this_start = Start;
- int this_end = End;
+ vacate_node (interval, null);
+ }
- if (left == null
- || (right != null && left.total_end < - right.total_start))
+ 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)
{
- interval.left = this;
- interval.right = right;
- interval.parent = parent;
- if (parent != null)
+ if (mtext != null)
+ mtext.intervals.Put (Key, interval);
+ }
+ else
+ {
+ if (this == Parent.Right)
+ Parent.Right = interval;
+ else
+ Parent.Left = interval;
+
+ int diff = Length;
+ if (interval != null)
+ diff -= interval.Length;
+ for (MInterval i = Parent; i != stop; i = i.Parent)
+ i.Length -= diff;
+ }
+ }
+
+ public void Delete (int start, int end)
+ {
+ update_from_to ();
+ M17n.DebugPrint ("delete({0} {1}) from {2}\n", start, end, this);
+
+ bool front_checked = false;
+ bool rear_checked = false;
+
+ if (start < From)
+ {
+ if (end <= From)
{
- if (total_start == 0)
- parent.left = interval;
- else
- parent.right = interval;
+ if (end == From && isFrontSensitive)
+ Stack.Clear ();
+ Left.Delete (start, end);
+ return;
+ }
+ if (isSensitive || isFrontSensitive)
+ Stack.Clear ();
+ Left.Delete (start, From);
+ To -= From - start;
+ end -= From - start;
+ From = start;
+ front_checked = true;
+ }
+ if (end > To)
+ {
+ if (start >= To)
+ {
+ if (start == To && isRearSensitive)
+ Stack.Clear ();
+ Right.Delete (start, end);
+ return;
+ }
+ if (isSensitive || isRearSensitive)
+ Stack.Clear ();
+ Right.Delete (To, end);
+ end = To;
+ rear_checked = true;
+ }
+ if (start == From && end == To)
+ {
+ if (! front_checked
+ && start > 0
+ && isRearSensitive)
+ Prev.Stack.Clear ();
+ if (! rear_checked
+ && end < mtext.Length
+ && isFrontSensitive)
+ Next.Stack.Clear ();
+ if (Right == null)
+ {
+ vacate_node (Left);
+ }
+ else
+ {
+ if (Left != null)
+ {
+ MInterval i;
+
+ for (i = Right; i.Left != null; i = i.Left)
+ i.Length += Left.Length;
+ i.Length += Left.Length;
+ i.Left = Left;
+ Left.Parent = i;
+ }
+ vacate_node (Right);
}
- total_start = 0;
- total_end = pos - this_start;
}
else
{
- interval.total_start = pos - this_end;
- interval.total_end = 0;
- if (right != null)
- right.parent = interval;
- right = interval;
+ int len = end - start;
+
+ if (isAnySensitive)
+ Stack.Clear ();
+ for (MInterval i = this; i != null; i = i.Parent)
+ i.Length -= len;
+ }
+ }
+
+ public void Push (int start, int end, MProperty prop)
+ {
+ update_from_to ();
+ M17n.DebugPrint ("push({0} {1}) at {2}\n", start, end, this);
+ if (start < From)
+ {
+ if (end <= From)
+ {
+ Left.Push (start, end, prop);
+ return;
+ }
+ Left.Push (start, From, prop);
+ start = From;
+ }
+ if (end > To)
+ {
+ if (start >= To)
+ {
+ Right.Push (start, end, prop);
+ return;
+ }
+ Right.Push (To, end, prop);
+ end = To;
}
- return interval;
+ if (! Stack.IsEmpty && isAnySensitive)
+ Stack.Clear ();
+ if (start > From)
+ divide_left (start);
+ if (end < To)
+ divide_right (end);
+ Stack.Push (prop.key, prop);
}
- private MInterval divide_left (int pos)
+ /// Combine intervals between HEAD and TAIL (both inclusive) to
+ /// the common parent of HEAD and TAIL. Callers should assure
+ /// that the intervals are mergeable in advance.
+ private static void combine (MInterval head, MInterval tail)
{
- MInterval interval = CopyNode ();
- int this_start = Start;
- int this_end = End;
+ M17n.DebugPrint ("combining {0} through {1}", head, tail);
- if (left == null
- || (right != null && left.total_end < - right.total_start))
+ head.update_from_to ();
+ tail.update_from_to ();
+ 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);
+
+ M17n.DebugPrint (" with common root {0}\n", root);
+
+ if (from < root.From)
{
- interval.total_start = 0;
- interval.total_end = pos - this_start;
- if (left != null)
- left.parent = interval;
- left = interval;
+ MInterval prev = root.Prev;
+
+ while (true)
+ {
+ M17n.DebugPrint ("merging {0}\n", prev);
+ prev.vacate_node (prev.Left, root);
+ if (prev == head)
+ break;
+ if (prev.Left != null)
+ prev = prev.Left.RightMost;
+ else
+ prev = prev.Parent;
+ }
+ root.update_from_to ();
}
- else
+ if (root.To < to)
{
- interval.right = this;
- interval.left = left;
- interval.parent = parent;
- if (parent != null)
+ MInterval next = root.Next;
+
+ while (true)
{
- if (total_start == 0)
- parent.left = interval;
+ M17n.DebugPrint ("merging {0}\n", next);
+ next.vacate_node (next.Right, root);
+ if (next == tail)
+ break;
+ if (next.Right != null)
+ next = next.Right.LeftMost;
else
- parent.right = interval;
+ next = next.Parent;
}
- total_start = pos - this_end;
- total_end = 0;
+ root.update_from_to ();
}
-
- return interval;
}
- private void push (int start, int end, MTextProperty prop)
+ public void MergeAfterChange (int start, int end)
{
- int this_start = Start;
- int this_end = End;
+ update_from_to ();
+
+ MInterval head = find_head (start), i = head;
+ MInterval tail = start < end ? find_tail (end) : head;
- if (start < this_start)
+ if (tail.To < Length)
+ tail = tail.Next;
+
+ if (start == head.From && start > 0)
+ {
+ MInterval prev = head.Prev;
+ if (head.mergeable (prev))
+ head = prev;
+ }
+ M17n.DebugPrint ("merge between {0} and {1}\n", head, tail);
+ while (i != tail)
{
- if (end <= this_start)
+ MInterval next = i.Next;
+
+ if (! i.mergeable (next))
{
- Left.push (start, end, prop);
+ if (head != i)
+ combine (head, i);
+ head = next;
+ }
+ i = next;
+ }
+ if (head != i)
+ combine (head, i);
+ }
+
+ public void Pop (int start, int end)
+ {
+ update_from_to ();
+ M17n.DebugPrint ("pop({0} {1}) at {2}\n", start, end, this);
+ if (start < From)
+ {
+ if (end <= From)
+ {
+ Left.Pop (start, end);
return;
}
- Left.push (start, this_start, prop);
- start = this_start;
+ Left.Pop (start, From);
+ start = From;
}
- if (this_end < end)
+ if (end > To)
{
- if (this_end < start)
+ if (start >= To)
{
- Right.push (start, end, prop);
+ Right.Pop (start, end);
return;
}
- Right.push (this_end, end, prop);
- end = this_end;
+ Right.Pop (To, end);
+ end = To;
+ }
+
+ if (! Stack.IsEmpty)
+ {
+ if (isAnySensitive)
+ Stack.Clear ();
+ else
+ {
+ if (start > From)
+ divide_left (start);
+ if (end < To)
+ divide_right (end);
+ Stack.Pop ();
+ }
+ }
+ }
+
+ 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);
+ }
+
+ public override string ToString ()
+ {
+ string str = String.Format ("#{0}({1} {2} {3} [", ID, Length, From, To);
+ bool first = true;
+ foreach (MPlist p in Stack)
+ {
+ if (first)
+ {
+ str += ((MProperty) p.Val).Val;
+ first = false;
+ }
+ else
+ str += " " + ((MProperty) p.Val).Val;
+ }
+ return (str + "])");
+ }
+
+ private void DumpOne (bool with_prop, bool newline, bool force)
+ {
+ if (force || M17n.debug)
+ {
+ Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
+ 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 ();
+ if (Length <= 0)
+ throw new Exception ("Invalid interval length");
+ }
+ }
+
+ 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 (Key.ToString () + ":", 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);
+ }
+ Console.Write (indent);
+ if (indent_type == 0)
+ Console.Write (".-");
+ else if (indent_type == 2)
+ Console.Write ("`-");
+ DumpOne (true, true, true);
+ if (Right != null)
+ {
+ if (indent_type >= 1)
+ Right.DumpNested (indent + " ", force);
+ else
+ Right.DumpNested (indent + "| ", force);
+ }
}
- if (this_start < start)
- divide_left (start);
- if (end < this_end)
- divide_right (end);
- stack.Push (prop);
}
}