Exemplo n.º 1
0
        /// <summary>
        /// Tests an interval to see if it wholly contains another interval.
        /// </summary>
        /// <typeparam name="T">The type of values included in the intervals.</typeparam>
        /// <param name="interval">The interval to test.</param>
        /// <param name="other">The other interval.</param>
        /// <returns>true, if <paramref name="interval"/> contains <paramref name="other"/>; false, otherwise.</returns>
        /// <remarks>
        /// All intervals (including empty ones) contain every other empty interval.  So, if <paramref name="other"/> is empty, this method always returns true.
        /// Otherwise, this method only returns true if the start and end values of <paramref name="interval"/> surround the values of <paramref name="other"/>.
        /// </remarks>
        public static bool Contains <T>(this IInterval <T> interval, IInterval <T> other) where T : IComparable <T>
        {
            if (other.IsEmpty())
            {
                return(true);
            }

            var intersection = other.IntersectWith(interval);

            return(!intersection.IsEmpty() &&
                   intersection == other);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the difference of one interval to another.
        /// </summary>
        /// <typeparam name="T">The type of values included in the intervals.</typeparam>
        /// <param name="interval">The source interval.</param>
        /// <param name="other">The interval to exclude.</param>
        /// <returns>A set that contains every part of <paramref name="interval"/> that is not also contained by <paramref name="other"/>.</returns>
        public static IList <IInterval <T> > DifferenceWith <T>(this IInterval <T> interval, IInterval <T> other) where T : IComparable <T>
        {
            if (interval.IsEmpty())
            {
                return(null);
            }

            var intersection = interval.IntersectWith(other);

            if (intersection.IsEmpty())
            {
                return(new[] { interval });
            }
            else if (intersection == interval)
            {
                return(null);
            }

            var intervals = new List <IInterval <T> >();

            var startToStart = interval.Start.CompareTo(intersection.Start);

            if (startToStart != 0 ||
                (interval.StartInclusive && !intersection.StartInclusive))
            {
                intervals.Add(interval.Clone(
                                  interval.Start,
                                  interval.StartInclusive,
                                  intersection.Start,
                                  !intersection.StartInclusive));
            }

            var endToEnd = interval.End.CompareTo(intersection.End);

            if (endToEnd != 0 ||
                (interval.EndInclusive && !intersection.EndInclusive))
            {
                intervals.Add(interval.Clone(
                                  intersection.End,
                                  !intersection.EndInclusive,
                                  interval.End,
                                  interval.EndInclusive));
            }

            return(intervals);
        }