Exemplo n.º 1
0
        Value IntersectIntervals <T>(IntervalValue <T> interval, IEnumerable <IntervalValue <T> > intervals)
            where T : IComparable, IComparable <T>, IEquatable <T>
        {
            T maxStart = interval.Start;
            T minEnd   = interval.End;

            foreach (var item in intervals)
            {
                if (maxStart.CompareTo(item.Start) < 0)
                {
                    maxStart = item.Start;
                }

                if (minEnd.CompareTo(item.End) > 0)
                {
                    minEnd = item.End;
                }
            }

            if (maxStart.Equals(minEnd))
            {
                if (maxStart is int)
                {
                    return(valueFactory.CreateInt(System.Convert.ToInt32(maxStart)));
                }
                else if (maxStart is long)
                {
                    return(valueFactory.CreateLong(System.Convert.ToInt64(maxStart)));
                }
                if (maxStart is double)
                {
                    return(valueFactory.CreateDouble(System.Convert.ToDouble(maxStart)));
                }
                else
                {
                    throw new NotSupportedException(string.Format("Interval type \"{0}\" is not supported.", interval.GetType()));
                }
            }
            if (maxStart.CompareTo(minEnd) < 0)
            {
                if (maxStart is int)
                {
                    return(valueFactory.CreateIntegerInterval(System.Convert.ToInt32(maxStart), System.Convert.ToInt32(minEnd)));
                }
                else if (maxStart is long)
                {
                    return(valueFactory.CreateLongintInterval(System.Convert.ToInt64(maxStart), System.Convert.ToInt64(minEnd)));
                }
                if (maxStart is double)
                {
                    return(valueFactory.CreateFloatInterval(System.Convert.ToDouble(maxStart), System.Convert.ToDouble(minEnd)));
                }
                else
                {
                    throw new NotSupportedException(string.Format("Interval type \"{0}\" is not supported.", interval.GetType()));
                }
            }
            else
            {
                // Empty interval
                return(valueFactory.UndefinedValue);
            }
        }
Exemplo n.º 2
0
        Value IntersectIntervals(IntervalValue interval, IEnumerable <IntervalValue> intervals)
        {
            if (intervals == null || intervals.Count() == 0)
            {
                return(interval);
            }

            bool hasLong  = intervals.Any(a => a is LongintIntervalValue);
            bool hasFloat = intervals.Any(a => a is FloatIntervalValue);
            bool hasInt   = intervals.Any(a => a is IntegerIntervalValue);

            if (hasFloat || interval is FloatIntervalValue)
            {
                return(IntersectIntervals(TypeConversion.ToFloatInterval(valueFactory, interval), intervals.Select(a => TypeConversion.ToFloatInterval(valueFactory, a))));
            }
            else if (hasLong || interval is LongintIntervalValue)
            {
                return(IntersectIntervals(TypeConversion.ToLongInterval(valueFactory, interval), intervals.Select(a => TypeConversion.ToLongInterval(valueFactory, a))));
            }
            else if (hasInt || interval is IntegerIntervalValue)
            {
                return(IntersectIntervals(TypeConversion.ToIntegerInterval(valueFactory, interval), intervals.Select(a => TypeConversion.ToIntegerInterval(valueFactory, a))));
            }
            else
            {
                throw new NotSupportedException(string.Format("Interval type \"{0}\" is not supported.", interval.GetType()));
            }
        }