using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using Newtonsoft.Json; using System.Collections; namespace Misc { /// /// int范围类, /// public struct RangeStruct { #region property /// /// 开始 /// public int Begin { get; set; } /// /// 结束 /// public int End { get; set; } #endregion #region just get property /// /// 本范围 中心位置 /// [JsonIgnore] public int Mid { get { if (Misc.MyBase.ISVALIDATA(Begin)) return (End + Begin) / 2; else return Misc.MyBase.NULL_VALUE; } } /// /// 本范围宽度 /// [JsonIgnore] public int Width { get { if (IsValid) return End - Begin + 1; else return Misc.MyBase.NULL_VALUE; } } /// /// 是否有效 /// [JsonIgnore] public bool IsValid { get { if (Misc.MyBase.ISVALIDATA(Begin) && Misc.MyBase.ISVALIDATA(End)) return true; else return false; } } #endregion public RangeStruct(int begin, int end) { Begin = begin; End = end; } #region methods /// /// 范围 r 在 本范围内 /// /// /// public bool Contain(RangeStruct r) { if ((Begin <= r.Begin) && (r.End <= End)) return true; else return false; } /// /// 点 p 在 本范围内 /// /// /// public bool Contain(int p) { if ((Begin <= p) && (p <= End)) return true; else return false; } /// /// 无效值 /// public static RangeStruct InvalidValue { get { return new RangeStruct(Misc.MyBase.NULL_VALUE, Misc.MyBase.NULL_VALUE); } } /// /// 范围并集 /// /// /// /// public static RangeStruct operator |(RangeStruct a, RangeStruct b) { return new RangeStruct() { Begin = (a.Begin < b.Begin) ? a.Begin : b.Begin, End = (a.End > b.End) ? a.End : b.End }; } /// /// 范围交集 /// /// /// /// public static RangeStruct operator &(RangeStruct a, RangeStruct b) { RangeStruct r = new RangeStruct() { Begin = (a.Begin > b.Begin) ? a.Begin : b.Begin, End = (a.End < b.End) ? a.End : b.End }; if (r.End < r.Begin) { return InvalidValue; } else { return r; } } /// /// 范围 减 /// /// /// /// public static RangeStruct operator -(RangeStruct a, RangeStruct b) { if (a.Begin < b.Begin) { return new RangeStruct(a.Begin, b.Begin - 1); } else { if (a.End > b.End) return new RangeStruct(b.End + 1, a.End); else return InvalidValue; } } /// /// 范围 a小于b /// /// /// /// public static bool operator <(RangeStruct a, RangeStruct b) { if (a.End < b.Begin)//[a.b,a.e] [b.b,b.e] { return true; } else { return false; } } /// /// 范围 a大于b /// /// /// /// public static bool operator >(RangeStruct a, RangeStruct b) { if (b.End < (a.Begin))// [b.b,b.e][a.b,a.e] { return true; } else { return false; } } public static bool operator <(int p, RangeStruct r) { if (p < r.Begin) { return true; } else { return false; } } public static bool operator >(int p, RangeStruct r) { if (p > r.End) { return true; } else { return false; } } public static bool operator <(RangeStruct r, int p) { if (r.End < p) { return true; } else { return false; } } public static bool operator >(RangeStruct r, int p) { if (r.Begin > p) { return true; } else { return false; } } public static RangeStruct operator +(RangeStruct a, int b) { return new RangeStruct() { Begin = a.Begin + b, End = a.End + b }; } public static RangeStruct operator -(RangeStruct a, int b) { return new RangeStruct() { Begin = a.Begin - b, End = a.End - b }; } /// /// 向外扩大 /// /// /// public RangeStruct ZoomIn(int b) { return new RangeStruct(Begin - b, End + b); } public RangeStruct ZoomIn(int b, int e) { return new RangeStruct(Begin - b, End + e); } /// /// 是否有交集 /// /// /// public bool HasIntersection(RangeStruct r) { if (Contain(r.Begin)) return true; if (Contain(r.End)) return true; if (r.Contain(Begin)) return true; if (r.Contain(End)) return true; return false; } /// /// 可以合并 /// /// public bool CanUnion(RangeStruct r) { if (HasIntersection(r)) return true; if (End == (r.Begin - 1)) return true; if (Begin == (r.End + 1)) return true; return false; } #endregion /// /// /// /// public override string ToString() { if (IsValid) return $"[{Begin},{End}]={Width}"; else return "invalid"; } } /// /// Range 辅助类 /// public static class EListRangeStruct { /// /// rlist 必须是由小到大排列 /// /// /// public static void Union(this List rlist, RangeStruct r) { for (int i = 0; i < rlist.Count(); i++) { RangeStruct r_dest = rlist[i]; if (r < r_dest)//在 r_dest 前面 { rlist.Insert(i, r); return;//完成 } else if (r.CanUnion(r_dest)) //r 与 r_dest 有交集 { //r 与 r_dest 合体 r = r | r_dest; rlist.RemoveAt(i); i--; //还需要继续判断 } } //r 在 rlist 的后面 rlist.Add(r); } /// /// 联合 /// /// /// public static void Union(this List rlist1, List rlist2) { for (int i = 0; i < rlist2.Count(); i++) { rlist1.Union(rlist2[i]); } } } }