File: mkcfr.c

package info (click to toggle)
vmatch 2.3.1%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,108 kB
  • sloc: ansic: 70,897; sh: 7,139; perl: 4,152; makefile: 1,181; xml: 642; awk: 563; ruby: 306; haskell: 288; sed: 60
file content (457 lines) | stat: -rw-r--r-- 10,808 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "divmodmul.h"
#include "debugdef.h"
#include "errordef.h"
#include "virtualdef.h"
#include "minmax.h"
#include "arraydef.h"
#include "spacedef.h"
#include "vnodedef.h"
#include "fhandledef.h"

#include "outindextab.pr"
#include "accvirt.pr"
#include "readvirt.pr"
#include "safescpy.pr"
#include "filehandle.pr"

#include "pathmax.h"

/*
  This file contains the function makereversetable for the construction of 
  the reverse tables in the affix affix array data structure
*/

DEFINEEVALLCP

#define SUFFIXLEN(S)  (virtualtree->multiseq.totallength - (S))
#define SUFFIX(S)     (virtualtree->multiseq.sequence + (S))
#define RPREFIX(S)    (virtualtree->multiseq.sequence + \
                       (virtualtree->multiseq.totallength-1-(S)))

#define SEQOFFSET(V) V->multiseq.sequence

#define TEXTPOS(V,F,I)\
        ((F)? (SEQOFFSET(V)+I) : (SEQOFFSET(V)+(V)->multiseq.totallength-1-(I)))

#define COMP(I,LCPSTART,PDIR,TDIR)\
        if (PDIR) \
        {\
          if (TDIR)\
          {\
            COMPARE1(I,LCPSTART)\
          } else\
	  {\
            COMPARE2(I,LCPSTART)\
          }\
        } else\
        {\
          if (TDIR)\
          {\
            REVCOMPARE1(I,LCPSTART)\
          } else\
	  {\
            REVCOMPARE2(I,LCPSTART)\
          }\
        }

//pattern forward, text forward

#define COMPARE1(I,LCPSTART) \
        start = virtualtree->suftab[I];\
        slen = SUFFIXLEN(start);\
        s = SUFFIX(start);\
        len = MIN(wlen+leftstart,slen);\
        for(lcplen=LCPSTART; lcplen<len; lcplen++)\
        {\
          if(w[lcplen-leftstart] < s[lcplen])\
          {\
            retcode = (Sint) -1;\
            break;\
          }\
          if(w[lcplen-leftstart] > s[lcplen])\
          {\
            retcode = (Sint) 1;\
            break;\
          }\
        }\
        if(lcplen == len)\
        {\
          if(wlen+leftstart <= slen)\
          {\
            retcode = 0;\
          } else\
          {\
            retcode = (Sint) -1;\
          }\
        }


//pattern forward, text backward

#define COMPARE2(I,LCPSTART) \
	start = virtualtree->suftab[I];\
	slen = SUFFIXLEN(start);\
        s = RPREFIX(start);\
        len = MIN(wlen+leftstart,slen);\
        for(lcplen=LCPSTART; lcplen<len; lcplen++)\
	{\
	  if(w[lcplen-leftstart] < s[-lcplen])\
	  {\
            retcode = (Sint) -1;\
            break;\
          }\
          if(w[lcplen-leftstart] > s[-lcplen])\
          {\
            retcode = (Sint) 1;\
            break;\
          }\
        }\
        if(lcplen == len)\
        {\
          if(wlen+leftstart <= slen)\
          {\
            retcode = 0;\
          } else\
          {\
            retcode = (Sint) -1;\
	  }\
        }

//pattern backward, text forward

#define REVCOMPARE1(I,LCPSTART)\
        start = virtualtree->suftab[I];\
        slen = SUFFIXLEN(start);\
        s = SUFFIX(start);\
        len = MIN(wlen+leftstart,slen);\
        for(lcplen=LCPSTART; lcplen<len; lcplen++)\
        {\
          if(w[leftstart-lcplen] < s[lcplen])\
          {\
            retcode = (Sint) -1;\
            break;\
          }\
          if(w[leftstart-lcplen] > s[lcplen])\
          {\
            retcode = (Sint) 1;\
            break;\
          }\
        }\
        if(lcplen == len)\
        {\
          if(wlen+leftstart <= slen)\
          {\
            retcode = 0;\
          } else\
          {\
            retcode = (Sint) -1;\
          }\
        }
      
// pattern backward, text backward

#define REVCOMPARE2(I,LCPSTART)\
	start = virtualtree->suftab[I];\
	slen = SUFFIXLEN(start);\
        s = RPREFIX(start);\
        len = MIN(wlen+leftstart,slen);\
        for(lcplen=LCPSTART; lcplen<len; lcplen++)\
	{\
	  if(w[leftstart-lcplen] < s[-lcplen])\
	  {\
            retcode = (Sint) -1;\
            break;\
          }\
          if(w[leftstart-lcplen] > s[-lcplen])\
          {\
            retcode = (Sint) 1;\
            break;\
          }\
        }\
        if(lcplen == len)\
        {\
          if(wlen+leftstart <= slen)\
          {\
            retcode = 0;\
          } else\
          {\
            retcode = (Sint) -1;\
          }\
        }

static void showonstdout(char *s)
{
  printf("%s\n",s);
}

/*
  The following function determines the left and right border of the lcp-
  interval representing the pattern \texttt{w} of length \texttt{wlen}
  in the virtual suffix tree \texttt{virtualtree} by binary search.
  The start interval of the search is given by \texttt{vnode->left}
  and {vnode->right}, 
  the result is given by an update of \texttt{vnode->left} and 
  \texttt{vnode->right}.
  \texttt{pdir} indicates the orientation of the pattern (1 means forward,
  0 means backward).
  \texttt{tdir} indicates the orientation of the text (1 means forward, 
  0 means backward).
*/  
 
static BOOL mmsearchvstree(Virtualtree *virtualtree,Vnode *vnode, Uint lcontext,
		           Uint matchlength, Uchar *w,Uint wlen,
                           BOOL pdir, BOOL tdir)
{
  Uint leftstart, left, leftsave, mid, right, start, lcplen, slen, 
       len, lpref, rpref;
  Uchar *s;
  Sint retcode = 0;

  leftstart = lcontext+matchlength;
  leftsave = left = vnode->left;
  
  right = vnode->right;

  COMP(left,leftstart,pdir,tdir);
 
  if(retcode > 0)
  {
    lpref = lcplen;

    COMP(right,leftstart,pdir,tdir);
    if(retcode > 0)
    {
      return False;
    } else
    {
      rpref = lcplen;
      while(right > left + 1)
      {
        mid = DIV2(left+right);
	COMP(mid,MIN(lpref,rpref),pdir,tdir);
        if(retcode <= 0)
        {
          right = mid;
          rpref = lcplen;
        } else
        {
          left = mid;
          lpref = lcplen;
        }
      }
      vnode->left = right;
    }
  }

  left = leftsave;
  right = vnode->right;

  COMP(left,leftstart,pdir,tdir);

  if(retcode < 0)
  {
    return False;
  } else
  {
    lpref = lcplen;
    COMP(right,leftstart,pdir,tdir);

    if(retcode >= 0)
    {
      vnode->right = right;
    } else
    {
      rpref = lcplen;
      while(right > left + 1)
      {
        mid = DIV2(left+right);
        COMP(mid,MIN(lpref,rpref),pdir,tdir);
        if(retcode >= 0)
        {
          left = mid;
          lpref = lcplen;
        } else
        {
          right = mid;
          rpref = lcplen;
        }
      }
      vnode->right = left;
    }
  }

  if (vnode->left>vnode->right) return False;

  return True;
}

#define PUSH(P)  STOREINARRAY(&lcpstack,PairUint,storesize,P)
#define POP   lcpstack.spacePairUint[--lcpstack.nextfreePairUint]

/*EE
  The following function returns the home of the interval [left,right]
  in the data structure sufarray/ given data structure sufarray
*/

static Uint gethome(Virtualtree *vtree, Uint left, Uint right)
{
  if (evallcp(vtree,left) >= evallcp(vtree, right+1))
    return(left);
  else
    return(right);
}

/*EE
  The following function generates a table revtab (incl. memory allocation)
  (type: Uint,size:sarray.totallength) so that revtab[gethome(I)] is set
  correctly for every lcp-interval I of \texttt{suftree}.
  The result of makereversetab is a pointer to the new table.
*/

static Sint makereversetable(Uint *revtab,Virtualtree *suftree,
                             Virtualtree *preftree, BOOL sufflag)
{
  Uint i, lcpval, left, right, maxlcp=0,
       storesize = UintConst(100),
       textlength = suftree->multiseq.totallength-1;
                   // this field is not allowed
                   // always use multiseq.totallength
  Vnode vnode;
  PairUint lcppair;
   
  ArrayPairUint lcpstack;
  INITARRAY(&lcpstack,PairUint);
  
  lcppair.uint0=0;
  lcppair.uint1=0;
  PUSH(lcppair);
  for (i=UintConst(1);i<=textlength+UintConst(1);i++)
  {
    if (evallcp(suftree,i)>maxlcp)
    {
      lcppair.uint0=i-1;
      maxlcp=evallcp(suftree,i);
      lcppair.uint1=maxlcp;
      PUSH(lcppair);
    }
    else
    {
      left=0;
      right=i-1;
      while (evallcp(suftree,i)<maxlcp)
      {
	lcppair=POP;
	//borders of lcp-interval:
	left=lcppair.uint0;
	lcpval=lcppair.uint1;

	//always start with complete preftree-interval: [0,totallength]
	vnode.left=0;
	vnode.right=suftree->multiseq.totallength-1;
	vnode.offset=0;

	if (!mmsearchvstree(preftree,&vnode,0,0,
			    TEXTPOS(suftree,sufflag,
				    suftree->suftab[left]+lcpval-1),
			    lcpval,sufflag ? False : True,
                                   sufflag ? False : True))
        {
	  ERROR2("String not found while constructing REVTAB. interval borders [%ld %ld]", 
                 (Showsint) vnode.left,
                 (Showsint) vnode.right);
          return (Sint) -1;
        }
	revtab[gethome(suftree,left,right)]= vnode.left;
	maxlcp=lcpstack.spacePairUint[lcpstack.nextfreePairUint-1].uint1;
      }	
      if (evallcp(suftree,i)>maxlcp)
      {
	lcppair.uint0=left;
	maxlcp=evallcp(suftree,i);
	lcppair.uint1=maxlcp;
	PUSH(lcppair);
      }      
    }
  }
  FREEARRAY(&lcpstack,PairUint);
  return 0;
}

MAINFUNCTION
{
  Virtualtree fvirt, rvirt;
  Uint *revtab;
  char findexname[PATH_MAX+1], 
       rindexname[PATH_MAX+4+1];

  DEBUGLEVELSET;

  VSTREECHECKARGNUM(2,"indexname");
  if(safestringcopy(&findexname[0],argv[1],PATH_MAX) != 0)
  {
    STANDARDMESSAGE;
  }
  if(mapvirtualtreeifyoucan(&fvirt,&findexname[0],
                            TISTAB | SUFTAB | LCPTAB) != 0)
  {
    STANDARDMESSAGE;
  }
  if(showvirtualtreestatus(&fvirt,&findexname[0],showonstdout) != 0)
  {
    STANDARDMESSAGE;
  }
  if(safestringcopy(&rindexname[0],argv[1],PATH_MAX) != 0)
  {
    STANDARDMESSAGE;
  }
  strcat(&rindexname[0],".rev");
  if(mapvirtualtreeifyoucan(&rvirt,&rindexname[0],
                            SUFTAB | LCPTAB) != 0)
  {
    STANDARDMESSAGE;
  }
  if(showvirtualtreestatus(&rvirt,&rindexname[0],showonstdout) != 0)
  {
    STANDARDMESSAGE;
  }
  ALLOCASSIGNSPACE(revtab,NULL,Uint,fvirt.multiseq.totallength);
  rvirt.multiseq.sequence = fvirt.multiseq.sequence;
  if(makereversetable(revtab,&fvirt,&rvirt,True) != 0)
  {
    STANDARDMESSAGE;
  }
  if(outindextab(&findexname[0],"cfr",(void *) revtab,(Uint) sizeof(Uint),
     fvirt.multiseq.totallength) != 0)
  {
    STANDARDMESSAGE;
  }
  if(makereversetable(revtab,&rvirt,&fvirt,False) != 0)
  {
    STANDARDMESSAGE;
  }
  if(outindextab(&rindexname[0],"crf",(void *) revtab,(Uint) sizeof(Uint),
                 fvirt.multiseq.totallength) != 0)
  {
    STANDARDMESSAGE;
  }
  FREESPACE(revtab);
  if(freevirtualtree(&fvirt) != 0)
  {
    STANDARDMESSAGE;
  }
  rvirt.multiseq.sequence = NULL;
  if(freevirtualtree(&rvirt) != 0)
  {
    STANDARDMESSAGE;
  }
#ifndef NOSPACEBOOKKEEPING
  checkspaceleak();
#endif
  mmcheckspaceleak();
  checkfilehandles();
  return EXIT_SUCCESS;
}