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];
33 static int[] subprop = new int[LENGTH], subend = new int[LENGTH];
34 static MSymbol[] subvalue = new MSymbol[LENGTH];
36 static void TestPushProp (int from, int to, MProperty newprop)
38 Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, newprop.Val);
42 if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
45 for (i = prop[from]; i < n; i++)
48 if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
51 for (i = to; i < n; i++)
54 for (i = from; i < to; i++)
57 value[from] = (MSymbol) newprop.Val;
59 mt.PushProp (from, to, newprop);
62 static void TestPopProp (int from, int to, MSymbol key)
64 Console.WriteLine ("from {0}, to {1}, pop.\n", from, to);
68 if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
69 for (i = prop[from]; i < from; i++)
72 if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
75 for (i = to; i < n; i++)
79 for (i = from; i < to; i++)
82 mt.PopProp (from, to, key);
85 static void TestDelIns (int from, int to, int from2)
87 Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
89 int i, n, l = to - from;
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++)
107 for (i = from; i < to; i++)
111 subprop[i - from] = prop[i] - from + from2;
112 subend[i - from] = end[i] - from + from2;
113 subvalue[i - from] = value[i];
116 subprop[i - from] = -1;
120 for (i = to; i < LENGTH; i++)
124 prop[i - l] = prop[i] - l;
125 end[i - l] = end[i] - l;
126 value[i - l] = value[i];
131 prop[LENGTH - l] = -1;
133 // sensitivity for insertion
134 if (prop[from2] != -1)
136 n = end[prop[from2]];
137 if (from2 > 0 && prop[from2 - 1] == prop[from2])
144 if (from2 > 0 && subprop[0] != -1)
145 for (i = 0; i < subend[0] - from2; i++)
149 for (i = LENGTH - 1; i >= from2 + l; i--)
151 if (prop[i - l] != -1)
153 prop[i] = prop[i - l] + l;
154 end[i] = end[i - l] + l;
155 value[i] = value[i - l];
162 for (i = from2; i < from2 + l; i++)
164 if (subprop[i - from2] != -1)
166 prop[i] = subprop[i - from2];
167 end[i] = subend[i - from2];
168 value[i] = subvalue[i - from2];
174 MText mt2 = mt.Dup ();
176 mt.Ins (from2, mt2, from, to);
179 static bool Compare ()
181 for (int i = 0; i < LENGTH; i++)
183 object val = mt.GetProp (i, key);
185 if (prop[i] == -1 && (MSymbol) val != null)
187 Console.WriteLine ("value[{0}] is nil, GetProp returned {1}",
189 val == null ? MSymbol.nil : val);
193 if (prop[i] != -1 && (value[prop[i]] != (MSymbol) val))
195 Console.WriteLine ("value[{0}] is {1}, GetProp returned {2}",
197 value[prop[i]] == null ? MSymbol.nil : value[prop[i]],
198 val == null ? MSymbol.nil : val);
207 for (int i = 0; i <= LENGTH; i++)
208 Console.Write ("{0} ", i);
209 Console.WriteLine ("\n-------------------");
214 Console.Write ("{0} ", value[prop[0]]);
216 for (int i = 1; i < LENGTH; i++)
220 else if (prop[i - 1] == prop[i])
221 Console.Write ("< ");
223 Console.Write ("{0} ", value[prop[i]]);
225 Console.Write ("\n");
228 public static void Main (string[] args)
230 Random r = new Random (int.Parse (args[0]));
231 int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
233 for (int i = 0; i < LENGTH; i++)
236 for (int loop = 0; loop < 1000000; loop++)
240 Console.WriteLine ("--- loop = {0} ---\n", loop);
243 mt.DumpPropNested ();
249 from = r.Next (LENGTH);
250 to = r.Next (LENGTH) + 1;
251 } while (from == to);
276 TestPushProp (from, to, prop);
279 TestPopProp (from, to, key);
282 TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
287 mt.DumpPropNested ();
289 if (Compare () == false)
291 Console.WriteLine ("");
293 Console.WriteLine ("Failed.");