/// <summary> /// Retrieves all intervals which enclose the search interval and adds them to the given list. /// </summary> /// <example> /// Given the following intervals [0]-[4] and the search interval [x]: /// [0] |--------| /// [1] |----------------| /// [2] |---------| /// [3] |----| /// [4] |-------| /// [x] |---------| /// [1], [2] would be included in the result set. [0], [3], [4] would not be included. /// </example> /// <param name="start">The lower boundary of the search interval.</param> /// <param name="end">The upper bound of the search interval.</param> /// <param name="list">A list to which resulting intervals can be added. Items are added in no particular order!</param> public void GetEnclosing(T start, T end, List <IInterval <T> > list) { if (Left != null && Left.IntervalMax.CompareTo(start) >= 0 && Left.IntervalMax.CompareTo(end) >= 0) { Left.GetEnclosing(start, end, list); } if (Right != null && Right.IntervalMin.CompareTo(end) <= 0 && Right.IntervalMin.CompareTo(start) <= 0) { Right.GetEnclosing(start, end, list); } if (OverlappingByStart is null || OverlappingMin.CompareTo(start) > 0 || OverlappingMax.CompareTo(end) < 0) { return; } list.AddRange(OverlappingByStart.Where(i => i.Start.CompareTo(start) <= 0 && i.End.CompareTo(end) >= 0)); }
/// <summary> /// Retrieves all intervals which overlap with the search interval (including just touching the start or end points) /// and adds them to the given list. /// </summary> /// <example> /// Given the following intervals [0]-[6] and the search interval [x]: /// [0] |--| /// [1] |--| /// [2] |----| /// [3] |----| /// [4] |-------| /// [5] |----| /// [6] |-----| /// [x] |---------| /// [1], [2], [3], [4], [5] would be included in the result set. [0], [6] would not be included. /// </example> /// <param name="start">The lower boundary of the search interval.</param> /// <param name="end">The upper bound of the search interval.</param> /// <param name="list">A list to which resulting intervals can be added. Items are added in no particular order!</param> public void GetOverlapping(T start, T end, List <IInterval <T> > list) { if (Left != null && Left.IntervalMax.CompareTo(start) >= 0) { Left.GetOverlapping(start, end, list); } if (Right != null && Right.IntervalMin.CompareTo(end) <= 0) { Right.GetOverlapping(start, end, list); } if (OverlappingByStart is null || OverlappingByEnd is null || OverlappingMax.CompareTo(start) < 0 || OverlappingMin.CompareTo(end) > 0) { return; } if (OverlappingMin.CompareTo(start) >= 0) { var index = 0; while (index < OverlappingByStart.Count && OverlappingByStart[index].Start.CompareTo(end) <= 0) { index++; } list.AddRange(OverlappingByStart.GetRange(0, index)); } else if (OverlappingMax.CompareTo(end) <= 0) { var index = OverlappingByEnd.Count - 1; while (index >= 0 && OverlappingByEnd[index].End.CompareTo(start) >= 0) { index--; } index++; list.AddRange(OverlappingByEnd.GetRange(index, OverlappingByEnd.Count - index)); } else { list.AddRange(OverlappingByStart.Where(i => i.End.CompareTo(start) >= 0 && i.Start.CompareTo(end) <= 0)); } }