/* nty1.c CJP Version 2.1 16/10/07 */ #include #include #include"alw1.h" #include"nty.h" void ntycons(NTY *nt) { int good=1; nt->ntys = &((*nt).n); nt->rels = &((*nt).r); alwcons(nt->ntys, MAXWORDS); alwcons(nt->rels, MAXRELS); nt->wordstore = (int*) malloc(sizeof(int)*WORDSTORE); if(nt->wordstore==NULL) good=0; nt->prevnth = (int*) malloc(sizeof(int)*MAXWORDS); if(nt->prevnth==NULL) good=0; nt->nexnth = (int*) malloc(sizeof(int)*MAXWORDS); if(nt->nexnth==NULL) good=0; nt->relcan = (int*) malloc(sizeof(int)*MAXWORDS); if(nt->relcan==NULL) good=0; nt->ntyrel = (int*) malloc(sizeof(int)*MAXRELS); if(nt->ntyrel==NULL) good=0; if(good==0) { printf("malloc failed in ntycons\n"); exit(22); } } void ntystart(NTY *nt, int ngens, int *inv) { alwstart(nt->ntys); alwstart(nt->rels); nt->currentpos = nt->wordstore; (*nt).spaceused=0; nt->inv = inv; (*nt).ngens = ngens; } void ntydelete(NTY *nt, int relh) { int nthan; int i,j; nthan = nt->ntyrel[relh]; alwdelete(nt->rels, relh); i = nthan; while(1) { if(i==2) break; i = nt->nexnth[nthan]; alwdelete(nt->ntys, nthan); nthan = i; } } int ntyinsert(NTY *nt, int *rel, int len) { int i,j,m; int relhan1; int nthan1,nthan2,nthan3; int temp[MAXWORDLEN]; nthan1 = alwfind(nt->ntys, rel, len); if(nthan1>0) { return 0; } nthan1 = nthan1*(-1); (*nt).spaceused += len*2; if((*nt).spaceused > WORDSTORE) { printf("Used up all the space in WORDSTORE in ntyinsert\n"); exit(23); } for(i=0;irels, rel, len); relhan1 = alwinsert(nt->rels, nt->currentpos, len, relhan1); nthan2 = alwinsert(nt->ntys, nt->currentpos, len, nthan1); (*nt).prevnth[nthan2] = 1; nt->ntyrel[relhan1]=nthan2; nt->relcan[nthan2]=relhan1; // Add all rotations to the ALW for(i=1;intys, (*nt).currentpos+i, len); if(nthan2>0) { nthan2 = alwinsert(nt->ntys, (*nt).currentpos+i, len, nthan2); (*nt).prevnth[nthan2] = nthan1; (*nt).nexnth[nthan1] = nthan2; (*nt).relcan[nthan2] = relhan1; } else nthan2 = nthan1; } (*nt).nexnth[nthan2] = 2; nthan3 = nthan2; nt->currentpos += 2*len; // Check inverse for(i=0;iinv[rel[i]]; } nthan1 = alwfind(nt->ntys, temp, len); if(nthan1 > 0) { return relhan1; } nthan1 = nthan1 * (-1); // If not, add this to the list/store too. (*nt).spaceused += len*2; if((*nt).spaceused > WORDSTORE) { printf("Used up all the space in WORDSTORE in ntyinsert\n"); exit(23); } for(i=0;intys, nt->currentpos, len, nthan1); (*nt).prevnth[nthan2] = nthan3; (*nt).nexnth[nthan3] = nthan2; nt->relcan[nthan2]=relhan1; // Add all rotations (of the inverse) to the ALW for(i=1;intys, (*nt).currentpos+i, len); if(nthan2>0) { nthan2 = alwinsert(nt->ntys, (*nt).currentpos+i, len, nthan2); (*nt).prevnth[nthan2] = nthan1; (*nt).nexnth[nthan1] = nthan2; (*nt).relcan[nthan2] = relhan1; } else nthan2 = nthan1; } (*nt).nexnth[nthan2] = 2; nt->currentpos += 2*len; // End return relhan1; } int ntyrelnt(NTY *nt, int relh) { return nt->ntyrel[relh]; } int ntynexntr(NTY *nt, int nth) { return nt->nexnth[nth]; } int ntyntrel(NTY *nt, int nth) { return nt->relcan[nth]; } int *ntyntptr(NTY *nt, int nth) { return nt->n.word[nth]; } int ntyntlen(NTY *nt, int nth) { return nt->n.len[nth]; } int ntyrot(NTY *nt, int nth, int rot) { int i,j,k; int rot2,len,han; int *pt; int tmpword[MAXWORDLEN]; len = ntyntlen(nt,nth); rot2 = rot%len; rot2 += len; rot2 = rot2%len; pt = ntyntptr(nt,nth); for(i=0;intys, tmpword, len); } int ntynexnts(NTY *nt, int nth, int len) { int han; if(len<0) { return nt->ntys->nex[nth]; } han = nth; while(1) { han = nt->ntys->nex[han]; if(han==2) { return 2; } if(ntyntlen(nt,han)<=len) { return ntyntlen(nt,han); } } } int ntyprents(NTY *nt, int nth, int len) { int han; if(len<0) { return nt->ntys->prev[nth]; } han = nth; while(1) { han = nt->ntys->prev[han]; if(han==1) { return 1; } if(ntyntlen(nt,han)<=len) { return ntyntlen(nt,han); } } } int ntynexrel(NTY *nt, int relh) { return nt->rels->nex[relh]; } int ntyfind(NTY *nt, int *rel, int len) // Returns 0 if not found, nth otherwise { int current=1; int i; int *pt; while(1) { current = ntynexnts(nt,current,-1); if(current==2) return 0; if(ntyntlen(nt,current)!=len) continue; pt = ntyntptr(nt,current); for(i=0;i