/* Qxw is a program to help construct and publish crosswords. Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant http://www.quinapalus.com E-mail: qxw@quinapalus.com This file is part of Qxw. Qxw is free software: you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. Qxw 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 Qxw. If not, see or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* Interface to treatment.c comprises: pregetinitflist(); getinitflist(); postgetinitflist(); Interface to qxw.c / gui.c comprises: Calls out: mkfeas(); updatefeas(); updategrid(); Calls in: filler_init(); filler_start(); filler_stop(); filler_finit(); getposs(); */ #include #include #include #include #include "common.h" #include "filler.h" #include "treatment.h" #include "qxw.h" #include "gui.h" #include "dicts.h" static GThread*fth; static int ct_malloc=0,ct_free=0; // counters for debugging volatile int abort_flag=0; volatile unsigned int filler_seed=0; static int fillmode=0; // 0=stopped, 1=filling all, 2=filling selection, 3=word lists only (for preexport) static clock_t ct0; int filler_status=0; // return code: -5: aborted; -3, -4: initflist errors; -2: out of stack; -1: out of memory; 0: stopped; 1: no fill found; 2: fill found; 3: running // the following stacks keep track of the filler state as it recursively tries to fill the grid static int sdep=-1; // stack pointer static char**sposs=0; // possibilities for this entry, 0-terminated static int*spossp=0; // which possibility we are currently trying (index into sposs) static int***sflist; // pointers to restore feasible word list flist static struct jdata***sjdata=0; // jumble data static ABM***sjflbm=0; // jumble data feasible list bitmaps static struct sdata***ssdata=0; // spread data static int**sflistlen=0; // pointers to restore flistlen static ABM**sentryfl=0; // feasible letter bitmap for this entry static int*sentry=0; // entry considered at this depth static unsigned char*aused=0; // answer already used while filling static unsigned char*lused=0; // light already used while filling #define isused(l) (lused[lts[l].uniq]|aused[lts[l].ans+NMSG]) #define setused(l,v) lused[lts[l].uniq]=v,aused[lts[l].ans+NMSG]=v // ,printf("setused(%d,%d)->%d\n",l,v,lts[l].uniq) static void pstate(int f) { int i,j,jmode; struct word*w; for(i=0;ilp->emask&EM_JUM) jmode=1; else if(w->lp->emask&EM_SPR) jmode=2; else jmode=0; printf("W%d: fe=%d jmode=%d nent=%d wlen=%d jlen=%d E: ",i,w->fe,jmode,w->nent,w->wlen,w->jlen); for(j=0;jnent;j++) printf("%d ",(int)(w->e[j]-entries)); printf("= "); for(j=0;jnent;j++) pabm(w->e[j]->flbm,1); printf("\n"); if(f) { printf(" "); if(w->flistlen<8) j=0; else { for(j=0;j<3;j++) { printf(" "); printICCs(lts[w->flist[j]].s); printf("[%d]",lts[w->flist[j]].uniq); } printf(" ..."); j=w->flistlen-3; } for(;jflistlen;j++) { printf(" "); printICCs(lts[w->flist[j]].s); printf("[%d]",lts[w->flist[j]].uniq); } printf(" (%d)\n",w->flistlen); } } } // transfer progress info to display static void progress(void) { DEB_F1 printf("ct_malloc=%d ct_free=%d diff=%d\n",ct_malloc,ct_free,ct_malloc-ct_free); gdk_threads_enter(); updategrid(); gdk_threads_leave(); } static int initjdata(int j) {struct word*w; int i; w=words+j; if(!(w->lp->emask&EM_JUM)) return 0; w->jdata=malloc(w->flistlen*sizeof(struct jdata)); if(!w->jdata) return -1; w->jflbm=malloc(w->flistlen*w->jlen*sizeof(ABM)); if(!w->jflbm) return -1; for(i=0;iflistlen*w->jlen;i++) w->jflbm[i]=ABM_ALL; return 0; } static int initsdata(int j) {struct word*w; int i,k; w=words+j; if(!(w->lp->emask&EM_SPR)) return 0; if(w->fe) return 0; w->sdata=malloc(w->flistlen*sizeof(struct sdata)); if(!w->sdata) return -1; for(i=0;iflistlen;i++) for(k=0;knent;k++) w->sdata[i].flbm[k]=ABM_ALL; return 0; } // Determine if one string is a non-trivial cyclic permutation of another static int strcyccmp(char*s,char*t,int l) { int i,j,k; for(i=1;ilp->emask; m=w->jlen; for(k=0;kjflbm[j*m+k]:w->e[k]->flbm); // build up forward and reversed versions f[k]=0; r[k]=0; t=lts[w->flist[j]].s; DEB_F3 { printf("checkperm("); printICCs(f); printf(" : "); printICCs(t); printf(" em=%d mode=%d)\n",em,mode); } if((em&EM_FWD)==0) if(!strncmp(f,t,m)) return 0; if((em&EM_REV)==0) if(!strncmp(r,t,m)) return 0; if((em&EM_CYC)==0) if(strcyccmp(f,t,m)) return 0; if((em&EM_RCY)==0) if(strcyccmp(r,t,m)) return 0; return 1; } // Approximate test to see if a jumble of #wn in the flist for word w can fit. Writes deductions to flbm etc. in jdata. static int checkjword(struct word*w,int wn) { unsigned char hi[MAXICC+1]; ABM bm[MXFL],u,v,*jbm; unsigned char edone[MXFL]; // entries done unsigned char order[MAXICC+1]; int excess[MAXICC+1]; struct light*l; int c,c0,c1,f,i,i0,k,m,n,norder,nuf; int mex,mmex; l=lts+w->flist[wn]; m=w->jlen; jbm=w->jflbm+wn*m; DEB_F3 { printf("checkjword(w=%ld,\"",(long int)(w-words)); printICCs(l->s); printf("\") jlen=%d lbm=",m); pabm(l->lbm,1); printf("\nhist="); for(i=1;ihist[i]); printf("\n"); } for(k=0;ke[k]->flbm&l->lbm; // here bm[k] is the array of feasible letter bitmaps, and l->hist[] is the histogram of available letters DEB_F3 { printf("entering main loop with\nbm[k]= "); pabms(bm,m,1); printf("\n"); } do { memcpy(hi,l->hist,sizeof(hi)); memset(edone,0,sizeof(edone)); l0: f=0; for(k=0;k0) { v=ICCTOABM(i); for(k=0,c=0;k0&&excess[i]>mmex&&excess[i]0&&excess[i]==mex) order[norder++]=i; mmex=mex; } while(mmex<=MXFL); for(u=0,n=0,i=0,i0=0;ijdata[wn].ufhist[i]=hi[i]; v=ICCTOABM(i); for(c0=0,k=0;kjdata[wn].poscnt[i]=c0; } for(nuf=0,k=0;kjdata[wn].nuf=nuf; DEB_F3 { printf("checkjword returning w=%ld \"",(long int)(w-words)); printICCs(l->s); printf("\" nuf=%d\n",nuf); printf("ufhist="); for(i=1;ijdata[wn].ufhist[i]); printf("\n"); printf("poscnt="); for(i=1;ijdata[wn].poscnt[i]); printf("\n"); printf(" flbm: "); pabms(jbm,m,1); printf("\n"); } for(i=0;iflist[wn]; m=w->nent; n=w->wlen; sd=w->sdata+wn; DEB_F3 { printf("scounts: w=%ld \"",(long int)(w-words)); printICCs(l->s); printf("\"\n"); } memset(ctl,0,sizeof(ctl)); ctl[0][0]=1; for(j=1;j<=m;j++) { u=bm[j-1]; for(i=0;i<=n;i++) { if(u&ABM_DASH) ctl[i][j]=ctl[i][j-1]; if(i>0&&(u&ICCTOABM((int)l->s[i-1]))) ctl[i][j]+=ctl[i-1][j-1]; } } memset(ctr,0,sizeof(ctr)); ctr[n][m]=1; for(j=m-1;j>=0;j--) { u=bm[j]; for(i=n;i>=0;i--) { if(u&ABM_DASH) ctr[i][j]=ctr[i][j+1]; if(is[i]))) ctr[i][j]+=ctr[i+1][j+1]; } } DEB_F3 { printf("CTL: # of ways to put chars [0,i) in slots [0,j)\n"); for(j=0;j<=m;j++) { for(i=0;i<=n;i++) printf("%5.1f ",ctl[i][j]); printf("\n"); } printf("CTR: # of ways to put chars [i,n) in slots [j,m)\n"); for(j=0;j<=m;j++) { for(i=0;i<=n;i++) printf("%5.1f ",ctr[i][j]); printf("\n"); } } // DEB_F3 {printf("memset0: %p 0 %d wn=%d\n",sd->ct,sizeof(sd->ct),wn); fflush(stdout); } memset(sd->ct,0,sizeof(sd->ct)); // DEB_F3 {printf("memset1: %p 0 %d\n",sd->ctd,sizeof(sd->ctd)); fflush(stdout); } memset(sd->ctd,0,sizeof(sd->ctd)); for(i=0;ict[i][j]=ctl[i][j]*ctr[i+1][j+1]; for(j=0;jctd[j]+=ctl[i][j]*ctr[i][j+1]; // do the spreading character as a special case DEB_F3 { printf(" " ); for(i=0;is[i]]); printf(" -\n"); for(j=0;jct[i][j]); printf(" %5.1f",sd->ctd[j]); printf("\n");} } memset(sd->flbm,0,sizeof(sd->flbm)); for(i=0;ict [i][j]) sd->flbm[j]|=ICCTOABM((int)l->s[i]); for (j=0;jctd[j]) sd->flbm[j]|=ABM_DASH; } // Test to see if a spread string can fit in a given word. Writes deductions to w->sdata[j]. static void checksword(struct word*w,int j) { ABM bm[MXFL]; struct light*l; struct sdata*sd; int k,m; l=lts+w->flist[j]; sd=w->sdata+j; m=w->nent; for(k=0;ke[k]->flbm; DEB_F3 { printf("checksword(w=%ld,\"",(long int)(w-words)); printICCs(l->s); printf("\") bm="); pabms(bm,m,1); printf(" j=%d\n",j); } scounts(w,j,bm); DEB_F3 { printf(" output bm="); pabms(sd->flbm,m,1); printf("\n"); } } // intersect light list q length l with letter position wp masked by bitmap m: result is stored in p and new length is returned static int listisect(int*p,int*q,int l,int wp,ABM m) {int i,j; for(i=0,j=0;i%d\n",wp,m,l,j); return j; } // find the entry to expand next, or -1 if all done static int findcritent(void) {int i,j,m;double k,l; m=-1; for(i=0;im) m=entries[i].checking; // find highest checking level // find highest checking level } j=-1; for(;m>0;m--) { // m>=2: loop over checked entries; then m=1: loop over unchecked entries j=-1;l=DBL_MAX; for(i=0;i=m) { // not already fixed? k=entries[i].crux; // get the priority for this entry if(k=1 otherwise static int settleents(void) { struct entry*e; struct word*w; struct jdata*jd; struct sdata*sd; ABM*jdf; int aed,f,i,j,k,l,m,mj,jmode; int*p; // DEB_F1 printf("settleents() sdep=%d\n",sdep); f=0; for(j=0;jlp->emask&EM_JUM) jmode=1; else if(w->lp->emask&EM_SPR) jmode=2; else jmode=0; // printf("j=%d jmode=%d emask=%d\n",j,jmode,w->lp->emask); m=w->nent; mj=w->jlen; for(k=0;ke[k]->upd) break; if(k==m) continue; // no update flags set on any entries for this word for(k=0;ke[k]->flbm)) break; aed=(k==m); // all entries determined? p=w->flist; l=w->flistlen; jd=w->jdata; sd=w->sdata; if(!p) continue; // no flist? if(sflistlen[sdep][j]==-1) { // then we mustn't trash words[].flist sflist [sdep][j]=p; sflistlen[sdep][j]=l; sjdata [sdep][j]=jd; sjflbm [sdep][j]=w->jflbm; ssdata [sdep][j]=sd; w->jdata=0; w->jflbm=0; w->sdata=0; w->flist=(int*)malloc(l*sizeof(int)); // new list can be at most as long as old one if(!w->flist) return -1; // out of memory ct_malloc++; if(jmode==1) { w->jdata=(struct jdata*)malloc(l*sizeof(struct jdata)); if(!w->jdata) return -1; // out of memory ct_malloc++; w->jflbm=(ABM*)malloc(l*mj*sizeof(ABM)); if(!w->jflbm) return -1; // out of memory ct_malloc++; } if(jmode==2) { w->sdata=(struct sdata*)malloc(l*sizeof(struct sdata)); if(!w->sdata) return -1; // out of memory ct_malloc++; } } if(afunique) { // the following test makes things quite a lot slower: consider optimising by keeping track of when an update might be needed for(i=0,k=0;iflist[k++]=p[i]; p=w->flist; l=k; } if(jmode==0) { // normal case for(k=0;ke[k]; if(!e->upd) continue; l=listisect(w->flist,p,l,k,e->flbm); // generate new feasible word list p=w->flist; if(l==0) break; } } else if(jmode==1) { // jumble case for(k=mj;ke[k]; if(!e->upd) continue; l=listisect(w->flist,p,l,k,e->flbm); // generate new feasible word list p=w->flist; } for(i=0,k=0;iflist[k]=p[i]; if(checkjword(w,k)) k++;} l=k; w->upd=1; f++; // need to do settlents() anyway in this case } else { // spread case for(i=0;iflist[i]=p[i]; checksword(w,i);} w->upd=1; f++; // need to do settlents() anyway in this case } if(l!=w->flistlen) { w->upd=1;f++; // word list has changed: feasible letter lists will need updating if(l) { p=realloc(w->flist,l*sizeof(int)); if(p) w->flist=p; if(w->jdata) { jd=realloc(w->jdata,l*sizeof(struct jdata)); if(jd) w->jdata=jd; jdf=realloc(w->jflbm,l*mj*sizeof(ABM)); if(jdf) w->jflbm=jdf; } if(w->sdata) { sd=realloc(w->sdata,l*sizeof(struct sdata)); if(sd) w->sdata=sd; } } } w->flistlen=l; if(l==0&&!w->fe) return -2; // no options left and was not fully entered by user if(!aed) continue; // not all entries determined yet, so don't commit if(jmode==1) { // final check that the "jumble" is not actually a cyclic permutation etc. for(i=0,k=0;iflist[k]=w->flist[i]; if(checkperm(w,k,0)) k++;} l=k; } w->flistlen=l; if(l==0&&!w->fe) return -2; // no options left and was not fully entered by user assert(w->commitdep==-1); for(k=0;kflist[k],1); // flag as used (can be more than one in jumble case) w->commitdep=sdep; } for(i=0;i0 otherwise static int settlewds(void) { int f,i,j,k,l,m,mj,jmode; int*p; struct entry*e; struct word*w; ABM entfl[MXFL]; DEB_F1 printf("settlewds()\n"); f=0; for(i=0;iupd) continue; // loop over updated word lists if(ifamode==1&&i!=curword) continue; if(w->fe) continue; if(w->lp->emask&EM_JUM) jmode=1; else if(w->lp->emask&EM_SPR) jmode=2; else jmode=0; m=w->nent; mj=w->jlen; p=w->flist; l=w->flistlen; for(k=0;kjflbm[j*mj+k]; // main work has been done in settleents() for( ;ksdata[j].flbm[k]; // main work has been done in settleents() DEB_F2 { printf("w=%d entfl: ",i); pabms(entfl,m,1); printf("\n"); } for(j=0;je[j]; // propagate from word to entry if(e->flbm&~entfl[j]) { // has this entry been changed by the additional constraint? e->flbm&=entfl[j]; e->upd=1;f++; // flag that it will need updating // printf("E%d %16llx\n",k,entries[k].flbm);fflush(stdout); } } } for(i=0;iflist[j]; m=w->jlen; jd=w->jdata+j; jbm=w->jflbm+j*m; nuf=jd->nuf; DEB_F3 { printf("jscores: w=%ld \"",(long int)(w-words)); printICCs(l->s); printf("\" nuf=%d\n",nuf); printf("ufhist="); for(i=1;iufhist[i]); printf("\n"); printf("poscnt="); for(i=1;iposcnt[i]); printf("\n"); printf(" flbm: "); pabms(jbm,m,1); } // first make a very poor man's estimate of total feasible permutations given jdata information for word t=nuf; tp=1; for(i=1;iufhist[i]; p=jd->poscnt[i]; if(!h||h==nuf) continue; // if none of this letter, or all unforced are the same letter, then nothing to do r=(double)(p-h)/(nuf-h)*(t-h)+h; // if p==h, r=h; if p==nuf, r=t; and linearly (!) in between for(k=1;k<=h;k++) tp*=r,tp/=k,r-=1; t-=h; } if(tp>DBL_MAX) tp=DBL_MAX; // tp could conceivably go infinite here DEB_F3 { printf(" tp=%g\n",tp); } memset(sc,0,m*(MAXICC+1)*sizeof(double)); for(k=0;kposcnt[i]==0) continue; if(u&(ICCTOABM(i))) { s=(double)jd->ufhist[i]/jd->poscnt[i]; // proportion of fills that will have letter c in each position r+=sc[k][i]=s; } } if(r>0) r=tp/r; for(i=1;iflist[wn]; m=w->nent; n=w->wlen; sd=w->sdata+wn; memset(sc,0,m*(MAXICC+1)*sizeof(double)); for(i=0;is[i]; for(j=0;jct[i][j]; } // now do the spreading character as a special case: i=#chars to left of candidate '-' for(j=0;jctd[j]; DEB_F3 for(k=0;kfe) continue; if(w->lp->emask&EM_JUM) jmode=1; else if(w->lp->emask&EM_SPR) jmode=2; else jmode=0; m=w->nent; mj=w->jlen; p=w->flist; l=w->flistlen; for(k=0;kcommitdep>=0) { // avoid zero score if we've committed if(l==1) for(k=0;kscore; for(k=0;k=0) f*=(double)ansp[k]->score; // score once for each feasible permutation; assume score=1 if a treatment light for(k=mj;ke[k]->score[j]*=sc[k][j]; } for(i=0;iscore[i]>k) k=e->score[i]; // find highest score k*=2; for(;;) { for(i=1,j=-DBL_MAX;iscore[i]>j&&e->score[i]score[i]; // peel off scores from top down DEB_F1 printf("getposs(%d): j=%g\n",(int)(e-entries),j); if(j<=0) break; for(i=1;iscore[i]==j) s[l++]=i; // add to output string k=j;} // get next highest set of equal scores s[l]='\0'; if(r==0) return; for(i=0;i=0&&mcommitdep>=sdep) { // word to uncommit? l=w->flistlen; DEB_F2 { printf("sdep=%d flistlen=%d uncommitting word %d commitdep=%d:",sdep,w->flistlen,i,w->commitdep); for(j=0;jflist[j]].s);} printf("\n"); } for(j=0;jflist[j],0); w->commitdep=-1; } if(sflistlen[sdep][i]!=-1&&w->flist!=0) { // word feasible list to free? free(w->flist); ct_free++; w->flist=sflist[sdep][i]; w->flistlen=sflistlen[sdep][i]; if(w->jdata) { free(w->jdata); ct_free++; w->jdata=sjdata[sdep][i]; } if(w->jflbm) { free(w->jflbm); ct_free++; w->jflbm=sjflbm[sdep][i]; } if(w->sdata) { free(w->sdata); ct_free++; w->sdata=ssdata[sdep][i]; } } } for(i=0;i=0);state_restore();sdep--;} // clear state stacks and free allocated memory static void state_finit(void) { while(sdep>=0) state_pop(); freestack(); } // build initial feasible lists, calling plug-in as necessary static int buildlists(void) {int u,i,j; DEB_F1 printf("buildlists() "); for(i=0;ichecking; } if(fillmode>0||ifamode==2||(ifamode==1&&i==curword)) { // only build the lists we need u=getinitflist(&words[i].flist,&words[i].flistlen,words[i].lp,words[i].wlen); if(u) {filler_status=-3;return 0;} if(initjdata(i)) {filler_status=-3;return 0;} if(initsdata(i)) {filler_status=-3;return 0;} } if(abort_flag) { DEB_F1 printf("aborted while building word lists\n"); filler_status=-5; return 1; } if(words[i].lp->ten) clueorderindex++; } DEB_F1 printf("\n"); if(postgetinitflist()) {filler_status=-4;return 1;} FREEX(aused); FREEX(lused); aused=(unsigned char*)calloc(atotal+NMSG,sizeof(unsigned char)); // enough for "msgword" answers too if(aused==NULL) {filler_status=-3;return 0;} lused=(unsigned char*)calloc(ultotal,sizeof(unsigned char)); if(lused==NULL) {filler_status=-3;return 0;} return 0; } // Main search routine. Returns // -5: told to abort // -1: out of memory // -2: out of stack // 1: all done, no result found // 2: all done, result found or only doing BG fill anyway static int search() { int e,f; unsigned char c; clock_t ct1; if(fillmode==0&&ifamode==1) { f=settleents(); if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} f=settlewds(); if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} f=mkscores(); if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} return 2; } // Initially entry flbms are not consistent with word lists or vice versa. So we // need to make sure we call both settlewds() and settleents() before proceeding. if(settlewds()==-3) {DEB_F1 printf("aborting...\n"); return -5;}; resettle: // "unit propagation" do { if(abort_flag) {DEB_F1 printf("aborting...\n"); return -5;} f=settleents(); // rescan entries if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} if(f==0) break; if(f==-1) return -1; // out of memory: abort if(f==-2) goto backtrack; // proved impossible f=settlewds(); // rescan words if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} } while(f); // need to iterate until everything settles down f=mkscores(); if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} if(fillmode==0||fillmode==3) return 2; // only doing BG/preexport fill? stop after first settle DEB_F2 pstate(1); // go one level deeper in search tree DEB_F2 { int w; for(w=0;wCLOCKS_PER_SEC*3||ct1=0) printf("assertion failing i=%d nw=%d words[i].commitdep=%d\n",i,nw,words[i].commitdep); assert(words[i].commitdep==-1); // ... and uncommitted } DEB_F0 printf("search done\n"); DEB_F0 {printf(">> ct_malloc=%d ct_free=%d diff=%d\n",ct_malloc,ct_free,ct_malloc-ct_free);fflush(stdout);} // j=0; for(i=0;i0||ifamode>0) filler_status=search(); else filler_status=2; if(fillmode!=3) searchdone(); // tidy up unless in pre-export mode DEB_F0 printf("search finished: %.3fs\n",(double)(clock()-ct)/CLOCKS_PER_SEC); ex0: DEB_F0 printf("fillerthread() terminating filler_status=%d\n",filler_status); return 0; } // EXTERNAL INTERFACE // returns !=0 on error int filler_start(int mode) {int i,j; assert(fth==0); DEB_F0 printf("filler_start(%d)\n",mode); DEB_F1 pstate(0); fillmode=mode; if(allocstack()) return 1; for(i=0;iflbm)) {words[i].fe=0; break;} } if(pregetinitflist()) return 1; state_init(); for(i=0;i