public bool Intersection(RangeElement other, out RangeElement intersection) { intersection = null; if (other == null) { return(false); } // before if (Begin.Begin.CompareTo(other.End.End) > 0) { return(false); } // after if (End.End.CompareTo(other.Begin.Begin) < 0) { return(false); } string begin = (Begin.Begin.CompareTo(other.Begin.Begin) < 0) ? other.Begin.Begin : Begin.Begin; string end = (End.End.CompareTo(other.End.End) > 0) ? other.End.End : End.End; intersection = new RangeElement(begin, end); return(true); }
public bool Equals(RangeElement other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return(Equals(other.Begin, Begin) && Equals(other.End, End)); }
public static IEnumerable <IRangeElement> RestrictTo(this IEnumerable <IRangeElement> requested, IEnumerable <IRangeElement> restriction) { int requestedCount = 0; foreach (IRangeElement element in requested) { requestedCount++; if (!restriction.Includes(element)) { var range = element as RangeElement; if (range != null) { RangeElement intersection = null; restriction .Where(x => x is RangeElement) .Where(x => ((RangeElement)x).Intersection(range, out intersection)) .FirstOrDefault(); if (intersection != null) { yield return(intersection); continue; } var includedResults = restriction .Where(x => x is StartsWithElement) .Where(range.Includes); foreach (IRangeElement includedResult in includedResults) { yield return(includedResult); } } continue; } yield return(element); } if (requestedCount == 0) { foreach (IRangeElement element in restriction) { yield return(element); } } }
public bool Union(RangeElement other, out RangeElement combined) { combined = null; if (other == null) { return(false); } if (Begin.Begin.CompareTo(other.Begin.Begin) <= 0 && End.End.CompareTo(other.Begin.Begin) >= 0) { combined = new RangeElement(Begin.Begin, (End.End.CompareTo(other.End.End) >= 0) ? End.End : other.End.End); return(true); } if (End.End.CompareTo(other.End.End) >= 0 && Begin.Begin.CompareTo(other.End.End) <= 0) { combined = new RangeElement((Begin.Begin.CompareTo(other.Begin.Begin) <= 0) ? Begin.Begin : other.Begin.Begin, End.End); return(true); } return(false); }