1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
//
// assembly: System
// namespace: System.Text.RegularExpressions
// file: interval.cs
//
// author: Dan Lewis (dlewis@gmx.co.uk)
// (c) 2002
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
namespace System.Text.RegularExpressions {
struct Interval : IComparable {
public int low;
public int high;
public bool contiguous;
public static Interval Empty {
get {
Interval i;
i.low = 0;
i.high = i.low - 1;
i.contiguous = true;
return i;
}
}
public static Interval Entire {
get { return new Interval (Int32.MinValue, Int32.MaxValue); }
}
public Interval (int low, int high) {
if (low > high) {
int t = low;
low = high;
high = t;
}
this.low = low;
this.high = high;
this.contiguous = true;
}
public bool IsDiscontiguous {
get { return !contiguous; }
}
public bool IsSingleton {
get { return contiguous && low == high; }
}
public bool IsRange {
get { return !IsSingleton && !IsEmpty; }
}
public bool IsEmpty {
get { return low > high; }
}
public int Size {
get {
if (IsEmpty)
return 0;
return high - low + 1;
}
}
public bool IsDisjoint (Interval i) {
if (IsEmpty || i.IsEmpty)
return true;
return !(low <= i.high && i.low <= high);
}
public bool IsAdjacent (Interval i) {
if (IsEmpty || i.IsEmpty)
return false;
return low == i.high + 1 || high == i.low - 1;
}
public bool Contains (Interval i) {
if (!IsEmpty && i.IsEmpty)
return true;
if (IsEmpty)
return false;
return low <= i.low && i.high <= high;
}
public bool Contains (int i) {
return low <= i && i <= high;
}
public bool Intersects (Interval i) {
if (IsEmpty || i.IsEmpty)
return false;
return ((Contains (i.low) && !Contains (i.high)) ||
(Contains (i.high) && !Contains (i.low)));
}
public void Merge (Interval i) {
if (i.IsEmpty)
return;
if (IsEmpty) {
this.low = i.low;
this.high = i.high;
}
if (i.low < low)
low = i.low;
if (i.high > high)
high = i.high;
}
public void Intersect (Interval i) {
if (IsDisjoint (i)) {
low = 0;
high = low - 1;
return;
}
if (i.low > low)
low = i.low;
if (i.high > high)
high = i.high;
}
public int CompareTo (object o) {
return low - ((Interval)o).low;
}
public new string ToString () {
if (IsEmpty)
return "(EMPTY)";
else if (!contiguous)
return "{" + low + ", " + high + "}";
else if (IsSingleton)
return "(" + low + ")";
else
return "(" + low + ", " + high + ")";
}
}
class IntervalCollection : ICollection, IEnumerable {
public IntervalCollection () {
intervals = new ArrayList ();
}
public Interval this[int i] {
get { return (Interval)intervals[i]; }
set { intervals[i] = value; }
}
public void Add (Interval i) {
intervals.Add (i);
}
public void Clear () {
intervals.Clear ();
}
public void Sort () {
intervals.Sort ();
}
public void Normalize () {
intervals.Sort ();
int j = 0;
while (j < intervals.Count - 1) {
Interval a = (Interval)intervals[j];
Interval b = (Interval)intervals[j + 1];
if (!a.IsDisjoint (b) || a.IsAdjacent (b)) {
a.Merge (b);
intervals[j] = a;
intervals.RemoveAt (j + 1);
}
else
++ j;
}
}
public delegate double CostDelegate (Interval i);
public IntervalCollection GetMetaCollection (CostDelegate cost_del) {
IntervalCollection meta = new IntervalCollection ();
Normalize ();
Optimize (0, Count - 1, meta, cost_del);
meta.intervals.Sort ();
return meta;
}
private void Optimize (int begin, int end, IntervalCollection meta, CostDelegate cost_del) {
Interval set;
set.contiguous = false;
int best_set_begin = -1;
int best_set_end = -1;
double best_set_cost = 0;
for (int i = begin; i <= end; ++ i) {
set.low = this[i].low;
double cost = 0.0;
for (int j = i; j <= end; ++ j) {
set.high = this[j].high;
cost += cost_del (this[j]);
double set_cost = cost_del (set);
if (set_cost < cost && cost > best_set_cost) {
best_set_begin = i;
best_set_end = j;
best_set_cost = cost;
}
}
}
if (best_set_begin < 0) {
// didn't find an optimal set: add original members
for (int i = begin; i <= end; ++ i)
meta.Add (this[i]);
}
else {
// found set: add it ...
set.low = this[best_set_begin].low;
set.high = this[best_set_end].high;
meta.Add (set);
// ... and optimize to the left and right
if (best_set_begin > begin)
Optimize (begin, best_set_begin - 1, meta, cost_del);
if (best_set_end < end)
Optimize (best_set_end + 1, end, meta, cost_del);
}
}
// ICollection implementation
public int Count {
get { return intervals.Count; }
}
public bool IsSynchronized {
get { return false; }
}
public object SyncRoot {
get { return intervals; }
}
public void CopyTo (Array array, int index) {
foreach (Interval i in intervals) {
if (index > array.Length)
break;
array.SetValue (i, index ++);
}
}
// IEnumerator implementation
public IEnumerator GetEnumerator () {
return new Enumerator (intervals);
}
private class Enumerator : IEnumerator {
public Enumerator (IList list) {
this.list = list;
Reset ();
}
public object Current {
get {
if (ptr >= list.Count)
throw new InvalidOperationException ();
return list[ptr];
}
}
public bool MoveNext () {
if (ptr > list.Count)
throw new InvalidOperationException ();
return ++ ptr < list.Count;
}
public void Reset () {
ptr = -1;
}
private IList list;
private int ptr;
}
// private fields
private ArrayList intervals;
}
}
|