public virtual void generateSwitch(string[] pairs, string default_value) { int N = pairs.Length / 2; Pair[] id_pairs = new Pair[N]; for (int i = 0; i != N; ++i) { id_pairs[i] = new Pair(pairs[2 * i], pairs[2 * i + 1]); } generateSwitch(id_pairs, default_value); }
public virtual void generateSwitch(Pair[] 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); }
public ArrayList Read(string fileName) { ArrayList pairGroups = new ArrayList(); string content = string.Empty; using (StreamReader sr = new StreamReader(fileName)) { content = sr.ReadToEnd(); } foreach (Match match in RegExpRegion.Matches(content)) { PairGroup pg = new PairGroup(); pg.Type = match.Groups[1].Value; foreach (Match mPair in RegExpPair.Matches(match.Groups[2].Value)) { string id = mPair.Groups[1].Value; Pair pair = new Pair (id, id.Substring (3)); switch (pair.Value) { case "STAR": pair.Value = "$*"; break; case "UNDERSCORE": pair.Value = "$_"; break; case "AMPERSAND": pair.Value = "$&"; break; case "PLUS": pair.Value = "$+"; break; case "BACKQUOTE": pair.Value = "$`"; break; case "QUOTE": pair.Value = "$'"; break; case "DOLLAR_0": pair.Value = "$0"; break; case "DOLLAR_1": pair.Value = "$1"; break; case "DOLLAR_2": pair.Value = "$2"; break; case "DOLLAR_3": pair.Value = "$3"; break; case "DOLLAR_4": pair.Value = "$4"; break; case "DOLLAR_5": pair.Value = "$5"; break; case "DOLLAR_6": pair.Value = "$6"; break; case "DOLLAR_7": pair.Value = "$7"; break; case "DOLLAR_8": pair.Value = "$8"; break; case "DOLLAR_9": pair.Value = "$9"; break; } pg.Pairs.Add(pair); } pairGroups.Add(pg); if (pg.Pairs.Count == 0) { Console.WriteLine("WARN: Found PairGroup (" + pg.Type + ") but no pairs."); } } return pairGroups; }
private static void heapify4(Pair[] array, int offset, int size, int i, int comparator) { int new_i1, new_i2, new_i3; Pair i_val = array[offset + i]; for (; ; ) { int base_Renamed = (i << 2); new_i1 = base_Renamed | 1; new_i2 = base_Renamed | 2; new_i3 = base_Renamed | 3; int new_i4 = base_Renamed + 4; if (new_i4 >= size) { break; } Pair val1 = array[offset + new_i1]; Pair val2 = array[offset + new_i2]; Pair val3 = array[offset + new_i3]; Pair 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) { Pair val1 = array[offset + new_i1]; if (new_i2 != size) { Pair val2 = array[offset + new_i2]; if (bigger(val2, val1, comparator)) { val1 = val2; new_i1 = new_i2; } if (new_i3 != size) { Pair 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; } } }
private static void makeHeap4(Pair[] array, int offset, int size, int comparator) { for (int i = ((size + 2) >> 2); i != 0; ) { --i; heapify4(array, offset, size, i, comparator); } }
private static void heap4Sort(Pair[] array, int offset, int size, int comparator) { if (size <= 1) { return ; } makeHeap4(array, offset, size, comparator); while (size > 1) { --size; Pair v1 = array[offset + size]; Pair v2 = array[offset + 0]; array[offset + size] = v2; array[offset + 0] = v1; heapify4(array, offset, size, 0, comparator); } }
private static bool bigger(Pair a, Pair 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.Id.Length > b.Id.Length; int diff = a.Value.Length - b.Value.Length; if (diff != 0) { return diff > 0; } return String.CompareOrdinal(a.Value, b.Value) > 0; } else { return a.Value[comparator] > b.Value[comparator]; } }