Example #1
0
        /// <summary>
        /// Determines the complement of an <see cref="Interval&lt;T&gt;"/>.
        /// </summary>
        /// <typeparam name="T">The value type of the interval.</typeparam>
        /// <param name="value">An <see cref="Interval&lt;T&gt;"/> reference.</param>
        /// <returns>An <see cref="IntervalCollection&lt;T&gt;"/> that contains elements not in the specified interval.</returns>
        /// <seealso href="http://en.wikipedia.org/wiki/Complement_(set_theory)">Complement (set theory)</seealso>
        public static IntervalCollection <T> ComplementOf <T>(Interval <T> value)
        {
            if (Interval.IsNullOrEmpty(value))
            {
                return(new IntervalCollection <T>
                       (
                           new Interval <T>(Bound <T> .LowestBound, Bound <T> .UpmostBound)
                       ));
            }

            var lowerBound = value.LowerBound;
            var upperBound = value.UpperBound;
            var result     = new List <Interval <T> >(2);

            if (object.Equals(lowerBound, Bound <T> .LowestBound) && object.Equals(upperBound, Bound <T> .UpmostBound))
            {
            }
            else if (object.Equals(lowerBound, Bound <T> .LowestBound))
            {
                result.Add(new Interval <T>
                               (new Bound <T>(BoundType.Lower, Bound.Reverse(upperBound.Direction), upperBound.Value)
                               , Bound <T> .UpmostBound));
            }
            else if (object.Equals(upperBound, Bound <T> .UpmostBound))
            {
                result.Add(new Interval <T>
                               (Bound <T> .LowestBound
                               , new Bound <T>(BoundType.Upper, Bound.Reverse(lowerBound.Direction), lowerBound.Value)));
            }
            else
            {
                var iLower = Bound <T> .LowestBound;
                var iUpper = new Bound <T>(BoundType.Upper, Bound.Reverse(lowerBound.Direction), lowerBound.Value);

                var jLower = new Bound <T>(BoundType.Lower, Bound.Reverse(upperBound.Direction), upperBound.Value);
                var jUpper = Bound <T> .UpmostBound;

                Interval <T> i;
                if (Interval.TryParse(iLower, iUpper, out i))
                {
                    result.Add(i);
                }

                Interval <T> j;
                if (Interval.TryParse(jLower, jUpper, out j))
                {
                    result.Add(j);
                }
            }

            return(new IntervalCollection <T>(result));
        }
Example #2
0
        internal static bool TryParse <T>(BoundDirection lowerDirection
                                          , T lowerValue
                                          , T upperValue
                                          , BoundDirection upperDirection
                                          , out Interval <T> result)
        {
            Bound <T> lowerBound, upperBound;

            if (Bound.TryParse(BoundType.Lower, lowerDirection, lowerValue, out lowerBound) &&
                Bound.TryParse(BoundType.Upper, upperDirection, upperValue, out upperBound))
            {
                return(Interval.TryParse(lowerBound, upperBound, out result));
            }

            result = default(Interval <T>);
            return(false);
        }