示例#1
0
        public IntInterval Intersect(IntInterval interval)
        {
            if (interval.IsEmpty())
            {
                return(m_Empty);
            }

            if (IsEmpty())
            {
                return(this);
            }

            // 3
            if (interval.Min <= m_Min && interval.Max >= m_Max)
            {
                return(this);
            }

            // 1, 6 : completely before or after
            if (interval.m_Max < m_Min || interval.m_Min > m_Max)
            {
                return(m_Empty);
            }

            // 2, 4, 5
            int min = Math.Max(m_Min, interval.m_Min);
            int max = Math.Min(m_Max, interval.m_Max);

            return(new IntInterval(min, max));
        }
示例#2
0
        public IntInterval Subtract(IntInterval interval)
        {
            if (IsEmpty() || interval.IsEmpty())
            {
                return(m_Empty);
            }

            return(new IntInterval(m_Min - interval.Max, m_Max - interval.Min));
        }
示例#3
0
        public IntInterval Add(IntInterval interval)
        {
            if (IsEmpty() || interval.IsEmpty())
            {
                return(m_Empty);
            }

            return(new IntInterval(m_Min + interval.Min, m_Max + interval.Max));
        }
示例#4
0
        static bool InZero(IntInterval interval)
        {
            if (interval.IsEmpty())
            {
                return(false);
            }

            return((!(interval.Min > 0)) &&
                   (!(interval.Max < 0)));
        }
示例#5
0
        public bool IntersectsWith(IntInterval interval)
        {
            if (IsEmpty() || interval.IsEmpty())
            {
                return(false);
            }

            return((interval.m_Min <= m_Max) &&
                   (interval.m_Max >= m_Min));
        }
示例#6
0
        public bool Contains(IntInterval interval)
        {
            if (IsEmpty() || interval.IsEmpty())
            {
                return(false);
            }

            return((interval.m_Min >= m_Min) &&
                   (interval.m_Max <= m_Max));
        }
示例#7
0
        public IntInterval Difference(IntInterval interval)
        {
            if (interval.IsEmpty())
            {
                return(this);
            }

            if (IsEmpty())
            {
                return(this);
            }

            // 4 : cannot divide into two intervals...
            if (interval.m_Min > m_Min && interval.m_Max < m_Max)
            {
                return(this);
            }

            // 1, 6 : completely before or after
            if (interval.m_Max < m_Min || interval.m_Min > m_Max)
            {
                return(this);
            }

            // 3 : completely remove interval => empty
            if (interval.m_Min <= m_Min && interval.m_Max >= m_Max)
            {
                return(m_Empty);
            }

            // 2 : left overlap
            if (interval.m_Min <= m_Min &&
                interval.m_Max < m_Max)
            {
                int min = interval.m_Max + 1;
                int max = m_Max;

                return(new IntInterval(min, max));
            }

            // 5 : right overlap
            if (interval.m_Min > m_Min &&
                interval.m_Max >= m_Max)
            {
                int min = m_Min;
                int max = interval.m_Min - 1;

                return(new IntInterval(min, max));
            }

            return(m_Empty);
        }
示例#8
0
        public IntInterval Multiply(IntInterval interval)
        {
            if (IsEmpty() || interval.IsEmpty())
            {
                return(m_Empty);
            }

            int min0 = interval.Min;
            int max0 = interval.Max;

            int min0min = min0 * m_Min;
            int min0max = min0 * m_Max;
            int max0min = max0 * m_Min;
            int max0max = max0 * m_Max;

            int min = Math.Min(Math.Min(min0min, min0max), Math.Min(max0min, max0max));
            int max = Math.Max(Math.Max(min0min, min0max), Math.Max(max0min, max0max));

            return(new IntInterval(min, max));
        }
示例#9
0
        public static IntInterval Divide1(IntInterval lhs, IntInterval rhs, ref bool b)
        {
            b = false;

            if (lhs.IsEmpty() || rhs.IsEmpty())
            {
                return(m_Empty);
            }

            if (InZero(rhs))
            {
                if (rhs.Min != 0)
                {
                    if (rhs.Max != 0)
                    {
                        return(DivideZeroPart1(lhs, rhs, ref b));
                    }
                    else
                    {
                        return(DivideNegative(lhs, rhs.Min));
                    }
                }
                else
                {
                    if (rhs.Max != 0)
                    {
                        return(DividePositive(lhs, rhs.Max));
                    }
                    else
                    {
                        return(m_Empty);
                    }
                }
            }
            else
            {
                return(DivideNonZero(lhs, rhs));
            }
        }
示例#10
0
        public IntInterval Union(IntInterval interval)
        {
            if (interval.IsEmpty())
            {
                return(this);
            }

            if (IsEmpty())
            {
                return(interval);
            }

            if (interval.m_Min >= m_Min && interval.m_Max <= m_Max)
            {
                return(this);
            }

            int min = Math.Min(m_Min, interval.m_Min);
            int max = Math.Max(m_Max, interval.m_Max);

            return(new IntInterval(min, max));
        }
示例#11
0
        public IntInterval Divide(IntInterval interval)
        {
            if (IsEmpty() || interval.IsEmpty())
            {
                return(m_Empty);
            }

            if (InZero(interval))
            {
                if (interval.Min != 0)
                {
                    if (interval.Max != 0)
                    {
                        return(DivideZero(this));
                    }
                    else
                    {
                        return(DivideNegative(this, interval.Min));
                    }
                }
                else
                {
                    if (interval.Max != 0)
                    {
                        return(DividePositive(this, interval.Max));
                    }
                    else
                    {
                        return(m_Empty);
                    }
                }
            }
            else
            {
                return(DivideNonZero(this, interval));
            }
        }
示例#12
0
        public IntInterval Multiply2(IntInterval interval)
        {
            if (IsEmpty() || interval.IsEmpty())
            {
                return(m_Empty);
            }

            int intvMin = interval.Min;
            int intvMax = interval.Max;

            if (m_Min < 0)
            {
                if (m_Max > 0)
                {
                    if (intvMin < 0)
                    {
                        if (intvMax > 0)                                        // M * M
                        {
                            return(new IntInterval(Math.Min(m_Min * intvMax, m_Max * intvMin),
                                                   Math.Max(m_Min * intvMin, m_Max * intvMax)));
                        }
                        else                                            // M * N
                        {
                            return(new IntInterval(m_Max * intvMin, m_Min * intvMin));
                        }
                    }
                    else
                    {
                        if (intvMax > 0)                                        // M * P
                        {
                            return(new IntInterval(m_Min * intvMax, m_Max * intvMax));
                        }
                        else                                            // M * Z
                        {
                            return(new IntInterval(0, 0));
                        }
                    }
                }
                else
                {
                    if (intvMin < 0)
                    {
                        if (intvMax > 0)                                        // N * M
                        {
                            return(new IntInterval(m_Min * intvMax, m_Min * intvMin));
                        }
                        else                                            // N * N
                        {
                            return(new IntInterval(m_Max * intvMax, m_Min * intvMin));
                        }
                    }
                    else
                    {
                        if (intvMax > 0)                                        // N * P
                        {
                            return(new IntInterval(m_Min * intvMax, m_Max * intvMin));
                        }
                        else                                            // N * Z
                        {
                            return(new IntInterval(0, 0));
                        }
                    }
                }
            }
            else
            {
                if (m_Max > 0)
                {
                    if (intvMin < 0)
                    {
                        if (intvMax > 0)                                                // P * M
                        {
                            return(new IntInterval(m_Max * intvMin, m_Max * intvMax));
                        }
                        else                                            // P * N
                        {
                            return(new IntInterval(m_Max * intvMin, m_Min * intvMax));
                        }
                    }
                    else
                    {
                        if (intvMax > 0)                                        // P * P
                        {
                            return(new IntInterval(m_Min * intvMin, m_Max * intvMax));
                        }
                        else                                            // P * Z
                        {
                            return(new IntInterval(0, 0));
                        }
                    }
                }
                else                                                                    // Z * ?
                {
                    return(new IntInterval(0, 0));
                }
            }
        }