internal static IntervalCollection <T> LowUnionOf <T>(IntervalCollection <T> collection) { if (collection == null) { throw new ArgumentNullException("collection"); } if (collection.Count == 0) { return(collection); } var sortedCol = new List <Interval <T> >(collection); sortedCol.Sort(); var result = new List <Interval <T> > { sortedCol[0] }; for (int i = 1; i < sortedCol.Count; i++) { var interval = sortedCol[i]; var col = new List <Interval <T> >(result); result = new List <Interval <T> >(sortedCol.Count); foreach (var item in col) { var u = Interval.LowUnionOf(item, interval); if (u.Count != 1) { result.AddRange(u); } else { result.RemoveAll(match => match == item || match == interval); result.AddRange(u); interval = u[0]; } } result = new List <Interval <T> >(result.Distinct()); } result.Sort(); return(new IntervalCollection <T>(result)); }
/// <summary> /// Gets the union of the current <see cref="T:IntervalCollection`1"/> according to the specified <paramref name="denominator"/>. /// </summary> /// <param name="denominator">The denominator considered while performing the union.</param> /// <value>The union.</value> /// <remarks> /// While performing an <see cref="Interval.UnionOf<T>">UnionOf</see> intervals, depending of the specified <see cref="UnionDenominator"/>, the result will be different. /// If you consider the following intervals: /// <list type="table"> /// <item> /// <term>a</term> /// <description>]∞ ; 3[</description> /// </item> /// <item> /// <term>b</term> /// <description>]0 ; 5[</description> /// </item> /// <item> /// <term>c</term> /// <description>[1 ; 2]</description> /// </item> /// </list> /// The union with the <see cref="UnionDenominator.Highest"/> denominator will return an <see cref="IntervalCollection<T>"/> of 1 element as shown below: /// <list type="table"> /// <item> /// <term>i</term> /// <description>]∞ ; 5[</description> /// </item> /// </list> /// The union with the <see cref="UnionDenominator.Lowest"/> denominator will return an <see cref="IntervalCollection<T>"/> of 5 element as shown below: /// <list type="table"> /// <item> /// <term>i</term> /// <description>]∞ ; 0]</description> /// </item> /// <item> /// <term>j</term> /// <description>]0 ; 1[</description> /// </item> /// <item> /// <term>k</term> /// <description>[1 ; 2]</description> /// </item> /// <item> /// <term>l</term> /// <description>]2 ; 3[</description> /// </item> /// <item> /// <term>m</term> /// <description>[3 ; 5[</description> /// </item> /// </list> /// </remarks> public IntervalCollection <T> Union(UnionDenominator denominator) { return((denominator == UnionDenominator.Highest) ? Interval.HighUnionOf(this) : Interval.LowUnionOf(this)); }