forked from Yufeng-shen/seshat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logspace.cc
244 lines (196 loc) · 5.64 KB
/
logspace.cc
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
/*Copyright 2014 Francisco Alvaro
This file is part of SESHAT.
SESHAT 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.
SESHAT 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 SESHAT. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logspace.h"
LogSpace::LogSpace(CellCYK *c, int nr, int dx, int dy) {
//List length
N=nr;
//Size of the "reference symbol"
RX = dx;
RY = dy;
//Create a vector to store the regions
data = new CellCYK*[N];
int i=0;
for(CellCYK *r=c; r; r=r->sig)
data[i++] = r;
//Sort the regions
quicksort(data, 0, N-1);
}
LogSpace::~LogSpace() {
delete[] data;
}
void LogSpace::getH(CellCYK *c, list<CellCYK*> *set) {
int sx, sy, ss, st;
//Set the region to search
sx = max(c->x+1, c->s-(int)(RX*2)); // (sx,sy)------
ss = c->s + RX*8; // ------------
sy = c->y - RY; // ------------
st = c->t + RY; // ------(ss,st)
//Retrieve the regions
bsearchHBP(sx, sy, ss, st, set, c);
}
//Below region
void LogSpace::getV(CellCYK *c, list<CellCYK*> *set) {
int sx, sy, ss, st;
//Set the region to search
sx = c->x - 2*RX;
ss = c->s + 2*RX;
sy = max(c->t - RY, c->y+1);
st = c->t + RY*3;
//Retrieve the regions
bsearchStv(sx, sy, ss, st, set, false, c);
}
//Above region. Although only Below is really considered this is necessary to
//solve the problem of the case | aaa|
// |bbbb|
//such that "a" would never find "b" because its 'sx' would start before "b.x"
void LogSpace::getU(CellCYK *c, list<CellCYK*> *set) {
int sx, sy, ss, st;
//Set the region to search
sx = c->x - 2*RX;
ss = c->s + 2*RX;
sy = c->y - RY*3;
st = min(c->y + RY, c->t-1);
//Retrieve the regions
bsearchStv(sx, sy, ss, st, set, true, c);
}
//Inside region (sqrt)
void LogSpace::getI(CellCYK *c, list<CellCYK*> *set) {
int sx, sy, ss, st;
//Set the region to search
sx = c->x + 1; // (sx,sy)------
ss = c->s + RX; // ------------
sy = c->y + 1; // ------------
st = c->t + RY; // ------(ss,st)
//Retrieve the regions
bsearch(sx, sy, ss, st, set);
}
//Mroot region (n-th sqrt)
void LogSpace::getM(CellCYK *c, list<CellCYK*> *set) {
int sx, sy, ss, st;
//Set the region to search
sx = c->x - 2*RX; // (sx,sy)------
ss = min(c->x + 2*RX, c->s); // ------------
sy = c->y - RY; // ------------
st = min(c->y + 2*RY, c->t); // ------(ss,st)
//Retrieve the regions
bsearch(sx, sy, ss, st, set);
}
//SubSupScript regions
void LogSpace::getS(CellCYK *c, list<CellCYK*> *set) {
int sx, sy, ss, st;
//Set the region to search
sx = c->x-1; // (sx,sy)------
ss = c->x+1; // ------------
sy = c->y - RY; // ------------
st = c->t + RY; // ------(ss,st)
bsearch(sx, sy, ss, st, set);
}
void LogSpace::bsearch(int sx, int sy, int ss, int st, list<CellCYK*> *set) {
//Binary search of "sx"
int i,j;
for(i=0, j=N; i<j; ) {
int m=(i+j)/2;
if( sx <= data[m]->x )
j=m;
else
i=m+1;
}
//Retrieve the compatible regions
while( i<N && data[i]->x <= ss ) {
if( data[i]->y <= st && data[i]->t >= sy ) {
set->push_back(data[i]);
}
i++;
}
}
//Version more strict with the upper/lower vertical positions
void LogSpace::bsearchStv(int sx, int sy, int ss, int st, list<CellCYK*> *set, bool U_V, CellCYK *cd) {
//Binary search of "sx"
int i,j;
for(i=0, j=N; i<j; ) {
int m=(i+j)/2;
if( sx <= data[m]->x )
j=m;
else
i=m+1;
}
//Retrieve the compatible regions
if( U_V ) { //Direction 'Up' (U)
while( i<N && data[i]->x <= ss ) {
if( data[i]->t <= st && data[i]->t >= sy && data[i]->s <= ss ) {
if( data[i]->t < cd->y )
sy = max(max(data[i]->y, data[i]->t-RY),sy);
set->push_back(data[i]);
}
i++;
}
}
else { //Direction 'Down' (V)
while( i<N && data[i]->x <= ss ) {
if( data[i]->y <= st && data[i]->y >= sy && data[i]->s <= ss ) {
if( data[i]->y > cd->t )
st = min(min(data[i]->t, data[i]->y+RY),st);
set->push_back(data[i]);
}
i++;
}
}
}
//Version that reduces the sx-ss region as soon as hypotheses are found
void LogSpace::bsearchHBP(int sx, int sy, int ss, int st, list<CellCYK*> *set, CellCYK *cd) {
//Binary search of "sx"
int i,j;
for(i=0, j=N; i<j; ) {
int m=(i+j)/2;
if( sx <= data[m]->x )
j=m;
else
i=m+1;
}
//Retrieve the compatible regions
while( i<N && data[i]->x <= ss ) {
if( data[i]->y <= st && data[i]->t >= sy ) {
if( data[i]->x > cd->s )
ss = min(min(data[i]->s, data[i]->x+RX),ss);
set->push_back(data[i]);
}
i++;
}
}
//Sort according to x coordinate of region (x,y)-(s,t)
void LogSpace::quicksort(CellCYK **vec, int ini, int fin) {
if( ini < fin ) {
int piv = partition(vec, ini, fin);
quicksort(vec, ini, piv);
quicksort(vec, piv+1, fin);
}
}
int LogSpace::partition(CellCYK **vec, int ini, int fin) {
int piv = vec[ini]->x;
int i=ini-1, j=fin+1;
do{
do{
j--;
}while(vec[j]->x > piv);
do{
i++;
}while(vec[i]->x < piv);
if( i<j ) {
CellCYK *aux = vec[i];
vec[i] = vec[j];
vec[j] = aux;
}
}while( i<j );
return j;
}