-
Notifications
You must be signed in to change notification settings - Fork 5
/
mainrepets.c
310 lines (269 loc) · 9.14 KB
/
mainrepets.c
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
/* mainrepets.c : computing repetitions of the first type (exact case) */
#include <stdlib.h>
#include <stdio.h>
#include "defs.h"
#define MINDIFLENPER 9
extern int allowsmall;
extern int *computeLIx (char *pattern, int patLen, int dir, int maxPerBnd);
extern int *computeLOx (char *pattern, int patLen, int dir, int maxPerBnd);
/* extern void NewFirstTypeRepeat(int newRepBgn, int newRepEnd); */
extern listreps *presortLists;
/* extern listreps *sortedLists; */
extern int maxPer;
extern char *seq_original;
/* extern float minExp; */
extern limits lim;
extern int step, LastWindow;
extern FILE *output_file; // output file (for xml output)
extern char * output_file_name;
extern int xmloutput;
void NewFirstTypeExactRepeat(int prRepBgn, int prRepEnd);
void startrepets(int curFacBgn, int nxtFacBgn);
void finalrepets (int prvFacBgn, int curFacBgn);
int Period; // declare it here! (rather than in searchforHeadGDR.c)
int prvFacLen, maxLftLen, curFacLen, maxLftRoot;
void mainrepets (int prvFacBgn, int curFacBgn, int nxtFacBgn)
/* searching for exact reps of the first type crossing the
current border between factors in Crochemore's s-factorisation,
and located within the current window;
called by maxreps */
{
int rightLen;
int *LPu, *LSt, *LPt, *LSu;
curFacLen=nxtFacBgn-curFacBgn;
prvFacLen=curFacBgn-prvFacBgn;
maxLftRoot=curFacLen+prvFacLen;
if ( (maxLftLen=maxLftRoot+prvFacLen)>curFacBgn )
{
maxLftLen = curFacBgn ;
if (maxLftRoot>curFacBgn)
{
maxLftRoot = curFacBgn+1;
if (maxLftRoot<=curFacLen)
{
startrepets(curFacBgn, nxtFacBgn);
return;
}
}
}
VERB(1) printf(" s-factor : [prvFacLen=%d, maxLftLen=%d, maxLftRoot=%d, curFacLen=%d]\n",
prvFacLen, maxLftLen, maxLftRoot, curFacLen ) ;
/* computing Main's extension functions */
LPu = computeLIx (seq_original+curFacBgn, curFacLen, 1, curFacLen);
LSt = computeLIx (seq_original+curFacBgn-1, maxLftLen, -1, maxLftRoot);
LPt = computeLOx (seq_original+curFacBgn, curFacLen, 1, maxLftRoot);
LSu = computeLOx (seq_original+curFacBgn-1, maxLftLen, -1, curFacLen);
Period=lim.min_period;
while( Period<curFacLen && Period<=maxPer ) // LIMIT
{
/* reps with a full period in previous factors */
VERB(3)
printf(" Period=%d *2* : LPt[%d]=%d, LSt[%d]=%d \n", Period,
Period, LPt[Period], Period, LSt[Period]);
if (LPt[Period] + LSt[Period] >= Period)
{
if ( (rightLen=LPt[Period]) >= Period)
{
if ( (rightLen<curFacLen) ||
(seq_original[nxtFacBgn] != seq_original[nxtFacBgn-Period]) )
NewFirstTypeExactRepeat( curFacBgn-(LSt[Period]+Period),
curFacBgn+rightLen );
Period++;
continue;
}
else
{
if ( ( rightLen>0 ) ||
( LSt[Period]+Period<prvFacLen ) )
NewFirstTypeExactRepeat( curFacBgn-(LSt[Period]+Period),
curFacBgn+rightLen );
}
}
/* m-repets with a full period in current factor */
VERB(3)
printf(" Period=%d *1* : LSu[%d]=%d, LPu[%d]=%d \n", Period ,
Period, LSu[Period], Period, LPu[Period]) ;
if (LSu[Period] + LPu[Period] >= Period)
{
if ( (LPu[Period]+Period<curFacLen) ||
(seq_original[nxtFacBgn] != seq_original[nxtFacBgn-Period]) )
NewFirstTypeExactRepeat( curFacBgn-LSu[Period],
curFacBgn+LPu[Period]+Period );
}
Period++;
}
while( Period<maxLftRoot && Period<=maxPer ) // LIMIT
{
/* reps with a full period in previous factors only */
VERB(3)
printf(" Period=%d *2* : LPt[%d]=%d, LSt[%d]=%d \n", Period,
Period, LPt[Period], Period, LSt[Period]);
if (LPt[Period] + LSt[Period] >= Period)
{
if ( (rightLen=LPt[Period])<curFacLen )
{
if ( ( rightLen>0 ) ||
( LSt[Period]+Period<prvFacLen ) )
NewFirstTypeExactRepeat( curFacBgn-(LSt[Period]+Period),
curFacBgn+rightLen );
}
else
{
if (seq_original[nxtFacBgn] != seq_original[nxtFacBgn-Period])
NewFirstTypeExactRepeat( curFacBgn-(LSt[Period]+Period),
curFacBgn+rightLen );
}
}
Period++;
}
free (LPu);
free (LSu);
free (LPt);
free (LSt);
}
void NewFirstTypeExactRepeat(int newRepBgn, int newRepEnd)
/* called from mainrepets, startrepets and finalrepets;
processing an exact rep of the first type. For a found rep,
creates the corresponding structure 'listreps' and
inserts it into the corresponing list from 'presortLists'
(array of lists of reps of the first type found in the
current window and sorted by end positions) */
{
listreps newListRep, nextRep;
repetition *newRep;
int rsize ;
rsize = newRepEnd-newRepBgn; // the size of the rep
if ( ((rsize-Period>=MINDIFLENPER) || allowsmall) &&
(step<0 || newRepBgn < VIRTUAL_STEP) // GK
)
{
if ((nextRep=presortLists[newRepEnd]))
{
if (nextRep->rep.initpos == newRepBgn)
{
VERB(3) printf (": *NO* \n") ;
return;
}
}
if ( (newListRep=(listreps)malloc(sizeof(struct s_listreps)))==NULL )
{
fprintf(stderr,"The output is too big.\n");
fprintf(stderr,"Narrow period bounds.\n");
RECOVER_XML_OUTPUT(output_file,output_file_name,20);
exit(20);
}
newRep=&(newListRep->rep);
newRep->initpos=newRepBgn;
newRep->endpos=newRepEnd;
newRep->period=Period;
newListRep->next=nextRep;
presortLists[newRepEnd]=newListRep;
VERB(3) printf (": *OK* \n") ;
}
else
VERB(3) printf ("\n");
}
void startrepets(int curFacBgn, int nxtFacBgn)
/* called from mainrepets;
similar to 'mainrepets' for the case when the reps can
contain the beginning of the current window */
{
int rightLen;
int *LPu, *LSt, *LPt, *LSu;
VERB(1) printf(" s-factor : [prvFacLen=%d, maxLftLen=%d, maxLftRoot=%d, curFacLen=%d]\n",
prvFacLen, maxLftLen, maxLftRoot, curFacLen ) ;
/* computing longest extension functions */
LPu = computeLIx (seq_original+curFacBgn, curFacLen, 1, curFacLen);
LSt = computeLIx (seq_original+curFacBgn-1, maxLftLen, -1, maxLftRoot);
LPt = computeLOx (seq_original+curFacBgn, curFacLen, 1, maxLftRoot);
LSu = computeLOx (seq_original+curFacBgn-1, maxLftLen, -1, curFacLen);
Period=lim.min_period;
while( Period<maxLftRoot && Period<=maxPer ) // LIMIT
{
/* reps with a full period in previous factors */
VERB(3) printf(" Period=%d *2* : LPt[%d]=%d, LSt[%d]=%d \n", Period,
Period, LPt[Period], Period, LSt[Period]);
if (LPt[Period] + LSt[Period] >= Period)
{
if ( (rightLen=LPt[Period]) >= Period)
{
if ( (rightLen<curFacLen) ||
(seq_original[nxtFacBgn] != seq_original[nxtFacBgn-Period]) )
NewFirstTypeExactRepeat( curFacBgn-(LSt[Period]+Period),
curFacBgn+rightLen );
Period++;
continue;
}
else
{
if ( ( rightLen>0 ) ||
( LSt[Period]+Period<prvFacLen ) )
NewFirstTypeExactRepeat( curFacBgn-(LSt[Period]+Period),
curFacBgn+rightLen );
}
}
/* reps with a full period in current factor */
VERB(3)
printf(" Period=%d *1* : LSu[%d]=%d, LPu[%d]=%d \n", Period ,
Period, LSu[Period], Period, LPu[Period]) ;
if (LSu[Period] + LPu[Period] >= Period)
{
if ( (LPu[Period]+Period<curFacLen) ||
(seq_original[nxtFacBgn] != seq_original[nxtFacBgn-Period]) )
NewFirstTypeExactRepeat( curFacBgn-LSu[Period],
curFacBgn+LPu[Period]+Period );
}
Period++;
}
while( Period<curFacLen && Period<=maxPer ) // LIMIT
{
/* reps with a full period in current factor only */
VERB(3)
printf(" Period=%d *1* : LSu[%d]=%d, LPu[%d]=%d \n", Period ,
Period, LSu[Period], Period, LPu[Period]) ;
if (LSu[Period] + LPu[Period] >= Period)
{
if ( (LPu[Period]+Period<curFacLen) ||
(seq_original[nxtFacBgn] != seq_original[nxtFacBgn-Period]) )
NewFirstTypeExactRepeat( curFacBgn-LSu[Period],
curFacBgn+LPu[Period]+Period );
}
Period++;
}
free (LPu);
free (LSu);
free (LPt);
free (LSt);
}
void finalrepets (int prvFacBgn, int curFacBgn)
/* called from maxreps;
searching for exact reps of the first type
containing the end of the current window,
but not the last border between s-factors (?)
(in other words, all exact reps of the first
type in the end of word, which
have not been found by mainrepets) */
{
int *LSt ;
prvFacLen=curFacBgn-prvFacBgn;
maxLftRoot=(1+prvFacLen)/2;
VERB(1) printf(" final repetitions : [prvFacLen=%d, maxLftRoot=%d, curFacLen=%d]\n",
prvFacLen, maxLftRoot, curFacLen ) ;
/* computing longest extension functions */
LSt = computeLIx (seq_original+curFacBgn-1, prvFacLen, -1, maxLftRoot);
Period=lim.min_period;
while( Period<maxLftRoot && Period<=maxPer ) // LIMIT
{
/* reps in previous factor completely */
VERB(3)
printf(" Period=%d *2* : LSt[%d]=%d \n", Period, Period, LSt[Period]);
if (LSt[Period] >= Period)
{
if ( LSt[Period]+Period<prvFacLen )
NewFirstTypeExactRepeat( curFacBgn-(LSt[Period]+Period),
curFacBgn );
}
Period++;
}
free (LSt);
}