-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrange_functions.h
1708 lines (1377 loc) · 36.6 KB
/
range_functions.h
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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
459
460
461
462
463
464
465
466
467
468
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
* BaitFilter (version 1.0.6) a program for selecting optimal bait regions
* Copyright 2013-2017 by Christoph Mayer
*
* This source file is part of the BaitFisher-package.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
*
*
* For any enquiries send an Email to Christoph Mayer
*
* When publishing work that is based on the results please cite:
* Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
*
*/
#ifndef RANGE_FUNCTIONS_H
#define RANGE_FUNCTIONS_H
#include <iostream>
#include <list>
#include <cstdlib>
#include "faststring2.h"
#include <cerrno>
#include <iterator>
#include <vector>
#define MACRO_MAX(x,y) ((x)<(y) ? (y) : (x))
#define MACRO_MIN(x,y) ((x)<(y) ? (x) : (y))
// This is a range class for unsigned numbers not for double numbers, which would
// be interesting in its own right.
// In principle one could make a template class out of this.
//
// General conventions:
// - ranges are undirected
// - start <= end
// - end is position after last position in range
// - ranges can be 0 or 1 based. Be careful!!!!
// - the preferred way to represent a vanishing range is (0,0).
// TODO: See comments in functions for which this convention does not apply.
// TODO: Several functions work independent of a convention here.
//
class CRangeList;
class Crange
{
friend class CRangeList;
unsigned b;
unsigned e;
public:
Crange(unsigned pb, unsigned pe):b(pb), e(pe)
{
order();
}
void order() // According to the conventions above: b <= e
{
if (b > e)
{
unsigned tmp = b;
b = e;
e = tmp;
}
}
friend unsigned overlap(Crange r1, Crange r2)
{
unsigned overlap_start = MACRO_MAX(r1.b, r2.b);
unsigned overlap_end = MACRO_MIN(r1.e, r2.e);
if (overlap_end > overlap_start)
return overlap_end - overlap_start;
else
return 0;
}
friend Crange overlap_range(Crange r1, Crange r2)
{
unsigned overlap_start = MACRO_MAX(r1.b, r2.b);
unsigned overlap_end = MACRO_MIN(r1.e, r2.e);
if (overlap_end > overlap_start)
{
Crange res(overlap_start, overlap_end);
return res;
}
else
return Crange(0,0);
}
friend void overlap_range(Crange r1, Crange r2, Crange &res1, Crange &res_over, Crange &res2)
{
unsigned overlap_start = MACRO_MAX(r1.b, r2.b);
unsigned overlap_end = MACRO_MIN(r1.e, r2.e);
unsigned res1_start = MACRO_MIN(r1.b, r2.b);
unsigned res2_end = MACRO_MAX(r1.e, r2.e);
if (overlap_end > overlap_start)
{
res_over.assign(overlap_start, overlap_end);
res1.assign(res1_start, overlap_start);
res2.assign(overlap_end, res2_end);
}
else
{
res_over.assign(0, 0);
if (r1 < r2)
{
res1 = r1;
res2 = r2;
}
else
{
res1 = r2;
res2 = r1;
}
}
}
friend double overlap_proportion(Crange r1, Crange r2)
{
unsigned overlap_start = MACRO_MAX(r1.b, r2.b);
unsigned overlap_end = MACRO_MIN(r1.e, r2.e);
if (overlap_end > overlap_start)
{
double overl = overlap_end - overlap_start;
double l = MACRO_MIN(r1.len(), r2.len());
return (overl/l);
}
else
return 0.0;
}
friend bool adjacent_or_overlap(Crange r1, Crange r2)
{
unsigned overlap_start = MACRO_MAX(r1.b, r2.b);
unsigned overlap_end = MACRO_MIN(r1.e, r2.e);
if (overlap_end >= overlap_start)
return true; // == means they are adjacent
else
return false;
}
Crange()
{
b = e = 0;
}
friend void swap(Crange &r1, Crange &r2)
{
Crange tmp = r2;
r2 = r1;
r1 = tmp;
}
friend void swap(Crange *r1, Crange *r2)
{
Crange tmp = *r2;
*r2 = *r1;
*r1 = tmp;
}
bool empty() const
{
return (b == e);
}
void limit_to_range(Crange limit_r)
{
// Nothing to be done in this case:
if (is_r2_Subrange_of_r1(limit_r, *this) )
return;
b = MACRO_MAX(limit_r.b, b);
e = MACRO_MIN(limit_r.e, e);
if (b >= e)
b = e = 0;
}
friend bool operator==(Crange r1, Crange r2)
{
return (r1.b == r2.b && r1.e == r2.e);
}
void assign(unsigned pb, unsigned pe)
{
b = pb;
e = pe;
order();
}
// true if r1 starts before r2
// true if r1 starts with r2 and if r1 is shorter
// r1 *******
// r2 *********** r1 < r2 is true
friend bool operator<(Crange r1, Crange r2)
{
if (r1.b != r2.b)
return r1.b < r2.b;
return r1.e < r2.e;
}
friend bool equalRange(Crange r1, Crange r2)
{
return (r1 == r2);
}
friend bool is_r2_Subrange_of_r1(Crange r1, Crange r2)
{
return (r2.b >= r1.b && r2.e <= r1.e);
}
friend Crange range_intersection(Crange r1, Crange r2)
{
Crange res( MACRO_MAX(r1.b,r2.b), MACRO_MIN(r1.e,r2.e));
if (res.b >= res.e)
res.e = res.b = 0;
return res;
}
friend bool before(Crange r1, Crange r2)
{
return r1.e <= r2.b;
}
friend bool after(Crange r1, Crange r2)
{
return r1.b >= r2.e;
}
// formerly called range_union, which was a misnomer.
// friend ......
friend Crange range_span(Crange r1, Crange r2)
{
Crange res( MACRO_MIN(r1.b,r2.b), MACRO_MAX(r1.e,r2.e));
return res;
}
void shift (unsigned s)
{
b +=s;
e +=s;
}
unsigned begin() const
{
return b;
}
unsigned end() const
{
return e;
}
unsigned inclusive_end() const
{
return e-1;
}
unsigned len() const
{
return e-b;
}
bool point_is_in_range(unsigned x) const
{
if (x>=b && x < e)
return true;
else
return false;
}
bool point_is_behind_range(unsigned x) const
{
if (x >= e)
return true;
else
return false;
}
bool point_is_before_range(unsigned x) const
{
if (x < b)
return true;
else
return false;
}
void print_with_brackets(std::ostream &os) const
{
os << "(" << b << "," << e << ")";
}
void print_with_brackets_inclusive_end(std::ostream &os) const
{
os << "(" << b << "," << (e-1) << ")";
}
void print_minus_deliminated_inclusive_end(std::ostream &os) const
{
os << b << "-" << (e-1);
}
void transform_start_end(int mult_start, int add_start, int mult_end, int add_end)
{
b = b*mult_start + add_start;
e = e*mult_end + add_end;
}
void transform_coord(std::vector<unsigned> &coord_trans)
{
// We subtract 1 from e before we look up this index in the field, since the end index of a range is 1 behind its last element.
if ( e-1 >= coord_trans.size() )
{
std::cerr << "Index out of range: " << e << std::endl;
exit(-33);
}
b = coord_trans[b];
e = coord_trans[e-1]+1; // We transform the coord of the last element and add 1.
}
}; // class Crange
/* class CRangeList_array */
/* { */
/* Crange limit; */
/* faststring rl_array; */
/* CRangeList_array(Crang l):limit(l), faststring('\0', l.end()-l.begin()+1) */
/* {} */
/* Crange get_limit() */
/* { */
/* return limit; */
/* } */
/* unsigned get_limit_begin() */
/* { */
/* return limit.begin(); */
/* } */
/* unsigned get_limit_end() */
/* { */
/* return limit.end(); */
/* } */
/* }; */
// NOTE: Code referring to array is either experimental or unfinished.
//
//
class CRangeList
{
std::list<Crange> rl;
bool sorted;
unsigned lower_bound;
unsigned upper_bound; // According to the convention this position comes after the last position in this list of ranges.
unsigned count; // Introduced since the empty() function could be
// via size()==0, whereas size could be O(n)
// which might be very inefficient.
// rl_array is not fully implemented: In the future it could be used to store "in" and "out of" the range list
// by setting chars to 1 or 0, which would make many functions more efficient.
faststring *rl_array;
public:
typedef std::list<Crange>::iterator CRangeList_iterator;
typedef std::list<Crange>::const_iterator CRangeList_iterator_const;
// Default constructor:
// Caution: lower_bound and upper_bound are set to 0, since they are needed by
// some functions. This is better than arbitrary values.
// even though one could say the values are not correct.
// But arbitrary values are not more correct than 0.
// Take care that all member functions set these variables
// if the first element of a list is added.
CRangeList():sorted(false), lower_bound(0), upper_bound(0), count(0), rl_array(NULL)
{}
// Copy constructor
CRangeList(const CRangeList &a):rl(a.rl), lower_bound(a.lower_bound), upper_bound(a.upper_bound), count(a.count), rl_array(NULL)
{
if (a.rl_array != NULL)
{
rl_array = new faststring(*a.rl_array);
}
}
void clear_rl_array()
{
if (rl_array)
{
delete rl_array;
rl_array = NULL;
}
}
~CRangeList()
{
clear_rl_array();
}
public:
void add(unsigned pb, unsigned pe)
{
Crange new_r (pb, pe);
if (new_r.empty())
return;
clear_rl_array();
// If the new element is > than the last element in the list,
// sorting is not affected, otherwise "it is false".
// Note: We cannot set sorted to true, if the rest of the list is not sorted.
if (count == 0)
{
sorted = true;
lower_bound = new_r.b;
upper_bound = new_r.e;
rl.push_back(new_r);
}
else
{
if ( !(rl.back() < new_r) )
sorted = false;
// else - do not change sorted status
if (lower_bound > new_r.b)
lower_bound = new_r.b;
if (upper_bound < new_r.e)
upper_bound = new_r.e;
rl.push_back(new_r);
}
++count;
}
CRangeList_iterator_const begin() const
{
return rl.begin();
}
CRangeList_iterator_const begin()
{
return rl.begin();
}
CRangeList_iterator_const end() const
{
return rl.end();
}
CRangeList_iterator_const end()
{
return rl.end();
}
// Test before using this function
void create_reset_rl_array()
{
if (rl_array)
{
rl_array->assign(upper_bound-lower_bound, '\0');
}
else
{
rl_array = new faststring('\0', upper_bound-lower_bound);
}
}
// Test before using this function
void fill_rl_array()
{
if (rl_array == NULL)
create_reset_rl_array();
CRangeList_iterator_const it = rl.begin();
CRangeList_iterator_const it_end = rl.end();
unsigned pos1, pos2;
// foreach range:
while (it != it_end)
{
pos1 = it->begin();
pos2 = it->end();
while (pos1 < pos2)
{
rl_array[pos1] = 1;
++pos1;
}
++it;
}
}
void add(Crange new_r)
{
// If the new element is > than the last element in the list,
// sorting is not affected, otherwise "it is false".
// Note: We cannot set sorted to true, if the rest of the list is not sorted.
clear_rl_array();
if (new_r.empty())
return;
if (count == 0)
{
sorted = true;
lower_bound = new_r.b;
upper_bound = new_r.e;
rl.push_back(new_r);
}
else
{
if ( !(rl.back() < new_r) )
sorted = false;
// else - do not change sorted status
if (lower_bound > new_r.b)
lower_bound = new_r.b;
if (upper_bound < new_r.e)
upper_bound = new_r.e;
rl.push_back(new_r);
}
++count;
}
void add(const CRangeList &a)
{
CRangeList_iterator_const it = a.rl.begin();
CRangeList_iterator_const it_end = a.rl.end();
while (it != it_end)
{
if ( !it->empty() )
add(*it);
++it;
}
}
bool add(faststring str)
{
faststring::size_t pos1=0, pos2, len=str.size();
unsigned c1,c2;
while (pos1 < len && str[pos1] != ';')
{
str.skip_symbol(pos1,' ');
errno = 0;
c1 = str.ToUnsigned(pos1, pos2);
// std::cerr << "c1 " << c1 << std::endl;
if (errno != 0)
{
return false;
}
str.skip_symbol(pos2,' ');
if (str[pos2] == ',' || str[pos2] == ';') // One number range.
{
add(c1,c1+1);
}
else if (str[pos2] == '-') // Two number range
{
pos1 = pos2+1; // Be careful '-' is read as minus
str.skip_symbol(pos1,' ');
// std::cerr << "pos1 " << pos1 << std::endl;
errno = 0;
c2 = str.ToUnsigned(pos1, pos2);
// std::cerr << "c2 " << c2 << std::endl;
if (errno != 0)
{
return false;
}
// std::cerr << "pos2 " << pos2 << std::endl;
str.skip_symbol(pos2,' ');
if (str[pos2] > '\0' && str[pos2] <= '\7')
{
unsigned tmp = c1;
unsigned delta = str[pos2]-'\0';
if (errno != 0)
{
return false;
}
++pos2;
str.skip_symbol(pos2,' ');
while (tmp < c2)
{
add(tmp, tmp+1);
tmp += delta;
}
}
else
{
add(c1, c2+1);
}
}
else
{
return false;
}
pos1 = pos2;
if (str[pos1] == ',') // we are not done yet - the list continues
{
++pos1;
str.skip_symbol(pos1,' ');
if (str[pos1] == ';')
return false;
}
}
return true;
}
bool add_delta(faststring str)
{
faststring::size_t pos1=0, pos2, len=str.size();
unsigned c1,c2;
while (pos1 < len && str[pos1] != ';')
{
str.skip_symbol(pos1,' ');
errno = 0;
c1 = str.ToUnsigned(pos1, pos2);
// std::cerr << "c1 " << c1 << std::endl;
if (errno != 0)
{
return false;
}
str.skip_symbol(pos2,' ');
if (str[pos2] == ',' || str[pos2] == ';') // One number range.
{
add(c1,c1+1);
}
else if (str[pos2] == '-') // Two number range
{
pos1 = pos2+1; // Be careful '-' is read as minus
str.skip_symbol(pos1,' ');
// std::cerr << "pos1 " << pos1 << std::endl;
errno = 0;
c2 = str.ToUnsigned(pos1, pos2);
// std::cerr << "c2 " << c2 << std::endl;
if (errno != 0)
{
return false;
}
// std::cerr << "pos2 " << pos2 << std::endl;
str.skip_symbol(pos2,' ');
if (str[pos2] == '\\')
{
++pos2;
str.skip_symbol(pos2,' ');
char d = str[pos2];
unsigned tmp = c1;
unsigned delta = d-'0';
if (errno != 0)
{
return false;
}
++pos2;
str.skip_symbol(pos2,' ');
while (tmp < c2)
{
add(tmp, tmp+1);
tmp += delta;
}
}
else
{
add(c1, c2+1);
}
}
else
{
return false;
}
pos1 = pos2;
if (str[pos1] == ',') // we are not done yet - the list continues
{
++pos1;
str.skip_symbol(pos1,' ');
if (str[pos1] == ';')
return false;
}
}
return true;
}
unsigned size() const
{
return count;
}
// Assignment operator: Use standard assignment operator. No pointer content needs to be copied
// This is still true since rl_array is not ready to use yet.
// Currently there is no need/no plan to implement it.
void clear()
{
rl.clear(); // Will call the destructor for all objects.
sorted = false;
clear_rl_array();
// I am not sure this is the best way to assign values to the bounds.
// Bounds are not well defined for an empty list.
lower_bound = upper_bound = 0;
count = 0;
}
void sort()
{
if (!sorted)
{
rl.sort();
sorted = true;
}
}
// We expect the use to check for count == 0
Crange bounds()
{
// There is one problem here: The last entry need to have the "highest coordinate"
// 100-150, 200-400, 250-300
Crange res(lower_bound, upper_bound);
return res;
}
// We expect the use to check for count == 0
unsigned get_lower_bound()
{
return lower_bound;
}
// We expect the use to check for count == 0
unsigned get_upper_bound()
{
return upper_bound;
}
// Find an island of overlapping ranges, starting at it_beg.
// The initial value of it_end (the second parameter) is overwritten,
// and is therefore irrelevant.
// When leaving this function, it_end points to the range behind the island.
// This is also true, if the island is the last island in the list.
// In this case, it_end == to list_of_sat_ptr.end()
unsigned findOverlappingIsland(CRangeList_iterator_const it_beg,
CRangeList_iterator_const &it_end) const
{
unsigned count=1;
it_end = it_beg;
++it_end;
while (it_end != rl.end())
{
if (overlap(*it_beg, *it_end)) // If they overlap, we move it_end,
{
++count;
++it_end;
}
else // if they do not overlap any more, we need not be out of the island.
{ // Let us move it_beg, which should now be behind or equal to it_end.
++it_beg;
if (it_beg == it_end) // this is the case of the last two sats do not overlap
break; // In this case it_end points one behind the last island.
}
}
return count;
}
unsigned findAdjacentIsland(CRangeList_iterator_const it_beg,
CRangeList_iterator_const &it_end) const
{
unsigned count=1; // The current range is the Nr 1.
it_end = it_beg;
++it_end;
while (it_end != rl.end())
{
if (adjacent_or_overlap(*it_beg, *it_end)) // If they overlap, we move it_end,
{
++count;
++it_end;
}
else // if they do not overlap any more, we need not be out of the island.
{ // Let us move it_beg, which should now be behind or equal to it_end.
++it_beg;
if (it_beg == it_end) // this is the case of the last two sats do not overlap
break; // In this case it_end points one behind the last island.
}
}
return count;
}
void findRangeWithMaxExtToRight(CRangeList_iterator_const it_beg,
CRangeList_iterator_const it_end,
CRangeList_iterator_const &it_extends_most) const
{
it_extends_most = it_beg;
if (it_beg == it_end)
return;
++it_beg;
while (it_beg != it_end)
{
if ((*it_beg).e > (*it_extends_most).e )
it_extends_most = it_beg;
++it_beg;
}
}
Crange range_of_island(CRangeList_iterator_const it_beg,
CRangeList_iterator_const &it_end_of_island)
{
Crange res;
CRangeList_iterator_const it_extends_most;
if (!sorted)
{
std::cerr << "Error: range_of_island requires the list of ranges to be sorted." << std::endl;
exit(0);
}
res.b = it_beg->b;
findOverlappingIsland(it_beg, it_end_of_island);
findRangeWithMaxExtToRight(it_beg, it_end_of_island, it_extends_most);
res.e = it_extends_most->e;
return res;
}
Crange range_of_adjacentisland(CRangeList_iterator_const it_beg,
CRangeList_iterator_const &it_end_of_island)
{
Crange res;
CRangeList_iterator_const it_extends_most;
if (!sorted)
{
std::cerr << "Internal error: range_of_adjacentisland requires the list of ranges to be sorted." << std::endl;
exit(0);
}
res.b = it_beg->b;
findAdjacentIsland(it_beg, it_end_of_island);
findRangeWithMaxExtToRight(it_beg, it_end_of_island, it_extends_most);
res.e = it_extends_most->e;
return res;
}
// coverage_len allows us to specify a range - double check that feature.
double coverage_rel(Crange bds)
{
CRangeList rl_tmp(*this);
rl_tmp.limit_to_range(bds);
double bds_len = bds.len();
return rl_tmp.coverage_len(bds)/bds_len;
}
unsigned coverage_len2(Crange bds)
{
CRangeList rl2;
get_consolidated_range_list(rl2);
rl2.limit_to_range(bds);
return rl2.coverage_raw();
}
unsigned coverage_raw()
{
unsigned lencount=0;
CRangeList_iterator_const it, it_end;
it = rl.begin();
it_end = rl.end();
while (it != it_end)
{
lencount += it->len();
++it;
}
return lencount;
}
// Side effect: *this will be sorted
unsigned coverage_len(Crange bds) // bounds in which to compute the coverage
{
unsigned cover = 0;
CRangeList_iterator_const it, it_end, end_island;
Crange r1, r2;
// std::cerr << "Computing coverage for this: " << std::endl;
// print_bracketed_list(std::cerr);
// std::cerr << std::endl;
sort();
it = rl.begin();
it_end = rl.end();
while (it != it_end && it->e < bds.b)
++it;
if (it == it_end)
return 0;
// it points to first range after start of bds
r1 = range_of_island(it, end_island);
cover += overlap(r1, bds);
it = end_island;
// std::cerr << "First overlap: " << cover << std::endl;
while (it != it_end && it->b < bds.e)
{
r1 = range_of_island(it, end_island);
it = end_island;
if (r1.e > bds.e)
{
cover += overlap(r1, bds);
break;
}
else