Example #1
0
		public virtual void GenerateSwitch(string[] pairs, string default_value)
		{
			int N = pairs.Length / 2;
			IdValuePair[] id_pairs = new IdValuePair[N];
			for (int i = 0; i != N; ++i)
			{
				id_pairs[i] = new IdValuePair(pairs[2 * i], pairs[2 * i + 1]);
			}
			GenerateSwitch(id_pairs, default_value);
		}
Example #2
0
		public virtual void GenerateSwitch(IdValuePair[] pairs, string default_value)
		{
			int begin = 0;
			int end = pairs.Length;
			if (begin == end)
			{
				return;
			}
			this.pairs = pairs;
			this.default_value = default_value;
			Generate_body(begin, end, 2);
		}
Example #3
0
		private static void MakeHeap4(IdValuePair[] array, int offset, int size, int comparator)
		{
			for (int i = ((size + 2) >> 2); i != 0; )
			{
				--i;
				Heapify4(array, offset, size, i, comparator);
			}
		}
Example #4
0
		private static void Heapify4(IdValuePair[] array, int offset, int size, int i, int comparator)
		{
			int new_i1;
			int new_i2;
			int new_i3;
			IdValuePair i_val = array[offset + i];
			for (; ; )
			{
				int @base = (i << 2);
				new_i1 = @base | 1;
				new_i2 = @base | 2;
				new_i3 = @base | 3;
				int new_i4 = @base + 4;
				if (new_i4 >= size)
				{
					break;
				}
				IdValuePair val1 = array[offset + new_i1];
				IdValuePair val2 = array[offset + new_i2];
				IdValuePair val3 = array[offset + new_i3];
				IdValuePair val4 = array[offset + new_i4];
				if (Bigger(val2, val1, comparator))
				{
					val1 = val2;
					new_i1 = new_i2;
				}
				if (Bigger(val4, val3, comparator))
				{
					val3 = val4;
					new_i3 = new_i4;
				}
				if (Bigger(val3, val1, comparator))
				{
					val1 = val3;
					new_i1 = new_i3;
				}
				if (Bigger(i_val, val1, comparator))
				{
					return;
				}
				array[offset + i] = val1;
				array[offset + new_i1] = i_val;
				i = new_i1;
			}
			if (new_i1 < size)
			{
				IdValuePair val1 = array[offset + new_i1];
				if (new_i2 != size)
				{
					IdValuePair val2 = array[offset + new_i2];
					if (Bigger(val2, val1, comparator))
					{
						val1 = val2;
						new_i1 = new_i2;
					}
					if (new_i3 != size)
					{
						IdValuePair val3 = array[offset + new_i3];
						if (Bigger(val3, val1, comparator))
						{
							val1 = val3;
							new_i1 = new_i3;
						}
					}
				}
				if (Bigger(val1, i_val, comparator))
				{
					array[offset + i] = val1;
					array[offset + new_i1] = i_val;
				}
			}
		}
Example #5
0
		private static void Heap4Sort(IdValuePair[] array, int offset, int size, int comparator)
		{
			if (size <= 1)
			{
				return;
			}
			MakeHeap4(array, offset, size, comparator);
			while (size > 1)
			{
				--size;
				IdValuePair v1 = array[offset + size];
				IdValuePair v2 = array[offset + 0];
				array[offset + size] = v2;
				array[offset + 0] = v1;
				Heapify4(array, offset, size, 0, comparator);
			}
		}
Example #6
0
		private static bool Bigger(IdValuePair a, IdValuePair b, int comparator)
		{
			if (comparator < 0)
			{
				// For length selection switch it is enough to compare just length,
				// but to detect same strings full comparison is essential
				//return a.idLength > b.idLength;
				int diff = a.idLength - b.idLength;
				if (diff != 0)
				{
					return diff > 0;
				}
				return string.CompareOrdinal(a.id, b.id) > 0;
			}
			else
			{
				return a.id[comparator] > b.id[comparator];
			}
		}
Example #7
0
		private EvaluatorException On_same_pair_fail(IdValuePair a, IdValuePair b)
		{
			int line1 = a.GetLineNumber();
			int line2 = b.GetLineNumber();
			if (line2 > line1)
			{
				int tmp = line1;
				line1 = line2;
				line2 = tmp;
			}
			string error_text = ToolErrorReporter.GetMessage("msg.idswitch.same_string", a.id, line2);
			return R.RuntimeError(error_text, source_file, line1, null, 0);
		}
Example #8
0
		private void Add_id(char[] array, int id_start, int id_end, int name_start, int name_end)
		{
			string name = new string(array, name_start, name_end - name_start);
			string value = new string(array, id_start, id_end - id_start);
			IdValuePair pair = new IdValuePair(name, value);
			pair.SetLineNumber(body.GetLineNumber());
			all_pairs.Add(pair);
		}
Example #9
0
		private void Generate_java_code()
		{
			P.Clear();
			IdValuePair[] pairs = new IdValuePair[all_pairs.Count];
			Sharpen.Collections.ToArray(all_pairs, pairs);
			SwitchGenerator g = new SwitchGenerator();
			g.char_tail_test_threshold = 2;
			g.SetReporter(R);
			g.SetCodePrinter(P);
			g.GenerateSwitch(pairs, "0");
		}