2 using System.Collections.Generic;
9 static MText mt = new MText ("012345678");
10 static MSymbol key = MSymbol.PropertyKey ("fse",
11 MProperty.Flags.FrontSensitive);
12 static MSymbol val0 = MSymbol.Of ("0");
13 static MSymbol val1 = MSymbol.Of ("1");
14 static MSymbol val2 = MSymbol.Of ("2");
15 static MProperty prop0 = new MProperty (key, val0);
16 static MProperty prop1 = new MProperty (key, val1);
17 static MProperty prop2 = new MProperty (key, val2);
19 // Each property is identified by its beginning point.
20 // The character at position i has the property that begins at prop[i].
21 // If the character has no property, prop[i] == -1.
22 static int[] prop = new int[LENGTH];
24 // The property beginning at position i ends at position end[i].
25 // If no property begins at position i, end[i] is undefined.
26 static int[] end = new int[LENGTH];
28 // The property beginning at position i has value value[i].
29 // If no property begings at positoin i, value[i] is undefined.
30 static MSymbol[] value = new MSymbol[LENGTH];
32 static void TestPushProp (int from, int to, MProperty newprop)
34 Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, newprop.Val);
38 if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
41 for (i = prop[from]; i < n; i++)
44 if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
47 for (i = to; i < n; i++)
50 for (i = from; i < to; i++)
53 value[from] = (MSymbol) newprop.Val;
55 mt.PushProp (from, to, newprop);
58 static void TestPopProp (int from, int to, MSymbol key)
60 Console.WriteLine ("from {0}, to {1}, pop.\n", from, to);
64 Console.WriteLine ("prop[from] = {0}", prop[from]);
65 if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
66 for (i = prop[from]; i < from; i++)
69 if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
72 for (i = to; i < n; i++)
76 for (i = from; i < to; i++)
79 mt.PopProp (from, to, key);
82 static void TestDelIns (int from, int to, int from2)
84 Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
86 int i, n, l = to - from;
87 int[] prop2 = new int[LENGTH], end2 = new int[LENGTH];
88 MSymbol[] value2 = new MSymbol[LENGTH];
91 // sensitivity for deletion
92 if (from > 0 && prop[from] != -1)
95 for (i = prop[from - 1] == prop[from] ? prop[from] : from; i < n; i++)
99 if (to < LENGTH && prop[to] != -1)
102 for (i = prop[to - 1] == prop[to] ? prop[to] : to; i < n; i++)
108 for (i = from; i < to; i++)
112 prop2[i - from] = prop[i] - from + from2;
113 end2[prop2[i - from]] = end[prop[i]] - from + from2;
114 value2[prop2[i - from]] = value[prop[i]];
117 prop2[i - from] = -1;
122 for (i = to; i < LENGTH; i++)
126 prop[i - l] = prop[i] - l;
127 end[prop[i - l]] = end[prop[i]] - l;
128 value[prop[i - l]] = value[prop[i]];
133 prop[LENGTH - l] = -1;
136 // sensitivity for insertion
137 if (prop[from2] != -1)
139 n = end[prop[from2]];
140 if (from2 > 0 && prop[from2 - 1] == prop[from2])
150 for (i = LENGTH - 1; i >= from2 + l; i--)
152 if (prop[i - l] != -1)
154 prop[i] = prop[i - l] + l;
155 end[prop[i]] = end[prop[i - l]] + l;
156 value[prop[i]] = value[prop[i - l]];
164 for (i = from2; i < from2 + l; i++)
166 if (prop2[i - from2] != -1)
168 prop[i] = prop2[i - from2];
169 end[prop[i]] = end2[prop2[i - from2]];
170 value[prop[i]] = value2[prop2[i - from2]];
177 MText mt2 = mt.Dup ();
179 mt.Ins (from2, mt2, from, to);
182 static bool Compare ()
184 for (int i = 0; i < LENGTH; i++)
186 object val = mt.GetProp (i, key);
188 if (prop[i] == -1 && (MSymbol) val != null)
190 Console.WriteLine ("value[{0}] is nil, GetProp returned {1}",
192 val == null ? MSymbol.nil : val);
196 if (prop[i] != -1 && (value[prop[i]] != (MSymbol) val))
198 Console.WriteLine ("value[{0}] is {1}, GetProp returned {2}",
200 value[prop[i]] == null ? MSymbol.nil : value[prop[i]],
201 val == null ? MSymbol.nil : val);
210 for (int i = 0; i <= LENGTH; i++)
211 Console.Write ("{0} ", i);
212 Console.WriteLine ("\n-------------------");
217 Console.Write ("{0} ", value[prop[0]]);
219 for (int i = 1; i < LENGTH; i++)
223 else if (prop[i - 1] == prop[i])
224 Console.Write ("< ");
226 Console.Write ("{0} ", value[prop[i]]);
228 Console.Write ("\n");
231 static void DebugDump (int n)
236 Console.Write ("\n#{0}\n ", n);
237 for (i = 0; i <= LENGTH; i++)
238 Console.Write ("{0} ", i);
239 Console.Write ("\n----------------------\nP ");
240 for (i = 0; i < LENGTH; i++)
243 Console.Write ("{0} ", prop[i]);
247 Console.Write ("\nE ");
249 Console.Write ("{0} ", end[0]);
252 for (i = 1; i < LENGTH; i++)
254 if (prop[i - 1] != prop[i] && prop[i] != -1)
255 Console.Write ("{0} ", end[i]);
259 Console.Write ("\nV ");
261 Console.Write ("{0} ", value[0]);
264 for (i = 1; i < LENGTH; i++)
266 if (prop[i - 1] != prop[i] && prop[i] != -1)
267 Console.Write ("{0} ", value[i]);
271 Console.Write ("\n");
275 public static void Main (string[] args)
277 Random r = new Random (int.Parse (args[0]));
278 int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
280 for (int i = 0; i < LENGTH; i++)
283 for (int loop = 0; loop < 1000000; loop++)
287 Console.WriteLine ("--- loop = {0} ---\n", loop);
290 mt.DumpPropNested ();
296 from = r.Next (LENGTH);
297 to = r.Next (LENGTH) + 1;
298 } while (from == to);
323 TestPushProp (from, to, prop);
326 TestPopProp (from, to, key);
329 TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
334 mt.DumpPropNested ();
336 if (Compare () == false)
338 Console.WriteLine ("");
340 Console.WriteLine ("Failed.");