3 using System.Collections;
4 using System.Collections.Generic;
11 public enum MTextFormat
13 MTEXT_FORMAT_US_ASCII,
15 MTEXT_FORMAT_UTF_16BE,
16 MTEXT_FORMAT_UTF_16LE,
17 MTEXT_FORMAT_UTF_32BE,
18 MTEXT_FORMAT_UTF_32LE,
22 public class MProperty
29 /// On inserting a text in between two characters, if the
30 /// preceding and following characters have Sticky properties of
31 /// the same key with same values, the inserted text inherits
32 /// those properties. In that case, properties of the inserted
33 /// text are overriden.
34 Sticky = 0x01, // 00000001
36 /// On inserting a text before a character, if the character has
37 /// FrontSticky properties, the inserted text inherits those
39 FrontSticky = 0x03, // 00000011
41 /// On inserting a text after a character, if the character has
42 /// RearSticky properties, the inserted text inherits those
44 RearSticky = 0x05, // 00000101
46 /// Like RearSticky, but if the inserted text inherits no
47 /// properties from the preceding character, it inherits
48 /// BothSticky properties from the following character if any.
49 BothSticky = 0x07, // 00000111
51 /// This property is deleted from a span of text if the span is
52 /// modified (i.e. one of a character is changed, a text is
53 /// inserted, some part is deleted). Here, "span" means a
54 /// sequence of characters that has this property with the same
55 /// value. This property is also deleted if a property of the
56 /// same key is added, which means that this property is not
57 /// stackable. In addition this property is never merged with
58 /// the same value of preceding or following property. At last,
59 /// this property can't be sticky in any way.
60 Sensitive = 0x10, // 00010000
62 /// Like Sensitive but also this property is deleted from a span
63 /// of text if a characeter just before the span is modified,
64 /// inserted, or deleted.
65 FrontSensitive = 0x30, // 00110000
67 /// Like Sensitive but also this property is deleted from a span
68 /// of text if a character just after the span is modified,
69 /// inserted, or deleted.
70 RearSensitive = 0x50, // 01010000
72 /// Same as (FrontSensitive | RearSensitive).
73 BothSensitive = 0x70, // 01110000
79 public MSymbol Key { get { return key; } }
80 public object Val { get { return val; } }
82 public MProperty (MSymbol key, object val)
84 if (key.flags == null)
85 key.flags = Flags.None;
90 public MProperty (string name, object val)
92 key = MSymbol.PropertyKey (name);
96 public static bool HasFlags (MSymbol key, Flags flags)
98 return ((key.flags & flags) == flags);
101 public override string ToString ()
103 return key.ToString () + ":" + val;
107 public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
110 public enum MTextFormat format;
112 private StringBuilder sb;
114 private int cache_pos;
115 private int cache_idx;
116 private MPlist intervals;
117 private MPlist default_property;
118 private bool read_only;
120 private static UTF8Encoding utf8 = new UTF8Encoding ();
122 private static int count_chars (String str)
124 int len = str.Length, n = 0;
126 for (int i = 0; i < len; i++, n++)
127 if (Char.IsHighSurrogate (str[i]))
132 private static int count_chars (StringBuilder str)
134 int len = str.Length, n = 0;
136 for (int i = 0; i < len; i++, n++)
137 if (Char.IsHighSurrogate (str[i]))
144 sb = new StringBuilder ();
145 intervals = new MPlist ();
148 public MText (byte[] str)
150 sb = new StringBuilder (utf8.GetString (str));
151 nchars = count_chars (sb);
152 intervals = new MPlist ();
155 public MText (byte[] str, int offset, int length)
157 sb = new StringBuilder (utf8.GetString (str, offset, length));
158 nchars = count_chars (sb);
159 intervals = new MPlist ();
162 public MText (String str)
164 sb = new StringBuilder (str);
165 nchars = count_chars (str);
166 intervals = new MPlist ();
169 public MText (StringBuilder str)
172 nchars = count_chars (str);
173 intervals = new MPlist ();
176 public MText (int c, int len) : this ()
182 public MText (int c) : this (c, 1) { }
184 public static MText operator+ (object obj, MText mt)
188 MText mtnew = new MText ((string) obj);
189 return mtnew.Ins (mtnew.Length, mt);
191 throw new Exception ("Unknown object type: " + obj.GetType());
194 public static MText operator+ (MText mt, object obj)
197 return mt + new MText ((string) obj);
199 return mt.Dup ().Ins (mt.Length, (int) obj);
201 return mt.Dup ().Ins (mt.Length, (int) ((char) obj));
202 throw new Exception ("Unknown object type: " + obj.GetType());
205 public static MText operator+ (MText mt1, MText mt2)
207 return mt1.Dup ().Ins (mt1.Length, mt2);
211 public bool ReadOnly { get { return read_only; } }
212 public int Length { get { return nchars; } }
216 // for IEnumerable interface
217 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
219 // for IEquatable interface
220 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
222 // for IComparable interface
223 public int CompareTo (MText other)
225 return this.sb.ToString ().CompareTo (other.sb.ToString ());
228 public override string ToString () { return sb.ToString (); }
230 private static int inc_idx (StringBuilder sb, int i)
232 return (i + (Char.IsHighSurrogate (sb[i]) ? 2 : 1));
235 private static int dec_idx (StringBuilder sb, int i)
237 return (i - (Char.IsLowSurrogate (sb[i - 1]) ? 2 : 1));
240 private static int pos_to_idx (MText mt, int pos)
242 if (pos == mt.cache_pos)
248 if (pos < mt.cache_pos)
250 if (mt.cache_pos == mt.cache_idx)
252 if (pos < mt.cache_pos - pos)
259 p = mt.cache_pos; i = mt.cache_idx;
265 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
266 return (mt.cache_idx + pos - mt.cache_pos);
267 if (pos - mt.cache_pos < mt.nchars - pos)
269 p = mt.cache_pos; i = mt.cache_idx;
274 p = mt.nchars; i = mt.sb.Length;
279 for (; p < pos; i = inc_idx (mt.sb, i), p++);
281 for (; p > pos; i = dec_idx (mt.sb, i), p--);
287 private void check_pos (int pos, bool tail_ok)
289 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
290 throw new Exception ("Invalid MText position:" + pos);
293 private bool check_range (int from, int to, bool zero_ok)
295 if (from < 0 || (zero_ok ? from > to : from >= to)
297 throw new Exception ("Invalid MText range");
301 private void insert (int pos, MText mt2, int from, int to)
303 check_pos (pos, true);
307 Console.Write ("inserting {0} to {1} of ", from, to);
308 mt2.DumpPropNested ();
312 foreach (MPlist plist in intervals)
314 MInterval root = (MInterval) plist.Val;
315 MPlist p = mt2.intervals.Find (plist.Key);
316 MInterval i = p == null ? null : (MInterval) p.Val;
318 root.Insert (pos, i, from, to);
320 foreach (MPlist plist in mt2.intervals)
321 if (intervals.Find (plist.Key) == null)
326 root = ((MInterval) plist.Val).Copy (this, from, to);
329 root = new MInterval (plist.Key, this);
330 root.Insert (pos, (MInterval) plist.Val, from, to);
332 intervals.Push (plist.Key, root);
335 int pos_idx = pos_to_idx (this, pos);
336 int from_idx = pos_to_idx (mt2, from);
337 int to_idx = pos_to_idx (mt2, to);
339 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
343 private void insert (int pos, int c)
345 check_pos (pos, true);
347 int pos_idx = pos_to_idx (this, pos);
352 sb.Insert (pos_idx, ch);
356 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
357 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
358 sb.Insert (pos_idx, low);
359 sb.Insert (pos_idx, high);
362 foreach (MPlist plist in intervals)
363 ((MInterval) plist.Val).Insert (pos, null, 0, 1);
366 public int this[int i]
369 i = pos_to_idx (this, i);
372 if (Char.IsHighSurrogate (sb[i]))
374 sb[i] = (char) value;
378 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
379 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
381 if (! Char.IsHighSurrogate (sb[i]))
389 i = pos_to_idx (this, i);
390 return (Char.IsHighSurrogate (sb[i])
391 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
396 public MText this[int from, int to]
404 get { return Dup (from, to); }
409 MText mt = new MText (sb.ToString ());
411 foreach (MPlist p in intervals)
412 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
416 public MText Dup (int from, int to)
418 if (check_range (from, to, true))
420 int from_idx = pos_to_idx (this, from);
421 int len = pos_to_idx (this, to) - from_idx;
422 MText mt = new MText (sb.ToString ().Substring (from_idx, len));
424 foreach (MPlist p in intervals)
425 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, from, to));
429 public MText Ins (int pos, int c)
435 public MText Ins (int pos, MText mt)
437 insert (pos, mt, 0, mt.nchars);
441 public MText Ins (int pos, MText mt, int from, int to)
443 insert (pos, mt, from, to);
447 public MText Cat (int c)
453 public MText Cat (MText mt)
455 insert (nchars, mt, 0, mt.Length);
459 public MText Cat (MText mt, int from, int to)
461 insert (nchars, mt, from, to);
465 public MText Del (int from, int to)
467 if (check_range (from, to, true))
469 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
472 foreach (MPlist plist in intervals)
474 MInterval root = (MInterval) plist.Val;
475 root.Delete (from, to);
476 if (from > 0 && from < nchars)
477 ((MInterval) plist.Val).MergeAfterChange (from, from);
486 public object GetProp (int pos, MSymbol key)
488 check_pos (pos, false);
490 MInterval i = (MInterval) intervals.Get (key);
494 MProperty prop = i.Get (pos);
495 return (prop != null ? prop.Val : null);
498 public object GetProp (int pos, MSymbol key, out MProperty prop)
500 check_pos (pos, false);
502 MInterval i = (MInterval) intervals.Get (key);
504 return (prop = null);
506 return (prop != null ? prop.Val : null);
509 public object GetProp (int pos, MSymbol key, out MProperty[] array)
511 check_pos (pos, false);
513 MInterval i = (MInterval) intervals.Get (key);
515 return (array = null);
516 MProperty prop = i.Get (pos, out array);
517 return (prop != null ? prop.Val : null);
520 public void PushProp (int from, int to, MSymbol key, object val)
522 if (! check_range (from, to, true))
523 PushProp (from, to, new MProperty (key, val));
526 public void PushProp (int from, int to, MProperty prop)
530 if (default_property == null)
531 default_property = new MPlist ();
532 default_property.Push (prop.key, prop.val);
536 if (check_range (from, to, true))
539 MPlist p = intervals.Find (prop.key);
544 root = new MInterval (prop.key, this);
545 intervals.Push (prop.key, root);
549 root = (MInterval) p.Val;
550 if (root.isSensitive)
552 root.PopSensitive (from, to);
553 root.MergeAfterChange (from, to);
554 root = (MInterval) p.Val;
559 root.Push (from, to, prop);
560 root.MergeAfterChange (from, to);
565 public void PopProp (int from, int to)
569 default_property = null;
573 if (check_range (from, to, true))
575 for (MPlist p = intervals; ! p.IsEmpty; p = p.next)
577 MInterval root = (MInterval) p.Val;
578 root.PopAll (from, to);
579 root = (MInterval) p.Val;
582 root.MergeAfterChange (from, to);
588 public void PopProp (int from, int to, MSymbol key)
592 if (default_property == null)
594 MPlist p = default_property.Find (key);
601 if (check_range (from, to, true))
604 MPlist p = intervals.Find (key);
608 MInterval root = (MInterval) p.Val;
609 if (root.isSensitive)
610 root.PopSensitive (from, to);
613 root = (MInterval) p.Val;
616 root.MergeAfterChange (from, to);
622 public object FindProp (MSymbol key, int pos, out int from, out int to)
625 check_pos (pos, false);
627 MInterval i = (MInterval) intervals.Get (key);
629 && (i = i.Find (pos, out from, out to)) != null)
630 return GetProp (from, key);
634 public void DumpProp ()
637 foreach (MPlist p in intervals)
638 ((MInterval) p.Val).Dump (true);
639 Console.WriteLine (")");
642 public void DumpPropNested ()
644 Console.WriteLine ("total length = {0}", Length);
645 foreach (MPlist p in intervals)
646 ((MInterval) p.Val).DumpNested (true);
649 private class MInterval
651 // position: 0 1 2 3 4 5 6 7
652 // | A | B | C | D | E F | G |
653 // interval |---|---|---|<->|-------|---|
654 // |---|<->|---| |<----->|---|
658 // [3 (1 2)] [3 (4 6)]
659 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
661 private static int count = 0;
664 private int From, To;
666 private MPlist Stack;
667 private MInterval Left, Right, Parent;
670 public MInterval (MSymbol key, MText mt, int length)
673 throw new Exception ("Invalid interval length");
677 Stack = new MPlist ();
681 public MInterval (MSymbol key, MText mt)
685 Length = mt.sb.Length;
688 Stack = new MPlist ();
692 /// POS must be smaller than Length;
693 public MProperty Get (int pos)
695 MInterval i = find_head (pos);
697 return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
700 /// POS must be smaller than Length;
701 public MProperty Get (int pos, out MProperty[] array)
703 MInterval i = find_head (pos);
712 array = new MProperty[i.Stack.Count];
716 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
717 array[idx] = (MProperty) p.Val;
721 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
728 Stack = stack == null ? new MPlist () : stack.Clone ();
732 private bool isRearSticky
734 get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
737 private bool isFrontSticky
739 get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
742 public bool isSensitive
744 get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
747 public bool isFrontSensitive
749 get { return MProperty.HasFlags (Key,
750 MProperty.Flags.FrontSensitive) ; }
753 public bool isRearSensitive
755 get { return MProperty.HasFlags (Key,
756 MProperty.Flags.RearSensitive) ; }
759 private void update_from_to ()
764 To = Length - RightLength;
766 else if (Parent.Left == this)
768 From = Parent.From - Length + LeftLength;
769 To = Parent.From - RightLength;
773 From = Parent.To + LeftLength;
774 To = Parent.To + Length - RightLength;
778 private int LeftLength
780 get { return (Left == null ? 0 : Left.Length); }
783 private int RightLength
785 get { return (Right == null ? 0 : Right.Length); }
788 private MInterval LeftMost
794 return Left.LeftMost;
798 private MInterval RightMost
804 return Right.RightMost;
808 private MInterval Prev {
814 for (i = Left; i.Right != null; i = i.Right)
820 MInterval child = this;
821 for (i = Parent; i != null && i.Left == child;
822 child = i, i = i.Parent);
828 private MInterval Next {
834 for (i = Right; i.Left != null; i = i.Left)
840 MInterval child = this;
841 for (i = Parent; i != null && i.Right == child;
842 child = i, i = i.Parent);
848 private MInterval find_head (int pos)
852 return Left.find_head (pos);
854 return Right.find_head (pos);
858 private MInterval find_tail (int pos)
862 return Left.find_tail (pos);
864 return Right.find_tail (pos);
868 private bool mergeable (MInterval i)
872 if (Stack.IsEmpty && i.Stack.IsEmpty)
876 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
877 p1 = p1.Next, p2 = p2.Next)
878 if (p1.Val != p2.Val)
880 return (p1.IsEmpty && p2.IsEmpty);
883 // p-. or .-p p-. or .-p
884 // .-this-. .-right-.
885 // left .-right-. -> .-this-. c2
887 private MInterval promote_right ()
889 MInterval c1 = Right.Left;
893 mtext.intervals.Put (Key, Right);
894 else if (Parent.Left == this)
897 Parent.Right = Right;
900 Right.Parent = Parent;
902 Right.Length += LeftLength + (To - From);
907 Length = LeftLength + (To - From) + RightLength;
909 // Update C1 if necessary.
913 Parent.update_from_to ();
917 // p-. or .-p p-. or .-p
919 // .-left-. .-right-. -> c1 .-this-.
921 private MInterval promote_left ()
923 MInterval c2 = Left.Right;
927 mtext.intervals.Put (Key, Left);
928 else if (Parent.Left == this)
934 Left.Parent = Parent;
936 Left.Length += (To - From) + RightLength;
941 Length = LeftLength + (To - From) + RightLength;
943 // Update C2 if necessary.
947 Parent.update_from_to ();
951 public MInterval Find (int pos, out int from, out int to)
953 MInterval i = find_head (pos);
966 public MInterval Balance ()
974 // .-left-. .-right-.
976 int diff = i.LeftLength - i.RightLength;
981 new_diff = (i.Length - i.LeftLength
982 + i.Left.RightLength - i.Left.LeftLength);
983 if (Math.Abs (new_diff) >= diff)
985 M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
986 i = i.promote_left ();
987 M17n.DebugPrint ("done\n");
992 new_diff = (i.Length - i.RightLength
993 + i.Right.LeftLength - i.Right.RightLength);
994 if (Math.Abs (new_diff) >= diff)
996 M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
997 i = i.promote_right ();
1006 public MInterval Copy (MText mt, int start, int end)
1008 MInterval copy, left_copy = null, right_copy = null;
1015 return Left.Copy (mt, start, end);
1016 left_copy = Left.Copy (mt, start, From);
1021 return Right.Copy (mt, start, end);
1022 right_copy = Right.Copy (mt, To, end);
1025 copy = new MInterval (Key, null, end - start, Stack);
1027 if (isSensitive && (From < start || end < To))
1028 copy.Stack.Clear ();
1029 if (left_copy != null)
1031 copy.Left = left_copy;
1032 left_copy.Parent = copy;
1034 if (right_copy != null)
1036 copy.Right = right_copy;
1037 right_copy.Parent = copy;
1042 public MInterval Copy (MText mt, int start, int end,
1043 bool first, bool last)
1045 MInterval copy = Copy (mt, start, end);
1046 MInterval head = find_head (start);
1047 MInterval tail = find_tail (end);
1049 M17n.DebugPrint ("Copying: {0}", copy);
1051 if (! head.Stack.IsEmpty
1052 && (isSensitive && head.From < start
1053 || (isFrontSensitive && ! first)))
1055 M17n.DebugPrint (" clear head");
1056 head = copy.find_head (0);
1057 head.Stack.Clear ();
1059 if (! tail.Stack.IsEmpty
1060 && (isSensitive && end < tail.To
1061 || (isRearSensitive && ! last)))
1063 M17n.DebugPrint (" clear tail");
1064 tail = copy.find_tail (copy.Length);
1065 tail.Stack.Clear ();
1067 M17n.DebugPrint ("\n");
1071 // this-. ==> this-.
1074 private MInterval divide_right (int pos)
1076 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
1078 M17n.DebugPrint ("divide-right({0}) at {1}\n", pos, this);
1082 interval.Right = Right;
1083 Right.Parent = interval;
1084 interval.Length += Right.Length;
1086 interval.Parent = this;
1091 // .-this ==> .-this
1094 private MInterval divide_left (int pos)
1096 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
1098 M17n.DebugPrint ("divide-left({0}) at {1}\n", pos, this);
1102 interval.Left = Left;
1103 Left.Parent = interval;
1104 interval.Length += Left.Length;
1106 interval.Parent = this;
1111 private void set_mtext (MText mt)
1115 Left.set_mtext (mt);
1117 Right.set_mtext (mt);
1120 private void enlarge (int len)
1124 for (MInterval prev = this, i = this.Parent; i != null;
1125 prev = i, i = i.Parent)
1136 private int graft_forward (MInterval interval, int start, int end)
1140 if (! Stack.IsEmpty && isRearSticky)
1142 else if (interval == null)
1143 len = Stack.IsEmpty ? end - start : 0;
1146 MInterval i = interval.find_head (start);
1150 && (isFrontSensitive || (isSensitive && i.From < start)))
1152 M17n.DebugPrint (" forward grafting {0}", i);
1159 while (i != null && i.From < end && mergeable (i))
1161 M17n.DebugPrint (" forward grafting {0}", i);
1162 len += i.To - i.From;
1164 len -= start - i.From;
1174 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1180 private int graft_backward (MInterval interval, int start, int end)
1184 if (! Stack.IsEmpty && isFrontSticky)
1186 else if (interval == null)
1187 len = Stack.IsEmpty ? end - start : 0;
1190 MInterval i = interval.find_tail (end);
1194 && (isRearSensitive || (isSensitive && end < i.To)))
1196 M17n.DebugPrint (" backward grafting {0}", i);
1197 if (i.From <= start)
1203 while (i != null && i.To <= start && mergeable (i))
1205 M17n.DebugPrint (" backward grafting {0}", i);
1206 len += i.To - i.From;
1209 if (i.From <= start)
1211 len -= start - i.From;
1218 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1224 public void Insert (int pos, MInterval interval, int start, int end)
1228 M17n.DebugPrint ("insert({0} to {1}) at {2} in {3}",
1229 start, end, pos, this);
1232 Left.Insert (pos, interval, start, end);
1233 else if (pos == From)
1235 MInterval prev = Left != null ? Prev : null;
1237 if (isFrontSensitive)
1239 if (prev != null && isRearSensitive)
1240 prev.Stack.Clear ();
1241 if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
1243 prev.enlarge (end - start);
1244 M17n.DebugPrint (" done\n");
1247 if (isFrontSticky && ! Stack.IsEmpty)
1249 enlarge (end - start);
1250 M17n.DebugPrint (" done\n");
1253 bool front_grafted = false, rear_grafted = false;
1256 && (grafted = prev.graft_forward (interval, start, end)) > 0)
1261 M17n.DebugPrint (" done\n");
1264 front_grafted = true;
1266 if ((grafted = graft_backward (interval, start, end)) > 0)
1271 M17n.DebugPrint (" done\n");
1274 rear_grafted = true;
1277 if (interval != null)
1278 interval = interval.Copy (mtext, start, end,
1280 || (prev == null && start == 0)),
1283 interval = new MInterval (Key, mtext, end - start, null);
1288 // .-this-. ==> .-this-.
1300 interval.Parent = i;
1301 for (; i != null; i = i.Parent)
1302 i.Length += interval.Length;
1306 if (! Stack.IsEmpty)
1310 else if (isFrontSticky || isRearSticky)
1312 enlarge (end - start);
1316 bool front_grafted = false, rear_grafted = false;
1318 if ((grafted = graft_forward (interval, start, end)) > 0)
1323 M17n.DebugPrint (" done\n");
1326 front_grafted = true;
1329 if ((grafted = graft_backward (interval, start, end)) > 0)
1334 M17n.DebugPrint (" done\n");
1337 rear_grafted = true;
1339 if (interval != null)
1340 interval = interval.Copy (mtext, start, end,
1341 front_grafted, rear_grafted);
1343 interval = new MInterval (Key, mtext, end - start, null);
1346 Right.Left = interval;
1347 interval.Parent = Right;
1348 for (MInterval i = Right; i != null; i = i.Parent)
1349 i.Length += interval.Length;
1353 MInterval next = Right != null ? Next : null;
1355 if (isRearSensitive)
1357 if (next != null && isFrontSensitive)
1358 next.Stack.Clear ();
1359 if (isRearSticky && ! Stack.IsEmpty)
1361 enlarge (end - start);
1362 M17n.DebugPrint (" done by enlarging this\n");
1365 if (next != null && isFrontSticky && ! next.Stack.IsEmpty)
1367 M17n.DebugPrint (" next is {0}\n", next);
1368 next.enlarge (end - start);
1369 M17n.DebugPrint (" done by enlarging next\n");
1372 bool front_grafted = false, rear_grafted = false;
1375 && (grafted = next.graft_backward (interval, start, end)) > 0)
1380 M17n.DebugPrint (" done\n");
1383 rear_grafted = true;
1385 if ((grafted = graft_forward (interval, start, end)) > 0)
1390 M17n.DebugPrint (" done\n");
1393 front_grafted = true;
1395 if (interval != null)
1396 interval = interval.Copy (mtext, start, end,
1399 || (next == null && end == interval.mtext.Length)));
1401 interval = new MInterval (Key, mtext, end - start, null);
1406 // .-this-. ==> .-this-.
1419 interval.Parent = i;
1420 for (; i != null; i = i.Parent)
1421 i.Length += interval.Length;
1424 Right.Insert (pos, interval, start, end);
1425 M17n.DebugPrint (" done\n");
1428 private void vacate_node (MInterval interval)
1430 vacate_node (interval, null);
1433 private void vacate_node (MInterval interval, MInterval stop)
1435 if (interval != null)
1436 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1438 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1439 if (interval != null)
1440 interval.Parent = Parent;
1444 mtext.intervals.Put (Key, interval);
1448 if (this == Parent.Right)
1449 Parent.Right = interval;
1451 Parent.Left = interval;
1454 if (interval != null)
1455 diff -= interval.Length;
1456 for (MInterval i = Parent; i != stop; i = i.Parent)
1461 public void Delete (int start, int end)
1464 M17n.DebugPrint ("delete({0} {1}) from {2}\n", start, end, this);
1466 bool front_checked = false;
1467 bool rear_checked = false;
1473 if (end == From && isFrontSensitive)
1475 Left.Delete (start, end);
1480 Left.Delete (start, From);
1482 end -= From - start;
1484 front_checked = true;
1490 if (start == To && isRearSensitive)
1492 Right.Delete (start, end);
1497 Right.Delete (To, end);
1499 rear_checked = true;
1505 Prev.Stack.Clear ();
1509 && isFrontSensitive)
1510 Next.Stack.Clear ();
1511 if (start == From && end == To)
1523 for (i = Right; i.Left != null; i = i.Left)
1524 i.Length += Left.Length;
1525 i.Length += Left.Length;
1529 vacate_node (Right);
1534 int len = end - start;
1538 for (MInterval i = this; i != null; i = i.Parent)
1543 public void Push (int start, int end, MProperty prop)
1546 M17n.DebugPrint ("push({0} {1}) at {2}\n", start, end, this);
1551 Left.Push (start, end, prop);
1554 Left.Push (start, From, prop);
1561 Right.Push (start, end, prop);
1564 Right.Push (To, end, prop);
1568 if (! Stack.IsEmpty && isSensitive)
1571 divide_left (start);
1574 Stack.Push (prop.key, prop);
1577 /// Combine intervals between HEAD and TAIL (both inclusive) to
1578 /// the common parent of HEAD and TAIL. Callers should assure
1579 /// that the intervals are mergeable in advance.
1580 private static void combine (MInterval head, MInterval tail)
1582 M17n.DebugPrint ("combining {0} through {1}", head, tail);
1584 head.update_from_to ();
1585 tail.update_from_to ();
1586 int from = head.From;
1589 // The nearest common parent of HEAD and TAIL.
1591 for (root = head; root.To + root.RightLength < to;
1592 root = root.Parent);
1594 M17n.DebugPrint (" with common root {0}\n", root);
1596 if (from < root.From)
1598 MInterval prev = root.Prev;
1602 M17n.DebugPrint ("merging {0}\n", prev);
1603 prev.vacate_node (prev.Left, root);
1606 if (prev.Left != null)
1607 prev = prev.Left.RightMost;
1611 root.update_from_to ();
1615 MInterval next = root.Next;
1619 M17n.DebugPrint ("merging {0}\n", next);
1620 next.vacate_node (next.Right, root);
1623 if (next.Right != null)
1624 next = next.Right.LeftMost;
1628 root.update_from_to ();
1632 public void MergeAfterChange (int start, int end)
1636 MInterval head = find_head (start), i = head;
1637 MInterval tail = start < end ? find_tail (end) : head;
1639 if (tail.To < Length)
1642 if (start == head.From && start > 0)
1644 MInterval prev = head.Prev;
1645 if (head.mergeable (prev))
1648 M17n.DebugPrint ("merge between {0} and {1}\n", head, tail);
1651 MInterval next = i.Next;
1653 if (! i.mergeable (next))
1665 public void Pop (int start, int end)
1668 M17n.DebugPrint ("pop({0} {1}) at {2}\n", start, end, this);
1673 Left.Pop (start, end);
1676 Left.Pop (start, From);
1683 Right.Pop (start, end);
1686 Right.Pop (To, end);
1690 if (! Stack.IsEmpty)
1697 divide_left (start);
1705 public void PopSensitive (int start, int end)
1708 MInterval head = find_head (start);
1709 MInterval tail = find_tail (end);
1710 Pop (head.From, tail.To);
1713 public void PopAll (int start, int end)
1716 M17n.DebugPrint ("popall({0} {1}) at {2}\n", start, end, this);
1721 Left.PopAll (start, end);
1724 Left.PopAll (start, From);
1731 Right.PopAll (start, end);
1734 Right.PopAll (To, end);
1738 if (! Stack.IsEmpty)
1745 divide_left (start);
1753 public override string ToString ()
1755 string str = String.Format ("#{0}({1} {2} {3} [", ID, Length, From, To);
1757 foreach (MPlist p in Stack)
1761 str += ((MProperty) p.Val).Val;
1765 str += " " + ((MProperty) p.Val).Val;
1767 return (str + "])");
1770 private void DumpOne (bool with_prop, bool newline, bool force)
1772 if (force || M17n.debug)
1774 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1775 if (with_prop && ! Stack.IsEmpty)
1777 string prepend = " [";
1778 foreach (MPlist p in Stack)
1780 Console.Write (prepend + ((MProperty) p.Val).Val);
1783 Console.Write ("]");
1785 Console.Write (")");
1787 Console.WriteLine ();
1789 throw new Exception ("Invalid interval length");
1793 public void Dump () { Dump (false); }
1795 public void Dump (bool force)
1797 if (force || M17n.debug)
1804 Console.Write (" ");
1805 DumpOne (true, false, force);
1812 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1815 public void DumpNested (bool force)
1817 DumpNested (" " + Key.ToString () + ":", force);
1820 public void DumpNested (string indent, bool force)
1822 if (force || M17n.debug)
1824 int indent_type = (Parent == null ? 1
1825 : Parent.Left == this ? 0 : 2);
1830 if (indent_type <= 1)
1831 Left.DumpNested (indent + " ", force);
1833 Left.DumpNested (indent + "| ", force);
1835 Console.Write (indent);
1836 if (indent_type == 0)
1837 Console.Write (".-");
1838 else if (indent_type == 2)
1839 Console.Write ("`-");
1840 DumpOne (true, true, true);
1843 if (indent_type >= 1)
1844 Right.DumpNested (indent + " ", force);
1846 Right.DumpNested (indent + "| ", force);
1852 private class MTextEnum : IEnumerator
1855 private int pos = -1;
1857 public MTextEnum (MText mt)
1862 public bool MoveNext ()
1865 return (pos < mt.nchars);
1868 public void Reset ()
1873 public object Current
1876 //if (pos < 0 || pos >= mt.nchars)
1877 //throw new InvalidOperationException ();