Exemplo n.º 1
0
        /// <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 FFloatRange[] Difference(FFloatRange x, FFloatRange y)
        {
            if (x.Overlaps(y))
            {
                FFloatRange lowerRange = new FFloatRange(x.LowerBound, FFloatRangeBound.FlipInclusion(y.LowerBound));
                FFloatRange upperRange = new FFloatRange(FFloatRangeBound.FlipInclusion(y.UpperBound), x.UpperBound);

                if (!lowerRange.IsEmpty())
                {
                    if (!upperRange.IsEmpty())
                    {
                        return(new FFloatRange[] { lowerRange, upperRange });
                    }
                    else
                    {
                        return(new FFloatRange[] { lowerRange });
                    }
                }

                if (!upperRange.IsEmpty())
                {
                    return(new FFloatRange[] { upperRange });
                }

                return(new FFloatRange[0]);
            }
            else
            {
                return(new FFloatRange[] { x });
            }
        }
Exemplo n.º 2
0
        /// <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 FFloatRange[] Union(FFloatRange x, FFloatRange y)
        {
            if (x.Contains(y))
            {
                return(new FFloatRange[]
                {
                    new FFloatRange(
                        FFloatRangeBound.MinLower(x.LowerBound, y.LowerBound),
                        FFloatRangeBound.MaxUpper(x.UpperBound, y.UpperBound))
                });
            }
            else
            {
                if (!x.IsEmpty())
                {
                    if (!y.IsEmpty())
                    {
                        return(new FFloatRange[] { x, y });
                    }
                    else
                    {
                        return(new FFloatRange[] { x });
                    }
                }

                if (!y.IsEmpty())
                {
                    return(new FFloatRange[] { y });
                }

                return(new FFloatRange[0]);
            }
        }
Exemplo n.º 3
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        FFloatRange[] Split(float element)
 {
     if (Contains(element))
     {
         return(new FFloatRange[]
         {
             new FFloatRange(LowerBound, FFloatRangeBound.Exclusive(element)),
             new FFloatRange(FFloatRangeBound.Inclusive(element), UpperBound)
         });
     }
     else
     {
         return(new FFloatRange[] { this });
     }
 }
Exemplo n.º 4
0
        /// <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 FFloatRange Intersection(FFloatRange x, FFloatRange y)
        {
            if (x.IsEmpty())
            {
                return(FFloatRange.Empty());
            }

            if (y.IsEmpty())
            {
                return(FFloatRange.Empty());
            }

            return(new FFloatRange(
                       FFloatRangeBound.MaxLower(x.LowerBound, y.LowerBound),
                       FFloatRangeBound.MinUpper(x.UpperBound, y.UpperBound)));
        }
Exemplo n.º 5
0
        /// <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 FFloatRange Hull(FFloatRange x, FFloatRange y)
        {
            if (x.IsEmpty())
            {
                return(y);
            }

            if (y.IsEmpty())
            {
                return(x);
            }

            return(new FFloatRange(
                       FFloatRangeBound.MinLower(x.LowerBound, y.LowerBound),
                       FFloatRangeBound.MaxUpper(x.UpperBound, y.UpperBound)));
        }
Exemplo n.º 6
0
 /// <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 FFloatRange LessThan(float value)
 {
     return(new FFloatRange(FFloatRangeBound.Open(), FFloatRangeBound.Exclusive(value)));
 }
Exemplo n.º 7
0
 /// <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 FFloatRange(float a)
 {
     LowerBound = FFloatRangeBound.Inclusive(a);
     UpperBound = FFloatRangeBound.Inclusive(a);
 }
Exemplo n.º 8
0
 /// <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 FFloatRange Inclusive(float min, float max)
 {
     return(new FFloatRange(FFloatRangeBound.Inclusive(min), FFloatRangeBound.Inclusive(max)));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Return an empty range.
 /// </summary>
 /// <returns>Empty range.</returns>
 public static FFloatRange Empty()
 {
     return(new FFloatRange(FFloatRangeBound.Exclusive(default(float)), FFloatRangeBound.Exclusive(default(float))));
 }
Exemplo n.º 10
0
 /// <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 FFloatRange AtMost(float value)
 {
     return(new FFloatRange(FFloatRangeBound.Open(), FFloatRangeBound.Inclusive(value)));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Create an unbounded (open) range that contains all elements of the domain.
 /// </summary>
 /// <returns>A new range.</returns>
 public static FFloatRange All()
 {
     return(new FFloatRange(FFloatRangeBound.Open(), FFloatRangeBound.Open()));
 }
Exemplo n.º 12
0
 /// <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(FFloatRange other)
 {
     return((FFloatRangeBound.MinLower(LowerBound, other.LowerBound) == LowerBound) &&
            (FFloatRangeBound.MaxUpper(UpperBound, other.UpperBound) == UpperBound));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Assign the new upper bound for this range
 /// </summary>
 /// <param name="newUpperBound">The new upper bound to assign</param>
 public void SetUpperBound(FFloatRangeBound newUpperBound)
 {
     UpperBound = newUpperBound;
 }
Exemplo n.º 14
0
 /// <summary>
 /// Assign the new lower bound for this range
 /// </summary>
 /// <param name="newLowerBound">The new lower bound to assign</param>
 public void SetLowerBound(FFloatRangeBound newLowerBound)
 {
     LowerBound = newLowerBound;
 }
Exemplo n.º 15
0
 /// <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 FFloatRange(float a, float b)
 {
     LowerBound = FFloatRangeBound.Inclusive(a);
     UpperBound = FFloatRangeBound.Exclusive(b);
 }
Exemplo n.º 16
0
 /// <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 FFloatRange(FFloatRangeBound lowerBound, FFloatRangeBound upperBound)
 {
     LowerBound = lowerBound;
     UpperBound = upperBound;
 }
Exemplo n.º 17
0
 /// <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(float element)
 {
     return((FFloatRangeBound.MinLower(LowerBound, new FFloatRangeBound(element)) == LowerBound) &&
            (FFloatRangeBound.MaxUpper(UpperBound, new FFloatRangeBound(element)) == UpperBound));
 }