1 /// <summary> 2 /// 整数区间类 3 /// </summary> 4 private class Interval 5 { 6 private int _start = 0, _end = 0; 7 public int Start 8 { 9 get { return Math.Min(this._start, this._end); } 10 set { this._start = value; } 11 } 12 public int End 13 { 14 get { return Math.Max(this._start, this._end); } 15 set { this._end = value; } 16 } 17 public bool Inside(int Value) 18 { 19 return (Start <= Value && Value <= End); 20 } 21 /// <summary> 22 /// 区间求并运算,此方法不修改当前实例的值。 23 /// 若不可合并则返回null,否则返回新的区间。 24 /// </summary> 25 /// <param name="OtherInterval">另一个区间</param> 26 /// <returns>新的区间实例</returns> 27 public Interval Merge(Interval OtherInterval) 28 { 29 if (this.Start - OtherInterval.End > 1 || OtherInterval.Start - this.End > 1) 30 { 31 return null; 32 } 33 else 34 { 35 return new Interval() 36 { 37 Start = Math.Min(this.Start, OtherInterval.Start), 38 End = Math.Max(this.End, OtherInterval.End) 39 }; 40 } 41 } 42 } 43 /// <summary> 44 /// 区间集合类 45 /// </summary> 46 private class IntervalCollection 47 { 48 public List<Interval> Intervals = new List<Interval>(); 49 /// <summary> 50 /// 将一个区间并入区间集合中 51 /// </summary> 52 /// <param name="Start">开始位置</param> 53 /// <param name="End">结束位置</param> 54 public void Add(int Start, int End) 55 { 56 this.Add(new Interval() { Start = Start, End = End }); 57 } 58 /// <summary> 59 /// 将一个区间并入区间集合中 60 /// </summary> 61 /// <param name="NewInterval">需要并入的区间结构</param> 62 public void Add(Interval NewInterval) 63 { 64 Interval MergeInterval = null; 65 for (int i = 0; i < this.Intervals.Count; i++) 66 { 67 MergeInterval = this.Intervals[i].Merge(NewInterval); 68 if (MergeInterval != null) 69 { 70 this.Intervals[i] = MergeInterval; 71 break; 72 } 73 } 74 if (MergeInterval == null) 75 { 76 Intervals.Add(NewInterval); 77 } 78 } 79 /// <summary> 80 /// 检测区间集中是否包含有指定值 81 /// </summary> 82 /// <param name="Value">需要查询的数值</param> 83 /// <returns>包含返回true,否则返回false</returns> 84 public bool Inside(int Value) 85 { 86 foreach (Interval Inter in Intervals) 87 { 88 if (Inter.Inside(Value) == true) 89 { 90 return true; 91 } 92 } 93 return false; 94 } 95 /// <summary> 96 /// 将区间集的转换为字符串 97 /// </summary> 98 /// <typeparam name="T">转换类型</typeparam> 99 /// <param name="CompleteSet">全集,当序列化后若长度过长则用以求取补集</param> 100 /// <returns>返回转换后的字符串</returns> 101 public string ToString<T>(string CompleteSet = null) 102 { 103 StringBuilder Str = new StringBuilder("["); 104 foreach (Interval Inter in Intervals) 105 { 106 if (Inter.Start == Inter.End) 107 { 108 dynamic Vs = (object)Inter.Start; 109 Str.Append((T)Vs); 110 } 111 else if (Inter.End - Inter.Start == 1) 112 { 113 dynamic Vs = (object)Inter.Start; 114 dynamic Ve = (object)Inter.End; 115 Str.Append((T)Vs).Append((T)Ve); 116 } 117 else 118 { 119 dynamic Vs = (object)Inter.Start; 120 dynamic Ve = (object)Inter.End; 121 Str.AppendFormat("{0}-{1}", (T)Vs, (T)Ve); 122 } 123 } 124 Str.Append("]"); 125 if (Str[1] == '^') 126 { 127 Str = Str.Replace("^", "\\^", 0, 1); 128 } 129 // 求取补集以试图缩小代码长度 130 if (Str.Length >= 10 && CompleteSet != null) 131 { 132 StringBuilder Complement = new StringBuilder("[^"); 133 foreach (char c in CompleteSet) 134 { 135 if (this.Inside(c) == false) 136 { 137 Complement.Append(c); 138 } 139 } 140 Complement.Append("]"); 141 if (Complement.Length <= Str.Length) 142 { 143 return Complement.ToString(); 144 } 145 } 146 return Str.ToString(); 147 } 148 }