/// <summary> /// Gets all objects that are ranked higher than this one by extrapolating transitive ranks. /// </summary> /// <param name="result"></param> /// <returns></returns> public IEnumerable <T> FetchTransitiveHigherRankedObjects(AbstractComparisonResult result) { Stack <T> entitiesToFlatten = new Stack <T>(_higherRankedObjects); while (entitiesToFlatten.TryPop(out T entity)) { yield return(entity); foreach (var nextEntity in entity._higherRankedObjects.Where(ne => ne != this)) { entitiesToFlatten.Push(nextEntity); } } }
public virtual AbstractComparisonResult AbstractCompareTo(T other) { if (typeof(T) != GetType()) { throw new InvalidOperationException($"Invalid type, comparison objects must be of the same type."); } AbstractComparisonResult result = new AbstractComparisonResult(0, false); if (other == this) { result = new AbstractComparisonResult(0, true); } else if (_higherRankedObjects.Contains(other)) { result = new AbstractComparisonResult(-1, true); } else if (other._higherRankedObjects.Contains((T)this)) { result = new AbstractComparisonResult(1, true); } if (!result.ComparisonSucceeded) { var higherRankedObjects = FetchTransitiveHigherRankedObjects(result); if (higherRankedObjects.Any(o => o == other)) { result = new AbstractComparisonResult(-1, true); } } if (!result.ComparisonSucceeded) { var otherHigherRankedObjects = other.FetchTransitiveHigherRankedObjects(result); if (otherHigherRankedObjects.Any(o => o == this)) { result = new AbstractComparisonResult(1, true); } } return(result); }