root/trunk/lyfreq.c

Revision 389, 11.7 kB (checked in by heitor.barbieri, 3 weeks ago)

essage first commit

Line 
1
2/* --------------------------- ly.c -------------------------------- */
3
4#include <stdio.h>
5#include <ctype.h>
6#include <string.h>
7#include <limits.h>
8
9#include "cisis.h"
10#include "cirun.h"
11
12#define TRACE  0
13#define TRACE0 0
14#define TRACE1 0
15#define TRACE2 0
16
17#if TRACE0+TRACE1+TRACE2
18#define TRACE 1
19#endif
20
21
22#define MAXT 1024
23#define MINT 1
24
25
26/* ----------------------------- tlc.h ------------------------------ */
27
28typedef union {
29    L0STRU l0x;
30    L1STRU l1x;
31    L2STRU l2x;
32} LUSTRU;
33
34typedef struct maxstru {        /* list element */
35    char key[LE2+1];
36    int  len;
37    LONGX inf;
38    LONGX val;
39} MAXSTRU;
40
41MAXSTRU *vmaxp[MAXT+2];         /* entries + maxy ($) + maxz (in) */
42int nmaxt;                      /* no of entries = input parameter */
43
44LUSTRU tlcleaf;                 /* static for lp */
45
46extern int trmtrace;
47
48void main(argc,argv)
49int argc;
50char *argv[];
51{
52    char *dbnp;
53    int treecase;
54
55    char *up1p,*up2p;
56    int up1len,up2len;
57
58    LONGX punt1,punt2;
59
60    LONGX tell;
61    LONGX countell,rangtell;
62
63    int weight,unique;
64    int show1,show2,show3;
65#if TRACE
66    int now=0;
67#endif
68    char qlfx = '\0';
69    int fixx = 0;
70    int trace = 0;
71
72    TRMSTRU *trmp;
73    INVMAP *invp;
74    LUSTRU *lp;
75    MAXSTRU *maxp;
76
77    PUNT punt;
78    char up1[LE2+1];
79    char up2[LE2+1];
80    FILE *fp;
81
82    char *p;
83    int iarg,n,iock;
84    int onmaxy=0;
85    int maxy,maxz;
86    int maxi,maxj,maxk,xi,nval,mval,ninstall=0;
87    LONGX totval,medval,accval;
88    LONGX itrm;
89#if !LIND
90    LONGX last;
91#endif
92
93#define VP(xi)  vmaxp[xi]
94#define VPmaxi  vmaxp[maxi]
95#define VPmaxj  vmaxp[maxj]
96#define VPmaxk  vmaxp[maxk]
97#define VPmaxy  vmaxp[maxy]
98#define VPmaxz  vmaxp[maxz]
99
100#define keyp    vmaxp[maxz]->key
101#define keylen  vmaxp[maxz]->len
102#define info    vmaxp[maxz]->inf
103#define value   vmaxp[maxz]->val
104
105#define VPxkey  vmaxp[xi]->key
106#define VPxlen  vmaxp[xi]->len
107#define VPxinf  vmaxp[xi]->inf
108#define VPxval  vmaxp[xi]->val
109
110
111
112    if (argc < 4) {
113        printf("%s",cicopyr("Utility LYFREQ"));
114        printf("\n");
115        printf("lyfreq <dbn> <treecase> <maxt> <filout> [options..] \n");
116        printf("\n");
117        printf("options: {punt1/punt2}=<pos> \n");
118        printf("         {up1/up2}=<upkey> \n");
119        printf("         trace/show1/show2/show3 %s \n",(TRACE)?"[now]":" ");
120        printf("         tell=<npages> ...\n");
121        printf("         qlf=<char> \n");
122        printf("         fix=<bytes> \n");
123        printf("         weight%s \n",(!LIND)?"/unique":" ");
124        printf("\n");
125        exit(1);
126    }
127
128    dbnp=argv[1];
129    invsetup(dbnp,0L,0L,0L);
130    invp=dbxstorp(dbnp)->dbxifmap;
131
132    if (sscanf(argv[2],"%d",&treecase) != 1) fatal(argv[3]);
133    if (treecase != 1 && treecase != 2) fatal("treecase 1/2");
134    treecase--;
135
136    if (sscanf(argv[3],"%d",&nmaxt) != 1) fatal(argv[3]);
137    if (nmaxt >= MAXT) fatal("MAXT Overflow");
138    if (nmaxt <  MINT) fatal("MINT Underflow");
139
140    maxy=nmaxt; maxz=nmaxt+1;           /* maxy + maxz */
141    for (xi=0; xi < nmaxt+2; xi++) {    /* maxy + maxz */
142        VP(xi)=(MAXSTRU *)ALLOC((ALLOPARM)sizeof(MAXSTRU));
143        if (VP(xi) == NULL)
144            fatal("Not enough memory");
145        memset(VP(xi),0x00,sizeof(MAXSTRU));
146        VPxval=0;
147    }
148
149    if ((fp=fopen(argv[4],"w")) == NULL) fatal(argv[4]);
150
151
152    up1p=up2p=NULL; itrm=1L;
153    punt1=1; punt2=invp->cn[treecase].fmaxpos;
154    show1=show2=show3=0; weight=unique=0; tell=0;
155
156    for (p=up1, n=LE1; n--; ) *p++ = '!'; *p='\0';
157    for (p=up2, n=LE2; n--; ) *p++ = '!'; *p='\0';
158
159    for (iarg=5; iarg < argc; iarg++) {
160        p=argv[iarg];
161
162        if (strncmp(p,"up1=",4) == 0) {
163            up1p=p+4; up1len=strlen(up1p);
164            if (up1len > vlex[treecase]) fatal(p);
165            strcpy(up1,up1p);
166            for (n=up1len; n < vlex[treecase]; ) up1[n++] = '!';
167            up1[vlex[treecase]] = '\0';
168            continue;
169        }
170        if (strncmp(p,"up2=",4) == 0) {
171            up2p=p+4; up2len=strlen(up2p);
172            if (up2len > vlex[treecase]) fatal(p);
173            strcpy(up2,up2p);
174            for (n=up2len; n < vlex[treecase]; ) up2[n++] = '!';
175            up2[vlex[treecase]] = '\0';
176            continue;
177        }
178
179        if (strncmp(p,"punt1=",6) == 0) {
180            if (sscanf(p+6,"%"_LD_,&punt1) != 1) fatal("punt1=");
181            if (punt1 < 1) fatal("punt1");
182            continue;
183        }
184        if (strncmp(p,"punt2=",6) == 0) {
185            if (sscanf(p+6,"%"_LD_,&punt2) != 1) fatal("punt2=");
186            if (punt2 < 1) fatal("punt2");
187            continue;
188        }
189
190        if (strcmp(p,"show1") == 0) {
191            show1=1; continue;
192        }
193        if (strcmp(p,"trace") == 0) {
194            trmtrace=trace=1; continue;
195        }
196        if (strcmp(p,"show2") == 0) {
197            show2=1; continue;
198        }
199        if (strcmp(p,"show3") == 0) {
200            show3=1; continue;
201        }
202#if TRACE
203        if (strcmp(p,"now") == 0) {
204            now=1; continue;
205        }
206#endif
207
208        if (strncmp(p,"tell=",5) == 0) {
209            if (sscanf(p+5,"%"_LD_,&tell) != 1) fatal("tell=");
210            if (tell < 0) fatal("tell");
211            continue;
212        }
213
214        if (strcmp(p,"weight") == 0) {
215            weight=1; continue;
216        }
217#if !LIND
218        if (strcmp(p,"unique") == 0) {
219            unique=1; continue;
220        }
221#endif
222
223        if (strncmp(p,"qlf=",4) == 0) {
224            qlfx=p[4]; continue;
225        }
226
227        if (strncmp(p,"fix=",4) == 0) {
228            if (sscanf(p+4,"%d",&fixx) != 1) fatal("fix=");
229            if (fixx <= 0) fatal("fix");
230            continue;
231        }
232
233
234        fatal(p);
235    }
236
237    if (trace)
238        printf("punt1=%"_LD_" punt2=%"_LD_" fmaxpos=%"_LD_" %s\n",
239            punt1,punt2,invp->cn[treecase].fmaxpos,
240            (unique) ? "- unique" : " ");
241
242    if (up1p) {
243        TERM(-itrm,dbnp,up1);
244        if (TRMlcase != treecase)
245            fatal("up1p");
246        punt1 = (treecase) ? TRMl2buf.pos : TRMl1buf.pos;
247        if (trace)
248            printf("up1=%s(%d) punt1=%"_LD_" \n",up1p,up1len,punt1);
249    }
250    if (up2p) {
251        TERM(-itrm,dbnp,up2);
252        if (TRMlcase != treecase)
253            fatal("up2p");
254        punt2 = (treecase) ? TRMl2buf.pos : TRMl1buf.pos;
255        if (trace)
256            printf("up2=%s(%d) punt2=%"_LD_" \n",up2p,up2len,punt2);
257    }
258
259#if !LIND
260    if (!up1p)
261        if (!up2p) {
262            p = (treecase) ? up2 : up1;
263            TERM(-itrm, dbnp, p);
264            if (TRMlcase != treecase)
265                fatal("upxp");
266        if (trace)
267                printf("upx=%s(%d) puntx=%"_LD_" \n",
268                    p,strlen(p),(treecase) ? TRMl2buf.pos : TRMl1buf.pos);
269        }
270#endif
271
272    if (tell) {
273        tell=(rangtell=punt2-punt1+1)/tell;
274        if (tell <= 0) tell=1;
275        if (rangtell <= 0) rangtell=1;
276    }
277    countell=0;
278
279
280    /*
281        for each leaf
282    */
283    lp= &tlcleaf; /* static */
284    totval=0;
285
286    if (punt1 > punt2)
287        fatal("punt1/punt2");
288
289#if 0
290    for (punt=punt1; punt <= punt2; punt++) {
291#else
292    for (punt=punt1; punt2; punt=lp->l0x.ps) {
293#endif
294        if (!punt) break;
295       
296        leafread((char *)lp,invp,treecase,punt,0); /* read a leaf*/
297        ++countell;
298        if (tell)
299            if (countell % tell == 0) {
300                fprintf(stderr,"+%6"_LD_"p %3"_LD_"%% ",countell,
301                    (10 * (countell*100/rangtell) + 5) / 10);
302                n=ninstall-1;
303                if (n < 0) n = 0;
304                if (n > nmaxt-1) n=nmaxt-1;
305                fprintf(stderr,"     t=%9"_LD_"   %4d=%9"_LD_"   %4d=%9"_LD_" \n",
306                    totval,1,VP(1-1)->val,n+1,VP(n+1-1)->val);
307            }
308
309        /*
310            for each term
311        */
312        for (iock=0; iock < lp->l0x.ock; iock++) {
313
314            keylen=vlex[treecase];
315
316            p = (treecase) ? lp->l2x.idx[iock].key
317                           : lp->l1x.idx[iock].key;
318
319            memcpy(keyp,p,keylen);
320
321#if LIND
322/*          /* if LIND */
323
324            if (treecase) {
325#if !SAMEL
326                info=lp->l2x.idx[iock].info2;   /* tlc - tot no of postings */
327#else
328#define isamel 0
329                info=lp->l2x.idx[iock].lxidxei[isamel].info2;
330#endif
331            }
332            else {
333#if !SAMEL
334                info=lp->l1x.idx[iock].info2;   /* tlc - tot no of postings */
335#else
336                info=lp->l1x.idx[iock].lxidxei[isamel].info2;
337#endif
338            }
339
340#else
341/*          /* else LIND */
342
343            if (TRMlcase != treecase)
344                fatal("TRMlcase");
345
346            if (iock == 0)
347                if (treecase)
348                    memcpy(&TRMl2buf,lp,L2BSIZ);
349                else
350                    memcpy(&TRMl1buf,lp,L1BSIZ);
351
352            TRMliock(treecase)=iock;
353
354            /* posthdr1 still uses TRMlidxp(TRMlcase) */
355            TRMlidxp(treecase) = (treecase) ? lp->l2x.idx[iock].key
356                                            : lp->l1x.idx[iock].key;
357            posthdr1(trmp);
358
359            info=TRMifhdr.ifptotp;
360            if (unique)
361                if (info > 1)
362                    for (last=info=0; posting(itrm,TRMpost+1) > 0; ) {
363                        if (TRMpmfn == last) continue;
364                        info++; last=TRMpmfn;
365                    }
366
367            if (trace) {
368                memcpy(TRMkey,keyp,keylen); *(TRMkey+keylen)='\0';
369                printf("TRMrc=%d  TRMlcase=%d/%d,%p  TRMkey=%s(%d)\n",
370                    TRMrc,TRMlcase,
371                    TRMliock(TRMlcase),TRMlidxp(TRMlcase),
372                    TRMkey,strlen(TRMkey));
373            }
374
375/*          /* endif LIND */
376#endif
377
378
379            if (show1) {
380                printf("         |%7"_LD_"|%2d|",info,keylen);
381                for (p=keyp, n=keylen; n--; p++) printf("%c",*p);
382#if TRACE0
383                printf("|***");
384                if (now) printf("\n"); else getchar();
385#else
386                printf("|\n");
387#endif
388            }
389
390            if (up2p) {
391                if (memcmp(keyp,up2p,up2len) > 0) {
392                    if (show3) {
393                        for (p=keyp+keylen-1; keylen; ) {
394                            if (*p != ' ') break;
395                            p--; keylen--;
396                        }
397                        printf("+++++++++|%7"_LD_"|%2d|",info,keylen);
398                        for (p=keyp, n=keylen; n--; p++)
399                            printf("%c",*p);
400                        printf("|\n");
401                    }
402                    punt2=0;
403                    break;
404                }
405            }
406
407            for (p=keyp+keylen-1; keylen; ) {
408                if (*p != ' ') break;
409                p--; keylen--;
410            }
411
412            if (!keylen)
413                continue;
414
415            if (up1p) {
416                if (keylen < up1len) fatal("up1len");
417                if (memcmp(keyp,up1p,up1len) < 0) {
418                    if (show3) {
419                        printf("---------|%7"_LD_"|%2d|",info,keylen);
420                        for (p=keyp, n=keylen; n--; p++)
421                            printf("%c",*p);
422                        printf("|\n");
423                    }
424                    continue;
425                }
426                else up1p=NULL;
427            }
428
429            value = (weight) ? info*keylen : info;
430
431            if (qlfx || fixx) {
432                if (fixx)
433                    if (keylen >= fixx) {
434                        keylen=fixx;
435                        value = (weight) ? info*keylen : info;
436                    }
437                if (qlfx)
438                    if (memchr(keyp,qlfx,keylen)) {
439                        for (p=keyp, keylen=0; *p++ != qlfx; ) keylen++;
440                        value = (weight) ? info*keylen : info;
441                    }
442                if (onmaxy == 0) {
443                    memcpy(VPmaxy,VPmaxz,sizeof(MAXSTRU));
444                    onmaxy=1;
445                    continue;           /* end qlfx - 1st */
446                }
447                if (keylen == VPmaxy->len) {
448                    if (memcmp(keyp,VPmaxy->key,keylen) == 0) {
449                        VPmaxy->inf++;
450                        if (qlfx)
451                            VPmaxy->inf = 0; /* + */
452                        VPmaxy->val += value;
453                        continue;       /* end qlfx */
454                    }
455                }
456                xi=maxy;
457                maxy=maxz;      /* maxy points to processed input */
458                maxz=xi;        /* maxz is tabulated */
459            }
460
461
462            if (value > VP(nmaxt-1)->val) {             /* last = minimum */
463
464#if TRACE1
465printf("... \n");
466for (xi=0; xi < nmaxt; xi++) {
467 printf("%9"_LD_"|%7"_LD_"|%2d|",VPxval,VPxinf,VPxlen);
468 for (p=VPxkey, n=VPxlen; n--; p++) printf("%c",*p);
469 printf("|\n");
470}
471#endif
472                if (show2) {
473                    xi=maxz;
474                    printf("%9"_LD_"|%7"_LD_"|%2d|",VPxval,VPxinf,VPxlen);
475                    for (p=VPxkey, n=VPxlen; n--; p++) printf("%c",*p);
476                    printf("|\n");
477                }
478
479                for (maxi=nmaxt-2; maxi >= 0; maxi--)   /* previous */
480                    if (VPmaxi->val > value)            /* stop moving up */
481                        break;
482                maxi++;
483
484                totval-=VP(nmaxt-1)->val;
485                for (maxj=nmaxt-1; maxj > maxi; maxj--) {   /* shift down */
486                    maxk=maxj-1; /* top */
487                    maxp=VPmaxk;
488                    VPmaxk=VPmaxj;
489                    VPmaxj=maxp;
490                }
491
492                memcpy(VPmaxi->key,keyp,keylen);            /* install it */
493                VPmaxi->len=keylen;
494                VPmaxi->inf=info;
495                VPmaxi->val=value;
496                ninstall++;
497#if TRACE2
498printf("=== \n");
499for (xi=0; xi < nmaxt; xi++) {
500 printf("%9"_LD_"|%7"_LD_"|%2d|",VPxval,VPxinf,VPxlen);
501 for (p=VPxkey, n=VPxlen; n--; p++) printf("%c",*p);
502 printf("|\n");
503}
504#endif
505                totval+=value;
506            }
507        }
508    }
509
510
511    if (tell) {
512        fprintf(stderr,"+%6"_LD_"p %3"_LD_"%% ",countell,
513            (10 * (countell*100/rangtell) + 5) / 10);
514    }
515
516    medval=(10*totval+5)/10/2; mval=0;
517    for (accval=0, maxi=0; maxi < nmaxt; maxi++)
518        if (VPmaxi->val) {
519            mval++;
520            accval+=VPmaxi->val;
521            if (accval >= medval)
522                break;
523        }
524    for (totval=0, nval=0, maxi=0; maxi < nmaxt; maxi++)
525        if (VPmaxi->val) {
526            nval++;
527            totval+=VPmaxi->val;
528            xi=maxi;
529            fprintf(fp,"%9"_LD_"|%7"_LD_"|%2d|",VPxval,VPxinf,VPxlen);
530            for (p=VPxkey, n=VPxlen; n--; p++) fprintf(fp,"%c",*p);
531            fprintf(fp,"|");
532            for (n=vlex[NTREE-1]-VPxlen; n--; ) fprintf(fp," ");
533            fprintf(fp,"|%6d|%9"_LD_"|%d|%d\n",nval,totval,
534                (nval <= mval) ? 1 : 0, treecase+1);
535        }
536
537    if (tell) {
538        fprintf(stderr,"     t=%9"_LD_"   %4d=%9"_LD_"   %4d=%9"_LD_" \n",
539            totval,1,VP(1-1)->val,nval,VP(nval-1)->val);
540
541        fprintf(stderr,"                 p50=%9"_LD_"   %4d=%9"_LD_" \n",
542            medval,mval,accval);
543    }
544
545
546    exit(0);
547}
Note: See TracBrowser for help on using the browser.