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 { /// /// 包含 /// Contain = 0, /// /// 被包含 /// IsContained = 1, //Intersect = 2,//相交 /// /// 分离 /// Separate = 3, /// /// 相交,自己在别人的右边 /// Intersect_Right=4, /// /// 相交,自己在别人的左边 /// Intersect_Left = 5// } /// /// 是否有相同部分 /// /// /// 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;//分离 } } /// /// /获取相同部分 /// /// /// /// 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; } } /// /// 放大范围 /// /// public void zoom(int range)// { int len = end - start + 1; start -= (range - len) / 2; end = start + range - 1; } /// /// 放大,右限制 /// /// /// public void zoom_rightLimit(int range, int rightLimit) { zoom(range); if (end > rightLimit) end = rightLimit; } /// /// 放大,左限制 /// /// /// public void zoom_leftLimit(int range, int leftLimit) { zoom(range); if (start < leftLimit) start = leftLimit; } /// /// 减去 /// /// /// /// /// 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; } } } }