| 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 |
#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; |
| 58 |
volatile int abort_flag=0; |
| 59 |
volatile unsigned int filler_seed=0; |
| 60 |
static int fillmode=0; |
| 61 |
static clock_t ct0; |
| 62 |
|
| 63 |
int filler_status=0; |
| 64 |
|
| 65 |
|
| 66 |
static int sdep=-1; |
| 67 |
|
| 68 |
static char**sposs=0; |
| 69 |
static int*spossp=0; |
| 70 |
static int***sflist; |
| 71 |
static struct jdata***sjdata=0; |
| 72 |
static ABM***sjflbm=0; |
| 73 |
static struct sdata***ssdata=0; |
| 74 |
static int**sflistlen=0; |
| 75 |
static ABM**sentryfl=0; |
| 76 |
static int*sentry=0; |
| 77 |
|
| 78 |
static unsigned char*aused=0; |
| 79 |
static unsigned char*lused=0; |
| 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 |
|
| 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 |
|
| 151 |
static int strcyccmp(char*s,char*t,int l) { |
| 152 |
int i,j,k; |
| 153 |
|
| 154 |
for(i=1;i<l;i++) { |
| 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 |
|
| 166 |
|
| 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); |
| 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 |
|
| 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]; |
| 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 |
|
| 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++) { |
| 214 |
if(edone[k]) continue; |
| 215 |
u=bm[k]; |
| 216 |
if(!u) {DEB_F3 printf("infeasible at entry %d\n",k); return 0;} |
| 217 |
if(onebit(u)) { |
| 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; |
| 223 |
} |
| 224 |
} |
| 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; |
| 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; |
| 232 |
|
| 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; |
| 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++) { |
| 256 |
|
| 257 |
c=order[i]; |
| 258 |
n+=hi[c]; |
| 259 |
v=ICCTOABM(c); |
| 260 |
u|=v; |
| 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++; |
| 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) { |
| 269 |
for(k=0;k<m;k++) if(!edone[k]) if(bm[k]&u) {if(bm[k]&~u) f=1; bm[k]&=u;} |
| 270 |
DEB_F3 printf("new group\n"); |
| 271 |
n=0; |
| 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++; |
| 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) { |
| 301 |
i=checkperm(w,wn,1); |
| 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 |
|
| 310 |
|
| 311 |
|
| 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 |
|
| 318 |
static double ctl[MXFL+1][MXFL+1]; |
| 319 |
static double ctr[MXFL+1][MXFL+1]; |
| 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 |
|
| 357 |
memset(sd->ct,0,sizeof(sd->ct)); |
| 358 |
|
| 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]; |
| 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 |
|
| 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 |
|
| 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 |
|
| 397 |
return j; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 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; |
| 405 |
if(onebit(entries[i].flbm)) continue; |
| 406 |
if(entries[i].checking>m) m=entries[i].checking; |
| 407 |
} |
| 408 |
j=-1; |
| 409 |
for(;m>0;m--) { |
| 410 |
j=-1;l=DBL_MAX; |
| 411 |
for(i=0;i<ne;i++) { |
| 412 |
if(fillmode==2&&entries[i].sel==0) continue; |
| 413 |
if(!onebit(entries[i].flbm)&&entries[i].checking>=m) { |
| 414 |
k=entries[i].crux; |
| 415 |
if(k<l) l=k,j=i;} |
| 416 |
} |
| 417 |
if(j!=-1) return j; |
| 418 |
} |
| 419 |
return j; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 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 |
|
| 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 |
|
| 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; |
| 445 |
for(k=0;k<m;k++) if(!onebit(w->e[k]->flbm)) break; |
| 446 |
aed=(k==m); |
| 447 |
p=w->flist; |
| 448 |
l=w->flistlen; |
| 449 |
jd=w->jdata; |
| 450 |
sd=w->sdata; |
| 451 |
if(!p) continue; |
| 452 |
if(sflistlen[sdep][j]==-1) { |
| 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)); |
| 462 |
if(!w->flist) return -1; |
| 463 |
ct_malloc++; |
| 464 |
if(jmode==1) { |
| 465 |
w->jdata=(struct jdata*)malloc(l*sizeof(struct jdata)); |
| 466 |
if(!w->jdata) return -1; |
| 467 |
ct_malloc++; |
| 468 |
w->jflbm=(ABM*)malloc(l*mj*sizeof(ABM)); |
| 469 |
if(!w->jflbm) return -1; |
| 470 |
ct_malloc++; |
| 471 |
} |
| 472 |
if(jmode==2) { |
| 473 |
w->sdata=(struct sdata*)malloc(l*sizeof(struct sdata)); |
| 474 |
if(!w->sdata) return -1; |
| 475 |
ct_malloc++; |
| 476 |
} |
| 477 |
} |
| 478 |
if(afunique) { |
| 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) { |
| 485 |
for(k=0;k<m;k++) { |
| 486 |
e=w->e[k]; |
| 487 |
if(!e->upd) continue; |
| 488 |
l=listisect(w->flist,p,l,k,e->flbm); |
| 489 |
p=w->flist; |
| 490 |
if(l==0) break; |
| 491 |
} |
| 492 |
} else if(jmode==1) { |
| 493 |
for(k=mj;k<m;k++) { |
| 494 |
e=w->e[k]; |
| 495 |
if(!e->upd) continue; |
| 496 |
l=listisect(w->flist,p,l,k,e->flbm); |
| 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++; |
| 502 |
} else { |
| 503 |
for(i=0;i<l;i++) {w->flist[i]=p[i]; checksword(w,i);} |
| 504 |
w->upd=1; f++; |
| 505 |
} |
| 506 |
|
| 507 |
if(l!=w->flistlen) { |
| 508 |
w->upd=1;f++; |
| 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; |
| 526 |
if(!aed) continue; |
| 527 |
if(jmode==1) { |
| 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; |
| 533 |
assert(w->commitdep==-1); |
| 534 |
for(k=0;k<l;k++) setused(w->flist[k],1); |
| 535 |
w->commitdep=sdep; |
| 536 |
} |
| 537 |
|
| 538 |
for(i=0;i<ne;i++) entries[i].upd=0; |
| 539 |
|
| 540 |
return f; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 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; |
| 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]); |
| 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]; |
| 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]; |
| 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]; |
| 578 |
if(e->flbm&~entfl[j]) { |
| 579 |
e->flbm&=entfl[j]; |
| 580 |
e->upd=1;f++; |
| 581 |
|
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
for(i=0;i<nw;i++) words[i].upd=0; |
| 586 |
DEB_F1 {printf("settlewds returns %d\n",f);fflush(stdout);} |
| 587 |
return f; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 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 |
|
| 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; |
| 621 |
r=(double)(p-h)/(nuf-h)*(t-h)+h; |
| 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; |
| 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; |
| 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]; |
| 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; |
| 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 |
|
| 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 |
|
| 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 |
|
| 672 |
|
| 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 |
|
| 679 |
static double sc[MXFL][MAXICC+1],tsc[MXFL][MAXICC+1]; |
| 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) { |
| 697 |
if(afunique&&w->commitdep>=0) { |
| 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]))) { |
| 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; |
| 705 |
} |
| 706 |
} |
| 707 |
} else if(jmode==1) { |
| 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; |
| 713 |
for(k=mj;k<m;k++) sc[k][(int)lts[p[j]].s[k]]+=f; |
| 714 |
} |
| 715 |
} else { |
| 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; |
| 727 |
} |
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 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; |
| 738 |
l=0; |
| 739 |
k=-DBL_MAX; for(i=1;i<nicc;i++) if(e->score[i]>k) k=e->score[i]; |
| 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]; |
| 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; |
| 746 |
k=j;} |
| 747 |
s[l]='\0'; |
| 748 |
if(r==0) return; |
| 749 |
for(i=0;i<l;i++) { |
| 750 |
m=i+rand()%(r*2+1); |
| 751 |
if(m>=0&&m<l) n=s[i],s[i]=s[m],s[m]=n; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
|
| 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++) { |
| 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 |
|
| 808 |
static void state_init(void) {sdep=-1;filler_status=0;} |
| 809 |
|
| 810 |
|
| 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; |
| 815 |
for(i=0;i<ne;i++) sentryfl[sdep][i]=entries[i].flbm; |
| 816 |
} |
| 817 |
|
| 818 |
|
| 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) { |
| 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) { |
| 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 |
|
| 858 |
static void state_pop(void) {assert(sdep>=0);state_restore();sdep--;} |
| 859 |
|
| 860 |
|
| 861 |
static void state_finit(void) { |
| 862 |
while(sdep>=0) state_pop(); |
| 863 |
freestack(); |
| 864 |
} |
| 865 |
|
| 866 |
|
| 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)) { |
| 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)); |
| 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 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 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 |
|
| 928 |
|
| 929 |
if(settlewds()==-3) {DEB_F1 printf("aborting...\n"); return -5;}; |
| 930 |
resettle: |
| 931 |
do { |
| 932 |
if(abort_flag) {DEB_F1 printf("aborting...\n"); return -5;} |
| 933 |
f=settleents(); |
| 934 |
if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} |
| 935 |
if(f==0) break; |
| 936 |
if(f==-1) return -1; |
| 937 |
if(f==-2) goto backtrack; |
| 938 |
f=settlewds(); |
| 939 |
if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} |
| 940 |
} while(f); |
| 941 |
f=mkscores(); |
| 942 |
if(f==-3) {DEB_F1 printf("aborting...\n"); return -5;} |
| 943 |
if(fillmode==0||fillmode==3) return 2; |
| 944 |
DEB_F2 pstate(1); |
| 945 |
|
| 946 |
|
| 947 |
DEB_F2 { int w; for(w=0;w<nw;w++) printf("[w%d: %d]",w,words[w].flistlen); printf("\n"); } |
| 948 |
e=findcritent(); |
| 949 |
if(e==-1) return 2; |
| 950 |
getposs(entries+e,sposs[sdep],afrandom,1); |
| 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; |
| 954 |
|
| 955 |
|
| 956 |
nextposs: |
| 957 |
e=sentry[sdep]; |
| 958 |
if(sposs[sdep][spossp[sdep]]=='\0') goto backtrack; |
| 959 |
c=sposs[sdep][spossp[sdep]++]; |
| 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; |
| 962 |
state_push(); |
| 963 |
entries[e].upd=1; |
| 964 |
entries[e].flbm=ICCTOABM((int)c); |
| 965 |
ct1=clock(); if(ct1-ct0>CLOCKS_PER_SEC*3||ct1<ct0) {progress();ct0=clock();} |
| 966 |
goto resettle; |
| 967 |
|
| 968 |
backtrack: |
| 969 |
state_pop(); |
| 970 |
if(sdep!=-1) goto nextposs; |
| 971 |
return 1; |
| 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) { |
| 980 |
DEB_F0 printf("finishing gracefully fillmode=%d filler_status=%d\n",fillmode,filler_status); |
| 981 |
if(filler_status==2) { |
| 982 |
mkfeas(); |
| 983 |
DEB_F1 pstate(1); |
| 984 |
} |
| 985 |
else { |
| 986 |
for(i=0;i<ne;i++) entries[i].flbm=0; |
| 987 |
llistp=NULL;llistn=0; |
| 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); |
| 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 |
|
| 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(); |
| 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 |
|
| 1036 |
|
| 1037 |
|
| 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; |
| 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 |
|
| 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 |
} |