Debian Codegraph @

filler.c

raw symbols SCIP
1 /*
2 Qxw is a program to help construct and publish crosswords.
3
4 Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant
5 http://www.quinapalus.com
6 E-mail: [email protected]
7
8 This file is part of Qxw.
9
10 Qxw is free software: you can redistribute it and/or modify
11 it under the terms of version 2 of the GNU General Public License
12 as published by the Free Software Foundation.
13
14 Qxw is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Qxw. If not, see <http://www.gnu.org/licenses/> or
21 write to the Free Software Foundation, Inc., 51 Franklin Street,
22 Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25 /*
26 Interface to treatment.c comprises:
27 pregetinitflist();
28 getinitflist();
29 postgetinitflist();
30
31 Interface to qxw.c / gui.c comprises:
32 Calls out:
33 mkfeas();
34 updatefeas();
35 updategrid();
36 Calls in:
37 filler_init();
38 filler_start();
39 filler_stop();
40 filler_finit();
41 getposs();
42 */
43
44 #include <glib.h>
45 #include <gdk/gdk.h>
46 #include <time.h>
47 #include <float.h>
48 #include "common.h"
49 #include "filler.h"
50 #include "treatment.h"
51 #include "qxw.h"
52 #include "gui.h"
53 #include "dicts.h"
54
55 static GThread*fth;
56
57 static int ct_malloc=0,ct_free=0; // counters for debugging
58 volatile int abort_flag=0;
59 volatile unsigned int filler_seed=0;
60 static int fillmode=0; // 0=stopped, 1=filling all, 2=filling selection, 3=word lists only (for preexport)
61 static clock_t ct0;
62
63 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
64
65 // the following stacks keep track of the filler state as it recursively tries to fill the grid
66 static int sdep=-1; // stack pointer
67
68 static char**sposs=0; // possibilities for this entry, 0-terminated
69 static int*spossp=0; // which possibility we are currently trying (index into sposs)
70 static int***sflist; // pointers to restore feasible word list flist
71 static struct jdata***sjdata=0; // jumble data
72 static ABM***sjflbm=0; // jumble data feasible list bitmaps
73 static struct sdata***ssdata=0; // spread data
74 static int**sflistlen=0; // pointers to restore flistlen
75 static ABM**sentryfl=0; // feasible letter bitmap for this entry
76 static int*sentry=0; // entry considered at this depth
77
78 static unsigned char*aused=0; // answer already used while filling
79 static unsigned char*lused=0; // light already used while filling
80
81 #define isused(l) (lused[lts[l].uniq]|aused[lts[l].ans+NMSG])
82 #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)
83
84 static void pstate(int f) {
85 int i,j,jmode;
86 struct word*w;
87
88 for(i=0;i<nw;i++) {
89 w=words+i;
90 if(w->lp->emask&EM_JUM) jmode=1;
91 else if(w->lp->emask&EM_SPR) jmode=2;
92 else jmode=0;
93 printf("W%d: fe=%d jmode=%d nent=%d wlen=%d jlen=%d E: ",i,w->fe,jmode,w->nent,w->wlen,w->jlen);
94 for(j=0;j<w->nent;j++) printf("%d ",(int)(w->e[j]-entries));
95 printf("= ");
96 for(j=0;j<w->nent;j++) pabm(w->e[j]->flbm,1);
97 printf("\n");
98 if(f) {
99 printf(" ");
100 if(w->flistlen<8) j=0;
101 else {
102 for(j=0;j<3;j++) {
103 printf(" ");
104 printICCs(lts[w->flist[j]].s);
105 printf("[%d]",lts[w->flist[j]].uniq);
106 }
107 printf(" ...");
108 j=w->flistlen-3;
109 }
110 for(;j<w->flistlen;j++) {
111 printf(" ");
112 printICCs(lts[w->flist[j]].s);
113 printf("[%d]",lts[w->flist[j]].uniq);
114 }
115 printf(" (%d)\n",w->flistlen);
116 }
117 }
118 }
119
120
121 // transfer progress info to display
122 static void progress(void) {
123 DEB_F1 printf("ct_malloc=%d ct_free=%d diff=%d\n",ct_malloc,ct_free,ct_malloc-ct_free);
124 gdk_threads_enter();
125 updategrid();
126 gdk_threads_leave();
127 }
128
129 static int initjdata(int j) {struct word*w; int i;
130 w=words+j;
131 if(!(w->lp->emask&EM_JUM)) return 0;
132 w->jdata=malloc(w->flistlen*sizeof(struct jdata));
133 if(!w->jdata) return -1;
134 w->jflbm=malloc(w->flistlen*w->jlen*sizeof(ABM));
135 if(!w->jflbm) return -1;
136 for(i=0;i<w->flistlen*w->jlen;i++) w->jflbm[i]=ABM_ALL;
137 return 0;
138 }
139
140 static int initsdata(int j) {struct word*w; int i,k;
141 w=words+j;
142 if(!(w->lp->emask&EM_SPR)) return 0;
143 if(w->fe) return 0;
144 w->sdata=malloc(w->flistlen*sizeof(struct sdata));
145 if(!w->sdata) return -1;
146 for(i=0;i<w->flistlen;i++) for(k=0;k<w->nent;k++) w->sdata[i].flbm[k]=ABM_ALL;
147 return 0;
148 }
149
150 // Determine if one string is a non-trivial cyclic permutation of another
151 static int strcyccmp(char*s,char*t,int l) {
152 int i,j,k;
153
154 for(i=1;i<l;i++) { // cyclic shift amount
155 for(j=0,k=i;j<l;j++,k++) {
156 if(k==l) k=0;
157 if(s[j]!=t[k]) goto ex0;
158 }
159 return 1;
160 ex0:;
161 }
162 return 0;
163 }
164
165 // Check that jumble is not a disallowed permutation
166 // mode=0: use entry flbm:s; 1: use jdata flbm:s
167 static int checkperm(struct word*w,int j,int mode) {
168 int em,k,m;
169 char f[MXFL+1],r[MXFL+1],*t;
170
171 em=w->lp->emask;
172 m=w->jlen;
173 for(k=0;k<m;k++) f[k]=r[m-1-k]=abmtoicc(mode?w->jflbm[j*m+k]:w->e[k]->flbm); // build up forward and reversed versions
174 f[k]=0; r[k]=0;
175 t=lts[w->flist[j]].s;
176 DEB_F3 { printf("checkperm("); printICCs(f); printf(" : "); printICCs(t); printf(" em=%d mode=%d)\n",em,mode); }
177 if((em&EM_FWD)==0) if(!strncmp(f,t,m)) return 0;
178 if((em&EM_REV)==0) if(!strncmp(r,t,m)) return 0;
179 if((em&EM_CYC)==0) if(strcyccmp(f,t,m)) return 0;
180 if((em&EM_RCY)==0) if(strcyccmp(r,t,m)) return 0;
181 return 1;
182 }
183
184 // Approximate test to see if a jumble of #wn in the flist for word w can fit. Writes deductions to flbm etc. in jdata.
185 static int checkjword(struct word*w,int wn) {
186 unsigned char hi[MAXICC+1];
187 ABM bm[MXFL],u,v,*jbm;
188 unsigned char edone[MXFL]; // entries done
189 unsigned char order[MAXICC+1];
190 int excess[MAXICC+1];
191 struct light*l;
192 int c,c0,c1,f,i,i0,k,m,n,norder,nuf;
193 int mex,mmex;
194
195 l=lts+w->flist[wn];
196 m=w->jlen;
197 jbm=w->jflbm+wn*m;
198 DEB_F3 {
199 printf("checkjword(w=%ld,\"",(long int)(w-words));
200 printICCs(l->s);
201 printf("\") jlen=%d lbm=",m);
202 pabm(l->lbm,1);
203 printf("\nhist="); for(i=1;i<MAXICC+1;i++) printf(" %d",l->hist[i]); printf("\n");
204 }
205 for(k=0;k<m;k++) bm[k]=w->e[k]->flbm&l->lbm;
206 // here bm[k] is the array of feasible letter bitmaps, and l->hist[] is the histogram of available letters
207 DEB_F3 { printf("entering main loop with\nbm[k]= "); pabms(bm,m,1); printf("\n"); }
208 do {
209 memcpy(hi,l->hist,sizeof(hi));
210 memset(edone,0,sizeof(edone));
211 l0:
212 f=0;
213 for(k=0;k<m;k++) { // first take any forced entries off the histogram
214 if(edone[k]) continue;
215 u=bm[k];
216 if(!u) {DEB_F3 printf("infeasible at entry %d\n",k); return 0;} // infeasible
217 if(onebit(u)) { // entry is forced
218 c=abmtoicc(u);
219 DEB_F3 printf("position %d is forced to %s\n",k,icctoutf8[c]);
220 if(hi[c]==0) {DEB_F3 printf("not enough in histogram for forced entries\n"); return 0;}
221 hi[c]--;
222 edone[k]=1; // mark entry as done
223 }
224 } // now hi[] is the histogram of available letters after forced entries are removed
225 for(i=1;i<MAXICC+1;i++) if(hi[i]==0) for(k=0;k<m;k++) if(!edone[k]) {
226 v=ICCTOABM(i);
227 if(bm[k]&v) bm[k]&=~v,f=1; // knock out characters that we have run out of; if we change bm[] we will go round again
228 }
229 DEB_F3 {printf("after removing forced entries:\nhist="); for(i=1;i<MAXICC+1;i++) printf(" %d",hi[i]); printf("\n"); }
230 DEB_F3 {printf("bm[k]= "); pabms(bm,m,1); printf("\n"); }
231 if(f) goto l0; // go round again if anything changed
232 // now all forced entries will be flagged in edone
233
234 for(i=1;i<MAXICC+1;i++) if(hi[i]>0) {
235 v=ICCTOABM(i);
236 for(k=0,c=0;k<m;k++) if(!edone[k]) if((bm[k]&v)!=0) c++;
237 excess[i]=c-hi[i];
238 DEB_F3 printf("character %s can go in %d place(s) excess=%d\n",icctoutf8[i],c,excess[i]);
239 if(excess[i]<0) { DEB_F3 printf("not enough room for all occurrences of character %s\n",icctoutf8[i]); return 0; }
240 if(excess[i]==0) {
241 DEB_F3 printf("only just enough room for all occurrences of character %s\n",icctoutf8[i]);
242 for(k=0;k<m;k++) if(!edone[k]) if((bm[k]&v)!=0) bm[k]=v,f=1;
243 }
244 }
245 if(f) goto l0; // go round again if anything changed
246
247 mmex=0; norder=0;
248 do {
249 mex=MXFL+1;
250 for(i=1;i<MAXICC+1;i++) if(hi[i]>0&&excess[i]>mmex&&excess[i]<mex) mex=excess[i];
251 for(i=1;i<MAXICC+1;i++) if(hi[i]>0&&excess[i]==mex) order[norder++]=i;
252 mmex=mex;
253 } while(mmex<=MXFL);
254
255 for(u=0,n=0,i=0,i0=0;i<norder;i++) { // work from biggest histogram entry down, greedily looking for contradictions
256 // a "group" is a consecutive set of letters in this order where the total histogram is equal to the size of the set
257 c=order[i];
258 n+=hi[c]; // accumulate histogram total of letters considered so far in this group
259 v=ICCTOABM(c);
260 u|=v; // accumulate bitmap of letters considered so far in this group
261 DEB_F3 {printf("considering group %d..%d=",i0,i); pabm(u,1); printf(": ");}
262 for(c1=0,k=0;k<m;k++) {
263 if(edone[k]) continue;
264 if(bm[k]&u) c1++; // count places where letters in this group can go ("slots")
265 }
266 DEB_F3 printf(" c=%s group hist total=%d slots[group]=%d\n",icctoutf8[c],n,c1);
267 if(c1< n) {DEB_F3 { printf("not enough slots for group "); pabm(u,1); printf(" slot count=%d group count=%d\n",c1,n); } return 0;}
268 if(c1==n) { // only just enough slots to go round for this group
269 for(k=0;k<m;k++) if(!edone[k]) if(bm[k]&u) {if(bm[k]&~u) f=1; bm[k]&=u;} // set f if we change any bm[k]
270 DEB_F3 printf("new group\n");
271 n=0; // start a new group
272 u=0;
273 i0=i+1;
274 }
275 }
276 } while(f);
277 memcpy(jbm,bm,m*sizeof(ABM));
278 for(i=1;i<MAXICC+1;i++) {
279 w->jdata[wn].ufhist[i]=hi[i];
280 v=ICCTOABM(i);
281 for(c0=0,k=0;k<m;k++) {
282 if(edone[k]) continue;
283 if(bm[k]&v) c0++; // count places where this letter can go
284 }
285 w->jdata[wn].poscnt[i]=c0;
286 }
287 for(nuf=0,k=0;k<m;k++) if(!edone[k]) nuf++;
288 w->jdata[wn].nuf=nuf;
289 DEB_F3 {
290 printf("checkjword returning w=%ld \"",(long int)(w-words));
291 printICCs(l->s);
292 printf("\" nuf=%d\n",nuf);
293 printf("ufhist="); for(i=1;i<MAXICC+1;i++) printf("%2d",w->jdata[wn].ufhist[i]); printf("\n");
294 printf("poscnt="); for(i=1;i<MAXICC+1;i++) printf("%2d",w->jdata[wn].poscnt[i]); printf("\n");
295 printf(" flbm: ");
296 pabms(jbm,m,1);
297 printf("\n");
298 }
299 for(i=0;i<m;i++) if(!onebit(jbm[i])) break;
300 if(i==m) { // all entries are forced, so we are done
301 i=checkperm(w,wn,1); // is it one of the special permutations (reversed etc.) that is disallowed?
302 DEB_F3 printf("checkperm returns %d\n",i);
303 if(i==0) return 0;
304 }
305 DEB_F3 printf("checkjword: OK\n");
306 return 1;
307 }
308
309 // Calculate number of possible spreads that put each possible letter in each position
310 // given implications of flbm:s.
311 // should not overflow as 250!/125!/125! < 2^250, which is much less than 1e308
312 static void scounts(struct word*w,int wn,ABM*bm) {
313 struct sdata*sd;
314 struct light*l;
315 int i,j,m,n;
316 ABM u;
317 // following are static to avoid overflowing stack (!) in Windows version
318 static double ctl[MXFL+1][MXFL+1]; // ctl[i][j] is # of arrangements where chars [0,i) fit in slots [0,j)
319 static double ctr[MXFL+1][MXFL+1]; // ctr[i][j] is # of arrangements where chars [i,n) fit in slots [j,m)
320
321 l=lts+w->flist[wn];
322 m=w->nent;
323 n=w->wlen;
324 sd=w->sdata+wn;
325 DEB_F3 { printf("scounts: w=%ld \"",(long int)(w-words)); printICCs(l->s); printf("\"\n"); }
326 memset(ctl,0,sizeof(ctl));
327 ctl[0][0]=1;
328 for(j=1;j<=m;j++) {
329 u=bm[j-1];
330 for(i=0;i<=n;i++) {
331 if(u&ABM_DASH) ctl[i][j]=ctl[i][j-1];
332 if(i>0&&(u&ICCTOABM((int)l->s[i-1]))) ctl[i][j]+=ctl[i-1][j-1];
333 }
334 }
335 memset(ctr,0,sizeof(ctr));
336 ctr[n][m]=1;
337 for(j=m-1;j>=0;j--) {
338 u=bm[j];
339 for(i=n;i>=0;i--) {
340 if(u&ABM_DASH) ctr[i][j]=ctr[i][j+1];
341 if(i<n&&(u&ICCTOABM((int)l->s[i]))) ctr[i][j]+=ctr[i+1][j+1];
342 }
343 }
344 DEB_F3 {
345 printf("CTL: # of ways to put chars [0,i) in slots [0,j)\n");
346 for(j=0;j<=m;j++) {
347 for(i=0;i<=n;i++) printf("%5.1f ",ctl[i][j]);
348 printf("\n");
349 }
350 printf("CTR: # of ways to put chars [i,n) in slots [j,m)\n");
351 for(j=0;j<=m;j++) {
352 for(i=0;i<=n;i++) printf("%5.1f ",ctr[i][j]);
353 printf("\n");
354 }
355 }
356 // DEB_F3 {printf("memset0: %p 0 %d wn=%d\n",sd->ct,sizeof(sd->ct),wn); fflush(stdout); }
357 memset(sd->ct,0,sizeof(sd->ct));
358 // DEB_F3 {printf("memset1: %p 0 %d\n",sd->ctd,sizeof(sd->ctd)); fflush(stdout); }
359 memset(sd->ctd,0,sizeof(sd->ctd));
360 for(i=0;i<n;i++) for(j=0;j<m;j++) sd->ct[i][j]=ctl[i][j]*ctr[i+1][j+1];
361 for(j=0;j<m;j++) if(bm[j]&ABM_DASH) for(i=0;i<=n;i++) sd->ctd[j]+=ctl[i][j]*ctr[i][j+1]; // do the spreading character as a special case
362 DEB_F3 {
363 printf(" " ); for(i=0;i<n;i++) printf(" %s ",icctoutf8[(int)l->s[i]]); printf(" -\n");
364 for(j=0;j<m;j++) {printf(" e%2d:",j); for(i=0;i<n;i++) printf(" %5.1f",sd->ct[i][j]); printf(" %5.1f",sd->ctd[j]); printf("\n");}
365 }
366 memset(sd->flbm,0,sizeof(sd->flbm));
367 for(i=0;i<n;i++) for(j=0;j<m;j++) if(sd->ct [i][j]) sd->flbm[j]|=ICCTOABM((int)l->s[i]);
368 for (j=0;j<m;j++) if(sd->ctd[j]) sd->flbm[j]|=ABM_DASH;
369 }
370
371 // Test to see if a spread string can fit in a given word. Writes deductions to w->sdata[j].
372 static void checksword(struct word*w,int j) {
373 ABM bm[MXFL];
374 struct light*l;
375 struct sdata*sd;
376 int k,m;
377
378 l=lts+w->flist[j];
379 sd=w->sdata+j;
380 m=w->nent;
381 for(k=0;k<m;k++) bm[k]=w->e[k]->flbm;
382 DEB_F3 {
383 printf("checksword(w=%ld,\"",(long int)(w-words));
384 printICCs(l->s);
385 printf("\") bm=");
386 pabms(bm,m,1);
387 printf(" j=%d\n",j);
388 }
389 scounts(w,j,bm);
390 DEB_F3 { printf(" output bm="); pabms(sd->flbm,m,1); printf("\n"); }
391 }
392
393 // intersect light list q length l with letter position wp masked by bitmap m: result is stored in p and new length is returned
394 static int listisect(int*p,int*q,int l,int wp,ABM m) {int i,j;
395 for(i=0,j=0;i<l;i++) if(m&(ICCTOABM((int)(lts[q[i]].s[wp])))) p[j++]=q[i];
396 //printf("listisect l(wp=%d m=%16llx) %d->%d\n",wp,m,l,j);
397 return j;
398 }
399
400 // find the entry to expand next, or -1 if all done
401 static int findcritent(void) {int i,j,m;double k,l;
402 m=-1;
403 for(i=0;i<ne;i++) {
404 if(fillmode==2&&entries[i].sel==0) continue; // filling selection only: only check relevant entries
405 if(onebit(entries[i].flbm)) continue;
406 if(entries[i].checking>m) m=entries[i].checking; // find highest checking level // find highest checking level
407 }
408 j=-1;
409 for(;m>0;m--) { // m>=2: loop over checked entries; then m=1: loop over unchecked entries
410 j=-1;l=DBL_MAX;
411 for(i=0;i<ne;i++) {
412 if(fillmode==2&&entries[i].sel==0) continue; // filling selection only: only check relevant entries
413 if(!onebit(entries[i].flbm)&&entries[i].checking>=m) { // not already fixed?
414 k=entries[i].crux; // get the priority for this entry
415 if(k<l) l=k,j=i;}
416 }
417 if(j!=-1) return j; // return entry with highest priority if one found
418 }
419 return j; // return -1 if no entries left to expand
420 }
421
422 // check updated entries and rebuild feasible word lists
423 // returns -3 for aborted, -2 for infeasible, -1 for out of memory, 0 if no feasible word lists affected, >=1 otherwise
424 static int settleents(void) {
425 struct entry*e;
426 struct word*w;
427 struct jdata*jd;
428 struct sdata*sd;
429 ABM*jdf;
430 int aed,f,i,j,k,l,m,mj,jmode;
431 int*p;
432 // DEB_F1 printf("settleents() sdep=%d\n",sdep);
433 f=0;
434 for(j=0;j<nw;j++) {
435 if(abort_flag) return -3;
436 w=words+j;
437 if(w->lp->emask&EM_JUM) jmode=1;
438 else if(w->lp->emask&EM_SPR) jmode=2;
439 else jmode=0;
440 // printf("j=%d jmode=%d emask=%d\n",j,jmode,w->lp->emask);
441 m=w->nent;
442 mj=w->jlen;
443 for(k=0;k<m;k++) if(w->e[k]->upd) break;
444 if(k==m) continue; // no update flags set on any entries for this word
445 for(k=0;k<m;k++) if(!onebit(w->e[k]->flbm)) break;
446 aed=(k==m); // all entries determined?
447 p=w->flist;
448 l=w->flistlen;
449 jd=w->jdata;
450 sd=w->sdata;
451 if(!p) continue; // no flist?
452 if(sflistlen[sdep][j]==-1) { // then we mustn't trash words[].flist
453 sflist [sdep][j]=p;
454 sflistlen[sdep][j]=l;
455 sjdata [sdep][j]=jd;
456 sjflbm [sdep][j]=w->jflbm;
457 ssdata [sdep][j]=sd;
458 w->jdata=0;
459 w->jflbm=0;
460 w->sdata=0;
461 w->flist=(int*)malloc(l*sizeof(int)); // new list can be at most as long as old one
462 if(!w->flist) return -1; // out of memory
463 ct_malloc++;
464 if(jmode==1) {
465 w->jdata=(struct jdata*)malloc(l*sizeof(struct jdata));
466 if(!w->jdata) return -1; // out of memory
467 ct_malloc++;
468 w->jflbm=(ABM*)malloc(l*mj*sizeof(ABM));
469 if(!w->jflbm) return -1; // out of memory
470 ct_malloc++;
471 }
472 if(jmode==2) {
473 w->sdata=(struct sdata*)malloc(l*sizeof(struct sdata));
474 if(!w->sdata) return -1; // out of memory
475 ct_malloc++;
476 }
477 }
478 if(afunique) { // the following test makes things quite a lot slower: consider optimising by keeping track of when an update might be needed
479 for(i=0,k=0;i<l;i++) if(!isused(p[i])) w->flist[k++]=p[i];
480 p=w->flist;
481 l=k;
482 }
483
484 if(jmode==0) { // normal case
485 for(k=0;k<m;k++) { // think about moving this loop inside listisect()
486 e=w->e[k];
487 if(!e->upd) continue;
488 l=listisect(w->flist,p,l,k,e->flbm); // generate new feasible word list
489 p=w->flist;
490 if(l==0) break;
491 }
492 } else if(jmode==1) { // jumble case
493 for(k=mj;k<m;k++) { // loop over tags if any
494 e=w->e[k];
495 if(!e->upd) continue;
496 l=listisect(w->flist,p,l,k,e->flbm); // generate new feasible word list
497 p=w->flist;
498 }
499 for(i=0,k=0;i<l;i++) {w->flist[k]=p[i]; if(checkjword(w,k)) k++;}
500 l=k;
501 w->upd=1; f++; // need to do settlents() anyway in this case
502 } else { // spread case
503 for(i=0;i<l;i++) {w->flist[i]=p[i]; checksword(w,i);}
504 w->upd=1; f++; // need to do settlents() anyway in this case
505 }
506
507 if(l!=w->flistlen) {
508 w->upd=1;f++; // word list has changed: feasible letter lists will need updating
509 if(l) {
510 p=realloc(w->flist,l*sizeof(int));
511 if(p) w->flist=p;
512 if(w->jdata) {
513 jd=realloc(w->jdata,l*sizeof(struct jdata));
514 if(jd) w->jdata=jd;
515 jdf=realloc(w->jflbm,l*mj*sizeof(ABM));
516 if(jdf) w->jflbm=jdf;
517 }
518 if(w->sdata) {
519 sd=realloc(w->sdata,l*sizeof(struct sdata));
520 if(sd) w->sdata=sd;
521 }
522 }
523 }
524 w->flistlen=l;
525 if(l==0&&!w->fe) return -2; // no options left and was not fully entered by user
526 if(!aed) continue; // not all entries determined yet, so don't commit
527 if(jmode==1) { // final check that the "jumble" is not actually a cyclic permutation etc.
528 for(i=0,k=0;i<l;i++) {w->flist[k]=w->flist[i]; if(checkperm(w,k,0)) k++;}
529 l=k;
530 }
531 w->flistlen=l;
532 if(l==0&&!w->fe) return -2; // no options left and was not fully entered by user
533 assert(w->commitdep==-1);
534 for(k=0;k<l;k++) setused(w->flist[k],1); // flag as used (can be more than one in jumble case)
535 w->commitdep=sdep;
536 }
537
538 for(i=0;i<ne;i++) entries[i].upd=0; // all entry update effects now propagated into word updates
539 // DEB_F1 printf("settleents returns %d\n",f);fflush(stdout);
540 return f;
541 }
542
543 // check updated word lists, rebuild feasible entry lists
544 // returns -3 for aborted, 0 if no feasible letter lists affected, >0 otherwise
545 static int settlewds(void) {
546 int f,i,j,k,l,m,mj,jmode;
547 int*p;
548 struct entry*e;
549 struct word*w;
550 ABM entfl[MXFL];
551 DEB_F1 printf("settlewds()\n");
552 f=0;
553 for(i=0;i<nw;i++) {
554 if(abort_flag) return -3;
555 w=words+i;
556 if(!w->upd) continue; // loop over updated word lists
557 if(ifamode==1&&i!=curword) continue;
558 if(w->fe) continue;
559 if(w->lp->emask&EM_JUM) jmode=1;
560 else if(w->lp->emask&EM_SPR) jmode=2;
561 else jmode=0;
562 m=w->nent;
563 mj=w->jlen;
564 p=w->flist;
565 l=w->flistlen;
566
567 for(k=0;k<m;k++) entfl[k]=0;
568 if(jmode==0) for(j=0;j<l;j++) for(k=0;k<m;k++) entfl[k]|=ICCTOABM((int)lts[p[j]].s[k]); // find all feasible letters from word list
569 else if(jmode==1) for(j=0;j<l;j++) {
570 for(k=0;k<mj;k++) entfl[k]|=w->jflbm[j*mj+k]; // main work has been done in settleents()
571 for( ;k<m ;k++) entfl[k]|=ICCTOABM((int)lts[p[j]].s[k]);
572 }
573 else if(jmode==2) for(j=0;j<l;j++) for(k=0;k<m;k++) entfl[k]|=w->sdata[j].flbm[k]; // main work has been done in settleents()
574
575 DEB_F2 { printf("w=%d entfl: ",i); pabms(entfl,m,1); printf("\n"); }
576 for(j=0;j<m;j++) {
577 e=w->e[j]; // propagate from word to entry
578 if(e->flbm&~entfl[j]) { // has this entry been changed by the additional constraint?
579 e->flbm&=entfl[j];
580 e->upd=1;f++; // flag that it will need updating
581 // printf("E%d %16llx\n",k,entries[k].flbm);fflush(stdout);
582 }
583 }
584 }
585 for(i=0;i<nw;i++) words[i].upd=0; // all word list updates processed
586 DEB_F1 {printf("settlewds returns %d\n",f);fflush(stdout);}
587 return f;
588 }
589
590 // Very approximate attempt to estimate number of possible jumbles that put each possible letter in each position
591 // given implications of flbm:s.
592 // Returns equally poor estimate of total number of permutations.
593 static double jscores(struct word*w,int j,double(*sc)[MAXICC+1]) {
594 struct jdata*jd;
595 struct light*l;
596 int h,i,k,m,nuf,p,t;
597 double r,s;
598 double tp;
599 ABM u,*jbm;
600
601 l=lts+w->flist[j];
602 m=w->jlen;
603 jd=w->jdata+j;
604 jbm=w->jflbm+j*m;
605 nuf=jd->nuf;
606 DEB_F3 {
607 printf("jscores: w=%ld \"",(long int)(w-words));
608 printICCs(l->s);
609 printf("\" nuf=%d\n",nuf);
610 printf("ufhist="); for(i=1;i<MAXICC+1;i++) printf("%2d",jd->ufhist[i]); printf("\n");
611 printf("poscnt="); for(i=1;i<MAXICC+1;i++) printf("%2d",jd->poscnt[i]); printf("\n");
612 printf(" flbm: ");
613 pabms(jbm,m,1);
614 }
615 // first make a very poor man's estimate of total feasible permutations given jdata information for word
616 t=nuf;
617 tp=1;
618 for(i=1;i<MAXICC+1;i++) {
619 h=jd->ufhist[i]; p=jd->poscnt[i];
620 if(!h||h==nuf) continue; // if none of this letter, or all unforced are the same letter, then nothing to do
621 r=(double)(p-h)/(nuf-h)*(t-h)+h; // if p==h, r=h; if p==nuf, r=t; and linearly (!) in between
622 for(k=1;k<=h;k++) tp*=r,tp/=k,r-=1;
623 t-=h;
624 }
625 if(tp>DBL_MAX) tp=DBL_MAX; // tp could conceivably go infinite here
626 DEB_F3 { printf(" tp=%g\n",tp); }
627 memset(sc,0,m*(MAXICC+1)*sizeof(double));
628 for(k=0;k<m;k++) {
629 u=jbm[k];
630 if(u==0) continue;
631 if(onebit(u)) sc[k][abmtoicc(u)]=tp; // forced entry
632 else {
633 for(r=0,i=1;i<MAXICC+1;i++) {
634 if(jd->poscnt[i]==0) continue;
635 if(u&(ICCTOABM(i))) {
636 s=(double)jd->ufhist[i]/jd->poscnt[i]; // proportion of fills that will have letter c in each position
637 r+=sc[k][i]=s;
638 }
639 }
640 if(r>0) r=tp/r;
641 for(i=1;i<MAXICC+1;i++) sc[k][i]*=r; // rescale so scores in each position add to tp
642 }
643 DEB_F3 { printf(" e%3d:",k); for(i=1;i<27;i++) printf("%7.1f",sc[k][i]); printf("\n"); }
644 }
645 return tp;
646 }
647
648
649
650 // calculate scores for spread entry
651 static void sscores(struct word*w,int wn,double(*sc)[MAXICC+1]) {
652 struct sdata*sd;
653 struct light*l;
654 int c,i,j,k,m,n;
655
656 l=lts+w->flist[wn];
657 m=w->nent;
658 n=w->wlen;
659 sd=w->sdata+wn;
660 memset(sc,0,m*(MAXICC+1)*sizeof(double));
661 for(i=0;i<n;i++) {
662 c=(int)l->s[i];
663 for(j=0;j<m;j++) sc[j][c]+=sd->ct[i][j];
664 }
665 // now do the spreading character as a special case: i=#chars to left of candidate '-'
666 for(j=0;j<m;j++) sc[j][ICC_DASH]+=sd->ctd[j];
667 DEB_F3 for(k=0;k<m;k++) {printf(" e%2d:",k); for(i=1;i<MAXICC+1;i++) {printf(" %5.1f",sc[k][i]); if(i==26||i==36) printf(" ");} printf("\n"); }
668 }
669
670
671 // calculate per-entry scores
672 // returns -3 if aborted
673 static int mkscores(void) {
674 int c,i,j,k,l,m,mj,jmode;
675 int*p;
676 double f;
677 struct word*w;
678 // following static to reduce stack use
679 static double sc[MXFL][MAXICC+1],tsc[MXFL][MAXICC+1]; // weighted count of number of words that put a given letter in a given place
680
681 for(i=0;i<ne;i++) for(j=1;j<MAXICC+1;j++) entries[i].score[j]=1.0;
682 for(i=0;i<nw;i++) {
683 if(abort_flag) return -3;
684 w=words+i;
685 if(w->fe) continue;
686 if(w->lp->emask&EM_JUM) jmode=1;
687 else if(w->lp->emask&EM_SPR) jmode=2;
688 else jmode=0;
689 m=w->nent;
690 mj=w->jlen;
691 p=w->flist;
692 l=w->flistlen;
693 for(k=0;k<m;k++) for(j=1;j<MAXICC+1;j++) sc[k][j]=0.0;
694 if(!p) continue;
695
696 if(jmode==0) { // normal case
697 if(afunique&&w->commitdep>=0) { // avoid zero score if we've committed
698 if(l==1) for(k=0;k<m;k++) sc[k][(int)lts[p[0]].s[k]]+=1.0;
699 }
700 else {
701 for(j=0;j<l;j++) if(!(afunique&&isused(p[j]))) { // for each remaining feasible word
702 if(lts[p[j]].ans<0) f=1;
703 else f=(double)ansp[lts[p[j]].ans]->score;
704 for(k=0;k<m;k++) sc[k][(int)lts[p[j]].s[k]]+=f; // add in its score to this cell's score
705 }
706 }
707 } else if(jmode==1) { // jumble case
708 for(j=0;j<l;j++) {
709 f=jscores(w,j,tsc);
710 for(k=0;k<mj;k++) for(c=1;c<MAXICC+1;c++) sc[k][c]+=tsc[k][c];
711 k=lts[p[j]].ans;
712 if(k>=0) f*=(double)ansp[k]->score; // score once for each feasible permutation; assume score=1 if a treatment light
713 for(k=mj;k<m;k++) sc[k][(int)lts[p[j]].s[k]]+=f;
714 }
715 } else { // spread case
716 for(j=0;j<l;j++) {
717 sscores(w,j,tsc);
718 for(k=0;k<m;k++) for(c=1;c<MAXICC+1;c++) sc[k][c]+=tsc[k][c];
719 }
720 }
721
722 for(k=0;k<m;k++) for(j=1;j<MAXICC+1;j++) w->e[k]->score[j]*=sc[k][j];
723 }
724 for(i=0;i<ne;i++) {
725 f=-DBL_MAX; for(j=1;j<MAXICC+1;j++) f=MX(f,entries[i].score[j]);
726 entries[i].crux=f; // crux at an entry is the greatest score over all possible letters
727 }
728 return 0;
729 }
730
731
732 // sort possible letters into order of decreasing favour with randomness r; write results to s in internal character code
733 void getposs(struct entry*e,char*s,int r,int dash) {
734 int i,l,m,n,nicc;
735 double j,k;
736 DEB_F1 printf("getposs(%d)\n",(int)(e-entries));
737 nicc=dash?MAXICC+1:MAXICC; // avoid outputting dashes?
738 l=0;
739 k=-DBL_MAX; for(i=1;i<nicc;i++) if(e->score[i]>k) k=e->score[i]; // find highest score
740 k*=2;
741 for(;;) {
742 for(i=1,j=-DBL_MAX;i<nicc;i++) if(e->score[i]>j&&e->score[i]<k) j=e->score[i]; // peel off scores from top down
743 DEB_F1 printf("getposs(%d): j=%g\n",(int)(e-entries),j);
744 if(j<=0) break;
745 for(i=1;i<nicc;i++) if(e->score[i]==j) s[l++]=i; // add to output string
746 k=j;} // get next highest set of equal scores
747 s[l]='\0';
748 if(r==0) return;
749 for(i=0;i<l;i++) { // randomise if necessary
750 m=i+rand()%(r*2+1); // candidate for swap: distance depends on randomisation level
751 if(m>=0&&m<l) n=s[i],s[i]=s[m],s[m]=n; // swap candidates
752 }
753 }
754
755 // indent according to stack depth
756 static void sdepsp(void) {int i; if(sdep<0) printf("<%d",sdep); for(i=0;i<sdep;i++) printf(" ");}
757
758 static void freestack() {int i;
759 for(i=0;i<=ne;i++) {
760 if(sposs ) FREEX(sposs [i]);
761 if(sflist ) FREEX(sflist [i]);
762 if(sjdata ) FREEX(sjdata [i]);
763 if(sjflbm ) FREEX(sjflbm [i]);
764 if(ssdata ) FREEX(ssdata [i]);
765 if(sflistlen ) FREEX(sflistlen [i]);
766 if(sentryfl ) FREEX(sentryfl [i]);
767 }
768 FREEX(sposs);
769 FREEX(spossp);
770 FREEX(sflist);
771 FREEX(sjdata);
772 FREEX(sjflbm);
773 FREEX(ssdata);
774 FREEX(sflistlen);
775 FREEX(sentryfl);
776 FREEX(sentry);
777 for(i=0;i<nw;i++) {
778 FREEX(words[i].jdata);
779 FREEX(words[i].jflbm);
780 FREEX(words[i].sdata);
781 }
782 }
783
784 static int allocstack() {int i;
785 freestack();
786 if(!(sposs =calloc(ne+1,sizeof(char* )))) return 1;
787 if(!(spossp =calloc(ne+1,sizeof(int )))) return 1;
788 if(!(sflist =calloc(ne+1,sizeof(int** )))) return 1;
789 if(!(sjdata =calloc(ne+1,sizeof(struct jdata**)))) return 1;
790 if(!(sjflbm =calloc(ne+1,sizeof(ABM** )))) return 1;
791 if(!(ssdata =calloc(ne+1,sizeof(struct sdata**)))) return 1;
792 if(!(sflistlen =calloc(ne+1,sizeof(int* )))) return 1;
793 if(!(sentryfl =calloc(ne+1,sizeof(ABM* )))) return 1;
794 if(!(sentry =calloc(ne+1,sizeof(int )))) return 1;
795 for(i=0;i<=ne;i++) { // for each stack depth that can be reached
796 if(!(sposs [i]=malloc(MAXICC+2 ))) return 1;
797 if(!(sflist [i]=malloc(nw*sizeof(int* )))) return 1;
798 if(!(sjdata [i]=malloc(nw*sizeof(struct jdata*)))) return 1;
799 if(!(sjflbm [i]=malloc(nw*sizeof(ABM* )))) return 1;
800 if(!(ssdata [i]=malloc(nw*sizeof(struct sdata*)))) return 1;
801 if(!(sflistlen [i]=malloc(nw*sizeof(int )))) return 1;
802 if(!(sentryfl [i]=malloc(ne*sizeof(ABM )))) return 1;
803 }
804 return 0;
805 }
806
807 // initialise state stacks
808 static void state_init(void) {sdep=-1;filler_status=0;}
809
810 // push stack
811 static void state_push(void) {int i;
812 sdep++;
813 assert(sdep<=ne);
814 for(i=0;i<nw;i++) sflistlen[sdep][i]=-1; // flag that flists need allocating
815 for(i=0;i<ne;i++) sentryfl[sdep][i]=entries[i].flbm; // feasible letter lists
816 }
817
818 // undo effect of last deepening operation
819 static void state_restore(void) {int i,j,l; struct word*w;
820 for(i=0;i<nw;i++) {
821 w=words+i;
822 if(w->commitdep>=sdep) { // word to uncommit?
823 l=w->flistlen;
824 DEB_F2 {
825 printf("sdep=%d flistlen=%d uncommitting word %d commitdep=%d:",sdep,w->flistlen,i,w->commitdep);
826 for(j=0;j<l;j++) {printf(" "); printICCs(lts[w->flist[j]].s);}
827 printf("\n");
828 }
829 for(j=0;j<l;j++) setused(w->flist[j],0);
830 w->commitdep=-1;
831 }
832 if(sflistlen[sdep][i]!=-1&&w->flist!=0) { // word feasible list to free?
833 free(w->flist);
834 ct_free++;
835 w->flist=sflist[sdep][i];
836 w->flistlen=sflistlen[sdep][i];
837 if(w->jdata) {
838 free(w->jdata);
839 ct_free++;
840 w->jdata=sjdata[sdep][i];
841 }
842 if(w->jflbm) {
843 free(w->jflbm);
844 ct_free++;
845 w->jflbm=sjflbm[sdep][i];
846 }
847 if(w->sdata) {
848 free(w->sdata);
849 ct_free++;
850 w->sdata=ssdata[sdep][i];
851 }
852 }
853 }
854 for(i=0;i<ne;i++) entries[i].flbm=sentryfl[sdep][i];
855 }
856
857 // pop stack
858 static void state_pop(void) {assert(sdep>=0);state_restore();sdep--;}
859
860 // clear state stacks and free allocated memory
861 static void state_finit(void) {
862 while(sdep>=0) state_pop();
863 freestack();
864 }
865
866 // build initial feasible lists, calling plug-in as necessary
867 static int buildlists(void) {int u,i,j;
868 DEB_F1 printf("buildlists() ");
869 for(i=0;i<nw;i++) {
870 DEB_F1 { printf("."); fflush(stdout); }
871 FREEX(words[i].flist);
872 FREEX(words[i].jdata);
873 FREEX(words[i].jflbm);
874 FREEX(words[i].sdata);
875 lightx=words[i].gx0;
876 lighty=words[i].gy0;
877 lightdir=words[i].ldir;
878 for(j=0;j<words[i].nent;j++) {
879 gridorderindex[j]=words[i].goi[j];
880 checking[j]=words[i].e[j]->checking;
881 }
882 if(fillmode>0||ifamode==2||(ifamode==1&&i==curword)) { // only build the lists we need
883 u=getinitflist(&words[i].flist,&words[i].flistlen,words[i].lp,words[i].wlen);
884 if(u) {filler_status=-3;return 0;}
885 if(initjdata(i)) {filler_status=-3;return 0;}
886 if(initsdata(i)) {filler_status=-3;return 0;}
887 }
888 if(abort_flag) {
889 DEB_F1 printf("aborted while building word lists\n");
890 filler_status=-5;
891 return 1;
892 }
893 if(words[i].lp->ten) clueorderindex++;
894 }
895 DEB_F1 printf("\n");
896 if(postgetinitflist()) {filler_status=-4;return 1;}
897 FREEX(aused);
898 FREEX(lused);
899 aused=(unsigned char*)calloc(atotal+NMSG,sizeof(unsigned char)); // enough for "msgword" answers too
900 if(aused==NULL) {filler_status=-3;return 0;}
901 lused=(unsigned char*)calloc(ultotal,sizeof(unsigned char));
902 if(lused==NULL) {filler_status=-3;return 0;}
903 return 0;
904 }
905
906 // Main search routine. Returns
907 // -5: told to abort
908 // -1: out of memory
909 // -2: out of stack
910 // 1: all done, no result found
911 // 2: all done, result found or only doing BG fill anyway
912 static int search() {
913 int e,f;
914 unsigned char c;
915 clock_t ct1;
916
917 if(fillmode==0&&ifamode==1) {
918 f=settleents();
919 if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;}
920 f=settlewds();
921 if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;}
922 f=mkscores();
923 if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;}
924 return 2;
925 }
926
927 // Initially entry flbms are not consistent with word lists or vice versa. So we
928 // need to make sure we call both settlewds() and settleents() before proceeding.
929 if(settlewds()==-3) {DEB_F1 printf("aborting...\n"); return -5;};
930 resettle: // "unit propagation"
931 do {
932 if(abort_flag) {DEB_F1 printf("aborting...\n"); return -5;}
933 f=settleents(); // rescan entries
934 if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;}
935 if(f==0) break;
936 if(f==-1) return -1; // out of memory: abort
937 if(f==-2) goto backtrack; // proved impossible
938 f=settlewds(); // rescan words
939 if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;}
940 } while(f); // need to iterate until everything settles down
941 f=mkscores();
942 if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;}
943 if(fillmode==0||fillmode==3) return 2; // only doing BG/preexport fill? stop after first settle
944 DEB_F2 pstate(1);
945
946 // go one level deeper in search tree
947 DEB_F2 { int w; for(w=0;w<nw;w++) printf("[w%d: %d]",w,words[w].flistlen); printf("\n"); }
948 e=findcritent(); // find the most critical entry, over whose possible letters we will iterate
949 if(e==-1) return 2; // all done, result found
950 getposs(entries+e,sposs[sdep],afrandom,1); // find feasible letter list in descending order of score
951 DEB_F1 { printf("D%3d ",sdep);sdepsp();printf("crit E%d ",e);printICCs(sposs[sdep]);printf("\n");fflush(stdout); }
952 sentry[sdep]=e;
953 spossp[sdep]=0; // start on most likely possibility
954
955 // try one possibility at the current critical entry
956 nextposs:
957 e=sentry[sdep];
958 if(sposs[sdep][spossp[sdep]]=='\0') goto backtrack; // none left: backtrack
959 c=sposs[sdep][spossp[sdep]++]; // get letter to try
960 DEB_F1 { printf("D%3d ",sdep);sdepsp();printf("trying E%d=%s\n",e,icctoutf8[(int)c]);fflush(stdout); }
961 if(sdep==ne) return -2; // out of stack space (should never happen)
962 state_push();
963 entries[e].upd=1;
964 entries[e].flbm=ICCTOABM((int)c); // fix feasible list
965 ct1=clock(); if(ct1-ct0>CLOCKS_PER_SEC*3||ct1<ct0) {progress();ct0=clock();} // update display every three seconds or so
966 goto resettle; // update internal data from new entry
967
968 backtrack:
969 state_pop();
970 if(sdep!=-1) goto nextposs;
971 return 1; // all done, no solution found
972 }
973
974 static void searchdone() {
975 int i;
976 DEB_F0 printf("searchdone: A\n");
977 gdk_threads_enter();
978 DEB_F0 printf("searchdone: B\n");
979 if(abort_flag==0) { // finishing gracefully?
980 DEB_F0 printf("finishing gracefully fillmode=%d filler_status=%d\n",fillmode,filler_status);
981 if(filler_status==2) {
982 mkfeas(); // construct feasible word list
983 DEB_F1 pstate(1);
984 }
985 else {
986 for(i=0;i<ne;i++) entries[i].flbm=0; // clear feasible letter bitmaps
987 llistp=NULL;llistn=0; // no feasible word list
988 DEB_F0 printf("BG fill failed\n"),fflush(stdout);
989 }
990 if(fillmode==1||fillmode==2) {
991 DEB_F0 printf("calling killfipdia();\n");
992 killfipdia();
993 }
994 updatefeas();
995 }
996 updategrid();
997 gdk_threads_leave();
998 DEB_F0 printf("searchdone: C\n");
999 state_finit();
1000 for(i=0;i<nw;i++) {
1001 if(words[i].commitdep>=0) printf("assertion failing i=%d nw=%d words[i].commitdep=%d\n",i,nw,words[i].commitdep);
1002 assert(words[i].commitdep==-1); // ... and uncommitted
1003 }
1004 DEB_F0 printf("search done\n");
1005 DEB_F0 {printf(">> ct_malloc=%d ct_free=%d diff=%d\n",ct_malloc,ct_free,ct_malloc-ct_free);fflush(stdout);}
1006 // j=0; for(i=0;i<ltotal;i++) j+=isused[i]; printf("total lused=%d\n",j);fflush(stdout);
1007 return;
1008 }
1009
1010 static gpointer fillerthread(gpointer data) {
1011 int i;
1012 clock_t ct;
1013
1014 DEB_F0 printf("filler_seed=0x%08x (set this using -F)\n",filler_seed);
1015 srand(filler_seed);
1016 ct=ct0=clock();
1017 clueorderindex=0;
1018 if(buildlists()) goto ex0;
1019 DEB_F1 pstate(1);
1020 for(i=0;i<ne;i++) entries[i].upd=1;
1021 for(i=0;i<nw;i++) words[i].upd=1;
1022 if(fillmode>0||ifamode>0) filler_status=search();
1023 else filler_status=2;
1024 if(fillmode!=3) searchdone(); // tidy up unless in pre-export mode
1025 DEB_F0 printf("search finished: %.3fs\n",(double)(clock()-ct)/CLOCKS_PER_SEC);
1026 ex0:
1027 DEB_F0 printf("fillerthread() terminating filler_status=%d\n",filler_status);
1028 return 0;
1029 }
1030
1031
1032
1033
1034
1035 // EXTERNAL INTERFACE
1036
1037 // returns !=0 on error
1038 int filler_start(int mode) {int i,j;
1039 assert(fth==0);
1040 DEB_F0 printf("filler_start(%d)\n",mode);
1041 DEB_F1 pstate(0);
1042 fillmode=mode;
1043 if(allocstack()) return 1;
1044 for(i=0;i<nw;i++) {
1045 words[i].fe=1;
1046 for(j=0;j<words[i].nent;j++) if(!onebit(words[i].e[j]->flbm)) {words[i].fe=0; break;}
1047 }
1048 if(pregetinitflist()) return 1;
1049 state_init();
1050 for(i=0;i<nw;i++) words[i].commitdep=-1; // flag word uncommitted
1051 state_push();
1052 filler_status=3;
1053 if(fseed) filler_seed=fseed;
1054 else filler_seed=(unsigned int)rand();
1055 fth=g_thread_create_full(&fillerthread,0,0,1,1,(fillmode!=3)?G_THREAD_PRIORITY_LOW:G_THREAD_PRIORITY_NORMAL,0);
1056 if(!fth) { state_finit(); return 1; }
1057 return 0;
1058 }
1059
1060 void filler_wait() {
1061 DEB_F0 printf("filler_wait() A\n");
1062 if(fth) {
1063 gdk_threads_leave();
1064 g_thread_join(fth);
1065 fth=0;
1066 gdk_threads_enter();
1067 }
1068 DEB_F0 printf("filler_wait() B\n");
1069 }
1070
1071 void filler_stop(void) {
1072 DEB_F0 printf("filler_stop() A\n");
1073 // if(filler_status!=3) return;
1074 abort_flag=1;
1075 filler_wait();
1076 abort_flag=0;
1077 DEB_F0 printf("filler_stop() B\n");
1078 state_finit();
1079 DEB_F0 printf("filler_stop() C\n");
1080 }
1081
1082 void filler_init() {
1083 }
1084
1085 void filler_finit() {
1086 }