/// <summary> /// Calculate the difference between two ranges, i.e. X - Y. /// </summary> /// <param name="x">The first range to subtract from.</param> /// <param name="y">The second range to subtract with.</param> /// <returns>Between 0 and 2 remaining ranges.</returns> public static FInt32Range[] Difference(FInt32Range x, FInt32Range y) { if (x.Overlaps(y)) { FInt32Range lowerRange = new FInt32Range(x.LowerBound, FInt32RangeBound.FlipInclusion(y.LowerBound)); FInt32Range upperRange = new FInt32Range(FInt32RangeBound.FlipInclusion(y.UpperBound), x.UpperBound); if (!lowerRange.IsEmpty()) { if (!upperRange.IsEmpty()) { return(new FInt32Range[] { lowerRange, upperRange }); } else { return(new FInt32Range[] { lowerRange }); } } if (!upperRange.IsEmpty()) { return(new FInt32Range[] { upperRange }); } return(new FInt32Range[0]); } else { return(new FInt32Range[] { x }); } }
/// <summary> /// Return the union of two contiguous ranges. /// /// A union is a range or series of ranges that contains both ranges. /// </summary> /// <param name="x">The first range.</param> /// <param name="y">The second range.</param> /// <returns>The union, or both ranges if the two ranges are not contiguous, or no ranges if both ranges are empty.</returns> public static FInt32Range[] Union(FInt32Range x, FInt32Range y) { if (x.Contains(y)) { return(new FInt32Range[] { new FInt32Range( FInt32RangeBound.MinLower(x.LowerBound, y.LowerBound), FInt32RangeBound.MaxUpper(x.UpperBound, y.UpperBound)) }); } else { if (!x.IsEmpty()) { if (!y.IsEmpty()) { return(new FInt32Range[] { x, y }); } else { return(new FInt32Range[] { x }); } } if (!y.IsEmpty()) { return(new FInt32Range[] { y }); } return(new FInt32Range[0]); } }
/// <summary> /// Split the range into two ranges at the specified element. /// /// If a range [A, C) does not contain the element X, the original range is returned. /// Otherwise the range is split into two ranges [A, X) and [X, C), each of which may be empty. /// </summary> /// <param name="element">The element at which to split the range.</param> /// <returns></returns> public FInt32Range[] Split(int element) { if (Contains(element)) { return(new FInt32Range[] { new FInt32Range(LowerBound, FInt32RangeBound.Exclusive(element)), new FInt32Range(FInt32RangeBound.Inclusive(element), UpperBound) }); } else { return(new FInt32Range[] { this }); } }
/// <summary> /// Compute the intersection of two ranges. /// /// The intersection of two ranges is the largest range that is contained by both ranges. /// </summary> /// <param name="x">The first range.</param> /// <param name="y">The second range.</param> /// <returns>The intersection, or an empty range if the ranges do not overlap.</returns> public static FInt32Range Intersection(FInt32Range x, FInt32Range y) { if (x.IsEmpty()) { return(FInt32Range.Empty()); } if (y.IsEmpty()) { return(FInt32Range.Empty()); } return(new FInt32Range( FInt32RangeBound.MaxLower(x.LowerBound, y.LowerBound), FInt32RangeBound.MinUpper(x.UpperBound, y.UpperBound))); }
/// <summary> /// Compute the hull of two ranges. /// /// The hull is the smallest range that contains both ranges. /// </summary> /// <param name="x">The first range.</param> /// <param name="y">The second range.</param> /// <returns>The hull.</returns> public static FInt32Range Hull(FInt32Range x, FInt32Range y) { if (x.IsEmpty()) { return(y); } if (y.IsEmpty()) { return(x); } return(new FInt32Range( FInt32RangeBound.MinLower(x.LowerBound, y.LowerBound), FInt32RangeBound.MaxUpper(x.UpperBound, y.UpperBound))); }
/// <summary> /// Create a right-bounded range that contains all elements less than the specified value. /// </summary> /// <param name="value">The value.</param> /// <returns>A new range.</returns> public static FInt32Range LessThan(int value) { return(new FInt32Range(FInt32RangeBound.Open(), FInt32RangeBound.Exclusive(value))); }
/// <summary> /// Create a range with a single element. /// /// The created range is of the form [A, A]. /// </summary> /// <param name="a">The element in the range.</param> public FInt32Range(int a) { LowerBound = FInt32RangeBound.Inclusive(a); UpperBound = FInt32RangeBound.Inclusive(a); }
/// <summary> /// Create a range that includes the given minimum and maximum values. /// </summary> /// <param name="min">The minimum value to be included.</param> /// <param name="max">The maximum value to be included.</param> /// <returns>A new range.</returns> public static FInt32Range Inclusive(int min, int max) { return(new FInt32Range(FInt32RangeBound.Inclusive(min), FInt32RangeBound.Inclusive(max))); }
/// <summary> /// Return an empty range. /// </summary> /// <returns>Empty range.</returns> public static FInt32Range Empty() { return(new FInt32Range(FInt32RangeBound.Exclusive(default(int)), FInt32RangeBound.Exclusive(default(int)))); }
/// <summary> /// Create a right-bounded range that contains all elements less than or equal to the specified value. /// </summary> /// <param name="value">The value.</param> /// <returns>A new range.</returns> public static FInt32Range AtMost(int value) { return(new FInt32Range(FInt32RangeBound.Open(), FInt32RangeBound.Inclusive(value))); }
/// <summary> /// Create an unbounded (open) range that contains all elements of the domain. /// </summary> /// <returns>A new range.</returns> public static FInt32Range All() { return(new FInt32Range(FInt32RangeBound.Open(), FInt32RangeBound.Open())); }
/// <summary> /// Check whether this range contains another range. /// </summary> /// <param name="other">The range to check.</param> /// <returns>true if the range contains the other range, false otherwise.</returns> public bool Contains(FInt32Range other) { return((FInt32RangeBound.MinLower(LowerBound, other.LowerBound) == LowerBound) && (FInt32RangeBound.MaxUpper(UpperBound, other.UpperBound) == UpperBound)); }
/// <summary> /// Assign the new upper bound for this range /// </summary> /// <param name="newUpperBound">The new upper bound to assign</param> public void SetUpperBound(FInt32RangeBound newUpperBound) { UpperBound = newUpperBound; }
/// <summary> /// Assign the new lower bound for this range /// </summary> /// <param name="newLowerBound">The new lower bound to assign</param> public void SetLowerBound(FInt32RangeBound newLowerBound) { LowerBound = newLowerBound; }
/// <summary> /// Create and initializes a new range with the given lower and upper bounds. /// /// The created range is of the form [A, B). /// </summary> /// <param name="a">The range's lower bound value (inclusive).</param> /// <param name="b">The range's upper bound value (exclusive).</param> public FInt32Range(int a, int b) { LowerBound = FInt32RangeBound.Inclusive(a); UpperBound = FInt32RangeBound.Exclusive(b); }
/// <summary> /// Create and initializes a new range with the given lower and upper bounds. /// /// The created range is of the form [A, B). /// </summary> /// <param name="lowerBound">The range's lower bound value (inclusive).</param> /// <param name="upperBound">The range's upper bound value (exclusive).</param> public FInt32Range(FInt32RangeBound lowerBound, FInt32RangeBound upperBound) { LowerBound = lowerBound; UpperBound = upperBound; }
/// <summary> /// Check whether this range contains the specified element. /// </summary> /// <param name="element">The element to check.</param> /// <returns>true if the range contains the element, false otherwise.</returns> public bool Contains(int element) { return((FInt32RangeBound.MinLower(LowerBound, new FInt32RangeBound(element)) == LowerBound) && (FInt32RangeBound.MaxUpper(UpperBound, new FInt32RangeBound(element)) == UpperBound)); }