public override int Compare(int arity, MultiCandidate other_) { bool any_better = false, any_worse = false, any_diff = false; OverloadCandidate other = (OverloadCandidate)other_; for (int ix = 0; ix < arity; ix++) { Type t1 = ix >= args.Length ? param_array : args[ix]; Type t2 = ix >= other.args.Length ? other.param_array : other.args[ix]; int res = CompareType(t1, t2); if (t1 != t2) { any_diff = true; } if (res > 0) { any_better = true; } if (res < 0) { any_worse = true; } } if (any_better && !any_worse) { return(1); } if (any_worse && !any_better) { return(-1); } if (!any_diff) { if (param_array == null && other.param_array != null) { return(1); } if (param_array != null && other.param_array == null) { return(-1); } } return(0); }
public abstract int Compare(int arity, MultiCandidate other);
MultiCandidate[] SortCandidates(int arity) { List <MultiCandidate> afilt = new List <MultiCandidate>(); foreach (MultiCandidate mc in orig) { if (mc.AdmissableArity(arity)) { afilt.Add(mc); } } int n = afilt.Count; bool[] gt = new bool[n * n]; int[] blocks = new int[n]; // # of unused elements less than i int[] reorder = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { int comp = afilt[i].Compare(arity, afilt[j]); if (CLROpts.MMDDebug && comp != 0) { Console.WriteLine("{0} {1} {2}", afilt[i], (comp > 0 ? '>' : '<'), afilt[j]); } if (comp > 0) // $C_i > C_j$ { gt[i * n + j] = true; blocks[i]++; } else if (comp < 0) { gt[j * n + i] = true; blocks[j]++; } } } int assigned = 0; while (assigned != n) { int i; for (i = 0; i < n; i++) { if (blocks[i] == 0) { break; } } reorder[assigned++] = i; for (int j = 0; j < n; j++) { if (gt[j * n + i]) { blocks[j]--; } } blocks[i] = int.MaxValue; } MultiCandidate[] ret = new MultiCandidate[n]; for (int i = 0; i < n; i++) { ret[i] = afilt[reorder[i]]; List <int> conflicts = new List <int>(); for (int j = 0; j < i; j++) { if (!gt[reorder[i] * n + reorder[j]]) { conflicts.Add(j); } } ret[i].conflictors = conflicts.ToArray(); } if (CLROpts.MMDDebug) { Console.WriteLine("--- MMD CANDIDATE SORT ORDER ---"); for (int i = 0; i < n; i++) { Console.WriteLine("{0}: {1}", i, ret[i]); Console.WriteLine(" c: {0}", Kernel.JoinS(" ", ret[i].conflictors)); } } return(ret); }