2 using System.Collections.Generic;
9 static MText mt = new MText ("012345678");
10 static MSymbol key = MSymbol.PropertyKey ("rse",
11 MProperty.Flags.RearSensitive);
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 if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
65 for (i = prop[from]; i < from; i++)
68 if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
71 for (i = to; i < n; i++)
75 for (i = from; i < to; i++)
78 mt.PopProp (from, to, key);
81 static void TestDelIns (int from, int to, int from2)
83 Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
85 int i, j, n, l = to - from;
86 int[] prop2 = new int[LENGTH], end2 = new int[LENGTH];
87 MSymbol[] value2 = new MSymbol[LENGTH];
89 // sensitivity for deletion
90 if (from > 0 && prop[from - 1] != -1)
92 n = prop[from - 1] == prop[from] ? end[prop[from]] : from;
93 for (i = prop[from - 1]; i < n; i++)
97 if (to < LENGTH && prop[to - 1] != -1)
99 n = prop[to - 1] == prop[to] ? end[prop[to]] : to;
100 for (i = prop[to - 1]; i < n; i++)
105 for (i = from, j = from2; i < to; i++, j++)
109 prop2[j] = prop[i] - from + from2;
110 end2[j] = end[i] - from + from2;
111 value2[j] = value[i];
118 Console.Write ("\n ");
119 for (j = from2; j < from2 + l; j++)
120 Console.Write ("{0} ", j);
121 Console.Write ("\n--------------------\nP ");
122 for (j = from2; j < from2 + l; j++)
125 Console.Write ("{0} ", prop2[j]);
129 Console.Write ("\nE ");
130 for (j = from2; j < from2 + l; j++)
133 Console.Write ("{0} ", end2[j]);
137 Console.Write ("\nV ");
138 for (j = from2; j < from2 + l; j++)
141 Console.Write ("{0} ", value2[j]);
145 Console.Write ("\n");
149 for (i = to; i < LENGTH; i++)
153 prop[i - l] = prop[i] - l;
154 end[i - l] = end[i] - l;
155 value[i - l] = value[i];
161 // sensitivity for insertion
162 if (from2 > 0 && prop[from2 - 1] != -1)
164 n = prop[from2 - 1] == prop[from2] ? end[prop[from2]] : from2;
165 for (i = prop[from2 - 1]; i < n; i++)
168 if (from2 + l < LENGTH && prop2[from2 + l - 1] != -1)
169 for (i = prop2[from2 + l -1]; i < from2 + l; i++)
173 for (i = LENGTH - 1; i >= from2 + l; i--)
175 if (prop[i - l] != -1)
177 prop[i] = prop[i - l] + l;
178 end[i] = end[i - l] + l;
179 value[i] = value[i - l];
186 for (i = from2; i < from2 + l; i++)
192 value[i] = value2[i];
196 MText mt2 = mt.Dup ();
198 mt.Ins (from2, mt2, from, to);
201 static bool Compare ()
203 for (int i = 0; i < LENGTH; i++)
205 object val = mt.GetProp (i, key);
207 if (prop[i] == -1 && (MSymbol) val != null)
209 Console.WriteLine ("value[{0}] is nil, GetProp returned {1}",
211 val == null ? MSymbol.nil : val);
215 if (prop[i] != -1 && (value[prop[i]] != (MSymbol) val))
217 Console.WriteLine ("value[{0}] is {1}, GetProp returned {2}",
219 value[prop[i]] == null ? MSymbol.nil : value[prop[i]],
220 val == null ? MSymbol.nil : val);
229 for (int i = 0; i <= LENGTH; i++)
230 Console.Write ("{0} ", i);
231 Console.WriteLine ("\n-------------------");
236 Console.Write ("{0} ", value[prop[0]]);
238 for (int i = 1; i < LENGTH; i++)
242 else if (prop[i - 1] == prop[i])
243 Console.Write ("< ");
245 Console.Write ("{0} ", value[prop[i]]);
247 Console.Write ("\n");
251 static void DebugDump (int n)
255 Console.Write ("\n#{0}\n ", n);
256 for (i = 0; i <= LENGTH; i++)
257 Console.Write ("{0} ", i);
258 Console.Write ("\n----------------------\nP ");
259 for (i = 0; i < LENGTH; i++)
262 Console.Write ("{0} ", prop[i]);
266 Console.Write ("\nE ");
268 Console.Write ("{0} ", end[0]);
271 for (i = 1; i < LENGTH; i++)
273 if (prop[i - 1] != prop[i] && prop[i] != -1)
274 Console.Write ("{0} ", end[i]);
278 Console.Write ("\nV ");
280 Console.Write ("{0} ", value[0]);
283 for (i = 1; i < LENGTH; i++)
285 if (prop[i - 1] != prop[i] && prop[i] != -1)
286 Console.Write ("{0} ", value[i]);
290 Console.Write ("\n");
294 public static void Main (string[] args)
296 Random r = new Random (int.Parse (args[0]));
297 int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
299 for (int i = 0; i < LENGTH; i++)
302 for (int loop = 0; loop < 1000000; loop++)
306 Console.WriteLine ("--- loop = {0} ---\n", loop);
309 mt.DumpPropNested ();
315 from = r.Next (LENGTH);
316 to = r.Next (LENGTH) + 1;
317 } while (from == to);
342 TestPushProp (from, to, prop);
345 TestPopProp (from, to, key);
348 TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
353 mt.DumpPropNested ();
355 if (Compare () == false)
357 Console.WriteLine ("");
359 Console.WriteLine ("Failed.");