Exemplo n.º 1
0
        private static IntervalCollection <T> HighUnionOf <T>(Interval <T> a, Interval <T> b)
        {
            if (Interval.IsNullOrEmpty(a))
            {
                return(new IntervalCollection <T>(b));
            }

            if (Interval.IsNullOrEmpty(b))
            {
                return(new IntervalCollection <T>(a));
            }

            var lowestBound = Bound.Min(a.LowerBound, b.LowerBound);
            var upmostValue = Bound.Max(a.UpperBound, b.UpperBound);

            var lowerBound = default(Bound <T>);
            var upperBound = default(Bound <T>);

            //      a
            //-------------
            //      b
            //  ---------
            if (object.Equals(lowestBound, a.LowerBound) &&
                object.Equals(upmostValue, a.UpperBound))
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Max(a.LowerBound.Direction, b.LowerBound.Direction)
                        : a.LowerBound.Direction
                                           , a.LowerBound.Value);
                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Max(a.UpperBound.Direction, b.UpperBound.Direction)
                        : a.UpperBound.Direction
                                           , a.UpperBound.Value);
            }

            //      a
            //  ---------
            //      b
            //-------------
            else if (object.Equals(lowestBound, b.LowerBound) &&
                     object.Equals(upmostValue, b.UpperBound))
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Max(a.LowerBound.Direction, b.LowerBound.Direction)
                        : b.LowerBound.Direction
                                           , b.LowerBound.Value);
                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Max(a.UpperBound.Direction, b.UpperBound.Direction)
                        : b.UpperBound.Direction
                                           , b.UpperBound.Value);
            }

            //    a
            //---------
            //        b
            //  -------------
            else if (object.Equals(lowestBound, a.LowerBound) &&
                     object.Equals(upmostValue, b.UpperBound) &&
                     a.UpperBound.UnionCompareTo(b.LowerBound) >= 0)
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Max(a.LowerBound.Direction, b.LowerBound.Direction)
                        : a.LowerBound.Direction
                                           , a.LowerBound.Value);

                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Max(a.UpperBound.Direction, b.UpperBound.Direction)
                        : b.UpperBound.Direction
                                           , b.UpperBound.Value);
            }

            //        a
            //  -------------
            //    b
            //---------
            else if (object.Equals(lowestBound, b.LowerBound) &&
                     object.Equals(upmostValue, a.UpperBound) &&
                     b.UpperBound.UnionCompareTo(a.LowerBound) >= 0)
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Max(a.LowerBound.Direction, b.LowerBound.Direction)
                        : b.LowerBound.Direction
                                           , b.LowerBound.Value);

                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Max(a.UpperBound.Direction, b.UpperBound.Direction)
                        : a.UpperBound.Direction
                                           , a.UpperBound.Value);
            }
            else
            {
                return(new IntervalCollection <T>(a, b));
            }

            Interval <T> result;

            return(TryParse(lowerBound, upperBound, out result)
                ? new IntervalCollection <T>(result)
                : new IntervalCollection <T>(a, b));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines the intersection of two intervals.
        /// </summary>
        /// <typeparam name="T">The value type of the intervals.</typeparam>
        /// <param name="a">The first interval.</param>
        /// <param name="b">The second interval.</param>
        /// <returns>An <see cref="Interval&lt;T&gt;"/> that contains all elements of <paramref name="a"/>
        /// that also belong to <paramref name="b"/> (or equivalently, all elements of <paramref name="b"/>
        /// that also belong to <paramref name="a"/>), but no other elements.</returns>
        /// <seealso href="http://en.wikipedia.org/wiki/Intersection_(set_theory)">Intersection (set theory)</seealso>
        private static Interval <T> IntersectionOf <T>(Interval <T> a, Interval <T> b)
        {
            if (Interval.IsNullOrEmpty(a))
            {
                return(a);
            }
            if (Interval.IsNullOrEmpty(b))
            {
                return(b);
            }

            var lowestBound = Bound.Min(a.LowerBound, b.LowerBound);
            var upmostValue = Bound.Max(a.UpperBound, b.UpperBound);

            var lowerBound = default(Bound <T>);
            var upperBound = default(Bound <T>);

            //      a
            //-------------
            //      b
            //  ---------
            if (object.Equals(lowestBound, a.LowerBound) &&
                object.Equals(upmostValue, a.UpperBound))
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Min(a.LowerBound.Direction, b.LowerBound.Direction)
                        : b.LowerBound.Direction
                                           , b.LowerBound.Value);
                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Min(a.UpperBound.Direction, b.UpperBound.Direction)
                        : b.UpperBound.Direction
                                           , b.UpperBound.Value);
            }

            //      a
            //  ---------
            //      b
            //-------------
            else if (object.Equals(lowestBound, b.LowerBound) &&
                     object.Equals(upmostValue, b.UpperBound))
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Min(a.LowerBound.Direction, b.LowerBound.Direction)
                        : a.LowerBound.Direction
                                           , a.LowerBound.Value);
                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Min(a.UpperBound.Direction, b.UpperBound.Direction)
                        : a.UpperBound.Direction
                                           , a.UpperBound.Value);
            }

            //    a
            //---------
            //        b
            //  -------------
            else if (object.Equals(lowestBound, a.LowerBound) &&
                     object.Equals(upmostValue, b.UpperBound))
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Min(a.LowerBound.Direction, b.LowerBound.Direction)
                        : b.LowerBound.Direction
                                           , b.LowerBound.Value);

                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Min(a.UpperBound.Direction, b.UpperBound.Direction)
                        : a.UpperBound.Direction
                                           , a.UpperBound.Value);
            }

            //        a
            //  -------------
            //    b
            //---------
            else if (object.Equals(lowestBound, b.LowerBound) &&
                     object.Equals(upmostValue, a.UpperBound))
            {
                lowerBound = new Bound <T>(BoundType.Lower
                                           , object.Equals(a.LowerBound.Value, b.LowerBound.Value)
                        ? Bound.Min(a.LowerBound.Direction, b.LowerBound.Direction)
                        : a.LowerBound.Direction
                                           , a.LowerBound.Value);

                upperBound = new Bound <T>(BoundType.Upper
                                           , object.Equals(a.UpperBound.Value, b.UpperBound.Value)
                        ? Bound.Min(a.UpperBound.Direction, b.UpperBound.Direction)
                        : b.UpperBound.Direction
                                           , b.UpperBound.Value);
            }
            else
            {
                return(Interval.Empty <T>());
            }

            Interval <T> result;

            return(TryParse(lowerBound, upperBound, out result)
                ? result
                : Interval.Empty <T>());
        }