| 1 |
package structures; |
| 2 |
import java.util.ArrayList; |
| 3 |
import java.util.Collections; |
| 4 |
|
| 5 |
import shared.Tools; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
public class Range implements Comparable<Range>, Cloneable{ |
| 10 |
|
| 11 |
public Range(int aa, int bb){ |
| 12 |
assert(aa<=bb) : aa+">"+bb; |
| 13 |
a=aa; |
| 14 |
b=bb; |
| 15 |
} |
| 16 |
|
| 17 |
public Range(int aa, int bb, Object oo){ |
| 18 |
assert(aa<=bb) : aa+">"+bb; |
| 19 |
a=aa; |
| 20 |
b=bb; |
| 21 |
obj1=oo; |
| 22 |
} |
| 23 |
|
| 24 |
public Range clone() { |
| 25 |
try { |
| 26 |
return (Range) super.clone(); |
| 27 |
} catch (CloneNotSupportedException e) { |
| 28 |
throw new RuntimeException(e); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public boolean includes(int p){ |
| 35 |
return p>=a && p<=b; |
| 36 |
} |
| 37 |
|
| 38 |
public boolean intersects(int p1, int p2){ |
| 39 |
return overlaps(a, b, p1, p2); |
| 40 |
} |
| 41 |
|
| 42 |
public <K extends Feature> boolean overlaps(K f) { |
| 43 |
return intersects(f.start(), f.stop()); |
| 44 |
} |
| 45 |
|
| 46 |
public boolean overlaps(Range f) { |
| 47 |
return intersects(f.a, f.b); |
| 48 |
} |
| 49 |
|
| 50 |
public int overlap(Range f) { |
| 51 |
return overlap(a, b, f.a, f.b); |
| 52 |
} |
| 53 |
|
| 54 |
public boolean adjacent(int p1, int p2) { |
| 55 |
return adjacent(a, b, p1, p2); |
| 56 |
} |
| 57 |
|
| 58 |
public boolean adjacent(int p1) { |
| 59 |
return adjacent(a, b, p1); |
| 60 |
} |
| 61 |
|
| 62 |
public boolean touches(int p1, int p2) { |
| 63 |
return touch(a, b, p1, p2); |
| 64 |
} |
| 65 |
|
| 66 |
public boolean touches(int p1) { |
| 67 |
return touch(a, b, p1); |
| 68 |
} |
| 69 |
|
| 70 |
public boolean includes(int p1, int p2){ |
| 71 |
return include(a, b, p1, p2); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public boolean intersects(Range r){ |
| 77 |
return intersects(r.a, r.b); |
| 78 |
} |
| 79 |
|
| 80 |
public boolean touches(Range r){ |
| 81 |
return (intersects(r.a, r.b) || adjacent(r.a, r.b)); |
| 82 |
} |
| 83 |
|
| 84 |
public boolean includes(Range r){ |
| 85 |
return includes(r.a, r.b); |
| 86 |
} |
| 87 |
|
| 88 |
public int length() { |
| 89 |
return b-a+1; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public Range merge(Range r){ |
| 95 |
assert(touches(r)); |
| 96 |
Range r2=new Range(min(a, r.a), max(b, r.b), obj1); |
| 97 |
|
| 98 |
assert(r2.includes(this)); |
| 99 |
assert(r2.includes(r)); |
| 100 |
assert(r2.length()<=length()+r.length()); |
| 101 |
return r2; |
| 102 |
} |
| 103 |
|
| 104 |
public void absorb(Range r){ |
| 105 |
assert(touches(r)); |
| 106 |
a=min(a, r.a); |
| 107 |
b=max(b, r.b); |
| 108 |
} |
| 109 |
|
| 110 |
public void absorb(int p){ |
| 111 |
assert(touches(p)); |
| 112 |
a=min(a, p); |
| 113 |
b=max(b, p); |
| 114 |
} |
| 115 |
|
| 116 |
public void fixBounds(int length) { |
| 117 |
a=Tools.max(0, a); |
| 118 |
b=Tools.min(length-1, b); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public int hashCode(){ |
| 123 |
return Integer.rotateLeft(~a, 16)^b; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public boolean equals(Object r){ |
| 128 |
return equals((Range)r); |
| 129 |
} |
| 130 |
|
| 131 |
public boolean equals(Range r){ |
| 132 |
return a==r.a && b==r.b; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public int compareTo(Range r) { |
| 137 |
if(a!=r.a) {return a-r.a;} |
| 138 |
return b-r.b; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public String toString(){ |
| 143 |
return a+(a==b ? "" : ("-"+b)); |
| 144 |
} |
| 145 |
|
| 146 |
public ByteBuilder appendTo(ByteBuilder bb){ |
| 147 |
bb.append(a); |
| 148 |
return (b==a ? bb : bb.dash().append(b)); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public static int mergeList(ArrayList<Range> ranges, boolean sort) { |
| 154 |
if(ranges.size()<2){return 0;} |
| 155 |
if(sort){Collections.sort(ranges);} |
| 156 |
|
| 157 |
Range current=ranges.get(0); |
| 158 |
int removed=0; |
| 159 |
for(int i=1; i<ranges.size(); i++) { |
| 160 |
Range r=ranges.get(i); |
| 161 |
if(current.touches(r)) { |
| 162 |
current.absorb(r); |
| 163 |
ranges.set(i, null); |
| 164 |
removed++; |
| 165 |
}else{ |
| 166 |
current=r; |
| 167 |
} |
| 168 |
} |
| 169 |
if(removed>0){ |
| 170 |
Tools.condenseStrict(ranges); |
| 171 |
} |
| 172 |
return removed; |
| 173 |
} |
| 174 |
|
| 175 |
public static boolean include(int a1, int b1, int p){ |
| 176 |
assert(a1<=b1) : a1+", "+b1+", "+p; |
| 177 |
return p>=a1 && p<=b1; |
| 178 |
} |
| 179 |
|
| 180 |
public static boolean include(int a1, int b1, int a2, int b2){ |
| 181 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 182 |
return a2>=a1 && b2<=b1; |
| 183 |
} |
| 184 |
|
| 185 |
public static boolean overlaps(int a1, int b1, int a2, int b2){ |
| 186 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 187 |
return a2<=b1 && b2>=a1; |
| 188 |
} |
| 189 |
|
| 190 |
public static int overlap(int a1, int b1, int a2, int b2){ |
| 191 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 192 |
if(a1>a2) {return overlap(a2, b2, a1, b1);} |
| 193 |
assert(a1<=a2); |
| 194 |
|
| 195 |
if(b1<a2) {return b1-a2+1;} |
| 196 |
else if(b2<=b1) {return b2-a2+1;} |
| 197 |
return b1-a2+1; |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
public static int lopsidedness(int a1, int b1, int a2, int b2){ |
| 202 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 203 |
if(a1>a2) {return lopsidedness(a2, b2, a1, b1);} |
| 204 |
assert(a1<=a2); |
| 205 |
int left=a2-a1; |
| 206 |
int right=b2-b1; |
| 207 |
return Tools.mid(0, left, right); |
| 208 |
} |
| 209 |
|
| 210 |
public static boolean touch(int a1, int b1, int p){ |
| 211 |
assert(a1<=b1) : a1+", "+b1+", "+p; |
| 212 |
return p<=b1+1 && p>=a1-1; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
public static boolean touch(int a1, int b1, int a2, int b2){ |
| 217 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 218 |
return a2<=b1+1 && b2>=a1-1; |
| 219 |
} |
| 220 |
|
| 221 |
public static boolean adjacent(int a1, int b1, int p){ |
| 222 |
assert(a1<=b1) : a1+", "+b1+", "+p; |
| 223 |
return p==b1+1 || p==a1-1; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
public static boolean adjacent(int a1, int b1, int a2, int b2){ |
| 228 |
assert(a1<=b1 && a2<=b2) : a1+", "+b1+", "+a2+", "+b2; |
| 229 |
return a2==b1+1 || b2==a1-1; |
| 230 |
} |
| 231 |
|
| 232 |
private static final int min(int x, int y){return x<y ? x : y;} |
| 233 |
private static final int max(int x, int y){return x>y ? x : y;} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static <K extends Feature> Range[] toRanges(ArrayList<K> features){ |
| 240 |
if(features==null || features.isEmpty()) { |
| 241 |
assert(false) : "No features found."; |
| 242 |
return null; |
| 243 |
} |
| 244 |
IntList starts=new IntList(features.size()); |
| 245 |
IntList stops=new IntList(features.size()); |
| 246 |
for(K f : features) { |
| 247 |
if(f.stop()>=f.start()) { |
| 248 |
starts.add(f.start()); |
| 249 |
stops.add(f.stop()); |
| 250 |
}else { |
| 251 |
assert(false); |
| 252 |
} |
| 253 |
} |
| 254 |
starts.sort(); |
| 255 |
stops.sort(); |
| 256 |
starts.shrinkToUnique(); |
| 257 |
stops.shrinkToUnique(); |
| 258 |
ArrayList<Range> ranges=new ArrayList<Range>(starts.size+stops.size); |
| 259 |
int i=0, j=0; |
| 260 |
int openFeatures=0; |
| 261 |
int lastPoint=-1; |
| 262 |
boolean lastPointWasStart=false; |
| 263 |
int a=-1, b=-1; |
| 264 |
while(i<starts.size && j<stops.size) { |
| 265 |
a=starts.get(i); |
| 266 |
b=stops.get(j); |
| 267 |
if(a<=b) { |
| 268 |
|
| 269 |
if(lastPoint>=0) { |
| 270 |
Range r=null; |
| 271 |
if(lastPointWasStart) { |
| 272 |
if(lastPoint<=a-1) { |
| 273 |
|
| 274 |
r=new Range(lastPoint, a-1); |
| 275 |
} |
| 276 |
}else { |
| 277 |
if(lastPoint+1<=a-1) { |
| 278 |
|
| 279 |
r=new Range(lastPoint+1, a-1); |
| 280 |
} |
| 281 |
} |
| 282 |
if(r!=null) {ranges.add(r);} |
| 283 |
} |
| 284 |
lastPoint=a; |
| 285 |
lastPointWasStart=true; |
| 286 |
openFeatures++; |
| 287 |
i++; |
| 288 |
}else { |
| 289 |
|
| 290 |
assert(lastPoint>=0); |
| 291 |
Range r=null; |
| 292 |
if(lastPointWasStart) { |
| 293 |
if(lastPoint<=b) { |
| 294 |
|
| 295 |
r=new Range(lastPoint, b); |
| 296 |
} |
| 297 |
}else{ |
| 298 |
if(lastPoint+1<=b) { |
| 299 |
|
| 300 |
r=new Range(lastPoint+1, b); |
| 301 |
} |
| 302 |
} |
| 303 |
if(r!=null) {ranges.add(r);} |
| 304 |
lastPoint=b; |
| 305 |
lastPointWasStart=false; |
| 306 |
openFeatures--; |
| 307 |
j++; |
| 308 |
} |
| 309 |
} |
| 310 |
assert(i>=starts.size); |
| 311 |
while(j<stops.size) { |
| 312 |
b=stops.get(j); |
| 313 |
|
| 314 |
assert(lastPoint>=0); |
| 315 |
Range r; |
| 316 |
if(lastPointWasStart) { |
| 317 |
r=new Range(lastPoint, b); |
| 318 |
}else{ |
| 319 |
try { |
| 320 |
r=new Range(lastPoint+1, b); |
| 321 |
} catch (Throwable e) { |
| 322 |
System.err.println("Error! lastPointWasStart="+lastPointWasStart+", lastPoint="+lastPoint+"\nstarts="+starts+"\nstops="+stops); |
| 323 |
throw new RuntimeException(e); |
| 324 |
} |
| 325 |
} |
| 326 |
ranges.add(r); |
| 327 |
lastPoint=b; |
| 328 |
lastPointWasStart=false; |
| 329 |
openFeatures--; |
| 330 |
j++; |
| 331 |
} |
| 332 |
return ranges.isEmpty() ? null : ranges.toArray(new Range[ranges.size()]); |
| 333 |
} |
| 334 |
|
| 335 |
public static <K extends Feature> void populateRanges(Range[] ranges, ArrayList<K> features) { |
| 336 |
for(K f : features) { |
| 337 |
|
| 338 |
|
| 339 |
int index=findIndex(f.start(), ranges); |
| 340 |
if(index>-1) { |
| 341 |
for(int i=index; i<ranges.length; i++) { |
| 342 |
Range r=ranges[i]; |
| 343 |
if(r.overlaps(f)) { |
| 344 |
ArrayList<K> list=(ArrayList<K>) r.obj1; |
| 345 |
if(list==null) {r.obj1=list=new ArrayList<K>(2);} |
| 346 |
list.add(f); |
| 347 |
}else if(r.a>f.stop()) { |
| 348 |
break; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
public static int findIndex(int p, Range[] ranges) { |
| 356 |
final int x=findIndexBinary(p, ranges); |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
return x; |
| 363 |
} |
| 364 |
|
| 365 |
private static int findIndexLinear(int p, Range[] ranges) { |
| 366 |
if(verbose) {System.err.println("findIndexLinear("+p+", ranges)");} |
| 367 |
|
| 368 |
for(int i=0; i<ranges.length; i++){ |
| 369 |
Range r=ranges[i]; |
| 370 |
if(verbose) {System.err.println("p="+p+", r="+r);} |
| 371 |
|
| 372 |
if(p<r.a) { |
| 373 |
if(verbose) {System.err.println("returning "+(-i-1));} |
| 374 |
return -i-1; |
| 375 |
}else if(p<=r.b) { |
| 376 |
if(verbose) {System.err.println("returning "+i);} |
| 377 |
return i; |
| 378 |
} |
| 379 |
} |
| 380 |
if(verbose) {System.err.println("returning "+(-ranges.length-1));} |
| 381 |
return -ranges.length-1; |
| 382 |
} |
| 383 |
|
| 384 |
private static int findIndexBinary(int p, Range[] ranges) { |
| 385 |
|
| 386 |
|
| 387 |
if(verbose) {System.err.println("findIndexBinary("+p+", ranges)");} |
| 388 |
int a=0, b=ranges.length-1; |
| 389 |
while(a<=b) { |
| 390 |
final int mid=(a+b)/2; |
| 391 |
final Range r=ranges[mid]; |
| 392 |
if(verbose) {System.err.println("a="+a+", b="+b+", p="+p+", r="+r);} |
| 393 |
if(p<r.a) {b=mid-1;} |
| 394 |
else if(p>r.b) {a=mid+1;} |
| 395 |
else { |
| 396 |
if(verbose) {System.err.println("Returning "+mid);} |
| 397 |
|
| 398 |
return mid; |
| 399 |
} |
| 400 |
} |
| 401 |
assert(a>b); |
| 402 |
final int ret=-max(a, b)-1; |
| 403 |
if(verbose) {System.err.println("Returning "+ret);} |
| 404 |
|
| 405 |
return ret; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public static <K extends Feature> int findIndexAfterBinary(int p, ArrayList<K> list) { |
| 411 |
|
| 412 |
if(list.isEmpty()) {return -1;} |
| 413 |
int a=0, b=list.size()-1; |
| 414 |
while(a<b) { |
| 415 |
int mid=(a+b)/2; |
| 416 |
K f=list.get(mid); |
| 417 |
if(p<f.start()) {b=mid-1;} |
| 418 |
else{a=mid+1;} |
| 419 |
} |
| 420 |
assert(a==b); |
| 421 |
|
| 422 |
|
| 423 |
K fm1=(a==0 ? null : list.get(a-1)); |
| 424 |
K f=list.get(a); |
| 425 |
K fp1=(a==list.size()-1 ? null : list.get(a+1)); |
| 426 |
assert((f.start()>p && (fm1==null || fm1.start()<=p)) || |
| 427 |
(f.start()<=p && (fp1==null || fp1.start()>p))); |
| 428 |
while(a<list.size() && list.get(a).start()<=p) {a++;} |
| 429 |
return a; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public static Range toRange(String s){ |
| 459 |
String[] s2=s.replace("[","").replace("]","").replace("(","").replace(")","").replace(",","").split("-"); |
| 460 |
|
| 461 |
int a, b; |
| 462 |
if(s2.length==1){ |
| 463 |
a=b=Integer.parseInt(s2[0]); |
| 464 |
}else{ |
| 465 |
a=Integer.parseInt(s2[0]); |
| 466 |
b=Integer.parseInt(s2[1]); |
| 467 |
} |
| 468 |
return new Range(a, b); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public int a; |
| 591 |
public int b; |
| 592 |
|
| 593 |
public Object obj1=null; |
| 594 |
public static final boolean verbose=false; |
| 595 |
|
| 596 |
} |