using System.Collections.Generic;
interface ICanCompare<T> where T : ICanCompare<T>
Self_CompareResult CompareTo (T b);
public static class CompareHelper
static CompareResult ToCompareResult_A (this Self_CompareResult a) => a switch
Self_CompareResult.Equal => CompareResult.Equal,
Self_CompareResult.Unknown => CompareResult.Unknown,
Self_CompareResult.ThisIsGreaterThan => CompareResult.AisLargest,
Self_CompareResult.ThisIsLessThan => CompareResult.BisLargest,
_ => throw new NotImplementedException()
static CompareResult ToCompareResult_B (this Self_CompareResult b) => b switch
Self_CompareResult.Equal => CompareResult.Equal,
Self_CompareResult.Unknown => CompareResult.Unknown,
Self_CompareResult.ThisIsGreaterThan => CompareResult.BisLargest,
Self_CompareResult.ThisIsLessThan => CompareResult.AisLargest,
_ => throw new NotImplementedException()
static CompareResult Compare<T> (T a, T b) where T : ICanCompare<T>
var cra = a.CompareTo(b).ToCompareResult_A();
var crb = b.CompareTo(a).ToCompareResult_B();
if (cra == crb) return cra;
if (cra == CompareResult.Unknown) return crb;
if (crb == CompareResult.Unknown) return cra;
throw new Exception("Inconsistent comparison found!!!");
static T Largest<T> (this IEnumerable<T> comparables) where T : ICanCompare<T>
var comparablesList = comparables.ToList();
List<T> largestFound = new List<T> { comparablesList[0] };
for (int n = 1; n < comparablesList.Count; n++)
var item = comparablesList[n];
var compareResult = Compare(item, largestFound[0]);
case CompareResult.AisLargest:
case CompareResult.BisLargest:
case CompareResult.Equal:
case CompareResult.Unknown:
throw new NotImplementedException();
throw new NotImplementedException();
if (largestFound.Count != 1) throw new Exception("Ambiguous max!!!");