using System;
using System.Collections.Generic;
using System.Text;

namespace Misc
{
    //判断是否有交集
    public class DATARANGE
    {
        public DATARANGE(int start, int end)
        {
            this.start = start;
            this.end = end;
            if (this.start > this.end)
            {
                isEmpty = true;
            }
            else
            {
                isEmpty = false;
            }
        }
        public int start;
        public int end;
        public bool isEmpty;
        public enum STATE
        {
            /// <summary>
            /// 包含
            /// </summary>
            Contain = 0,
            /// <summary>
            /// 被包含
            /// </summary>
            IsContained = 1,
            //Intersect = 2,//相交

            /// <summary>
            /// 分离
            /// </summary>
            Separate = 3,
            /// <summary>
            /// 相交，自己在别人的右边
            /// </summary>
            Intersect_Right=4,
            /// <summary>
            /// 相交，自己在别人的左边
            /// </summary>
            Intersect_Left = 5//
        }
        /// <summary>
        /// 是否有相同部分 
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public STATE hasSame(DATARANGE dr)//
        {
            if (dr.start < start)
            {
                if (dr.end < start)
                {
                    return STATE.Separate;//分离
                }
                else if (dr.end <= end)
                {
                    return STATE.Intersect_Right;//相交,在右边
                }
                else
                {
                    return STATE.IsContained;//被包含
                }
            }
            else if (dr.start <= end)
            {
                if (dr.end <= end)
                {
                    return STATE.Contain;//包含
                }
                else
                {
                    return STATE.Intersect_Left;//相交在左边
                }
            }
            else
            {
                return STATE.Separate;//分离
            }
        }

        /// <summary>
        /// /获取相同部分
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="ret"></param>
        /// <returns></returns>
        public STATE getSame(DATARANGE dr, out DATARANGE ret)
        {
            switch (hasSame(dr))
            {
                case STATE.Contain:
                    ret = new DATARANGE(dr.start, dr.end);
                    return STATE.Contain;
                case STATE.IsContained:
                    ret = new DATARANGE(start, end);
                    return STATE.IsContained;
                case STATE.Intersect_Left:
                    ret = new DATARANGE(dr.start, end);
                    return STATE.Intersect_Left;
                case STATE.Intersect_Right:
                    ret = new DATARANGE(start, dr.end);
                    return STATE.Intersect_Right;
                default:
                    //case STATE.Separate:
                    ret = new DATARANGE(0, 0);
                    return STATE.Separate;
            }
        }
        /// <summary>
        /// 放大范围
        /// </summary>
        /// <param name="range"></param>
        public void zoom(int range)// 
        {
            int len = end - start + 1;
            start -= (range - len) / 2;
            end = start + range - 1;
        }
        /// <summary>
        /// 放大，右限制
        /// </summary>
        /// <param name="range"></param>
        /// <param name="rightLimit"></param>
        public void zoom_rightLimit(int range, int rightLimit)
        {
            zoom(range);
            if (end > rightLimit) end = rightLimit;
        }
        /// <summary>
        /// 放大，左限制
        /// </summary>
        /// <param name="range"></param>
        /// <param name="leftLimit"></param>
        public void zoom_leftLimit(int range, int leftLimit)
        {
            zoom(range);
            if (start < leftLimit) start = leftLimit;
        }
        /// <summary>
        /// 减去
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="ret01"></param>
        /// <param name="ret02"></param>
        /// <returns></returns>
        public STATE cut(DATARANGE dr, out DATARANGE ret01, out DATARANGE ret02)
        {
            switch (hasSame(dr))
            {
                case STATE.Contain:
                    ret01 = new DATARANGE(start, dr.start - 1);
                    ret02 = new DATARANGE(dr.end + 1, end);
                    return STATE.Contain;
                case STATE.IsContained:
                    ret01 = new DATARANGE(0, -1);
                    ret02 = new DATARANGE(0, -1);
                    return STATE.IsContained;
                case STATE.Intersect_Left:
                    ret01 = new DATARANGE(start, dr.start - 1);
                    ret02 = new DATARANGE(0, -1);
                    return STATE.Intersect_Left;
                case STATE.Intersect_Right:
                    ret01 = new DATARANGE(0, -1);
                    ret02 = new DATARANGE(dr.end + 1, end);
                    return STATE.Intersect_Right;
                default:
                    //case STATE.Separate:
                    ret01 = new DATARANGE(start, end);
                    ret02 = new DATARANGE(0, -1);
                    return STATE.Separate;
            }
        }
    }
}
