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 (surrogate_high_p (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 (surrogate_high_p (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 static MText operator+ (object obj, MText mt)
180 MText mtnew = new MText ((string) obj);
181 return mtnew.Ins (mtnew.Length, mt);
183 throw new Exception ("Unknown object type: " + obj.GetType());
186 public static MText operator+ (MText mt, object obj)
189 return mt + new MText ((string) obj);
191 return mt.Dup ().Ins (mt.Length, (int) obj);
193 return mt.Dup ().Ins (mt.Length, (int) ((char) obj));
194 throw new Exception ("Unknown object type: " + obj.GetType());
197 public static MText operator+ (MText mt1, MText mt2)
199 return mt1.Dup ().Ins (mt1.Length, mt2);
203 public bool ReadOnly { get { return read_only; } }
204 public int Length { get { return nchars; } }
208 // for IEnumerable interface
209 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
211 // for IEquatable interface
212 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
214 // for IComparable interface
215 public int CompareTo (MText other)
217 return this.sb.ToString ().CompareTo (other.sb.ToString ());
220 public override string ToString () { return sb.ToString (); }
222 private static bool surrogate_high_p (char c)
224 return (c >= 0xD800 && c < 0xDC00);
227 private static bool surrogate_low_p (char c)
229 return (c >= 0xDC00 && c < 0xE000);
232 private static int inc_idx (StringBuilder sb, int i)
234 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
237 private static int dec_idx (StringBuilder sb, int i)
239 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
242 private static int pos_to_idx (MText mt, int pos)
244 if (pos == mt.cache_pos)
250 if (pos < mt.cache_pos)
252 if (mt.cache_pos == mt.cache_idx)
254 if (pos < mt.cache_pos - pos)
261 p = mt.cache_pos; i = mt.cache_idx;
267 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
268 return (mt.cache_idx + pos - mt.cache_pos);
269 if (pos - mt.cache_pos < mt.nchars - pos)
271 p = mt.cache_pos; i = mt.cache_idx;
276 p = mt.nchars; i = mt.sb.Length;
281 for (; p < pos; i = inc_idx (mt.sb, i), p++);
283 for (; p > pos; i = dec_idx (mt.sb, i), p--);
289 private void check_pos (int pos, bool tail_ok)
291 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
292 throw new Exception ("Invalid MText position:" + pos);
295 private bool check_range (int from, int to, bool zero_ok)
297 if (from < 0 || (zero_ok ? from > to : from >= to)
299 throw new Exception ("Invalid MText range");
303 private void insert (int pos, MText mt2, int from, int to)
305 check_pos (pos, true);
309 Console.Write ("inserting {0} to {1} of ", from, to);
310 mt2.DumpPropNested ();
314 foreach (MPlist plist in intervals)
316 MInterval root = (MInterval) plist.Val;
317 MPlist p = mt2.intervals.Find (plist.Key);
318 MInterval i = p == null ? null : (MInterval) p.Val;
320 root.Insert (pos, i, from, to);
322 foreach (MPlist plist in mt2.intervals)
323 if (intervals.Find (plist.Key) == null)
328 root = ((MInterval) plist.Val).Copy (this, from, to);
331 root = new MInterval (plist.Key, this);
332 root.Insert (pos, (MInterval) plist.Val, from, to);
334 intervals.Push (plist.Key, root);
337 int pos_idx = pos_to_idx (this, pos);
338 int from_idx = pos_to_idx (mt2, from);
339 int to_idx = pos_to_idx (mt2, to);
341 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
345 private void insert (int pos, int c)
347 check_pos (pos, true);
349 int pos_idx = pos_to_idx (this, pos);
354 sb.Insert (pos_idx, ch);
358 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
359 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
360 sb.Insert (pos_idx, low);
361 sb.Insert (pos_idx, high);
364 foreach (MPlist plist in intervals)
365 ((MInterval) plist.Val).Insert (pos, null, 0, 1);
368 public int this[int i]
371 i = pos_to_idx (this, i);
374 if (surrogate_high_p (sb[i]))
376 sb[i] = (char) value;
380 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
381 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
383 if (! surrogate_high_p (sb[i]))
390 i = pos_to_idx (this, i);
391 return (surrogate_high_p (sb[i])
392 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
399 MText mt = new MText (sb.ToString ());
401 foreach (MPlist p in intervals)
402 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
406 public MText Dup (int from, int to)
408 if (check_range (from, to, true))
410 int from_idx = pos_to_idx (this, from);
411 int len = pos_to_idx (this, to) - from_idx;
412 MText mt = new MText (sb.ToString ().Substring (from_idx, len));
414 foreach (MPlist p in intervals)
415 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, from, to));
419 public MText Ins (int pos, int c)
425 public MText Ins (int pos, MText mt)
427 insert (pos, mt, 0, mt.nchars);
431 public MText Ins (int pos, MText mt, int from, int to)
433 insert (pos, mt, from, to);
437 public MText Cat (int c)
443 public MText Cat (MText mt)
445 insert (nchars, mt, 0, mt.Length);
449 public MText Del (int from, int to)
451 if (check_range (from, to, true))
454 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
458 foreach (MPlist plist in intervals)
460 MInterval root = (MInterval) plist.Val;
461 root.Delete (from, to);
462 if (from > 0 && from < nchars)
463 ((MInterval) plist.Val).MergeAfterChange (from, from);
472 public object GetProp (int pos, MSymbol key)
474 check_pos (pos, false);
476 MInterval i = (MInterval) intervals.Get (key);
480 MProperty prop = i.Get (pos);
481 return (prop != null ? prop.Val : null);
484 public object GetProp (int pos, MSymbol key, out MProperty prop)
486 check_pos (pos, false);
488 MInterval i = (MInterval) intervals.Get (key);
490 return (prop = null);
492 return (prop != null ? prop.Val : null);
495 public object GetProp (int pos, MSymbol key, out MProperty[] array)
497 check_pos (pos, false);
499 MInterval i = (MInterval) intervals.Get (key);
501 return (array = null);
502 MProperty prop = i.Get (pos, out array);
503 return (prop != null ? prop.Val : null);
506 public void PushProp (int from, int to, MSymbol key, object val)
508 if (! check_range (from, to, true))
509 PushProp (from, to, new MProperty (key, val));
512 public void PushProp (int from, int to, MProperty prop)
516 if (default_property == null)
517 default_property = new MPlist ();
518 default_property.Push (prop.key, prop.val);
522 if (check_range (from, to, true))
525 MPlist p = intervals.Find (prop.key);
530 root = new MInterval (prop.key, this);
531 intervals.Push (prop.key, root);
535 root = (MInterval) p.Val;
536 if (root.isSensitive)
538 root.PopSensitive (from, to);
539 root.MergeAfterChange (from, to);
540 root = (MInterval) p.Val;
545 root.Push (from, to, prop);
546 root.MergeAfterChange (from, to);
551 public void PopProp (int from, int to, MSymbol key)
555 if (default_property == null)
557 MPlist p = default_property.Find (key);
564 if (check_range (from, to, true))
567 MPlist p = intervals.Find (key);
571 MInterval root = (MInterval) p.Val;
572 if (root.isSensitive)
573 root.PopSensitive (from, to);
576 root = (MInterval) p.Val;
579 root.MergeAfterChange (from, to);
585 public void DumpProp ()
588 foreach (MPlist p in intervals)
589 ((MInterval) p.Val).Dump (true);
590 Console.WriteLine (")");
593 public void DumpPropNested ()
595 Console.WriteLine ("total length = {0}", Length);
596 foreach (MPlist p in intervals)
597 ((MInterval) p.Val).DumpNested (true);
600 private class MInterval
602 // position: 0 1 2 3 4 5 6 7
603 // | A | B | C | D | E F | G |
604 // interval |---|---|---|<->|-------|---|
605 // |---|<->|---| |<----->|---|
609 // [3 (1 2)] [3 (4 6)]
610 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
612 private static int count = 0;
615 private int From, To;
617 private MPlist Stack;
618 private MInterval Left, Right, Parent;
621 public MInterval (MSymbol key, MText mt, int length)
624 throw new Exception ("Invalid interval length");
628 Stack = new MPlist ();
632 public MInterval (MSymbol key, MText mt)
636 Length = mt.sb.Length;
639 Stack = new MPlist ();
643 /// POS must be smaller than Length;
644 public MProperty Get (int pos)
646 MInterval i = find_head (pos);
648 return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
651 /// POS must be smaller than Length;
652 public MProperty Get (int pos, out MProperty[] array)
654 MInterval i = find_head (pos);
663 array = new MProperty[i.Stack.Count];
667 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
668 array[idx] = (MProperty) p.Val;
672 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
679 Stack = stack == null ? new MPlist () : stack.Clone ();
683 private bool isRearSticky
685 get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
688 private bool isFrontSticky
690 get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
693 public bool isSensitive
695 get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
698 public bool isFrontSensitive
700 get { return MProperty.HasFlags (Key,
701 MProperty.Flags.FrontSensitive) ; }
704 public bool isRearSensitive
706 get { return MProperty.HasFlags (Key,
707 MProperty.Flags.RearSensitive) ; }
710 private void update_from_to ()
715 To = Length - RightLength;
717 else if (Parent.Left == this)
719 From = Parent.From - Length + LeftLength;
720 To = Parent.From - RightLength;
724 From = Parent.To + LeftLength;
725 To = Parent.To + Length - RightLength;
729 private int LeftLength
731 get { return (Left == null ? 0 : Left.Length); }
734 private int RightLength
736 get { return (Right == null ? 0 : Right.Length); }
739 private MInterval LeftMost
745 return Left.LeftMost;
749 private MInterval RightMost
755 return Right.RightMost;
759 private MInterval Prev {
765 for (i = Left; i.Right != null; i = i.Right)
771 MInterval child = this;
772 for (i = Parent; i != null && i.Left == child;
773 child = i, i = i.Parent);
779 private MInterval Next {
785 for (i = Right; i.Left != null; i = i.Left)
791 MInterval child = this;
792 for (i = Parent; i != null && i.Right == child;
793 child = i, i = i.Parent);
799 private MInterval find_head (int pos)
803 return Left.find_head (pos);
805 return Right.find_head (pos);
809 private MInterval find_tail (int pos)
813 return Left.find_tail (pos);
815 return Right.find_tail (pos);
819 private bool mergeable (MInterval i)
823 if (Stack.IsEmpty && i.Stack.IsEmpty)
827 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
828 p1 = p1.Next, p2 = p2.Next)
829 if (p1.Val != p2.Val)
831 return (p1.IsEmpty && p2.IsEmpty);
834 // p-. or .-p p-. or .-p
835 // .-this-. .-right-.
836 // left .-right-. -> .-this-. c2
838 private MInterval promote_right ()
840 MInterval c1 = Right.Left;
844 mtext.intervals.Put (Key, Right);
845 else if (Parent.Left == this)
848 Parent.Right = Right;
851 Right.Parent = Parent;
853 Right.Length += LeftLength + (To - From);
858 Length = LeftLength + (To - From) + RightLength;
860 // Update C1 if necessary.
864 Parent.update_from_to ();
868 // p-. or .-p p-. or .-p
870 // .-left-. .-right-. -> c1 .-this-.
872 private MInterval promote_left ()
874 MInterval c2 = Left.Right;
878 mtext.intervals.Put (Key, Left);
879 else if (Parent.Left == this)
885 Left.Parent = Parent;
887 Left.Length += (To - From) + RightLength;
892 Length = LeftLength + (To - From) + RightLength;
894 // Update C2 if necessary.
898 Parent.update_from_to ();
902 public MInterval Balance ()
910 // .-left-. .-right-.
912 int diff = i.LeftLength - i.RightLength;
917 new_diff = (i.Length - i.LeftLength
918 + i.Left.RightLength - i.Left.LeftLength);
919 if (Math.Abs (new_diff) >= diff)
921 M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
922 i = i.promote_left ();
923 M17n.DebugPrint ("done\n");
928 new_diff = (i.Length - i.RightLength
929 + i.Right.LeftLength - i.Right.RightLength);
930 if (Math.Abs (new_diff) >= diff)
932 M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
933 i = i.promote_right ();
942 public MInterval Copy (MText mt, int start, int end)
944 MInterval copy, left_copy = null, right_copy = null;
951 return Left.Copy (mt, start, end);
952 left_copy = Left.Copy (mt, start, From);
957 return Right.Copy (mt, start, end);
958 right_copy = Right.Copy (mt, To, end);
961 copy = new MInterval (Key, null, end - start, Stack);
963 if (isSensitive && (From < start || end < To))
965 if (left_copy != null)
967 copy.Left = left_copy;
968 left_copy.Parent = copy;
970 if (right_copy != null)
972 copy.Right = right_copy;
973 right_copy.Parent = copy;
978 public MInterval Copy (MText mt, int start, int end,
979 bool first, bool last)
981 MInterval copy = Copy (mt, start, end);
982 MInterval head = find_head (start);
983 MInterval tail = find_tail (end);
985 M17n.DebugPrint ("Copying: {0}", copy);
987 if (! head.Stack.IsEmpty
988 && (isSensitive && head.From < start
989 || (isFrontSensitive && ! first)))
991 M17n.DebugPrint (" clear head");
992 head = copy.find_head (0);
995 if (! tail.Stack.IsEmpty
996 && (isSensitive && end < tail.To
997 || (isRearSensitive && ! last)))
999 M17n.DebugPrint (" clear tail");
1000 tail = copy.find_tail (copy.Length);
1001 tail.Stack.Clear ();
1003 M17n.DebugPrint ("\n");
1007 // this-. ==> this-.
1010 private MInterval divide_right (int pos)
1012 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
1014 M17n.DebugPrint ("divide-right({0}) at {1}\n", pos, this);
1018 interval.Right = Right;
1019 Right.Parent = interval;
1020 interval.Length += Right.Length;
1022 interval.Parent = this;
1027 // .-this ==> .-this
1030 private MInterval divide_left (int pos)
1032 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
1034 M17n.DebugPrint ("divide-left({0}) at {1}\n", pos, this);
1038 interval.Left = Left;
1039 Left.Parent = interval;
1040 interval.Length += Left.Length;
1042 interval.Parent = this;
1047 private void set_mtext (MText mt)
1051 Left.set_mtext (mt);
1053 Right.set_mtext (mt);
1056 private void enlarge (int len)
1060 for (MInterval prev = this, i = this.Parent; i != null;
1061 prev = i, i = i.Parent)
1072 private int graft_forward (MInterval interval, int start, int end)
1076 if (! Stack.IsEmpty && isRearSticky)
1078 else if (interval == null)
1079 len = Stack.IsEmpty ? end - start : 0;
1082 MInterval i = interval.find_head (start);
1086 && (isFrontSensitive || (isSensitive && i.From < start)))
1088 M17n.DebugPrint (" forward grafting {0}", i);
1095 while (i != null && i.From < end && mergeable (i))
1097 M17n.DebugPrint (" forward grafting {0}", i);
1098 len += i.To - i.From;
1100 len -= start - i.From;
1110 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1116 private int graft_backward (MInterval interval, int start, int end)
1120 if (! Stack.IsEmpty && isFrontSticky)
1122 else if (interval == null)
1123 len = Stack.IsEmpty ? end - start : 0;
1126 MInterval i = interval.find_tail (end);
1130 && (isRearSensitive || (isSensitive && end < i.To)))
1132 M17n.DebugPrint (" backward grafting {0}", i);
1133 if (i.From <= start)
1139 while (i != null && i.To <= start && mergeable (i))
1141 M17n.DebugPrint (" backward grafting {0}", i);
1142 len += i.To - i.From;
1145 if (i.From <= start)
1147 len -= start - i.From;
1154 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1160 public void Insert (int pos, MInterval interval, int start, int end)
1164 M17n.DebugPrint ("insert({0} to {1}) at {2} in {3}",
1165 start, end, pos, this);
1168 Left.Insert (pos, interval, start, end);
1169 else if (pos == From)
1171 MInterval prev = Left != null ? Prev : null;
1173 if (isFrontSensitive)
1175 if (prev != null && isRearSensitive)
1176 prev.Stack.Clear ();
1177 if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
1179 prev.enlarge (end - start);
1180 M17n.DebugPrint (" done\n");
1183 if (isFrontSticky && ! Stack.IsEmpty)
1185 enlarge (end - start);
1186 M17n.DebugPrint (" done\n");
1189 bool front_grafted = false, rear_grafted = false;
1192 && (grafted = prev.graft_forward (interval, start, end)) > 0)
1197 M17n.DebugPrint (" done\n");
1200 front_grafted = true;
1202 if ((grafted = graft_backward (interval, start, end)) > 0)
1207 M17n.DebugPrint (" done\n");
1210 rear_grafted = true;
1213 if (interval != null)
1214 interval = interval.Copy (mtext, start, end,
1216 || (prev == null && start == 0)),
1219 interval = new MInterval (Key, mtext, end - start, null);
1224 // .-this-. ==> .-this-.
1236 interval.Parent = i;
1237 for (; i != null; i = i.Parent)
1238 i.Length += interval.Length;
1242 if (! Stack.IsEmpty)
1246 else if (isFrontSticky || isRearSticky)
1248 enlarge (end - start);
1252 bool front_grafted = false, rear_grafted = false;
1254 if ((grafted = graft_forward (interval, start, end)) > 0)
1259 M17n.DebugPrint (" done\n");
1262 front_grafted = true;
1265 if ((grafted = graft_backward (interval, start, end)) > 0)
1270 M17n.DebugPrint (" done\n");
1273 rear_grafted = true;
1275 if (interval != null)
1276 interval = interval.Copy (mtext, start, end,
1277 front_grafted, rear_grafted);
1279 interval = new MInterval (Key, mtext, end - start, null);
1282 Right.Left = interval;
1283 interval.Parent = Right;
1284 for (MInterval i = Right; i != null; i = i.Parent)
1285 i.Length += interval.Length;
1289 MInterval next = Right != null ? Next : null;
1291 if (isRearSensitive)
1293 if (next != null && isFrontSensitive)
1294 next.Stack.Clear ();
1295 if (isRearSticky && ! Stack.IsEmpty)
1297 enlarge (end - start);
1298 M17n.DebugPrint (" done by enlarging this\n");
1301 if (next != null && isFrontSticky && ! next.Stack.IsEmpty)
1303 M17n.DebugPrint (" next is {0}\n", next);
1304 next.enlarge (end - start);
1305 M17n.DebugPrint (" done by enlarging next\n");
1308 bool front_grafted = false, rear_grafted = false;
1311 && (grafted = next.graft_backward (interval, start, end)) > 0)
1316 M17n.DebugPrint (" done\n");
1319 rear_grafted = true;
1321 if ((grafted = graft_forward (interval, start, end)) > 0)
1326 M17n.DebugPrint (" done\n");
1329 front_grafted = true;
1331 if (interval != null)
1332 interval = interval.Copy (mtext, start, end,
1335 || (next == null && end == interval.mtext.Length)));
1337 interval = new MInterval (Key, mtext, end - start, null);
1342 // .-this-. ==> .-this-.
1355 interval.Parent = i;
1356 for (; i != null; i = i.Parent)
1357 i.Length += interval.Length;
1360 Right.Insert (pos, interval, start, end);
1361 M17n.DebugPrint (" done\n");
1364 private void vacate_node (MInterval interval)
1366 vacate_node (interval, null);
1369 private void vacate_node (MInterval interval, MInterval stop)
1371 if (interval != null)
1372 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1374 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1375 if (interval != null)
1376 interval.Parent = Parent;
1380 mtext.intervals.Put (Key, interval);
1384 if (this == Parent.Right)
1385 Parent.Right = interval;
1387 Parent.Left = interval;
1390 if (interval != null)
1391 diff -= interval.Length;
1392 for (MInterval i = Parent; i != stop; i = i.Parent)
1397 public void Delete (int start, int end)
1400 M17n.DebugPrint ("delete({0} {1}) from {2}\n", start, end, this);
1402 bool front_checked = false;
1403 bool rear_checked = false;
1409 if (end == From && isFrontSensitive)
1411 Left.Delete (start, end);
1416 Left.Delete (start, From);
1418 end -= From - start;
1420 front_checked = true;
1426 if (start == To && isRearSensitive)
1428 Right.Delete (start, end);
1433 Right.Delete (To, end);
1435 rear_checked = true;
1441 Prev.Stack.Clear ();
1445 && isFrontSensitive)
1446 Next.Stack.Clear ();
1447 if (start == From && end == To)
1459 for (i = Right; i.Left != null; i = i.Left)
1460 i.Length += Left.Length;
1461 i.Length += Left.Length;
1465 vacate_node (Right);
1470 int len = end - start;
1474 for (MInterval i = this; i != null; i = i.Parent)
1479 public void Push (int start, int end, MProperty prop)
1482 M17n.DebugPrint ("push({0} {1}) at {2}\n", start, end, this);
1487 Left.Push (start, end, prop);
1490 Left.Push (start, From, prop);
1497 Right.Push (start, end, prop);
1500 Right.Push (To, end, prop);
1504 if (! Stack.IsEmpty && isSensitive)
1507 divide_left (start);
1510 Stack.Push (prop.key, prop);
1513 /// Combine intervals between HEAD and TAIL (both inclusive) to
1514 /// the common parent of HEAD and TAIL. Callers should assure
1515 /// that the intervals are mergeable in advance.
1516 private static void combine (MInterval head, MInterval tail)
1518 M17n.DebugPrint ("combining {0} through {1}", head, tail);
1520 head.update_from_to ();
1521 tail.update_from_to ();
1522 int from = head.From;
1525 // The nearest common parent of HEAD and TAIL.
1527 for (root = head; root.To + root.RightLength < to;
1528 root = root.Parent);
1530 M17n.DebugPrint (" with common root {0}\n", root);
1532 if (from < root.From)
1534 MInterval prev = root.Prev;
1538 M17n.DebugPrint ("merging {0}\n", prev);
1539 prev.vacate_node (prev.Left, root);
1542 if (prev.Left != null)
1543 prev = prev.Left.RightMost;
1547 root.update_from_to ();
1551 MInterval next = root.Next;
1555 M17n.DebugPrint ("merging {0}\n", next);
1556 next.vacate_node (next.Right, root);
1559 if (next.Right != null)
1560 next = next.Right.LeftMost;
1564 root.update_from_to ();
1568 public void MergeAfterChange (int start, int end)
1572 MInterval head = find_head (start), i = head;
1573 MInterval tail = start < end ? find_tail (end) : head;
1575 if (tail.To < Length)
1578 if (start == head.From && start > 0)
1580 MInterval prev = head.Prev;
1581 if (head.mergeable (prev))
1584 M17n.DebugPrint ("merge between {0} and {1}\n", head, tail);
1587 MInterval next = i.Next;
1589 if (! i.mergeable (next))
1601 public void Pop (int start, int end)
1604 M17n.DebugPrint ("pop({0} {1}) at {2}\n", start, end, this);
1609 Left.Pop (start, end);
1612 Left.Pop (start, From);
1619 Right.Pop (start, end);
1622 Right.Pop (To, end);
1626 if (! Stack.IsEmpty)
1633 divide_left (start);
1641 public void PopSensitive (int start, int end)
1644 MInterval head = find_head (start);
1645 MInterval tail = find_tail (end);
1646 Pop (head.From, tail.To);
1649 public override string ToString ()
1651 string str = String.Format ("#{0}({1} {2} {3} [", ID, Length, From, To);
1653 foreach (MPlist p in Stack)
1657 str += ((MProperty) p.Val).Val;
1661 str += " " + ((MProperty) p.Val).Val;
1663 return (str + "])");
1666 private void DumpOne (bool with_prop, bool newline, bool force)
1668 if (force || M17n.debug)
1670 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1671 if (with_prop && ! Stack.IsEmpty)
1673 string prepend = " [";
1674 foreach (MPlist p in Stack)
1676 Console.Write (prepend + ((MProperty) p.Val).Val);
1679 Console.Write ("]");
1681 Console.Write (")");
1683 Console.WriteLine ();
1685 throw new Exception ("Invalid interval length");
1689 public void Dump () { Dump (false); }
1691 public void Dump (bool force)
1693 if (force || M17n.debug)
1700 Console.Write (" ");
1701 DumpOne (true, false, force);
1708 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1711 public void DumpNested (bool force)
1713 DumpNested (" " + Key.ToString () + ":", force);
1716 public void DumpNested (string indent, bool force)
1718 if (force || M17n.debug)
1720 int indent_type = (Parent == null ? 1
1721 : Parent.Left == this ? 0 : 2);
1726 if (indent_type <= 1)
1727 Left.DumpNested (indent + " ", force);
1729 Left.DumpNested (indent + "| ", force);
1731 Console.Write (indent);
1732 if (indent_type == 0)
1733 Console.Write (".-");
1734 else if (indent_type == 2)
1735 Console.Write ("`-");
1736 DumpOne (true, true, true);
1739 if (indent_type >= 1)
1740 Right.DumpNested (indent + " ", force);
1742 Right.DumpNested (indent + "| ", force);
1748 private class MTextEnum : IEnumerator
1751 private int pos = -1;
1753 public MTextEnum (MText mt)
1758 public bool MoveNext ()
1761 return (pos < mt.nchars);
1764 public void Reset ()
1769 public object Current
1772 //if (pos < 0 || pos >= mt.nchars)
1773 //throw new InvalidOperationException ();