interval.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 void Merge (Interval i) {
  77. if (i.IsEmpty)
  78. return;
  79. if (IsEmpty) {
  80. this.low = i.low;
  81. this.high = i.high;
  82. }
  83. if (i.low < low)
  84. low = i.low;
  85. if (i.high > high)
  86. high = i.high;
  87. }
  88. public void Intersect (Interval i) {
  89. if (IsDisjoint (i)) {
  90. low = 0;
  91. high = low - 1;
  92. return;
  93. }
  94. if (i.low > low)
  95. low = i.low;
  96. if (i.high > high)
  97. high = i.high;
  98. }
  99. public int CompareTo (object o) {
  100. return low - ((Interval)o).low;
  101. }
  102. public new string ToString () {
  103. if (IsEmpty)
  104. return "(EMPTY)";
  105. else if (!contiguous)
  106. return "{" + low + ", " + high + "}";
  107. else if (IsSingleton)
  108. return "(" + low + ")";
  109. else
  110. return "(" + low + ", " + high + ")";
  111. }
  112. }
  113. class IntervalCollection : ICollection, IEnumerable {
  114. public IntervalCollection () {
  115. intervals = new ArrayList ();
  116. }
  117. public Interval this[int i] {
  118. get { return (Interval)intervals[i]; }
  119. set { intervals[i] = value; }
  120. }
  121. public void Add (Interval i) {
  122. intervals.Add (i);
  123. }
  124. public void Clear () {
  125. intervals.Clear ();
  126. }
  127. public void Sort () {
  128. intervals.Sort ();
  129. }
  130. public void Normalize () {
  131. intervals.Sort ();
  132. int j = 0;
  133. while (j < intervals.Count - 1) {
  134. Interval a = (Interval)intervals[j];
  135. Interval b = (Interval)intervals[j + 1];
  136. if (!a.IsDisjoint (b) || a.IsAdjacent (b)) {
  137. a.Merge (b);
  138. intervals[j] = a;
  139. intervals.RemoveAt (j + 1);
  140. }
  141. else
  142. ++ j;
  143. }
  144. }
  145. public delegate double CostDelegate (Interval i);
  146. public IntervalCollection GetMetaCollection (CostDelegate cost_del) {
  147. IntervalCollection meta = new IntervalCollection ();
  148. Normalize ();
  149. Optimize (0, Count - 1, meta, cost_del);
  150. meta.intervals.Sort ();
  151. return meta;
  152. }
  153. private void Optimize (int begin, int end, IntervalCollection meta, CostDelegate cost_del) {
  154. Interval set;
  155. set.contiguous = false;
  156. int best_set_begin = -1;
  157. int best_set_end = -1;
  158. double best_set_cost = 0;
  159. for (int i = begin; i <= end; ++ i) {
  160. set.low = this[i].low;
  161. double cost = 0.0;
  162. for (int j = i; j <= end; ++ j) {
  163. set.high = this[j].high;
  164. cost += cost_del (this[j]);
  165. double set_cost = cost_del (set);
  166. if (set_cost < cost && cost > best_set_cost) {
  167. best_set_begin = i;
  168. best_set_end = j;
  169. best_set_cost = cost;
  170. }
  171. }
  172. }
  173. if (best_set_begin < 0) {
  174. // didn't find an optimal set: add original members
  175. for (int i = begin; i <= end; ++ i)
  176. meta.Add (this[i]);
  177. }
  178. else {
  179. // found set: add it ...
  180. set.low = this[best_set_begin].low;
  181. set.high = this[best_set_end].high;
  182. meta.Add (set);
  183. // ... and optimize to the left and right
  184. if (best_set_begin > begin)
  185. Optimize (begin, best_set_begin - 1, meta, cost_del);
  186. if (best_set_end < end)
  187. Optimize (best_set_end + 1, end, meta, cost_del);
  188. }
  189. }
  190. // ICollection implementation
  191. public int Count {
  192. get { return intervals.Count; }
  193. }
  194. public bool IsSynchronized {
  195. get { return false; }
  196. }
  197. public object SyncRoot {
  198. get { return intervals; }
  199. }
  200. public void CopyTo (Array array, int index) {
  201. foreach (Interval i in intervals) {
  202. if (index > array.Length)
  203. break;
  204. array.SetValue (i, index ++);
  205. }
  206. }
  207. // IEnumerator implementation
  208. public IEnumerator GetEnumerator () {
  209. return new Enumerator (intervals);
  210. }
  211. private class Enumerator : IEnumerator {
  212. public Enumerator (IList list) {
  213. this.list = list;
  214. Reset ();
  215. }
  216. public object Current {
  217. get {
  218. if (ptr >= list.Count)
  219. throw new InvalidOperationException ();
  220. return list[ptr];
  221. }
  222. }
  223. public bool MoveNext () {
  224. if (ptr > list.Count)
  225. throw new InvalidOperationException ();
  226. return ++ ptr < list.Count;
  227. }
  228. public void Reset () {
  229. ptr = -1;
  230. }
  231. private IList list;
  232. private int ptr;
  233. }
  234. // private fields
  235. private ArrayList intervals;
  236. }
  237. }