Ejemplo n.º 1
0
        public bool IntersectsWith(IntDomain2 domain)
        {
            if (ReferenceEquals(domain, this))
            {
                return(true);
            }

            if (domain.IsEmpty())
            {
                return(false);
            }

            // 1, 6
            if (!m_Interval.IntersectsWith(domain.m_Interval))
            {
                return(false);
            }

            if (domain.IsSimple())
            {
                return(IntersectsWith(domain.m_Interval));
            }

            if (IsSimple())
            {
                return(domain.IntersectsWith(m_Interval));
            }

            int  index0    = 0;
            int  index1    = 0;
            bool intersect = false;

            while (index0 < m_IntervalList.Count &&
                   index1 < domain.m_IntervalList.Count &&
                   !intersect)
            {
                IntInterval ival0 = m_IntervalList[index0];
                IntInterval ival1 = domain.m_IntervalList[index1];

                if (ival0.Max < ival1.Min)
                {
                    ++index0;
                }
                else if (ival1.Max < ival0.Min)
                {
                    ++index1;
                }
                else
                {
                    intersect = true;
                }
            }

            return(intersect);
        }
Ejemplo n.º 2
0
        public IntDomain2 Intersect(IntDomain2 domain)
        {
            if (ReferenceEquals(domain, this))
            {
                return(this);
            }

            if (domain.IsEmpty())
            {
                return(m_Empty);
            }

            if (domain.IsSimple())
            {
                return(Intersect(domain.m_Interval));
            }

            // 1, 6
            if (!IntersectsWith(domain))
            {
                return(m_Empty);
            }

            // If the target is contained, then the result would be equal to the domain, hence we return the domain.
            if (IsSimple())
            {
                if (domain.Contains(m_Interval))
                {
                    return(this);
                }

                if (m_Interval.Contains(domain.m_Interval))
                {
                    return(domain);
                }
            }
            // Need to be a bit smart, put our interval in a list, in case it gets chopped up.
            List <IntInterval> intervalList;

            if (IsSimple())
            {
                intervalList = new List <IntInterval>(1);
                intervalList.Add(m_Interval);
            }
            else
            {
                intervalList = m_IntervalList;
            }

            IntDomain2 result = new IntDomain2();

            result.m_IntervalList = new List <IntInterval>(intervalList.Count + domain.m_IntervalList.Count);

            int  idx    = 0;
            int  domIdx = 0;
            bool equal  = true;

            while (idx < intervalList.Count ||
                   domIdx < domain.m_IntervalList.Count)
            {
                // 6
                if (idx == intervalList.Count)
                {
                    domIdx = domain.m_IntervalList.Count;
                }
                // 1
                else if (domIdx == domain.m_IntervalList.Count)
                {
                    idx   = intervalList.Count;
                    equal = false;
                }
                else
                {
                    IntInterval intv    = intervalList[idx];
                    IntInterval domIntv = domain.m_IntervalList[domIdx];

                    // 1 : completely before
                    if (domIntv.Max < intv.Min - 1)
                    {
                        ++domIdx;
                    }
                    // 6 : completely after
                    else if (domIntv.Min > intv.Max + 1)
                    {
                        ++idx;
                        equal = false;
                    }
                    // 3
                    else if (domIntv.Min <= intv.Min && domIntv.Max >= intv.Max)
                    {
                        result.m_IntervalList.Add(intv);
                        ++idx;
                    }
                    // ~4 : divide into two intervals...
                    else if (domIntv.Min > intv.Min && domIntv.Max < intv.Max)
                    {
                        result.m_IntervalList.Add(domIntv);
                        ++domIdx;
                        equal = false;
                    }
                    // 2
                    else if (domIntv.Min <= intv.Min && domIntv.Max < intv.Max)
                    {
                        result.m_IntervalList.Add(intv.Intersect(domIntv));
                        ++domIdx;
                        equal = false;
                    }
                    // 5
                    else if (domIntv.Min > intv.Min && domIntv.Max >= intv.Max)
                    {
                        result.m_IntervalList.Add(intv.Intersect(domIntv));
                        ++idx;
                        equal = false;
                    }
                }
            }

            if (equal)
            {
                return(this);
            }

            result.UpdateInterval();

            return(result);
        }
Ejemplo n.º 3
0
        public IntDomain2 Difference(IntDomain2 domain)
        {
            if (ReferenceEquals(domain, this))
            {
                return(m_Empty);
            }

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

            if (domain.IsSimple())
            {
                return(Difference(domain.m_Interval));
            }

            //1, 6
            if (!IntersectsWith(domain.m_Interval))
            {
                return(this);
            }

            // Need to be a bit smart, put our interval in a list, in case it gets chopped up.
            List <IntInterval> intervalList;

            if (IsSimple())
            {
                intervalList = new List <IntInterval>(1);
                intervalList.Add(m_Interval);
            }
            else
            {
                intervalList = m_IntervalList;
            }

            IntDomain2 result = new IntDomain2();

            result.m_IntervalList = new List <IntInterval>(intervalList.Count + domain.m_IntervalList.Count);

            int idx    = 0;
            int domIdx = 0;

            while (idx < intervalList.Count ||
                   domIdx < domain.m_IntervalList.Count)
            {
                // 6
                if (idx == intervalList.Count)
                {
                    domIdx = domain.m_IntervalList.Count;
                }
                // 1
                else if (domIdx == domain.m_IntervalList.Count)
                {
                    idx = intervalList.Count;
                }
                else
                {
                    IntInterval intv    = intervalList[idx];
                    IntInterval domIntv = domain.m_IntervalList[domIdx];

                    // 1 : completely before
                    if (domIntv.Max < intv.Min - 1)
                    {
                        ++domIdx;
                    }
                    // 6 : completely after
                    else if (domIntv.Min > intv.Max + 1)
                    {
                        result.m_IntervalList.Add(intv);
                        ++idx;
                    }
                    // 3
                    else if (domIntv.Min < intv.Min && domIntv.Max > intv.Max)
                    {
                        ++idx;
                    }
                    // 4 : divide into two intervals...
                    else if (domIntv.Min > intv.Min && domIntv.Max < intv.Max)
                    {
                        while (domIdx < domain.m_IntervalList.Count &&
                               domIntv.Min > intv.Min &&
                               domIntv.Max < intv.Max)
                        {
                            result.m_IntervalList.Add(new IntInterval(intv.Min, domIntv.Min - 1));

                            intv = new IntInterval(domIntv.Max + 1, intv.Max);

                            ++domIdx;

                            if (domIdx < domain.m_IntervalList.Count)
                            {
                                domIntv = domain.m_IntervalList[domIdx];
                            }
                        }

                        result.m_IntervalList.Add(intv);
                    }
                    // 2
                    else if (domIntv.Min <= intv.Min && domIntv.Max < intv.Max)
                    {
                        result.m_IntervalList.Add(intv.Difference(domIntv));

                        ++domIdx;
                    }
                    // 5
                    else if (domIntv.Min > intv.Min && domIntv.Max >= intv.Max)
                    {
                        result.m_IntervalList.Add(intv.Difference(domIntv));

                        ++idx;
                    }
                }
            }

            result.UpdateInterval();

            return(result);
        }
Ejemplo n.º 4
0
        public IntDomain2 Union(IntDomain2 domain)
        {
            if (ReferenceEquals(domain, this))
            {
                return(this);
            }

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

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

            if (domain.IsSimple())
            {
                return(Union(domain.m_Interval));
            }

            if (IsSimple())
            {
                if (m_Interval.Contains(domain.m_Interval))
                {
                    return(this);
                }

                return(domain.Union(m_Interval));
            }
            else
            {
                IntDomain2 result = new IntDomain2();
                result.m_Interval     = m_Interval.Union(domain.m_Interval);
                result.m_IntervalList = new List <IntInterval>(m_IntervalList.Count + domain.m_IntervalList.Count);

                // 1 : completely before
                if (domain.m_Interval.Max < m_Interval.Min - 1)
                {
                    result.m_IntervalList.AddRange(domain.m_IntervalList);
                    result.m_IntervalList.AddRange(m_IntervalList);
                }

                // 6 : completely after
                else if (domain.m_Interval.Min > m_Interval.Max + 1)
                {
                    result.m_IntervalList.AddRange(m_IntervalList);
                    result.m_IntervalList.AddRange(domain.m_IntervalList);
                }
                else
                {
                    int  idx    = 0;
                    int  domIdx = 0;
                    bool equal  = true;

                    while (idx < m_IntervalList.Count ||
                           domIdx < domain.m_IntervalList.Count)
                    {
                        // 6 :
                        if (idx == m_IntervalList.Count)
                        {
                            equal = false;

                            for ( ; domIdx < domain.m_IntervalList.Count; ++domIdx)
                            {
                                result.m_IntervalList.Add(domain.m_IntervalList[domIdx]);
                            }
                        }

                        // 1 : remaining intervals after
                        else if (domIdx == domain.m_IntervalList.Count)
                        {
                            for ( ; idx < m_IntervalList.Count; ++idx)
                            {
                                result.m_IntervalList.Add(m_IntervalList[idx]);
                            }
                        }
                        else
                        {
                            IntInterval intv    = m_IntervalList[idx];
                            IntInterval domIntv = domain.m_IntervalList[domIdx];

                            // 1 : completely before
                            if (domIntv.Max < intv.Min - 1)
                            {
                                equal = false;

                                result.m_IntervalList.Add(domIntv);
                                ++domIdx;
                            }
                            // 6 : completely after
                            else if (domIntv.Min > intv.Max + 1)
                            {
                                result.m_IntervalList.Add(intv);
                                ++idx;
                            }
                            // 3
                            else if (domIntv.Min < intv.Min && domIntv.Max > intv.Max)
                            {
                                equal = false;

                                ++idx;
                            }
                            // 4
                            else if (domIntv.Min >= intv.Min && domIntv.Max <= intv.Max)
                            {
                                ++domIdx;
                            }
                            // 2, 5
                            else
                            {
                                equal = false;

                                IntInterval intvTmp = intv.Union(domIntv);

                                ++idx;

                                // contains...
                                for ( ; idx < m_IntervalList.Count &&
                                      intvTmp.Contains(m_IntervalList[idx]);
                                      ++idx)
                                {
                                }
                                ;

                                // adjacent | intersect...
                                while (idx < m_IntervalList.Count &&
                                       IntersectsWithOrIsAdjacent(m_IntervalList[idx], intvTmp))
                                {
                                    intvTmp = intvTmp.Union(m_IntervalList[idx]);
                                    ++idx;
                                }

                                result.m_IntervalList.Add(intvTmp);

                                ++domIdx;
                            }
                        }
                    }

                    if (equal)
                    {
                        return(this);
                    }


                    result.CheckSimple();
                }

                return(result);
            }
        }