interval.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // assembly: System
  3. // namespace: System.Text.RegularExpressions
  4. // file: interval.cs
  5. //
  6. // author: Dan Lewis ([email protected])
  7. // (c) 2002
  8. using System;
  9. using System.Collections;
  10. namespace System.Text.RegularExpressions {
  11. struct Interval : IComparable {
  12. public int low;
  13. public int high;
  14. public bool contiguous;
  15. public static Interval Empty {
  16. get {
  17. Interval i;
  18. i.low = 0;
  19. i.high = i.low - 1;
  20. i.contiguous = true;
  21. return i;
  22. }
  23. }
  24. public static Interval Entire {
  25. get { return new Interval (Int32.MinValue, Int32.MaxValue); }
  26. }
  27. public Interval (int low, int high) {
  28. if (low > high) {
  29. int t = low;
  30. low = high;
  31. high = t;
  32. }
  33. this.low = low;
  34. this.high = high;
  35. this.contiguous = true;
  36. }
  37. public bool IsDiscontiguous {
  38. get { return !contiguous; }
  39. }
  40. public bool IsSingleton {
  41. get { return contiguous && low == high; }
  42. }
  43. public bool IsRange {
  44. get { return !IsSingleton && !IsEmpty; }
  45. }
  46. public bool IsEmpty {
  47. get { return low > high; }
  48. }
  49. public int Size {
  50. get {
  51. if (IsEmpty)
  52. return 0;
  53. return high - low + 1;
  54. }
  55. }
  56. public bool IsDisjoint (Interval i) {
  57. if (IsEmpty || i.IsEmpty)
  58. return true;
  59. return !(low <= i.high && i.low <= high);
  60. }
  61. public bool IsAdjacent (Interval i) {
  62. if (IsEmpty || i.IsEmpty)
  63. return false;
  64. return low == i.high + 1 || high == i.low - 1;
  65. }
  66. public bool Contains (Interval i) {
  67. if (!IsEmpty && i.IsEmpty)
  68. return true;
  69. if (IsEmpty)
  70. return false;
  71. return low <= i.low && i.high <= high;
  72. }
  73. public bool Contains (int i) {
  74. return low <= i && i <= high;
  75. }
  76. public bool Intersects (Interval i) {
  77. if (IsEmpty || i.IsEmpty)
  78. return false;
  79. return ((Contains (i.low) && !Contains (i.high)) ||
  80. (Contains (i.high) && !Contains (i.low)));
  81. }
  82. public void Merge (Interval i) {
  83. if (i.IsEmpty)
  84. return;
  85. if (IsEmpty) {
  86. this.low = i.low;
  87. this.high = i.high;
  88. }
  89. if (i.low < low)
  90. low = i.low;
  91. if (i.high > high)
  92. high = i.high;
  93. }
  94. public void Intersect (Interval i) {
  95. if (IsDisjoint (i)) {
  96. low = 0;
  97. high = low - 1;
  98. return;
  99. }
  100. if (i.low > low)
  101. low = i.low;
  102. if (i.high > high)
  103. high = i.high;
  104. }
  105. public int CompareTo (object o) {
  106. return low - ((Interval)o).low;
  107. }
  108. public new string ToString () {
  109. if (IsEmpty)
  110. return "(EMPTY)";
  111. else if (!contiguous)
  112. return "{" + low + ", " + high + "}";
  113. else if (IsSingleton)
  114. return "(" + low + ")";
  115. else
  116. return "(" + low + ", " + high + ")";
  117. }
  118. }
  119. class IntervalCollection : ICollection, IEnumerable {
  120. public IntervalCollection () {
  121. intervals = new ArrayList ();
  122. }
  123. public Interval this[int i] {
  124. get { return (Interval)intervals[i]; }
  125. set { intervals[i] = value; }
  126. }
  127. public void Add (Interval i) {
  128. intervals.Add (i);
  129. }
  130. public void Clear () {
  131. intervals.Clear ();
  132. }
  133. public void Sort () {
  134. intervals.Sort ();
  135. }
  136. public void Normalize () {
  137. intervals.Sort ();
  138. int j = 0;
  139. while (j < intervals.Count - 1) {
  140. Interval a = (Interval)intervals[j];
  141. Interval b = (Interval)intervals[j + 1];
  142. if (!a.IsDisjoint (b) || a.IsAdjacent (b)) {
  143. a.Merge (b);
  144. intervals[j] = a;
  145. intervals.RemoveAt (j + 1);
  146. }
  147. else
  148. ++ j;
  149. }
  150. }
  151. public delegate double CostDelegate (Interval i);
  152. public IntervalCollection GetMetaCollection (CostDelegate cost_del) {
  153. IntervalCollection meta = new IntervalCollection ();
  154. Normalize ();
  155. Optimize (0, Count - 1, meta, cost_del);
  156. meta.intervals.Sort ();
  157. return meta;
  158. }
  159. private void Optimize (int begin, int end, IntervalCollection meta, CostDelegate cost_del) {
  160. Interval set;
  161. set.contiguous = false;
  162. int best_set_begin = -1;
  163. int best_set_end = -1;
  164. double best_set_cost = 0;
  165. for (int i = begin; i <= end; ++ i) {
  166. set.low = this[i].low;
  167. double cost = 0.0;
  168. for (int j = i; j <= end; ++ j) {
  169. set.high = this[j].high;
  170. cost += cost_del (this[j]);
  171. double set_cost = cost_del (set);
  172. if (set_cost < cost && cost > best_set_cost) {
  173. best_set_begin = i;
  174. best_set_end = j;
  175. best_set_cost = cost;
  176. }
  177. }
  178. }
  179. if (best_set_begin < 0) {
  180. // didn't find an optimal set: add original members
  181. for (int i = begin; i <= end; ++ i)
  182. meta.Add (this[i]);
  183. }
  184. else {
  185. // found set: add it ...
  186. set.low = this[best_set_begin].low;
  187. set.high = this[best_set_end].high;
  188. meta.Add (set);
  189. // ... and optimize to the left and right
  190. if (best_set_begin > begin)
  191. Optimize (begin, best_set_begin - 1, meta, cost_del);
  192. if (best_set_end < end)
  193. Optimize (best_set_end + 1, end, meta, cost_del);
  194. }
  195. }
  196. // ICollection implementation
  197. public int Count {
  198. get { return intervals.Count; }
  199. }
  200. public bool IsSynchronized {
  201. get { return false; }
  202. }
  203. public object SyncRoot {
  204. get { return intervals; }
  205. }
  206. public void CopyTo (Array array, int index) {
  207. foreach (Interval i in intervals) {
  208. if (index > array.Length)
  209. break;
  210. array.SetValue (i, index ++);
  211. }
  212. }
  213. // IEnumerator implementation
  214. public IEnumerator GetEnumerator () {
  215. return new Enumerator (intervals);
  216. }
  217. private class Enumerator : IEnumerator {
  218. public Enumerator (IList list) {
  219. this.list = list;
  220. Reset ();
  221. }
  222. public object Current {
  223. get {
  224. if (ptr >= list.Count)
  225. throw new InvalidOperationException ();
  226. return list[ptr];
  227. }
  228. }
  229. public bool MoveNext () {
  230. if (ptr > list.Count)
  231. throw new InvalidOperationException ();
  232. return ++ ptr < list.Count;
  233. }
  234. public void Reset () {
  235. ptr = -1;
  236. }
  237. private IList list;
  238. private int ptr;
  239. }
  240. // private fields
  241. private ArrayList intervals;
  242. }
  243. }