-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.c
1431 lines (1330 loc) · 38.1 KB
/
infer.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sku - analysis tool for Sudoku puzzles
* Copyright (C) 2005 Richard P. Curnow
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "sku.h"
/* ============================================================================ */
struct ws;
struct queue;
struct link {/*{{{*/
/* Node for holding a cell or group index in a queue. */
struct link *next;
struct link *prev;
int index;
struct queue *q; /* which queue the link is on, NULL o/w */
struct queue *base_q; /* which queue the link has to go back to when its resouce is updated. */
};
/*}}}*/
struct score {
double foo;
};
/* Returns -1 if an error has been detected,
* 0 if no work got done,
* 1 if work was done. */
typedef int (*WORKER)(int, struct layout *, struct ws *, int, struct score *score);
struct queue {/*{{{*/
struct link links; /* .index ignored */
struct queue *next_to_run;
struct queue *next_to_push;
int opt;
char *name;
WORKER worker;
};
/*}}}*/
static struct queue *mk_queue(WORKER worker, struct queue *next_to_run, struct queue *next_to_push, int opt, char *name)/*{{{*/
{
struct queue *x;
x = new(struct queue);
x->links.next = x->links.prev = &x->links;
x->worker = worker;
x->next_to_run = next_to_run;
x->next_to_push = next_to_push;
x->opt = opt;
x->name = strdup(name);
return x;
}
/*}}}*/
static void free_queue(struct queue *x)/*{{{*/
{
free(x->name);
free(x);
}
/*}}}*/
static struct link *dequeue(struct queue *q) /*{{{*/
{
struct link *base, *first;
base = &q->links;
first = base->next;
if (first == base) {
/* Empty queue */
return NULL;
}
first->next->prev = base;
base->next = first->next;
first->next = first->prev = first;
first->q = NULL;
return first;
}
/*}}}*/
static void enqueue(struct link *lk, struct queue *q)/*{{{*/
{
struct link *base;
if (lk->q) {
fprintf(stderr, "Can't enqueue %d, it's already on a queue!!\n", lk->index);
exit(2);
}
base = &q->links;
lk->prev = base->prev;
lk->next = base;
base->prev->next = lk;
base->prev = lk;
lk->q = q;
}
/*}}}*/
static void move_to_queue(struct link *lk, struct queue *to_q)/*{{{*/
{
struct link *base;
if (lk->q == to_q) {
return;
}
/* Take off old queue. */
if (lk->q) {
lk->next->prev = lk->prev;
lk->prev->next = lk->next;
}
/* Push onto new queue */
base = &to_q->links;
lk->prev = base->prev;
lk->next = base;
lk->q = to_q;
base->prev->next = lk;
base->prev = lk;
}
/*}}}*/
/* ============================================================================ */
struct ws {/*{{{*/
int nc, ng, ns;
int *poss;
int *todo;
int solvepos;
int spec_depth;
int n_todo;
int n_marked_todo;
double score;
/* Pointers/values passed in at the outer level. */
int options;
int *order;
int *state;
/* Queues for what to scan next. */
struct queue *base_q;
struct queue *base_cell_q;
struct queue *base_line_q;
struct queue *base_block_q;
struct link *group_links;
struct link *cell_links;
};
/*}}}*/
static struct ws *make_ws(int nc, int ng, int ns)/*{{{*/
{
struct ws *ws = new(struct ws);
int fill;
int i;
ws->nc = nc;
ws->ng = ng;
ws->ns = ns;
ws->spec_depth = 0;
fill = (1<<ns) - 1;
ws->todo = new_array(int, ng);
ws->poss = new_array(int, nc);
ws->n_todo = 0;
ws->n_marked_todo = -1;
ws->score = 0.0;
for (i=0; i<ng; i++) ws->todo[i] = fill;
for (i=0; i<nc; i++) ws->poss[i] = fill;
ws->group_links = new_array(struct link, ng);
for (i=0; i<ng; i++) {
ws->group_links[i].next = ws->group_links[i].prev = &ws->group_links[i];
ws->group_links[i].index = i;
ws->group_links[i].q = NULL;
}
ws->cell_links = new_array(struct link, nc);
for (i=0; i<nc; i++) {
ws->cell_links[i].next = ws->cell_links[i].prev = &ws->cell_links[i];
ws->cell_links[i].index = i;
ws->cell_links[i].q = NULL;
}
return ws;
}
/*}}}*/
static void set_base_queues(const struct layout *lay, struct ws *ws)/*{{{*/
{
int gi, ci;
for (gi=0; gi<lay->ng; gi++) {
struct link *x;
x = ws->group_links + gi;
if (lay->is_block[gi]) {
x->base_q = ws->base_block_q;
} else {
x->base_q = ws->base_line_q;
}
}
for (ci=0; ci<lay->nc; ci++) {
struct link *x;
x = ws->cell_links + ci;
x->base_q = ws->base_cell_q;
}
}
/*}}}*/
static int *copy_array(int n, int *data)/*{{{*/
{
int *result;
result = new_array(int, n);
memcpy(result, data, n * sizeof(int));
return result;
}
/*}}}*/
static struct ws *clone_ws(const struct ws *src)/*{{{*/
{
struct ws *ws;
ws = new(struct ws);
ws->nc = src->nc;
ws->ng = src->ng;
ws->ns = src->ns;
ws->spec_depth = 1 + src->spec_depth;
ws->n_todo = src->n_todo;
ws->n_marked_todo = src->n_marked_todo;
ws->solvepos = src->solvepos;
ws->poss = copy_array(src->nc, src->poss);
ws->todo = copy_array(src->ng, src->todo);
ws->options = src->options;
ws->order = src->order;
ws->state = NULL;
ws->score = 0.0;
/* When we need to clone, we know the queues must be empty! We share this
* with the outer context(s) since sharing is cheaper. */
ws->base_q = src->base_q;
ws->base_cell_q = src->base_cell_q;
ws->base_line_q = src->base_line_q;
ws->base_block_q = src->base_block_q;
ws->group_links = src->group_links;
ws->cell_links = src->cell_links;
return ws;
}
/*}}}*/
static void free_cloned_ws(struct layout *lay, struct ws *ws)/*{{{*/
{
struct queue *q;
int i;
/* Clean queues. */
q = ws->base_q;
while (q) {
struct link *lk = &q->links;
lk->next = lk->prev = lk;
q = q->next_to_run;
}
for (i=0; i<lay->ng; i++) {
struct link *lk = ws->group_links + i;
lk->q = NULL;
}
for (i=0; i<lay->nc; i++) {
struct link *lk = ws->cell_links + i;
lk->q = NULL;
}
free(ws->poss);
free(ws->todo);
free(ws);
}
/*}}}*/
static void free_ws(struct ws *ws)/*{{{*/
{
struct queue *q;
free(ws->poss);
free(ws->todo);
q = ws->base_q;
while (q) {
struct queue *nq = q->next_to_run;
free_queue(q);
q = nq;
}
free(ws->group_links);
free(ws->cell_links);
free(ws);
}
/*}}}*/
/* ============================================================================ */
static int inner_infer(struct layout *lay, struct ws *ws);
/* ============================================================================ */
static void requeue_group(int gi, struct layout *lay, struct ws *ws)/*{{{*/
{
struct link *lk = ws->group_links + gi;
if (lk->base_q) {
move_to_queue(lk, lk->base_q);
} else {
/* No rules are available for this type of resource, ignore. */
}
}
/*}}}*/
static void requeue_groups(struct layout *lay, struct ws *ws, int ic)/*{{{*/
{
int i;
struct cell *cell = lay->cells + ic;
for (i=0; i<NDIM; i++) {
int gi = cell->group[i];
if (gi >= 0) requeue_group(gi, lay, ws);
else break;
}
}
/*}}}*/
static void requeue_cell(int ci, struct layout *lay, struct ws *ws)/*{{{*/
{
if (ws->state[ci] != CELL_BARRED) {
move_to_queue(ws->cell_links + ci, ws->base_cell_q);
}
}
/*}}}*/
static void allocate(struct layout *lay, struct ws *ws, int is_init, int ic, int val)/*{{{*/
{
int mask;
int j, k;
int NS;
short *base;
int other_poss;
mask = 1<<val;
NS = lay->ns;
if (ws->state[ic] == CELL_MARKED) {
--ws->n_marked_todo;
}
ws->state[ic] = val;
other_poss = ws->poss[ic] & ~mask;
ws->poss[ic] = 0;
if (!is_init && ws->order) {
ws->order[ic] = (ws->solvepos)++;
}
for (k=0; k<NDIM; k++) {
int gg = lay->cells[ic].group[k];
if (gg >= 0) {
ws->todo[gg] &= ~mask;
requeue_group(gg, lay, ws);
base = lay->groups + gg*NS;
for (j=0; j<NS; j++) {
int jc;
jc = base[j];
if (ws->poss[jc] & mask) {
ws->poss[jc] &= ~mask;
requeue_cell(jc, lay, ws);
requeue_groups(lay, ws, jc);
if (lay->cells[ic].is_terminal) {
lay->cells[ic].is_terminal = 0;
}
}
if (ws->poss[jc] & other_poss) {
/* The discovery of state[ic] has contributed to eventually solving [jc],
* so [ic] is now non-terminal. */
if (lay->cells[ic].is_terminal) {
lay->cells[ic].is_terminal = 0;
}
}
}
} else {
break;
}
}
}
/*}}}*/
static int try_group_allocate(int gi, struct layout *lay, struct ws *ws, int opt, struct score *score)/*{{{*/
{
/* Return -1 if the solution is broken,
* 0 if we didn't allocate anything,
* 1 if we did. */
int NS;
short *base;
int sym, mask;
int found_any = 0;
NS = lay->ns;
base = lay->groups + gi*NS;
for (sym=0; sym<NS; sym++) {
mask = 1<<sym;
if (ws->todo[gi] & mask) {
int j, count, xic;
xic = -1;
count = 0;
for (j=0; j<NS; j++) {
int ic = base[j];
if (ws->poss[ic] & mask) {
xic = ic;
count++;
if (count > 1) break;
}
}
if (count == 0) {
if (!(ws->options & OPT_SPECULATE)) {
fprintf(stderr, "Cannot allocate <%c> in <%s>\n",
lay->symbols[sym], lay->group_names[gi]);
}
return -1;
} else if (count == 1) {
if (score) {
score->foo += 1.0 / (double) count_bits(ws->todo[gi]);
found_any = 1;
} else {
if (ws->state[xic] != CELL_BARRED) {
if (ws->options & OPT_VERBOSE) {
fprintf(stderr, "(%c) Allocate <%c> to <%s> (allocate in <%s>)\n",
(lay->is_block[gi] ? ' ' : 'l'),
lay->symbols[sym], lay->cells[xic].name, lay->group_names[gi]);
}
--ws->n_todo;
allocate(lay, ws, 0, xic, sym);
if (ws->options & OPT_HINT) {
free_ws(ws);
free_layout(lay);
exit(0);
}
found_any = 1;
}
}
}
}
}
return found_any ? 1 : 0;
}
/*}}}*/
static int try_subsets(int gi, struct layout *lay, struct ws *ws, int opt, struct score *score)/*{{{*/
{
/* Couldn't do any allocates in the group.
* So try the more sophisticated analysis:
* Analyse the group to find out which subset of cells can contain
* each unallocated symbol. If this subset is also a subset of some other
* group, we can eliminate the symbol as a possibility from the rest of
* that other group.
*/
int NC, NS, NG;
char *flags;
int *counts;
int sym;
int n_poss_cells;
short *base;
int did_anything = 0;
NS = lay->ns;
NC = lay->nc;
NG = lay->ng;
flags = new_array(char, NC);
counts = new_array(int, NG);
base = lay->groups + gi*NS;
for (sym=0; sym<NS; sym++) {
int mask = (1 << sym);
if (ws->todo[gi] & mask) {
int j;
int found_something = 0;
memset(flags, 0, NC);
memset(counts, 0, NG*sizeof(int));
n_poss_cells = 0;
for (j=0; j<NS; j++) {
int ic = base[j];
if (ws->poss[ic] & mask) {
int m;
++n_poss_cells;
flags[ic] = 1;
for (m=0; m<NDIM; m++) {
int gm = lay->cells[ic].group[m];
if (gm >= 0) {
if (gm != gi) ++counts[gm];
} else {
break;
}
}
}
}
for (j=0; j<NG; j++) {
if (counts[j] == n_poss_cells) {
int m;
short *base = lay->groups + j*NS;
for (m=0; m<NS; m++) {
int ic = base[m];
if (!flags[ic]) { /* cell not in the original group. */
if (ws->poss[ic] & mask) {
if (score) {
} else {
if (ws->options & OPT_VERBOSE) {
fprintf(stderr, "(s) Removing <%c> from <%s> (in <%s> due to placement in <%s>)\n",
lay->symbols[sym], lay->cells[ic].name,
lay->group_names[j], lay->group_names[gi]);
}
ws->poss[ic] &= ~mask;
requeue_cell(ic, lay, ws);
requeue_groups(lay, ws, ic);
found_something = 1;
}
did_anything = 1;
}
}
}
}
}
}
}
free(counts);
free(flags);
return did_anything ? 1 : 0;
}
/*}}}*/
static int do_ext_remove(int gi, struct layout *lay, struct ws *ws, int n, int symbol_set, int matching_cells)/*{{{*/
{
int i;
int NS = lay->ns;
short *base;
int did_anything = 0;
base = lay->groups + (gi * NS);
#if 0
fprintf(stderr, "Symbol set : ");
show_symbols_in_set(NS, lay->symbols, symbol_set);
fprintf(stderr, "\n");
for (i=0; i<NS; i++) {
int ic = base[i];
fprintf(stderr, " %c %s : ",
((1<<i) & matching_cells) ? '*' : ' ',
lay->cells[ic].name);
show_symbols_in_set(NS, lay->symbols, ws->poss[ic]);
fprintf(stderr, "\n");
}
#endif
for (i=0; i<NS; i++) {
int ic = base[i];
int mask = (1 << i);
if (mask & matching_cells) continue;
if (ws->poss[ic] & symbol_set) {
did_anything = 1;
if (ws->options & OPT_VERBOSE) {
int k, fk;
fprintf(stderr, "(pe%d) Removing <", n);
show_symbols_in_set(NS, lay->symbols, symbol_set & ws->poss[ic]);
fprintf(stderr, "> from <%s:", lay->cells[ic].name);
show_symbols_in_set(NS, lay->symbols, ws->poss[ic]);
fprintf(stderr, ">, because <");
show_symbols_in_set(NS, lay->symbols, symbol_set);
fprintf(stderr, "> must be in <");
fk = 1;
for (k=0; k<NS; k++) {
if (matching_cells & (1<<k)) {
int ck = base[k];
if (!fk) {
fprintf(stderr, ",");
}
fk = 0;
fprintf(stderr, "%s(", lay->cells[ck].name);
show_symbols_in_set(NS, lay->symbols, ws->poss[ck]);
fprintf(stderr, ")");
}
}
fprintf(stderr, "> in <%s>\n", lay->group_names[gi]);
}
ws->poss[ic] &= ~symbol_set;
requeue_cell(ic, lay, ws);
requeue_groups(lay, ws, ic);
}
}
return did_anything;
}
/*}}}*/
static int do_int_remove(int gi, struct layout *lay, struct ws *ws, int n, int cell_set, int matching_symbols)/*{{{*/
{
int i;
int NS = lay->ns;
short *base;
int did_anything = 0;
base = lay->groups + (gi * NS);
#if 0
fprintf(stderr, "Matching symbols : ");
show_symbols_in_set(NS, lay->symbols, matching_symbols);
fprintf(stderr, "\n");
for (i=0; i<NS; i++) {
int ic = base[i];
fprintf(stderr, " %c %s : ",
((1<<i) & cell_set) ? '*' : ' ',
lay->cells[ic].name);
show_symbols_in_set(NS, lay->symbols, ws->poss[ic]);
fprintf(stderr, "\n");
}
#endif
for (i=0; i<NS; i++) {
if (cell_set & (1<<i)) {
int ic = base[i];
if (ws->poss[ic] & ~matching_symbols) {
if (ws->options & OPT_VERBOSE) {
fprintf(stderr, "(pi%d) Removing <", n);
show_symbols_in_set(NS, lay->symbols, ws->poss[ic] & ~matching_symbols);
fprintf(stderr, "> from <%s>, must be one of <", lay->cells[ic].name);
show_symbols_in_set(NS, lay->symbols, matching_symbols);
fprintf(stderr, "> in <%s>\n", lay->group_names[gi]);
}
did_anything = 1;
ws->poss[ic] &= matching_symbols;
requeue_cell(ic, lay, ws);
requeue_groups(lay, ws, ic);
}
}
}
return did_anything;
}
/*}}}*/
static int try_partition(int gi, struct layout *lay, struct ws *ws, int opt, struct score *score)/*{{{*/
{
int N, NN;
int cmap[64], icmap[64];
int smap[64], ismap[64];
int fposs[64], rposs[64];
int i, j, k;
short *base;
NN = N = count_bits(ws->todo[gi]);
N = (N+1) >> 1;
/* If we're being told to look for partitions bigger than 1/2 the number of
* entries left, we're wasting our time. */
if (opt > N) return 0;
base = lay->groups + (gi * lay->ns);
memset(rposs, 0, sizeof(int) * lay->ns);
/* Loop over symbols */
j = 0;
for (i=0; i<lay->ns; i++) {
int mask = 1<<i;
if (mask & ws->todo[gi]) {
smap[j] = i;
ismap[i] = j;
j++;
}
}
if (j != NN) {
fprintf(stderr, "j != NN at %d\n", __LINE__);
exit(1);
}
/* Loop over cells */
j = 0;
for (i=0; i<lay->ns; i++) {
int ic = base[i];
if (ws->state[ic] < 0) {
cmap[j] = i;
icmap[i] = j;
fposs[j] = ws->poss[ic];
for (k=0; k<lay->ns; k++) {
int mask = 1<<k;
int kk = ismap[k];
if (mask & ws->poss[ic]) {
/* Build map of which cells can take which symbols. */
rposs[kk] |= (1<<i);
}
}
j++;
}
}
if (j != NN) {
fprintf(stderr, "j != NN at %d\n", __LINE__);
exit(1);
}
switch (opt) {
case 2:/*{{{*/
{
int a0, a1;
for (a0 = 1; a0 < NN; a0++) {
for (a1 = 0; a1 < a0; a1++) {
int U;
U = fposs[a0] | fposs[a1];
if (count_bits(U) == 2) {
if (!score) {
int matching_cells = (1 << cmap[a0]) | (1 << cmap[a1]);
if (do_ext_remove(gi, lay, ws, 2, U, matching_cells))
return 1;
}
}
U = rposs[a0] | rposs[a1];
if (count_bits(U) == 2) {
if (!score) {
int matching_symbols = (1 << smap[a0]) | (1 << smap[a1]);
/* Hit : interior split */
if (do_int_remove(gi, lay, ws, 2, U, matching_symbols))
return 1;
}
}
}
}
}
break;
/*}}}*/
case 3:/*{{{*/
{
int a0, a1, a2;
for (a0 = 2; a0 < NN; a0++) {
for (a1 = 1; a1 < a0; a1++) {
for (a2 = 0; a2 < a1; a2++) {
int U;
U = fposs[a0] | fposs[a1] | fposs[a2];
if (count_bits(U) == 3) {
if (!score) {
int matching_cells = (1 << cmap[a0]) | (1 << cmap[a1]) | (1 << cmap[a2]);
if (do_ext_remove(gi, lay, ws, 3, U, matching_cells))
return 1;
}
}
U = rposs[a0] | rposs[a1] | rposs[a2];
if (count_bits(U) == 3) {
if (!score) {
int matching_symbols = (1 << smap[a0]) | (1 << smap[a1]) | (1 << smap[a2]);
/* Hit : interior split */
if (do_int_remove(gi, lay, ws, 3, U, matching_symbols))
return 1;
}
}
}
}
}
}
break;
/*}}}*/
case 4:/*{{{*/
{
int a0, a1, a2, a3;
for (a0 = 3; a0 < NN; a0++) {
for (a1 = 2; a1 < a0; a1++) {
for (a2 = 1; a2 < a1; a2++) {
for (a3 = 0; a3 < a2; a3++) {
int U;
U = fposs[a0] | fposs[a1] | fposs[a2] | fposs[a3];
if (count_bits(U) == 4) {
if (!score) {
int matching_cells = (1 << cmap[a0]) | (1 << cmap[a1]) | (1 << cmap[a2]) | (1 << cmap[a3]);
if (do_ext_remove(gi, lay, ws, 4, U, matching_cells))
return 1;
}
}
U = rposs[a0] | rposs[a1] | rposs[a2] | rposs[a3];
if (count_bits(U) == 4) {
if (!score) {
int matching_symbols = (1 << smap[a0]) | (1 << smap[a1]) | (1 << smap[a2]) | (1 << smap[a3]);
/* Hit : interior split */
if (do_int_remove(gi, lay, ws, 4, U, matching_symbols)) {
return 1;
}
}
}
}
}
}
}
}
break;
/*}}}*/
case 5:/*{{{*/
{
int a0, a1, a2, a3, a4;
for (a0 = 4; a0 < NN; a0++) {
for (a1 = 3; a1 < a0; a1++) {
for (a2 = 2; a2 < a1; a2++) {
for (a3 = 1; a3 < a2; a3++) {
for (a4 = 0; a4 < a3; a4++) {
int U;
U = fposs[a0] | fposs[a1] | fposs[a2] | fposs[a3] | fposs[a4];
if (count_bits(U) == 5) {
if (!score) {
int matching_cells = (1 << cmap[a0]) | (1 << cmap[a1]) | (1 << cmap[a2]) | (1 << cmap[a3]) | (1 << cmap[a4]);
if (do_ext_remove(gi, lay, ws, 5, U, matching_cells))
return 1;
}
}
U = rposs[a0] | rposs[a1] | rposs[a2] | rposs[a3] | rposs[a4];
if (count_bits(U) == 5) {
if (!score) {
int matching_symbols = (1 << smap[a0]) | (1 << smap[a1]) | (1 << smap[a2]) | (1 << smap[a3]) | (1 << smap[a4]);
/* Hit : interior split */
if (do_int_remove(gi, lay, ws, 5, U, matching_symbols)) {
return 1;
}
}
}
}
}
}
}
}
}
break;
/*}}}*/
default:
/* Until we get the capability to do bigger partitions... */
return 0;
}
return 0;
}
/*}}}*/
static int try_split_internal(int gi, struct layout *lay, struct ws *ws, int opt, struct score *score)/*{{{*/
{
/*
* Deal with this case: suppose the symbols 2,3,5,6 are unallocated within
* a group. Suppose there are two cells
* A that could be 2,3,5
* B that could be 2,3,6
* and neither 2 nor 3 could go anywhere else within the group under
* analysis. Then clearly we can eliminate 5 as a possibility on A and 6
* as a possibility on B.
* */
int NC, NS, NG;
int did_anything = 0;
int *intersect, *poss_map;
int sym, cell, ci;
int fill, mask;
short *base;
NS = lay->ns;
NC = lay->nc;
NG = lay->ng;
intersect = new_array(int, NS);
poss_map = new_array(int, NS);
base = lay->groups + gi*NS;
fill = (1 << NS) - 1;
for (sym=0; sym<NS; sym++) {
intersect[sym] = fill;
poss_map[sym] = 0;
mask = 1<<sym;
for (cell=0; cell<NS; cell++) {
ci = base[cell];
if (ws->poss[ci] & mask) {
intersect[sym] &= ws->poss[ci];
poss_map[sym] |= (1<<cell);
}
}
}
/* Now analyse to look for candidates. */
for (sym=0; sym<NS; sym++) {
if (count_bits(intersect[sym]) == count_bits(poss_map[sym])) {
/* that is a necessary condition... */
int sym1;
for (sym1=0; sym1<NS; sym1++) {
int mask = 1<<sym1;
if (sym1 == sym) continue;
if (intersect[sym] & mask) {
if ((intersect[sym] == intersect[sym1]) &&
(poss_map[sym] == poss_map[sym1])) {
} else {
goto examine_next_symbol;
}
}
}
/* Good subset: */
for (cell=0; cell<NS; cell++) {
if (poss_map[sym] & (1<<cell)) {
int ci = base[cell];
if (ws->poss[ci] != intersect[sym]) {
if (score) {
} else {
if (ws->options & OPT_VERBOSE) {
fprintf(stderr, "(i) Removing <");
show_symbols_in_set(NS, lay->symbols, ws->poss[ci] & ~intersect[sym]);
fprintf(stderr, "> from <%s>, must be one of <", lay->cells[ci].name);
show_symbols_in_set(NS, lay->symbols, intersect[sym]);
fprintf(stderr, "> in <%s>\n", lay->group_names[gi]);
}
ws->poss[ci] = intersect[sym];
requeue_cell(ci, lay, ws);
requeue_groups(lay, ws, ci);
}
did_anything = 1;
}
}
}
}
examine_next_symbol:
(void) 0;
}
free(intersect);
free(poss_map);
return did_anything ? 1 : 0;
}
/*}}}*/
static int subset_or_eq_p(int x, int y)/*{{{*/
{
if (x & ~y) return 0;
else return 1;
}
/*}}}*/
static int try_split_external(int gi, struct layout *lay, struct ws *ws, int opt, struct score *score)/*{{{*/
{
/*
* Deal with this case: suppose the symbols 2,3,5 are unallocated within
* a group. Suppose there are three cells
* A that could be 2,3
* B that could be 2,3
* C that could be 3,5
* and neither 2 nor 3 could go anywhere else within the group under
* analysis. Then we can eliminate 3 as an option on C.
* */
int NC, NS, NG;
int did_anything = 0;
int did_anything_this_iter;
int i, ci, j, cj;
short *base;
char *flags;
NS = lay->ns;
NC = lay->nc;
NG = lay->ng;
base = lay->groups + gi*NS;
flags = new_array(char, NS);
do {
did_anything_this_iter = 0;
for (i=0; i<NS; i++) {
int count, other_count;
ci = base[i];
if (!ws->poss[ci]) continue;
count = 0;
other_count = 0;
memset(flags, 0, NS);
for (j=0; j<NS; j++) {
cj = base[j];
if (opt ? (ws->poss[cj]
&& subset_or_eq_p(ws->poss[cj], ws->poss[ci]))
: (ws->poss[ci] == ws->poss[cj])) {
++count;
flags[j] = 1;
} else if ((ws->poss[cj] & ws->poss[ci]) &&
(opt ? !subset_or_eq_p(ws->poss[ci], ws->poss[cj])
: (ws->poss[ci] != ws->poss[cj])
)) {
other_count++; /* cells we'll be able to remove possibilities from */
}
}
/* count==1 is a normal allocate done elsewhere! */
if ((count > 1) && (other_count > 1) && (count == count_bits(ws->poss[ci]))) {
/* got one. */
for (j=0; j<NS; j++) {
cj = base[j];
if (!flags[j] && (ws->poss[cj] & ws->poss[ci])) {
did_anything = 1;
if (score) {
} else {
did_anything_this_iter = 1;
if (ws->options & OPT_VERBOSE) {
int k, fk;
fprintf(stderr, "(%c) Removing <", opt ? 'E' : 'e');
show_symbols_in_set(NS, lay->symbols, ws->poss[cj] & ws->poss[ci]);
fprintf(stderr, "> from <%s:", lay->cells[cj].name);
show_symbols_in_set(NS, lay->symbols, ws->poss[cj]);
fprintf(stderr, ">, because <");
show_symbols_in_set(NS, lay->symbols, ws->poss[ci]);
fprintf(stderr, "> must be in <");
fk = 1;
for (k=0; k<NS; k++) {
int ck = base[k];
if (flags[k]) {
if (!fk) {
fprintf(stderr, ",");
}
fk = 0;
fprintf(stderr, "%s(", lay->cells[ck].name);
show_symbols_in_set(NS, lay->symbols,ws->poss[ck]);
fprintf(stderr, ")");
}
}
fprintf(stderr, "> in <%s>\n", lay->group_names[gi]);
}
ws->poss[cj] &= ~ws->poss[ci];
requeue_cell(cj, lay, ws);
requeue_groups(lay, ws, cj);
}
}