annotate region_motif_lib/regions.cpp @ 0:19d2cffb8db3 draft

Initial upload
author jeremyjliu
date Wed, 06 Aug 2014 15:36:46 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
1 #include <iostream>
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
2 #include <vector>
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
3 #include <algorithm>
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
4 using namespace std;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
5
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
6 extern "C" {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
7 typedef pair <int,int> Se_t;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
8 bool se_lt (const Se_t &l,const Se_t &r) { return l.first < r.first; }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
9
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
10 void merge_regions(int *regions, int*nregionsR,int *merge_sepR) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
11 int nregs=nregionsR[0];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
12 if(nregs==0) return;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
13 int sep=merge_sepR[0];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
14 if(sep<1) sep=1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
15 vector<Se_t> reg(nregs);
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
16 for(int ireg=0;ireg<nregs;ireg++) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
17 reg[ireg]=make_pair(regions[ireg*2],regions[ireg*2+1]);
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
18 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
19 sort(reg.begin(),reg.end(),se_lt);
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
20 int *reg_index = new int[nregs];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
21 for(int ireg=1;ireg<nregs;ireg++) reg_index[ireg]=-1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
22 reg_index[0]=0;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
23 int last_ireg=0;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
24 int counter=1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
25 for(int ireg=1;ireg<nregs;ireg++) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
26 if(reg[ireg].first<=reg[last_ireg].second+sep) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
27 if(reg[ireg].second>reg[last_ireg].second) reg[last_ireg].second=reg[ireg].second;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
28 } else {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
29 last_ireg=ireg;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
30 reg_index[counter]=ireg;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
31 counter++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
32 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
33 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
34 for(int ireg=0;ireg<counter;ireg++) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
35 regions[ireg*2] = reg[reg_index[ireg]].first;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
36 regions[ireg*2+1] = reg[reg_index[ireg]].second;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
37 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
38 nregionsR[0]=counter;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
39 delete [] reg_index;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
40 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
41 void region_minus_region(int *regions, int*nregionsR,int *region2s, int*nregion2sR,int *updatedregions) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
42 int sep=1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
43 merge_regions(regions,nregionsR,&sep);
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
44 merge_regions(region2s,nregion2sR,&sep);
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
45 int nregs=nregionsR[0];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
46 int nreg2s=nregion2sR[0];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
47 for(int i=0;i<2*(nregs+nreg2s);i++) updatedregions[i]=-1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
48 if(nregs==0) return;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
49 int ireg = 0;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
50 int iregout = 0;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
51 for(int ireg2=0; ireg2<nreg2s;ireg2++) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
52 if(ireg==nregs) break;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
53 if(region2s[ireg2*2+1] < regions[2*ireg]) continue;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
54 if(region2s[ireg2*2] > regions[2*ireg+1]) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
55 updatedregions[2*iregout] = regions[2*ireg];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
56 updatedregions[2*iregout+1] = regions[2*ireg+1];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
57 ireg++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
58 ireg2--;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
59 iregout++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
60 continue;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
61 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
62 int s = regions[ireg*2];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
63 int e = regions[ireg*2+1];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
64 int s2 = region2s[ireg2*2];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
65 int e2 = region2s[ireg2*2+1];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
66 if(s2<=s && e2>=e) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
67 ireg++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
68 ireg2--;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
69 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
70 else if(s2<=s) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
71 regions[ireg*2] = e2+1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
72 continue;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
73 } else if(e2>=e) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
74 updatedregions[2*iregout] = s;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
75 updatedregions[2*iregout+1] = s2-1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
76 ireg2--;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
77 iregout++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
78 ireg++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
79 } else {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
80 updatedregions[2*iregout] = s;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
81 updatedregions[2*iregout+1] = s2-1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
82 regions[ireg*2] = e2+1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
83 iregout++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
84 ireg2--;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
85 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
86 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
87 while(ireg<nregs) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
88 updatedregions[2*iregout] = regions[2*ireg];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
89 updatedregions[2*iregout+1] = regions[2*ireg+1];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
90 ireg++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
91 iregout++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
92 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
93 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
94 void intersection_of_regions(int *regions, int*nregionsR,int *region2s, int*nregion2sR,int *updatedregions) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
95 int sep=1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
96 merge_regions(regions,nregionsR,&sep);
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
97 merge_regions(region2s,nregion2sR,&sep);
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
98 int nregs=nregionsR[0];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
99 int nreg2s=nregion2sR[0];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
100 for(int i=0;i<2*(nregs+nreg2s);i++) updatedregions[i]=-1;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
101 if(nregs==0) return;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
102 if(nreg2s==0) return;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
103 int ireg2 = 0;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
104 int iregout = 0;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
105 for(int ireg=0; ireg<nregs;ireg++) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
106 if(ireg2==nreg2s) return;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
107 if(regions[ireg*2+1] < region2s[2*ireg2]) continue;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
108 if(regions[ireg*2] > region2s[2*ireg2+1]) {ireg2++; ireg--; continue;}
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
109
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
110 int s = regions[ireg*2];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
111 if(s<region2s[ireg2*2]) s = region2s[ireg2*2];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
112 int e = regions[ireg*2+1];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
113 if(e>region2s[ireg2*2+1]) {
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
114 e = region2s[ireg2*2+1];
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
115 ireg2++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
116 ireg--;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
117 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
118 updatedregions[2*iregout] = s;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
119 updatedregions[2*iregout+1] = e;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
120 iregout++;
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
121 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
122 }
19d2cffb8db3 Initial upload
jeremyjliu
parents:
diff changeset
123 }