{
this.key = key;
this.val = val;
- flags |= Flag.RearSticky;
}
public MTextProperty (MSymbol key, object val,
{
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);
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);
+ }
}
}
public void DumpPropNested ()
{
- Console.Write ("(");
foreach (MPlist p in intervals)
((MInterval) p.Val).DumpNested (true);
- Console.WriteLine (")");
}
private class MInterval
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)
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 {
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;
}
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;
}
{
update_from_to ();
M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
- DumpOne (false, true);
+ DumpOne (false, false);
interval.set_mtext (mtext);
}
else // (pos > To)
Next.Insert (pos, interval);
+ M17N.DebugPrint (" done\n");
}
private void vacate_node (MInterval interval)
{
- M17N.DebugPrint ("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)
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;
}
}
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)
{
- prev.Parent.Right = prev.Left;
- while (prev.Parent != Left)
+ MInterval prev = root.Prev;
+
+ while (true)
{
- prev.Length -= len;
- prev = prev.Parent;
+ 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)
+ {
+ MInterval next = root.Next;
+
+ while (true)
+ {
+ 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 ();
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 ();
- }
}
}
Console.Write (")");
if (newline)
Console.WriteLine ();
+ if (Length <= 0)
+ throw new Exception ("Invalid interval length");
}
}
}
}
+ 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)
{
- update_from_to ();
+ int indent_type = (Parent == null ? 1
+ : Parent.Left == this ? 0 : 2);
- Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
- foreach (MPlist p in Stack)
- Console.Write (" " + p.Val);
+ update_from_to ();
if (Left != null)
{
- Console.Write (" left=");
- Left.DumpNested (force);
+ 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)
{
- Console.Write (" right=");
- Right.DumpNested (force);
+ if (indent_type >= 1)
+ Right.DumpNested (indent + " ", force);
+ else
+ Right.DumpNested (indent + "| ", force);
}
- Console.Write (")");
}
}
}